Tech News
← Back to articles

Compiling a Forth

read original related products more articles

I was curious how Forth worked so I built a bytecode compiler and a VM for a Forth-like language, as well as some visualizations to show how it all works.

You don't need to know anything about Forth to follow along, aside from the fact it's a stack-oriented language.

Here's a small program that prints the number three.

3 .

The number ( 3 ) is pushed to the data stack, and then the dot ( . ) pops it from the data stack and prints it.

We'll need more Forth features than this to build interesting programs.

Forth has two built-in stacks. The data stack (sometimes just called "the stack") and the return stack. When a word is called in Forth (words are like functions) the address of the next instruction is pushed to the return stack. When the word finishes executing, the return stack is popped into the instruction pointer.

\ (1) word declaration : PRINT10 \ (3) the word body is executed 10 . \ (4) ";" compiles an exit – at runtime it pops the return stack \ into the instruction pointer. ; \ (2) instruction pointer lands on a word, \ the next address is pushed to the return stack, \ and the instruction pointer is set to the word address PRINT10 \ (5) next address is executed

As well as words, my compiler also supports DO / LOOP s. These use the return stack too. When DO executes, it pops the limit and the iterator from the data stack and stores them in the return stack. This allows the inner loop to freely operate on the data stack. When LOOP executes, it pops the limit and iterator from the return stack, adds one to the iterator and compares it to the limit (and exits or loops again).

There are also variables, which can be declared with VARIABLE X , loaded with X @ , and stored with 1 X ! .

... continue reading