Out of the Home Computer Era BASICS, Commodore BASIC is one of the most alien to the dialect of BASIC in this toolset, perhaps only beaten on that score by Sinclair ZX BASIC.
BASICs like BBC BASIC or Sinclair SuperBASIC at least share some of the structured code elements of the language.
Commodore BASIC has no structured elements at all, and practically everything is done with GOTO or GOSUB and a form of IF…THEN that is not valid syntax in this dialect of BASIC.
And unlike some of the other earlier Microsoft BASICs, it is not even in a character set that can be simply transcoded to WTF-8 or UTF-8, nor is it even saved as text at all.
To humans, Commodore BASIC programs (for the PET, C64, or VIC-20) are written in PETSCII. To computers, they are saved as binary ‘PRG’ files that store a raw memory image of a tokenized program.
PETSCII is an 8‐bit single byte character set encoding based upon the old 1963 version of ASCII, and according to Leonard Tramiel added graphics symbols for Blackjack and other card games, for bar graphs, and for drawing pictures of a lunar lander and a certain Science Fiction spaceship.
It also includes control characters (different even to the 1963 ASCII ones) that move the cursor around the screen, change the current output colour, and clear the screen.
These are often embedded into strings in PRINT statements.
basic and basica, on the other hand, expect programs to be stored in WTF-8 and to contain (WTF-8 encoded) ECMA-48 control sequences.
To them, a PETSCII file with embedded graphics and control characters contains sequences of malformed WTF-8 characters.
The first step in converting a Commodore BASIC program is to decode the ‘PRG’ file into a PETSCII source text file.
This is done with the commodore-basic-detokenize utility:
$ commodore-basic-detokenize example.prg > example.petscii.bas $
This is a file that can be used by programs that expect PETSCII, such as perhaps emulator programs or other programs that work with native Commodore file formats. It cannot be used by the other tools in this toolset, as it stands, though.
Non-ASCII characters will generally occur in strings and comments, as they are not legal as keywords or variable names in Commodore BASIC, so a program that does not use PETSCII graphics or control characters is pretty much an ASCII program and can be used by WTF-8 or UTF-8 tooling. But, in particular, the PETSCII newline character is the same code value as the ASCII CR character, meaning that tools that expect Unix newline conventions will take the ASCII subset of PETSCII file as having one giant single line and fail to handle line numbers. Furthermore, many even simple Commodore BASIC programs will do things like home the cursor position with what is the ASCII DC3 control character, or clear the screen with the STS control character (which is not in the 7‐bit subset of PETSCII). So the next step is to transform the PETSCII into some usable encoding.
No known version of iconv can handle PETSCII, so there are three routes from this point:
Lie to iconv, making it think that it is converting ISO 8859-1 to UTF-8:
$ iconv --from-code=latin1 --to-code=utf8 example.petscii.bas > example.petscii-wtf8.bas $
This results in a file where the ASCII-compatible part is untouched, and the non-ASCII parts are reëncoded as WTF-8. Tools that expect WTF-8 or UTF-8 input will at least read the files as correctly encoded characters, although they will not assign their originally intended meanings to the code points.
Use petscii-to-wtf8:
$ petscii-to-wtf8 example.petscii.bas > example.petscii-wtf8.bas $
This results in a file where the ASCII-compatible part is untouched, but the non-ASCII parts are instead transcoded to their Unicode equivalents and encoded as WTF-8. Again, tools that expect WTF-8 or UTF-8 input will read the files as correctly encoded characters, but now they will output the correct graphics that the original PETSCII code intended, and control characters will not be misinterpreted. Such a file can be handled by tools that expect Unix newline conventions. However, quite a few PETSCII control characters cannot be converted to single Unicode code points, and they will be lost in the conversion process.
Use petscii-to-ecma48 in its --basic mode:
$ petscii-to-ecma48 --basic example.petscii.bas > example.petscii-ecma48.bas $
This results in a file where the ASCII-compatible part is untouched, but the non-ASCII parts are instead transcoded to their Unicode equivalents, including ECMA-48 control sequences, and encoded as WTF-8. As before, tools that expect WTF-8 or UTF-8 input will read the files as correctly encoded characters, and they will output the correct graphics and output controls that the original PETSCII code intended. Such a file can be handled by tools that expect Unix newline conventions, and more PETSCII control characters will have been retained.
The final stage is to deal with the program logic and language syntax. The programs now have to be modified as this BASIC dialect is not source code compatible with Commodore BASIC, nor has its hardware environment. Some general areas of attention are:
All variables have to be declared with LET, DIM, or FOR.
Integer variables, moreover, require marking with %, as without that floating point arithmetic will be used for things like integer FOR loops and array element accesses.
Control flow structures do not take their abbreviated Commodore BASIC forms.
FOR loops have to end in END&hbsp;FOR rather than NEXT; the abbreviated form of THEN plus a line number has to be turned into a proper control flow structure; and IF has to have a matching END IF.
PRINT TAB should be PRINT TAB$, and may need adjustment for assumptions that sometimes it will print a newline in Commodore BASIC, but never will in this dialect.
In general, embedded control characters for colours and cursor movements should be replaced by VDU$, CLS$, COLOUR$ and TAB$ which account for Unix terminal capability differences at runtime.
< > is two tokens in Commodore BASIC.
It does not have a single token for the inequality operator.
In this dialect of BASIC, as with most others, the operator is <> as a single token without spaces.
Likewise for < = and < = for the same reason.