This is a Python implementation of a Forth-like stack-based programming language interpreter. It provides a minimal but functional environment for experimenting with stack-based programming.
- Basic arithmetic operations (+, -, *, /, mod)
- Stack manipulation operations (dup, drop, swap, over, rot, nip, tuck)
- Comparison operations (=, <, >, <=, >=, <>)
- Logical operations (and, or, not)
- I/O operations (., .s, cr)
- User-defined words (functions)
- Simple memory operations (!, @)
- Conditional execution (if-then-else)
- Loop constructs (begin-until, begin-while-repeat)
- CREATE word for defining expandable words
- String literals and printing
Run the interpreter in interactive mode:
python forth.pyRun a Forth script file:
python forth.py example.fthRun tests:
python test_forth.pyThe interpreter will run some examples and then enter interactive mode where you can type Forth commands.
When running a script file, the interpreter will execute all commands in the file and exit.
Comments (lines starting with \) and empty lines are ignored when running script files.
> 5 3 + .
8
> 1 2 3 dup .s
<1 2 3 3>
> swap .s
<1 3 2>
> drop .s
<1 3>
> : square dup * ;
> 5 square .
25
> create sum 1 2 ;
> : add-sum sum + . cr ;
> add-sum
3
> 10 5 > if 1 . else 0 . then
1
> : countdown ( n -- ) begin dup . 1 - dup 0 = until drop ;
> 5 countdown
5 4 3 2 1
> 42 100 ! \ Store 42 at address 100
> 100 @ . \ Fetch value from address 100
42
+- Add top two numbers-- Subtract top number from second*- Multiply top two numbers/- Divide second number by top (integer division)mod- Modulo operation
dup- Duplicate top itemdrop- Remove top itemswap- Swap top two itemsover- Copy second item to toprot- Rotate third item to topnip- Remove second itemtuck- Copy top item below second itemdepth- Push stack depth onto stack
=- Equal<- Less than>- Greater than<=- Less than or equal>=- Greater than or equal<>- Not equal
and- Logical ANDor- Logical ORnot- Logical NOT
.- Print and remove top number.s- Print entire stackcr- Print newline."- Print a string literal (e.g.,." Hello, World!")vlist- List all defined words
Example:
> ." Hello, World!" cr
Hello, World!
> : greet ." Welcome to Forth!" cr ;
> greet
Welcome to Forth!
!- Store (value address !)@- Fetch (address @)
if ... then- Conditional executionif ... else ... then- Conditional with alternativebegin ... until- Loop until condition is truebegin ... while ... repeat- Loop while condition is true
: name ... ;- Define a new wordcreate name ... ;- Define an expandable word that can be used in other definitions
In Forth:
- Numbers are pushed onto a stack
- Operations consume values from the stack and push results back
- True is represented as -1, false as 0
- Words are separated by whitespace
- Comments start with
\and continue to the end of the line
The create word allows defining words that expand to their definition when used in other word definitions. This enables composability where complex words can be built from simpler ones.
Example:
create sum 1 2 ;
: add-sum sum + . cr ;
add-sum \ Prints 3
In this example:
create sum 1 2 ;defines a wordsumthat pushes 1 and 2 to the stack: add-sum sum + . cr ;defines a word that usessum- when compiled,sumexpands to1 2- When
add-sumis executed, it pushes 1 and 2 to the stack, adds them, and prints the result
Run the test suite:
python test_forth.pyThe test suite verifies all core functionality including arithmetic, stack operations, comparisons, logic operations, user-defined words, memory operations, and control flow.
This is a simplified implementation and lacks many features of a full Forth system:
- Simplified loop implementation
- No floating-point support
- No proper error handling for all cases
- No support for strings
- No file I/O
- No multitasking
This is free and unencumbered software released into the public domain.