Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : MMBasic Source Code Formatter
Author | Message | ||||
shoebuckle Senior Member Joined: 21/01/2012 Location: AustraliaPosts: 189 |
Hi, I am developing a utility to format MMBasic source code. You can download a copy at http://members.optusnet.com.au/hnmpb/Maximite/Format.zip. It isn't finished yet but can handle code with one statement per line. Future features should support: - code with line numbers - code with multiple statements per line. It currently assumes that Do & its Loop are on separate lines. Likewise For & Next, Sub & End Sub, Function & End Function must be on separate lines. However it does support both single and multiple line IF statements. Formatting rules used are: - Lines starting with a Label are aligned to the left margin. as are SUB, END SUB, FUNCTION and END FUNCTION. - The first non-label line is indented to the first level. - Lines following a single line IF, DO and FOR are not further indented. - Multiline DO and FOR have their respective LOOP and NEXT aligned. Intervening lines are indented one level. - Multiline IF statements have their ELSE, ELSEIF and ENDIF aligned under the relevant IF. Intervening statements are indented one level. - Nesting increases the indent one level. It doesn't care if your code works or not but I have just found out that it will highlight a extra ENDIF where one is not required by moving following code back unexpectedly one indent level. Things to note: - The program won't allow you to overwrite the input file. - It asks for the number of spaces to indent at each level (default 2) - You will find some testing code imbedded. This will be removed in the final version - just make sure Testing=False at the top. Let me know if you find a bug (give me a sample of code which misbehaves). I have tested it on many of the MMLib programs as you will see from the testing code at the top of the source. So far it seems to work. Cheers, Hugh p.s. I am a bit preoccupied with other things at present so enhancements may be rather slow in coming. |
||||
CircuitGizmos Guru Joined: 08/09/2011 Location: United StatesPosts: 1425 |
That is GREAT! Just the way I want to see code formatted. Micromites and Maximites! - Beginning Maximite |
||||
shoebuckle Senior Member Joined: 21/01/2012 Location: AustraliaPosts: 189 |
FORMAT28.BAS now supports code with line numbers. (The 28 indicates today's date). It sets the minimum indent (to which a label will be aligned) as one character more than the length of the longest line number in the program. I have also fixed a bug in which a single word command followed immediately by a colon such as PRINT: or CLS: was misinterpreted as a label. http://members.optusnet.com.au/hnmpb/Maximite/Format.zip . Now for multi-statement lines:-) Hugh |
||||
shoebuckle Senior Member Joined: 21/01/2012 Location: AustraliaPosts: 189 |
The final (I hope) version is there now. If you find a problem with it, please let me know. It now supports multi statement lines and lines with line numbers. Cheers, Hugh |
||||
paceman Guru Joined: 07/10/2011 Location: AustraliaPosts: 1329 |
Hi Hugh, Just ran your final version on a couple of my programs and they came out looking very pretty - and they still worked! Congratulations it's great! You should talk to TassyJim and ask him to incorporate the equivalent thing into his excellent MM Edit program that runs on the PC. That way we'd have a great suite of editing/formatting capabilities both Maximite based and PC based. Greg |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6099 |
It is something that's on the todo list. I have avoided it to date because it is not easy to get right. Jim VK7JH MMedit MMBasic Help |
||||
James_From_Canb Senior Member Joined: 19/06/2011 Location: AustraliaPosts: 265 |
You're 100% right about that. I offered to rewrite the RENUM.BAS program and I've been stuck for over a week trying to maintain the formatting of the original program while inserting different length line numbers. Or worse, trying to account for formatting when there are no line numbers. Or worst of all, when there's a mix of code with line numbers and without, and the code without line numbers starts at column one and the indent of that code block extends past the length of a line number. I have a working solution for the first two, but I'm having trouble with the third. I can't ignore it because that's how code is likely to be included from libraries. If I can't work it out I'll release the code as is and suggest the user runs the renumbered code through Hugh's reformatter. Now back to the code..... James My mind is aglow with whirling, transient nodes of thought careening through a cosmic vapor of invention. Hedley Lamarr, Blazing Saddles (1974) |
||||
shoebuckle Senior Member Joined: 21/01/2012 Location: AustraliaPosts: 189 |
Jim, You could CHAIN your Renumber program to a modified version of Format. - Write a temporary renumbered source from Renumber, use that as input to Format and Kill the temporary file at the end. - Remove the user filename prompts and introduction from Format. It would be a good test of the Chain command. I would be happy to have a go at that if you have got the renumbering bit done, but I don't want to take the fun out of it for you if you want to carry on. Cheers, Hugh |
||||
James_From_Canb Senior Member Joined: 19/06/2011 Location: AustraliaPosts: 265 |
That sounds like a good idea Hugh. I'll stop trying to find a solution based on the spaces at the beginning of each line. I suspect there isn't a solution unless I analyse the code that's being renumbered, in which case I'd have your program. James My mind is aglow with whirling, transient nodes of thought careening through a cosmic vapor of invention. Hedley Lamarr, Blazing Saddles (1974) |
||||
paceman Guru Joined: 07/10/2011 Location: AustraliaPosts: 1329 |
Maybe you should tell people it only works on code without line numbers. We could then re-do the files in the library that have line numbers by running them through MM Edit. I'll bet there's a heap of issues I haven't thought through with this idea. Greg |
||||
shoebuckle Senior Member Joined: 21/01/2012 Location: AustraliaPosts: 189 |
Greg, I think you misunderstand. The point is trying to get the Renumber program to format the code reasonably. Once the program has been renumbered, Format can take over and do the formatting (it works on code with or without line numbers). You can either run the programs one after the other or we can develop a combined version where Renumber chains to Format. Cheers, Hugh |
||||
paceman Guru Joined: 07/10/2011 Location: AustraliaPosts: 1329 |
Ah, OK Hugh I see it now - I should have read the thread more thoroughly. |
||||
MicroBlocks Guru Joined: 12/05/2012 Location: ThailandPosts: 2209 |
Hugh, Another task i often do by hand is to minimize the code. This means changing variable names to short ones (except function/subroutine names), removing comments, empty lines combining statements on one line etc. If you could add that to your 'toolbox' it would be a great addition. Once you make a library, fully documented and with good variable names and formatting you could then make a much smaller version to use in a project. It will free some memory and speed up things a little. It can also be used to obfuscated code if that is a wish. It is very popular in the web programming world, and most javascript libraries are available in both normal and 'minimized' form. Microblocks. Build with logic. |
||||
shoebuckle Senior Member Joined: 21/01/2012 Location: AustraliaPosts: 189 |
Mmmmm! That would be quite a challenge. I will have to think about that one. Another utility I had in mind was an index to all the variables in a program; an alphabetic list with the line numbers on which each appears. Many compilers do this for you as a matter of course. Again, quite a challenge in parsing the code. These two should keep me of the streets for a week or two. Cheers, Hugh |
||||
James_From_Canb Senior Member Joined: 19/06/2011 Location: AustraliaPosts: 265 |
There's another useful function that someone could write: When a library is added to a program it will include many routines that are not used. It would be really useful if there was a utility to remove the unused routines. The utility might be better if it ran outside of MMBasic because a couple of big libraries included in a big program could use up all the available memory on an MM. Some of the functionality suggested in the earlier postings is already fully or partially done in MM-Edit. James My mind is aglow with whirling, transient nodes of thought careening through a cosmic vapor of invention. Hedley Lamarr, Blazing Saddles (1974) |
||||
paceman Guru Joined: 07/10/2011 Location: AustraliaPosts: 1329 |
Hi Hugh, Yes, as James (Canb) said, a variable index was specifically added to TassyJim's last update of MM Edit. I'll bet you've still got a nice 'to-do' list left though. |
||||
shoebuckle Senior Member Joined: 21/01/2012 Location: AustraliaPosts: 189 |
Well, not really that much. TZ's request for a Compactor (I would like to call it Crunch) will undoubtedly keep me busy for a while. Is it something that many would use? Let me know if you would. Cheers, Hugh |
||||
MicroBlocks Guru Joined: 12/05/2012 Location: ThailandPosts: 2209 |
'Crunch', like that name. With added sound effects? The reason i use it although not currently on the MM is that you then would be able to store more on the internal A: drive and of course you save some program memory. Another reason is that when you have a product that you don't want others to meddle with certain parts (library) the advantage of a 'crunch' is that the programming gets very obscure. BASIC has the impression that it is simple and easy to change, but that is not always true especially with time critical routines. Maybe a 'Crunch level'? Stripping comments, removing whitespace, change variable names, could be different levels. Microblocks. Build with logic. |
||||
paceman Guru Joined: 07/10/2011 Location: AustraliaPosts: 1329 |
I wouldn't have a lot of use for it yet, mainly because my programs are not very long so far - with the exception of a DOS MMBasic one. I can see that it would useful though for the more complex ones and for a 'compacted' library. I wouldn't want to see the well documented library stuff go though, it would need to be an additional series. |
||||
shoebuckle Senior Member Joined: 21/01/2012 Location: AustraliaPosts: 189 |
Found a problem in Format when a single line contains nested FOR/NEXT loops and a NEXT closes more than one nest level. The updated version can be found at http://members.optusnet.com.au/hnmpb/Maximite/Format.zip and will be in the next MMLib update. It is probably a very rare condition and only likely to occur if you are combining lines as much as possible to reduce memory usage, but it should handle it now. There are also a couple of changes to support being Chained from James_from_Canb's Renumber program, which seems close to completion. Cheers, Hugh |
||||
Print this page |