WS-012 -- INCLUDE/INCLUDED:
- Injected source loader (core stays IO-free: CLI installs a
filesystem reader, web leaves it unset -> defined error). Recursive
include_file feeds files line-by-line through evaluate, so compile
state and SEE capture span lines for free. Cycle detection, depth
cap 16, paths relative to the including file, SOURCE-ID per nesting
level, parent input restored on success/error/BYE.
- CLI file mode now runs through the include machinery: `wafer x.fth`
gets file:line error context and a base dir for nested INCLUDEs.
- Unlocks the REMEMBER+INCLUDE reload loop.
WS-008 -- error reporting remainder:
- Errors inside included files carry `file.fth:12:` context
(anyhow context chain; CLI prints {e:#}).
- describe_uncaught now returns typed WaferError::UncaughtThrow
{ code, message } -- display text unchanged, THROW code reachable
via downcast for CLI/web consumers.
- compile_word emits a WASM name section; wasmtime trap backtraces
name the faulting word and runtime_native prefixes "in <WORD>:".
Batch/consolidated modules stay unnamed (no name plumbing there;
boot primitives rarely trap).
WS-003 -- SwiftForth correctness lane:
- compare_all_programs_sf64 runs the program corpus with sf64 as
oracle; whitespace-token comparison (sf64 prints numbers
space-prefixed and echoes piped lines). 34/35 parity; dot-quote
skipped (interpret-mode ." is a SwiftForth no-op). #[ignore]d like
the gforth lane; `just compare-correctness` runs both.
WS-011 leftovers:
- WORDS ALL: grouped full view -- one section per wordlist (search
order first), then internal words, each with counts. Backed by
Dictionary::visible_entries (name, wid, internal); visible_words
now derives from it.
- .RS / RDEPTH: return-stack introspection in boot.fth over a new
RP@ primitive (IrOp::RpFetch); BEGIN/WHILE walk so the walk never
touches the stack it prints. SPACES clamped per 6.1.2230.
549 unit + 11 compliance + 9(+2) comparison + 5 crypto + 1 bench
green; fmt/clippy clean; --no-default-features and wasm32 web checks
pass.
5.0 KiB
WAFER Project Conventions
What is WAFER?
WAFER (WebAssembly Forth Engine in Rust) is an optimizing Forth 2012 compiler targeting WebAssembly. Currently a working Forth system with 200+ words, JIT compilation, 12 word sets at 100% compliance, and a full optimization pipeline (peephole, constant folding, inlining, strength reduction, DCE, tail calls, stack-to-local promotion with loop/IF support, self-recursive direct calls, consolidation). Beats gforth on all benchmarks in release mode. Includes a browser-based REPL via wasm-pack.
Architecture
- Each Forth word compiles to its own WASM module via
wasm-encoder - Modules share memory, globals (dsp/rsp/fsp), and a function table via runtime imports
- IR-based compilation: Forth ->
Vec<IrOp>-> WASM codegen -> runtime instantiation - Dictionary: linked-list in a
Vec<u8>buffer simulating WASM linear memory - Primitives: either IR-based (compiled to WASM) or host functions (closures via
HostFn) - Runtime trait:
ForthVM<R: Runtime>is generic over the execution backendNativeRuntime(wasmtime) -- CLI, tests, AOT compilationWebRuntime(js-sys) -- browser REPL via wasm-pack
Crate Structure
crates/core-- compiler, optimizer, codegen, dictionary, runtime traits, outer interpretercrates/cli-- CLI REPL with rustyline,wafer build/wafer runcommandscrates/web-- browser REPL (wasm-bindgen entry point, WebRuntime, HTML/CSS/JS frontend)
Key Files
crates/core/src/outer.rs--ForthVM<R: Runtime>: outer interpreter, compiler, all primitivescrates/core/src/runtime.rs--Runtime+HostAccesstraits (execution backend abstraction)crates/core/src/runtime_native.rs--NativeRuntime: wasmtime implementation (behindnativefeature)crates/core/src/codegen.rs-- IR-to-WASM translation, module generationcrates/core/src/dictionary.rs-- Dictionary data structure with create/find/revealcrates/core/src/ir.rs-- IrOp enum (the intermediate representation)crates/core/src/memory.rs-- Memory layout constants (stack regions, dictionary base, etc.)crates/core/src/optimizer.rs-- IR optimization passes (peephole, fold, inline, DCE, etc.)crates/core/src/config.rs-- WaferConfig: unified optimization configurationcrates/core/src/consolidate.rs-- Consolidation recompiler (single-module direct calls)crates/core/boot.fth-- Bootstrap Forth definitions loaded at startupcrates/cli/src/main.rs-- CLI REPL with rustylinecrates/web/src/lib.rs--WaferReplwasm-bindgen entry pointcrates/web/src/runtime_web.rs--WebRuntime: browser WebAssembly API via js-syscrates/web/www/-- Frontend (index.html, style.css, app.js)
Feature Flags (wafer-core)
default = ["native"]-- includes wasmtime, NativeRuntime, runner, export, etc.native-- enablesdep:wasmtimeand all native-only modules- No features -- pure Rust only (dictionary, IR, optimizer, codegen, outer interpreter). Used by
wafer-web.
Adding a New Word
IR primitive (simple stack/arithmetic/logic -- preferred when possible):
self.register_primitive("WORD_NAME", false, vec![IrOp::Dup, IrOp::Mul])?;
Host function (needs Rust logic -- I/O, dictionary manipulation, complex stack access):
let shared_state = Arc::clone(&self.some_field);
let func: HostFn = Box::new(move |ctx: &mut dyn HostAccess| {
let sp = ctx.get_dsp();
let val = ctx.mem_read_i32(sp);
// ... logic using ctx for memory/global access ...
ctx.set_dsp(sp + CELL_SIZE);
Ok(())
});
self.register_host_primitive("WORD_NAME", false, func)?;
Special interpreter token (defining words like VARIABLE, CONSTANT, CREATE):
Handle in interpret_token_immediate() or compile_token() as a special case.
Code Style
cargo fmt --allandcargo clippy --workspacemust pass with no warnings- Every public function needs a doc comment
- Use
thiserrorfor error types in core crate,anyhowfor CLI - Prefer returning
Resultover panicking
Testing
- Run
cargo test --workspacebefore committing (currently 542 unit + 1 benchmark + 11 compliance + 9 comparison + 5 crypto) - Forth 2012 compliance:
cargo test -p wafer-core --test compliance - Cross-engine comparison (vs gforth):
cargo test -p wafer-core --test comparison - Performance benchmarks (release mode):
cargo test -p wafer-core --test comparison -- --nocapture --ignored - Test helper in outer.rs:
eval_output("forth code")returns printed output as String - Test helper:
eval_stack("forth code")returns data stack as Vec
Web REPL
- Build:
cd crates/web && wasm-pack build --target web --out-dir www/pkg - Serve:
python3 -m http.server -d crates/web/www 8080 - Open:
http://localhost:8080/ - Dev build (faster, unoptimized):
wasm-pack build --target web --dev --out-dir www/pkg
Key Principles
- Correctness first, performance second
- Maximize Forth, minimize Rust (self-hosting goal -- not yet started)
- Test-driven: if it's not tested, it doesn't work
- Every word set at 100% compliance before moving to the next
- Never break existing tests