This is how BASIC worked in the Spectrum at least. Each command was a single keystroke (you'd hit 'P'P and "PRINT"PRINT
would appear). Of the 256 possible character values, 91 were the BASIC commands and operators like "POKE"POKE
or "<>"<>
, not counting the single-character operators like ++
and so on. You can see the list here:
https://en.wikipedia.org/wiki/ZX_Spectrum_character_set#Character_seton Wikipedia.
So, the number '1'1
took four bytes to store on the spectrumSpectrum (if I remember right, it stored two copies of all 16 bit ints for some reason), but the commands "SGN PI"SGN PI
took only 2, one byte each: 0xBC 0xA7.
In BASIC, the commands to change the contents of memory, the equivalent of C's pointers, are "PEEK"PEEK
and "POKE"POKE
.
Again, I don't think this is what the question is asking. While BASIC (like any language, really) can be compiled to bytecode or assembly, it was typically implemented as a dynamically interpreted language from a bytestreambyte stream. Outside of the byte stream, there's no preparsingpre-parsing, splitting the language up into metasyntactic concepts akin to a "syntax tree", or anything like that.
As I understand it, Sinclair BASIC in particular was an interpreted language: the program counter went along line by line, passing each in turn to the interpreter, in a single pass with no backtracking other than via the "GO TO" contructGO TO
construct.
- the Timex Sinclair's addition to the Sinclair BASIC of the
DELETE <first line#> <last line#>
command permitted the removal of ranges of lines from a BASIC program.the Timex Sinclair's addition to the Sinclair BASIC of the
DELETE <first line#> <last line#>
command permitted the removal of ranges of lines from a BASIC program. - the
RENUM <first line$> <step size>
command renumbered code line numbers, affecting all GOTO, GOSUB, RESTORE, RUN, LINE, ON ERROR GOTO and other similar commands. The reason this could be considered self-modifying is that it would leave alone any expressions such asGOTO VAL "50"
,DELETE 10, 100
, and indeed further calls to RENUM.the
RENUM <first line$> <step size>
command renumbered code line numbers, affecting allGOTO
,GOSUB
,RESTORE
,RUN
,LINE
,ON ERROR GOTO
and other similar commands. The reason this could be considered self-modifying is that it would leave alone any expressions such asGOTO VAL "50"
,DELETE 10, 100
, and indeed further calls toRENUM
.So it's possible, though I've not tried it, that this:
10 GOTO VAL "40" 20 PRINT "nothing" 30 DELETE 10, 70 40 PRINT "word" 50 RENUM 10, 30 60 DELETE 130, 160 70 GOTO 10
...will, on hitting line 50, change itself to:
10 GOTO VAL "40" 40 PRINT "nothing" 70 DELETE 10, 70 100 PRINT "word" 130 RENUM 10, 30 160 DELETE 130, 160 190 GOTO 10
... then on hitting the new line 160, convert to:
10 GOTO VAL "40" 40 PRINT "nothing" 70 DELETE 10, 70 100 PRINT "word" 190 GOTO 10
... then on hitting the new line 70, convert to:
100 PRINT "word" 160 GOTO 10
... which is still valid since a
GOTO
goes to the next largest number. The resulting output would be:word nothing word word word ....
I have never used a Timex, so can't say for sure that's how they worked (did these update the program counter? Halt the program? Restart it? I can't find it documented). But either way, this is the closest I can find.
So it's possible, though I've not tried it, that this:
10 GOTO VAL "40"
20 PRINT "nothing"
30 DELETE 10, 70
40 PRINT "word"
50 RENUM 10, 30
60 DELETE 130, 160
70 GOTO 10
...will, on hitting line 50, change itself to:
10 GOTO VAL "40"
40 PRINT "nothing"
70 DELETE 10, 70
100 PRINT "word"
130 RENUM 10, 30
160 DELETE 130, 160
190 GOTO 10
... then on hitting the new line 160, convert to:
10 GOTO VAL "40"
40 PRINT "nothing"
70 DELETE 10, 70
100 PRINT "word"
190 GOTO 10
... then on hitting the new line 70, convert to:
100 PRINT "word"
160 GOTO 10
... which is still valid since a GOTO
goes to the next largest number. The resulting output would be:
word
nothing
word
word
word
....
I have never used a Timex, so can't say fersure that's how they worked (did these update the program counter? Halt the program? Restart it? I can't find it documented). But either way, this is the closest I can find.