Find a file
2022-11-15 19:30:21 +01:00
.github github: waforthc fixes 2022-11-15 19:30:21 +01:00
.vscode waforthc: Clean up dependencies 2022-11-15 18:51:26 +01:00
doc standalone: Windows support 2022-10-08 09:16:30 +02:00
public web: Support "Add to Home Screen" 2022-10-02 11:10:27 +02:00
scripts waforthc: Embed wasm-rt files as source 2022-11-12 14:11:59 +01:00
src github: waforthc fixes 2022-11-15 19:30:21 +01:00
.clang-format vscode: Add C auto-formatting 2022-05-22 16:38:54 +02:00
.eslintignore Fix lint errors 2022-07-06 19:53:21 +02:00
.eslintrc.yml Fix lint errors 2022-07-06 19:53:21 +02:00
.gitignore .(, C", PARSE, PARSE-NAME, PAD 2022-05-30 21:29:23 +02:00
build-package.js github: Add Windows support for some standalone steps 2022-10-06 23:29:12 +02:00
build-web.js web: Support "Add to Home Screen" 2022-10-02 11:10:27 +02:00
LICENSE.txt Add license 2018-07-31 09:17:08 +02:00
Makefile Rework processing script 2022-05-28 22:09:41 +02:00
package.json v0.17.1 2022-10-08 09:32:26 +02:00
README.md README tweaks 2022-11-11 20:16:28 +01:00
test-web.js Fix lint errors 2022-07-06 19:53:21 +02:00
tsconfig.json thurtle: Move about text to header 2022-05-14 21:37:14 +02:00
tsconfig.package.json build: Resolve waforth import from examples 2022-05-08 15:32:20 +02:00
yarn.lock Fix lint errors 2022-07-06 19:53:21 +02:00

WAForth: Forth Interpreter+Compiler for WebAssembly

Build

WAForth is a small but complete bootstrapping Forth interpreter and dynamic compiler for WebAssembly. You can see it in action in an interactive Forth console, and in a Logo-like Turtle graphics language.

WAForth is entirely written in (raw) WebAssembly, and the compiler generates WebAssembly code on the fly. The only parts for which it relies on external 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.

The WebAssembly module containing the interpreter, dynamic compiler, and all built-in words comes down to 14k (7k gzipped), with an extra 15k (7k gzipped) for the JavaScript wrapper, web UI, and encoding overhead.

WAForth implements all ANS Core Words (and passes Forth 200x Test Suite core word tests), and many ANS Core Extension Words. You can get the complete list of supported words from the interactive console.

You can read more about the internals and the design of WAForth in the Design document.

Thurtle program
WAForth integrated in Thurtle, a turtle graphics programming environment using Forth

Standalone shell

Although WebAssembly (and therefore WAForth) is typically used in a web environment (web browsers, Node.js), WAForth also has a standalone native command-line shell. You can download a pre-built binary of the standalone shell from the Releases page.

The standalone shell uses the Wasmtime engine, but its build configuration can easily be adapted to build using any WebAssembly engine that supports the WebAssembly C API (although some engines have known issues).

Thurtle program
Standalone WAForth shell executable

Native compiler

waforthc uses WAForth to compile a Forth program into a native executable. WebAssembly is used as the host runtime platform and intermediate representation during compilation, and then compiled into an executable that no longer contains any WebAssembly infrastructure.

Using WAForth in a JavaScript application

You can embed WAForth in any JavaScript application.

A simple example (CodePen) to illustrate starting WAForth, and binding JavaScript functions:

import WAForth from "waforth";

(async () => {
  // Create the UI
  document.body.innerHTML = `<button>Go!</button><pre></pre>`;
  const btn = document.querySelector("button");
  const log = document.querySelector("pre");

  // Initialize WAForth
  const forth = new WAForth();
  forth.onEmit = (c) =>
    log.appendChild(document.createTextNode(c));
  await forth.load();

  // Bind "prompt" call to a function that pops up a JavaScript 
  // prompt, and pushes the entered number back on the stack
  forth.bind("prompt", (stack) => {
    const message = stack.popString();
    const result = window.prompt(message);
    stack.push(parseInt(result));
  });

  // Load Forth code to bind the "prompt" call to a word, 
  // and call the word
  forth.interpret(`
( Call "prompt" with the given string )
: PROMPT ( c-addr u -- n )
  S" prompt" SCALL 
;

( Prompt the user for a number, and write it to output )
: ASK-NUMBER ( -- )
  S" Please enter a number" PROMPT
  ." The number was" SPACE .
;
`);

  btn.addEventListener("click", () => {
    forth.interpret("ASK-NUMBER");
  });
})();

Asynchronous bindings

For asynchronous bindings, use bindAsync instead of bind.

bindAsync expects an execution token on the stack, which is to be called with a success flag after the bound function is called. This is illustrated in the fetch example:

forth.bindAsync("ip?", async () => {
  const result = await (
    await fetch("https://api.ipify.org?format=json")
  ).json();
  forth.pushString(result.ip);
});

forth.interpret(`
( IP? callback. Called after IP address was received )
: IP?-CB ( true c-addr n | false -- )
  IF 
    ." Your IP address is " TYPE CR
  ELSE
    ." Unable to fetch IP address" CR
  THEN
;

( Fetch the IP address, and print it to console )
: IP? ( -- )
  ['] IP?-CB
  S" ip?" SCALL 
;
`);

Goals

Here are some of the goals (and non-goals) of WAForth:

  • WebAssembly-first: Implement as much as possible in (raw) WebAssembly. Only call out to JavaScript for functionality that is not available in WebAssembly (I/O, loading compiled WebAssembly code).
  • Simplicity: Keep the code as simple and clean as possible. Raw WebAssembly requires more effort to maintain than code in a high level language, so avoid complexity if you can.
  • Completeness: Implement a complete (and correct) Forth system, following the ANS Standard, including all ANS Core words.
  • Speed: If some speed gains can be gotten without paying much in simplicity (e.g. better design of the system, more efficient implementation of words, simple compiler improvements, ...), then I do it. However, generating the most efficient code would require a smart compiler, and a smart compiler would introduce a lot of complexity if implemented in raw WebAssembly, so speed is not an ultimate goal. Although the low level of WebAssembly gives some speed advantages, the design of the system will cause execution to consist almost exclusively of indirect calls to small functions, so there will be languages targeting WebAssembly that run faster.
  • Binary size: Since the entire system is written in raw WebAssembly, and since one of the main goals is simplicity, the resulting binary size is naturally quite small (±12k). However, I don't do any special efforts to save bytes here and there in the code (or the generated code) if it makes things more complex.
  • Ease of use: Like most Forths, I currently don't do much effort to provide functionality to make Forth programming easy and safe (helpful errors, stacktraces, strict bounds checks, ...). However, the compiler emits debug information to help step through the WebAssembly code of words, and I hope to add more debugging aids to the compiler in the future (if it doesn't add too much complexity)

Debugger view of a compiled word

Development

Install Dependencies

The build uses the WebAssembly Binary Toolkit for converting raw WebAssembly text format into the binary format, and Yarn (and therefore Node.JS) for managing the build process and the dependencies of the shell.

brew install wabt yarn
yarn

Building & Running

To build everything:

yarn build

To run the development server:

yarn dev

Testing

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

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

yarn test