Skip to content
/ forth Public

Interpreter in Haskell for a very simple subset of Forth

Notifications You must be signed in to change notification settings

olemorud/forth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

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

Starting FORTH

https://www.forth.com/starting-forth/0-starting-forth/

About

Interpreter in Haskell for a very simple subset of Forth

Topics

Resources

Stars

Watchers

Forks