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.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 addressPRINT10 \ (5) next address is executedAs well as words, my compiler also supports DO/LOOPs. 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 !.Putting these features together, here's how you can build 10 by adding 1 repeatedly.VARIABLE A : RUN 0 A ! \ initialize A 10 0 DO \ push limit and iterator for DO \ DO places these on the return stack A @ 1 + A ! \ A = A + 1 LOOP \ increment i and exits when i == limit A @ . \ prints 10; RUNThis set of features i...
First seen: 2025-10-07 05:08
Last seen: 2025-10-07 14:09