This project implements a minimal Forth-like compiler that reads a .forth program and generates x86_64 assembly code which can be compiled and executed on Linux systems.
The compiler parses basic arithmetic operations and print commands, generating corresponding assembly instructions.
- Supports pushing integers to the stack.
- Supports addition (
+). - Supports print (
.) — outputs the top of the stack. - Generates valid assembly code for x86_64 architecture.
- Can be assembled and linked using
gccoras/ld.
compiler.py— the Forth to assembly compiler.program.forth— sample Forth program.output.s— generated assembly code.output— compiled executable (after runninggcc).
3 4 + .
Expexted output: 7
How to Use
1. Write your Forth program in program.forth.
2. Generate assembly: python3 compiler.py program.forth output.s
3. Compile the assembly to an executable:gcc output.s -o output
4. Run the program: ./output
How It Works
The compiler performs these steps:
Reads the Forth source code and tokenizes it.
Translates numbers into immediate mov instructions or prepares them for stack-like operations.
Generates assembly for addition (+) and output (.) using printf.
Writes out .text and .rodata sections in valid x86-64 assembly format.
You can compile the generated assembly using gcc or another assembler.
:
Example Workflow
python3 compiler.py program.forth output.s
gcc output.s -o output
./output
Output: 7
Current Limitations
Only addition is supported (+).
Only integer printing (.).
No variables, no loops or conditionals.
Stack size is managed at compile time, no dynamic stack.
Minimal error handling.
Possible Extensions
If time permits:
Add subtraction (-) and multiplication (*).
Add basic variables.
Add stack underflow checks.
Support for more complex Forth features like loops or if/else.
Author
Levon Ghukasyan