Building a BASIC Interpreter, '80s Style, Redux

Wow!

I’m literally floored by the response to the previous article in this series, Building a BASIC Interpreter, ‘80s Style. Not only did it get a lot of views on Able.bio, but it got onto Reddit and Hacker News with some great discussion points as well. I never expected so many people to be interested in this kind of thing. So very cool!

Anyway, a few interesting points were brought up that I quickly wanted to touch on here before continuing on with the series proper.

Syntax Highlighted LIST

rbanffy suggested that it’d be really pretty cool to have LIST render the output with syntax highlighting. Most BASICs of the era could have done something fairly simple — they already knew if they were printing a keyword or something else, but Retroputer should be able to go one step further, since it also has quick access to variable type information, inline integers, and strings. This sounds like a cool addition to add, and, it turns out, is relatively trivial to do so.

The colors are technically configurable, but I’ve not got any syntax around that just yet. I’ll probably add a LISTO or LIST OPTIONS command that lets you set them (or disable them) from BASIC.

Indented LIST

Speaking of LISTO, pmiller laments that the output of LIST typically wasn’t very pretty at all — for example, most BASICs didn’t try to add any extra indentation, which would be very useful when dealing with loops and blocks.

It turns out that BBC Basic had a way to turn this on via LISTO 2, and this also seems like a good addition to add. Retroputer BASIC will support a reasonable number of block-based statements, including (eventually) a block IF-THEN-ELSE-ENDIF, various looping constructs and the like. So having indentation would be nice. I took a stab at that just this afternoon.

It turns out there’s a lot of edge cases that my implementation doesn’t currently handle. For example, how do you easily detect if THEN should start a new block or not? What about terminating two blocks at once with NEXT Y, X? This isn’t impossible to add, but it certainly involves a little more parsing than LIST currently has.

Oh, and if you LIST 40,70, for example, you’ll be incorrectly indented from the very start, since LIST doesn’t try to calculate the indentation for any lines not printed.

Even so, I think with a LIST OPTION, it could still be useful.

Optimizations

wizzwizz4 and tjalfi both provided some optimization hints. Retroputer BASIC is very far from being optimized, and so this is very welcome help! wizzwizz4 indicates that it should be easier to just use another loop when a string is detected rather than using a flag. That’s a great suggestion, and I’ll look at adding it. Tjalfi also pointed out the x86 ja instruction, which can also be used to optimize the comparison of the character to the “a”…”z” ASCII range. Retroputer’s ISA doesn’t have an exact analog, but it should still be possible to mimic.

Tweaks from the last post

As I’ve been playing around with some ideas for syntax for supporting Retroputer’s graphics capabilities (it supports multiple graphics layers, sprites, and fast memory moves and swaps), I realized that the current method for storing the token vectors was just too unmaintainable.

And so I added a simple JavaScript file that creates all the tables from a master list. This lets me quickly add new features, but also ensure that tokens are always ordered neatly and, (most important) ensures I don’t accidentally get the various jump tables out-of-sync by a line or two. Once BASIC reaches 1.0, the tokens will probably be frozen, but until then, it needs to be easy to continue adding functionality.

I also updated the last post’s links to point into the correct files above.

Useful Resources

Carl Gundel suggested “The Elements of Computing Systems” as a useful book for learning how to build a modern computer from first principles. I’d not heard of this, but I’m definitely taking a look now! It’s on Amazon. Also known as NAND To Tetris (which I had heard about). Now I’m doubly excited to try it.

Tjafi suggested “Hacker’s Delight” which deals with writing optimized and performant code. It’s also on Amazon.

I also want to point out some resources which I’ve found very useful when it comes to understanding computer hardware, programming concepts, and the like:

  • Ben Eater has amazing content on his website and on YouTube, including a series on building a 6502 machine on a breadboard, and building a CPU from scratch. Must watch!
  • Reverse Engineering the MOS 6502 is a really great YouTube video that covers the 6502 chip in extreme depth. Really great stuff.
  • “Computer Architecture and Organization” (John P. Hayes, Third Edition) was the textbook used for in the Computer Architecture course when I was in college. This was 20 years ago, but I remember the book being a really good primer, and may prove useful.
  • pagetable.com has proven invaluable in understanding how various features of my beloved Commodore 64c work — including BASIC and the KERNAL. When I’ve been curious about how a feature of BASIC was implemented, I look here, because they have a fully commented listing of the BASIC interpreter. I don’t always go the same direction, but it can be super handy to see how it was done in the past, if I’m on the right path, and if it even makes sense. It also can yield some surprising insights, which I’ll endeavor to write a post about.
  • “But How Do It Know?” (J Clark Scott) is a short read that goes into some of the basics of how computers work. I found it engaging, and you may as well. It’s on Amazon.

Posts in this series:

Up Next:

  • Statement and Expression parsing, 80’s style — and how much I love having even JavaScript’s super limited basic standard library.

Edit History:‌

  • ‌Merged “The Elements of Computing Systems” and NAND To Tetris resources together as they’re the same. Thanks to Jeff for the catch.


Jeff picture

Quick clarification: The Elements of Computing Systems is NAND to Tetris. Highly recommended, not as a bucket list item but just in general!

Kerri Shotts picture

Hahaha!!!! Woops! Thanks for the catch!

Rhett Trickett picture

It was a great first post about a really cool project! Glad to see it got some good attention. Able is a small but growing community that we're building just for posts like these, so thank you for sharing.

Just to mention that we plan to add better support for writing series of posts and also that our stats exlcude repeat views and crawlers, total views on your post were about three times more. If you have any suggestions or feedback feel free to drop something into our suggestion box. Otherwise, welcome! :)

Kerri Shotts picture

Happy to be here, and thanks for the welcome! I've got a few suggestions I'll send on the editing experience, but so far it's been the best one for me thus far... for me, writing needs to be easy, and your editor makes it so. :-)