Transliterating Old-School BASIC

submited by
Style Pass
2021-07-25 22:30:07

Once in a while, I run into a 1980s-era BASIC program, typically written for Commodore 64, Apple ][, or TRS-80. I’d like to transliterate it into a modern language, refactor it, and explore it. These old programs make a nice halfway point between refactoring assembly language and refactoring a modern language. 

In some BASICs, a colon (:) can be used to put two or more statements on the same line. If the colon follows a THEN clause, those statements are part of the THEN. 

Less common commands include: DATA (defines constant data), DEF (create one-liner functions), DIM (dimension – create arrays), INPUT, ON..GOTO (computed GOTO), READ (from DATA statements), RESTORE (reset DATA statements), STOP (re-startable stop).

To translate line numbers, we can put the whole code in a loop with a switch statement. This is effectively a state machine (see References) with the line representing the state. 

A statement such as IF, GOTO, or GOSUB that references  a line number can consult or set the line variable. The END statement can force the line number to 0. Other statements set it as their last step (or fall through). 

Leave a Comment