I've toyed with the idea of doing my own attempt at a BASIC interpreter. If you want to try your own, figure out how you can design things to deal with the following basic (pun intended) operations:
Accept a line of input preceded by a number and edit the program in memory to accommodate it.
Convert a program which is stored in memory into human-readable form.
Run a program in memory.
The last operation is probably the most performance-sensitive, but you should try to make the first one be fast as well. The second probably isn't likely to matter from a performance perspective, but you should ensure that the amount of code required to accomplish it is reasonable. Having an input scanner convert something like
into a sequence of tokens equivalent to:
var#1 var#2 var#3 var#4 add multiply add var#5 add printvalue printseparator
would not be overly difficult, and it would allow for very efficient
processing, but converting it back to the original form might be tricky.
As a general design, I would suggest that variables be converted to tokens
that identify a relative location within a symbol/value table (either an
index or a byte offset), and numbers should be converted to tokens that
store their value in binary. While converting expressions to prefix or
postfix form may slightly expedite processing, keeping them in infix will
make listing much simpler. If you store strings (literal or otherwise)
with a length prefix and ensure that each string in memory takes at least
three bytes total (perhaps by saying that a string pointer whose MSB equals
1 should be interpreted as a single-byte string) that may facilitate garbage
collection by allowing relocated strings to be replaced by a pointer to their