Forth-style Stack Language in C. Runs natively or, in browser via wasm.
Live demo at https://danielrholland.codeberg.page/forth-style-stack-lang/
Assuming you have clang and make installed, the native executable can be built with make build
, then run with ./forth
.
If you also have rlwrap installed, you can run make run-rlwrap
, to get completions, history, and more pleasant line-editing.
make build-wasm
will build the wasm version. Serve ./forthlib.wasm
, forthlib.js
, forth.js
and index.html
from a web server and open index.html
to use it.
Current builtin operations:
.
- pop and print valpeek
- peek and print val+
- pop top two vals and push second+top-
- pop top two vals and push second-top*
- pop top two vals and push second*top/
- pop top two vals and push second/topnegate
- replace top val with 0 - valueabs
- replace top val with its absolutemod
- second modulo topmax
- largest of top two valsmin
- largest of top two valsdup
- duplicate top valnot
- if top val is 0 set to 1, else set to 0=
- pop top two valsswap
- swap order of top two valsdrop
- discard top valover
- copy second val and push itrot
- rotate top three vals - top to third, second to top, third to secondpick
- pop a val n, then copy the nth value down from the top and push itroll
- pop a val n, then copy the nth value down from the top and push itdepth
- push that stack sizeclearstack
- clear the stackif <body> then
- an optional section.if
pops a val, and if that val is 0, skips to then. (Branch if zero): <name> <body> ;
- a procedure declaration. A new op is added, when it is run, the tokens in the body are evaluated. e.g.: incr 1 + ;
will define a new operationincr
, that increments the value at the top of the stack.nip
- discards the second val from the toptuck
- copies the top val and inserts it between the second and thirdincr
- increments the top value
Literals are pushed.
At present only integers are supported.
At present, loops haven't been added, just use recursion.
Released into the public domain.