Find a file
dependabot[bot] 29bd1383bd Bump lodash from 4.17.15 to 4.17.19
Bumps [lodash](https://github.com/lodash/lodash) from 4.17.15 to 4.17.19.
- [Release notes](https://github.com/lodash/lodash/releases)
- [Commits](https://github.com/lodash/lodash/compare/4.17.15...4.17.19)

Signed-off-by: dependabot[bot] <support@github.com>
2021-01-30 14:27:28 +01:00
experiments/waforth2c Remove prelude 2019-11-08 16:19:47 +01:00
public Use webpack for bundling 2018-05-29 20:54:45 +02:00
src Add .S implementation 2020-06-01 16:22:22 +02:00
tests Add .S implementation 2020-06-01 16:22:22 +02:00
.eslintignore Clean up benchmarks 2018-05-29 22:49:43 +02:00
.eslintrc Fix lint 2019-03-09 20:13:00 +01:00
.gitignore Support bulk memory operations 2019-11-09 20:42:34 +01:00
.travis.yml Replace assembler with preprocessor 2019-11-08 22:42:11 +01:00
LICENSE.txt Add license 2018-07-31 09:17:08 +02:00
Makefile Get rid of preprocessor need 2019-11-11 11:40:52 +01:00
package.json Bump lodash from 4.17.15 to 4.17.19 2021-01-30 14:27:28 +01:00
README.md README tweak 2020-01-04 19:57:07 +01:00
webpack.config.js Fix build 2019-11-09 20:54:22 +01:00
yarn.lock Bump lodash from 4.17.15 to 4.17.19 2021-01-30 14:27:28 +01:00

WAForth: Forth Interpreter+Compiler for WebAssembly

WAForth is a bootstrapping Forth interpreter and dynamic compiler for WebAssembly. You can see it in a demo here.

It is entirely written in (raw) WebAssembly, and the compiler generates WebAssembly code on the fly. The only parts for which it relies on external (JavaScript) code is to dynamically load modules (since WebAssembly doesn't support JIT yet), and the I/O primitives to read and write a character to a screen.

Parts of the implementation were influenced by jonesforth.

WAForth is still in an experimental stage. It implements most of the ANS Core Words, and passes most of the Forth 200x Test Suite core word tests.

Install Dependencies

The build uses the WebAssembly Binary Toolkit for converting raw WebAssembly text format into the binary format, and Yarn for managing the dependencies of the shell.

brew install wabt yarn
yarn

Building & Running

To build everything:

make

To run the development server:

make dev-server

Testing

The tests are served from /tests by the development server.

You can also run the tests in Node.JS by running

make check

Design

The WAForth core is written as a single module in WebAssembly's text format.

The Interpreter

The interpreter runs a loop that processes commands, and switches to and from compiler mode.

Contrary to some other Forth systems, this system doesn't use direct threading for executing code. WebAssembly doesn't allow unstructured jumps, let alone dynamic jumps. Instead, WAForth uses subroutine threading, where each word is implemented as a single WebAssembly function, and the system uses calls and indirect calls (see below) to execute words.

The Compiler

While in compile mode for a word, the compiler generates WebAssembly instructions in binary format (since there is no assembler infrastructure in the browser). Since WebAssembly doesn't support JIT compilation yet, a finished word is bundled into a separate binary WebAssembly module, and sent to the loader, which dynamically loads it and registers it with a shared function table at the next offset, which in turn is recorded in the word dictionary.

Because words reside in different modules, all calls to and from the words need to happen as indirect call_indirect calls through the shared function table. This of course introduces some overhead, although it appears limited.

As WebAssembly doesn't support unstructured jumps, control flow words (IF/ELSE/THEN, LOOP, REPEAT, ...) can't be implemented in terms of more basic words, unlike in jonesforth. However, since Forth only requires structured jumps, the compiler can easily be implemented using the loop and branch instructions available in WebAssembly.

Finally, the compiler adds minimal debug information about the compiled word in the name section, making it easier for doing some debugging in the browser.

Debugger view of a compiled
word

The Loader

The loader is a small bit of JavaScript that uses the WebAssembly JavaScript API to dynamically load a compiled word (in the form of a WebAssembly module), and ensuring that the shared function table is large enough for the module to register itself.

The Shell

The shell is a JavaScript class that wraps the WebAssembly module, and loads it in the browser. It provides the I/O primitives to the WebAssembly module to read and write characters to a terminal, and externally provides a run() function to execute a fragment of Forth code.

To tie everything together into an interactive system, there's a small console-based interface around this shell to type Forth code, which you can see in action here.

WAForth Console

Misc notes

  • The exposed return stack isn't used. Control flow is kept implicitly in the code (e.g. through branches, indirect calls, ...). This also means that control flow can't be influenced by code.