https://github.com/olemorud/forth
Interpreter in Haskell for a very simple subset of Forth
https://github.com/olemorud/forth
forth-like haskell
Last synced: about 2 months ago
JSON representation
Interpreter in Haskell for a very simple subset of Forth
- Host: GitHub
- URL: https://github.com/olemorud/forth
- Owner: olemorud
- Created: 2023-05-11T11:10:55.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-05-12T13:20:31.000Z (about 2 years ago)
- Last Synced: 2025-02-09T02:34:47.666Z (3 months ago)
- Topics: forth-like, haskell
- Language: Haskell
- Homepage:
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Forth Interpreter in Haskell
Implements a very simple subset of Forth
Inspired by https://exercism.org/tracks/haskell/exercises/forth
## Run
```sh
ghci forth.hs
```## Usage
Calculate 2 + 8
```haskell
*Forth> run "2 8 +"
Right ForthState [10]
```Push 1 to stack and duplicate it three times
```haskell
*Forth> run "1 DUP DUP DUP"
Right ForthState [1,1,1,1]
```Calculate 5 + (5\*8)
```haskell
*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
#### Starting _FORTH_
https://www.forth.com/starting-forth/0-starting-forth/