A bare bones Forth-like interpreter framework in Haskell
based on hsc3-forth from: http://rohandrape.net/t/hsc3-forth
There is a library, HForth that provides a data and control stack as well as a
number of basic primitives. The data stack is generic. so you can adapt it to
the base type of your choice. (The example apps use HForth with Rationals.)
As well as the generic data stack type, Strings can be stored on the data stack.
There is no access to raw addresses.
HForth has
- some execution control primitives
- ':' and ';' define words
IFELSETHENDOLOOPIJEXIT?EXITexit (conditionally) the excution of the current word<RR>call and returnEMITKEYTYPEperform IO.Sprints the data stack'pops the top of the stack and prints itFORKKILLKILLALLallow a word to run in parallel, and be terminatedPAUSEpauses the current threadS"pushes a string onto the stack.'puts the next word onto the stack andEXECUTEexecutes itINCLUDEDreads code from a fileBYEquitsVMSTATandTRACEprovide debugging information
- local words (enclosed in
{ }can be defined in a word definition DROPDUPOVERPICKROTSWAP2DUP0<-are stack primitivesRECURSIVEallows / forbids recursive name definitions depending on whether the top of the stack is true or false
There is a tutorial file. The fs folder contains some further examples.
> cabal run h-forth -- --help
Up to date
H-FORTH
Usage: h-forth [-r|--recursive] [-t|--tracing INT] [FORTH SOURCE FILENAMES...]
A rudimentary Forth repl written in Haskell
Available options:
-r,--recursive Allow recursive word definitions
-t,--tracing INT tracing level (-1 to 3) (default: 1)
-h,--help Show this help textrat-forth provides an example of how to define some further basic (arithmetic)
primitive words.
To run the tutorial, you need a couple of files defining additional words.
> cabal run rat-forth -- fs/stdlib.fs fs/ratlib.fs fs/tutorial.fsA further example is
> cabal run rat-forth -- fs/preForth-rts.pre fs/preForth-i386-backend.pre fs/preForth.preAnd some examples from buzzard.2
> cabal run buzzard fs/buzzard/third fs/buzzard/demo5.th