Spaces in variable names in ZX Spectrum BASIC
When developing a tool for the ZX Spectrum, I noticed that the BASIC interpreter allows spaces in variable names. This is not possible in most other BASICs, and it is not possible in the ZX Spectrum BASIC interpreter on the ZX81. I was curious to find out why this is possible, and how it works.
When the BASIC interpreter encounters a variable name, it looks up the name in a table of variable names, called
VARS. Any spaces in the variable name is removed, and the remaining characters are converted to lowercase. Because of this, all lines in the program below use the same variable:
10 LET hello world=42 20 INPUT HELL o WorLd 30 PRINT HelloWorld
Using spaces in variable names might seem strange at first, but it can be useful for readability. In more modern languages, it is common to use
This is one of several quirks in the ZX Spectrum BASIC interpreter, that can make it a bit tricky to write programs that parse BASIC programs.