Julien Verneuil builds a small Forth interpreter in JavaScript. Feed it 3 4 + and it pushes the numbers onto a stack, then runs + to replace them with their sum. The same loop reads new commands and adds them to the language.

  • Look up each token in a dictionary. Built-in and user-defined commands live together. The lookup starts with the newest definition, so you can redefine a command at the prompt.
  • Store a sequence of calls. Between : and ;, the interpreter builds a list of functions for the new command. A number adds a function that pushes the value when the command runs.
  • Let some commands run during compilation. The ; command runs as soon as the interpreter sees it. That lets it end the definition using the same dictionary as ordinary arithmetic.

The prototype leaves out standard Forth features such as loops and common stack operations. It gives you a small enough interpreter to trace by hand and see how a language can define new commands from the ones it already knows.