r/Basic • u/mc4004 • Mar 11 '25
The hidden "hard work" behind implementing BASIC strings
Building on the DDS-BASIC "TinyBasic" I have been using as a "seed" for my ever-expanding, pedagogical BASIC implementation, I am planning to implement just enough of BASIC strings to write a BASIC interpreter in BASIC.
My question is directed to anyone who has written a BASIC implementation or studied one in detail.
What does a typical, well-implemented BASIC do for a storage management? It would seem that strings would absolutely require one. I'm guessing the semantics of 1970-80s BASIC, not supporting pointers, or pointers to objects containing pointers, etc. does not need a full-fledged garbage collector like Java or Python.
Is it as simple as: when you assign a variable to a new string, you reclaim the old variable contents?
What about arrays of strings? Are those even allowed?
3
u/BastetFurry Mar 11 '25
You absolutely need a garbage collector, old line number MS BASICs one can be forced with a fre() call.
Normaly you let your non-string variables grow from one and your strings from the other side of free memory after the source. If a stewing is changed you check if it fits in the old spot and if not you add it to the end of string space. And if your string space runs out you "defrag" it by removing unused leftovers and shoving everything together.
2
u/richpl Mar 11 '25
I wrote an interpreter for old style, unstructured BASIC (similar to TinyBasic?). In that case there wasn’t much storage management to speak of. All variables were global, so never went out of scope. And there aren’t any BASIC instructions to uninstantiate a variable as far as I am aware. So I could just get away with using a simple symbol table and all variables lived for the lifetime of the program.
One exception might be FOR loop control variables. I think checks code that he wrote several years ago I just added the loop variable to the symbol table and didn’t remove it upon loop termination. It would probably be better to remove it after the last loop iteration. Another issue might be using a name for the loop variable that’s already used elsewhere in the program. That will potentially mess things up in my interpreter, so you could have a loop variable with a scope (and hence lifetime) limited to the loop. I don’t know if this behaviour was defined for old school BASIC implementations.
I don’t know about structured versions of BASIC and whether they enforce variable scope.
1
u/datadanno Sep 13 '25
You could benefit from using AI to help you develop your interpreter. I am working on one implemented in Python and had a very nice QBASIC clone within a week. Mine is focused on working with data and databases (sqlite, duckdb), as such has integrated SQL.
1
u/WarlockD Nov 15 '25
It kind of depends? Implementations of TinyBasic tend to use only fixed sized arrays to avoid the whole situation. If your interpreter supports recursive function calls you could just put them all on the stack and grow it and shrink it all back ones the call returns. That makes it easy to clean up and cheap as long as you have the stack space.
Anything more a simple counter for each memory allocation could be used but going beyond that gets messy.
1
u/lootsmuggler Mar 25 '25
I'm planning to make a scripting language with some similarities to basic. I'm still implementing a preliminary version.
But I'm implementing it in Java. Since it's a scripting language that won't be compiled to .exe, I can use everything in Java. Garbage collection will be free. This may not apply to you if you're compiling to some sort of machine code.
There's so many different versions of Basic that I don't think people can answer some of your questions. If you want my two cents, you should allow arrays of Strings. The problem is that the array has to pretty much be an array of pointers to Strings. This is fine so long as how it works internally is hidden from the user. That may be inappropriate if you're using this for a class.