Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Here's a cool dynamic WebAssembly based Forth:

https://el-tramo.be/blog/waforth/

>The Interpreter

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

>Contrary to some other Forth systems, WAForth doesn’t use direct threading for executing code, where generated code is interleaved with data, and the program jumps between these pieces of 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 (as there is no assembler infrastructure in the browser). Because 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 in 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.

>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.



Someone has done a port of PostScript (GhostScript) to WebAssembly:

https://chrome.google.com/webstore/detail/postscript-viewer/...

I wonder if NeWS, or something similar, could be resurrected in-browser?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: