Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including js-forth with all npm packages installed. Try it out:

var jsForth = require("js-forth")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

js-forth v0.2.0


An implementation of Forth in Javascript

Try it out here

ANS Forth

jsForth implements the full core ANS standard

The following word sets are implemented:

  • Core words - fully implemented
    • All tests pass except some integer multiplication/division edge cases where the ANS spec differs from the results given by the long package
  • Core plus words - fully implemented and all tests pass
  • Core extension words - fully implemented and all tests pass

The ANS Forth tests can be run using npm run test or in the intepreter with the following forth code

include test/verbose-tester.fth

The complete test suite is available here

js interop

  • global variable access js /document
  • array access js .0.2
  • property access js .name
  • property setting js .name!
  • function calling js .sin{1}
  • method calling js /document.getElementById{1}
  • new with params js .new{1}

Threading model - trampolined threading

All of the standard threading models require call or jump to execute the next instruction. In js these both translate into functions calls in tail position. Without tail call optimisation this will lead to a stack overflow. Therefore jsForth uses a trampoline to execute instructions.

ECMAScript 6 specifies tail call optimisation but unfortunately most browsers don't support it yet

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free