I want to write a BASIC interpreter, an interactive program that lets you enter commands and write and edit small programs in the BASIC programming language.

To make it hard, I’m going to do it the wrong way, not learning anything about interpreters or programming languages beforehand, but just starting programming something, hopefully learning along the way.

And to make it even harder, I’ve decided to write the whole thing in C - without any libraries (apart from standard input output and memory management).

I’ve decided to go with the version of BASIC I grew up with: Microsoft BASIC v2.0 as it existed on the Commodore 64, but I am probably going to make a lot of changes along the way.

Editor and runtime environment all in one

BASIC was quite unique for its time, in that it was both an environment, an editor and a programming language - kind of like the REPLs we see nowadays with Python and JavaScript, where you have a console that allows you to write single lines of code, or define functions, but more than that, in that the program itself was also written and edited inside that environment.

You didn’t write the code in a separate file, but simply started a line with a number, and then that became a line in the program. Like if you wrote 10 PRINT "HELLO" and pressed enter, you’d have added line 10 to your program, with the command PRINT and the parameter “HELLO”. If you wanted to edit the line, you either retyped it, or scrolled back to the line in your terminal/console, and edited that version - basically retyping it, but with the help of your terminal software.

In addition you could start, stop, pause, and continue the running of the program, inspect or change variables, and that way have a very interactive debugging environment.

I’m going to do the same, make an editor-environment to enter commands and edit and run programs.

Source code is available

The complete source - with all development steps - is available on Github: https://github.com/peterlinddk/basic-interpreter

Chapters

Introduction to commands

Editor

Implementing (some) commands

Tokens and tokenizing

Clean up code structure

Changing the Tokenizer a bit

Numbers