Lobsters9 min readintermediate
How I Wrote a Forth (Without Knowing How)
Summary
The author details building MoonForth, a Forth interpreter written in C that leverages Lua's C API for stack manipulation and word storage. The process was surprisingly straightforward, reinforcing the idea that Forth "writes itself" by iteratively filling missing functionality.
- JonesForth's x86 assembly source served as a conceptual guide for Forth word implementation, despite being in a different language.
- Lua's C API stack was directly used to implement core Forth stack manipulation words, treating it as the Forth data stack.
- Forth words are stored as Lua tables containing C function pointers, allowing flexible redefinition and literal storage.
- The "Forth writes itself" principle means development progresses by implementing missing words as needed to enable desired functionality.
Engineers interested in language implementation, Forth, or leveraging Lua's C API will find practical insights into building a stack-based language and interfacing C with Lua.
7/10