BASIC Interpreter

by pumpkinhead
See Code Download Embed
This is BASIC. I'm not going to write down all of how BASIC works, but here is a website that briefs on how it works:

Note that the website has some special variables/functions that are exclusive to their interpreter. There is no LEVEL constant and arrays don't work like that here. Also you need to declare variable types here. Also, the graphics capabilities I programmed here differ from the ones they say. Additionally, there is no modulo operator because % is already used for something else. But I added a modulo function in case you need that. mod(a, b)


The Quite BASIC help does not tell you how to make variables types, because their interpreter is special and does not use typed variables. But my interpreter does.

Let us have a variable named "TEST". If we want to make that a string:

LET TEST$ = "Hello, world!"

(I think $ was chosen because it looks like S, which could stand for string)

If we want to make that an integer:


And if we just want to make that a floating point number:

LET TEST = 3.14159265

Also you don't have to name your variables one letter with several digits after here, unlike what it says in the Quite BASIC help page. You can name it any text, like "TEST". "T123" works here too. And even "123T". That's weird


To get the last key pressed, use the GETCHAR function. It will return the keycode of the last key pressed.

For letter keys, they are the unicode value of the text from the unicode of () block. Here are a few useful keycodes:

LEFT: 37
UP: 38
DOWN: 40


I put in a few special commands/variables that allow you to draw things on screen. It uses turtle graphics because that is how pen works in Snap!.

- TGOTO x,y: Equivalent to the go to x: () y: () block
- TDOWN: Equivalent to the pen down block
- TUP: Equivalent to the pen up block
- TMOVE steps: Equivalent to the move () steps block
- TCLEAR: Equivalent to the clear block
- THUE hue: Sets pen hue
- TSAT saturation: Sets pen saturation
- TBRI brightness: Sets pen brightness
- THUEC d: Changes pen hue
- TSATC d: Changes pen saturation
- TBRIC d: Changes pen brightness 

Special variables:
- TURTLEX: X position of turtle
- TURTLEY: Y position of turtle
- TURTLEDIR: Direction of turtle
- TURTLESIZE: Pen size

These special variables work just like regular variables: you can set them using LET and get them by calling their name.


11/5/20: Expressions are compiled into an expression tree before running. This makes it much faster. In the circle example, it took about 20 seconds to draw the circle previously. Now it takes just 3 -- 566% faster.


Actually I didn't know how to program in BASIC before starting this project 

Created August 22, 2020

Last updated November 9, 2020

Published September 26, 2020