1.8 KiB
1.8 KiB
Forth Interpreter in Haskell
Implements a very simple subset of Forth
Inspired by https://exercism.org/tracks/haskell/exercises/forth
Run
ghci forth.hs
Usage
Calculate 2 + 8
*Forth> run "2 8 +"
Right ForthState [10]
Push 1 to stack and duplicate it three times
*Forth> run "1 DUP DUP DUP"
Right ForthState [1,1,1,1]
Calculate 5 + (5*8)
*Forth> run "5 8 OVER * +"
Right ForthState [45]
Instructions
| Instruction | Description | |
|---|---|---|
| + | [a, b, ...] -> [a+b, ...] | Pop two items, a and b, from stack. Push a+b to top |
| - | [a, b, ...] -> [a-b, ...] | Pop two items, a and b, from stack. Push a-b to top |
| * | [a, b, ...] -> [a*b, ...] | Pop two items, a and b, from stack. Push a*b to top |
| / | [a, b, ...] -> [a/b, ...] | Pop two items, a and b, from stack. Push a/b to the top Returns Left DivisionByZero if b is 0 |
| DUP | [a, b, ...] -> [a, a, b, ...] | Copy the top stack item and push it to the top |
| DROP | [a, b, ...] -> [b, ...] | Discard the top of the stack |
| SWAP | [a, b, ...] -> [b, a, ...] | Pop two values, a and b, from the stack. Push them back in swapped order |
| OVER | [a, b, ...] -> [b, a, b, ...] | Copy the next value after the top of the stack and push it to the top |
| Any number | [a, b, ...] -> [x, a, b, ...] | Push number to top of stack. Integers only |
Read more
Forth (programming language), Wikipedia
https://en.wikipedia.org/wiki/Forth_(programming_language)#Overview