Thursday, February 25, 2010

The SBASIC Preprocessor

I had become familiar with structured programming ideas from my casual experimentation with Forth. I wanted to do something to improve the quality of my Microsoft BASIC programming. I realized that I could write a program that would take BASIC source code without line numbers, and adding in named labels for the GOTO and GOSUB targets and output line numbered BASIC. Then I could run this code in an interpreter or compile it. This was a cool idea for me. After having done the CNC simulator this lit up a part of my brain that brought me closer to realizing that a compiler is just another kind computer program that treats source code as data.

I wasn't familiar with QuickBASIC at that time, so I didn't use Microsoft's syntax for this. Instead I used square brackets for branch labels like so:

[loopHere]
a = a + 1
print a
if a = 10 then [quit]
goto [loopHere]

[quit]
print "done."
end

Liberty BASIC programmers will recognize this syntax. I called this preprocessor SBASIC for Structured BASIC.

Here's what it looks like in QuickBASIC:

loopHere:
a = a + 1
print a
if a = 10 then quit
goto loopHere

quit:
print "done."
end

I think that my syntax is better. It certainly is more consistent because in QuickBASIC the branch label will either be quit or quit: but in SBASIC it is always [quit].

So here we see a direct precursor to Liberty BASIC. At this time I did not have plans to create my own BASIC. That wouldn't come for four more years.

No comments: