How to simplify code in programs?
Root / Programming Questions / [.]
DevinF06Created:
It's not like there aren't ways to:
You can use newlines instead of statement separator (:), only the single-line IF syntax, not use functions, use whitespace consistently, consistently indent nested blocks...
...but you'd still have to write a parser, it would just be a parser that recognizes 85% of the SmileBASIC syntax instead of 99%.
You aren't explaining what you really want here. It sounds like you want two things.
The first is a code formatter that would take exiting source code and reformat it. It would take things like ? and change it to PRINT, change . used as a number to 0 (why do people do this, they are both a single character), and add consistent padding and indentation levels. This would make other peoples code easier to read. The problem of course is that you can't do this with a bunch of simple search/replace calls. After all what happens if you use PRINT "How are you today?" and the formatter changes it to PRINT "How are you todayPRINT". In order to do it right you need to tokenize and parse the code then print it back out again in a standard form. The problem with that is of course that you are basically writing half of a compiler or interpreter. You are half way to writing SmileBasic in SmileBasic so you can SmileBasic while you are SmileBasic-ing (sorry couldn't resist the old meme). It would be sure nice to have that but probably no one wants to spend the time to write it.
The second thing you post almost looks like loop unrolling which again heads off into compiler/interpreter territory. But, I don't think that is what you really want. Instead, what you really want is a nice debugger. Then you could put breakpoints on things, single step through code, and inspect variables, or add watches. That would be even nicer than the code formatter. Unfortunately, SmileBasic doesn't give you the hooks to write one. Instead we have two commands. In old school basic there were two more TRON and TROFF for trace on and off to trace variables in the program, and yes that is what the movie TRON is named after. Unfortunately, it doesn't look like SmileBasic gave us those. So we only have STOP and CONT/CONTINUE. Stop will halt execution of your code. Then you can try to print out variables manually in direct mode. Then when you are ready you can type CONT or CONTINUE to resume execution.
TEST END DEF TEST VAR I FOR I = 1 TO 3 STOP PRINT I * I NEXT I ENDThen in direct mode you can RUN or hit the start button. It will say Break on 0:8 You can type in PRINT I to get a 1 then type CONT to have it continue. Repeat that three times and the program finishes. Unfortunately, that is all the tools we have to work with in SmileBasic. Well that and having a bunch of PRINT statements scattered throughout your code to write out status as things run.It is messy and primitive. If you have a terrible thing to debug you could try sending it over to your computer with PetitModem and then try to get it running under QB64. I think that one has a reasonable debugger, and is probably the closest computer equivalent of SmileBasic except it isn't really very close at all. The graphics and sound and such are all very much different between the two. I guess you have one more tool. Generate a key for the code, post a thread here, tell people what kind of problem you are having and where in the code. If you are lucky, the nice folks here may be able to help you fix the problem, or explain what is going on. Just don't abuse the help people provide, be thankful and patient, it can take a while.