Recover agent-loop progress: lua/prolog/forth/erlang/haskell phases 1-2

Salvaged from worktree-agent-* branches killed during sx-tree MCP outage:
- lua: tokenizer + parser + phase-2 transpile (~157 tests)
- prolog: tokenizer + parser + unification (72 tests, plan update lost to WIP)
- forth: phase-1 reader/interpreter + phase-2 colon/VARIABLE (134 tests)
- erlang: tokenizer + parser (114 tests)
- haskell: tokenizer + parse tests (43 tests)

Cherry-picked file contents only, not branch history, to avoid pulling in
unrelated ocaml-vm merge commits that were in those branches' bases.
This commit is contained in:
2026-04-24 16:03:00 +00:00
parent e274878052
commit 99753580b4
32 changed files with 7803 additions and 36 deletions

View File

@@ -49,24 +49,24 @@ Representation:
## Roadmap
### Phase 1 — reader + interpret mode
- [ ] `lib/forth/reader.sx`: whitespace-split, number parsing (base-aware)
- [ ] `lib/forth/runtime.sx`: stack as SX list, push/pop/peek helpers
- [ ] Core stack words: `DUP`, `DROP`, `SWAP`, `OVER`, `ROT`, `NIP`, `TUCK`, `PICK`, `ROLL`, `?DUP`, `2DUP`, `2DROP`, `2SWAP`, `2OVER`
- [ ] Arithmetic: `+`, `-`, `*`, `/`, `MOD`, `/MOD`, `NEGATE`, `ABS`, `MIN`, `MAX`, `1+`, `1-`, `2*`, `2/`
- [ ] Comparison: `=`, `<`, `>`, `<=`, `>=`, `0=`, `0<`, `0>`
- [ ] Logical: `AND`, `OR`, `XOR`, `INVERT`
- [ ] I/O: `.` (print), `.S` (show stack), `EMIT`, `CR`, `SPACE`, `SPACES`
- [ ] Interpreter loop: read word, look up, execute, repeat
- [ ] Unit tests in `lib/forth/tests/interp.sx`
- [x] `lib/forth/reader.sx`: whitespace-split, number parsing (base-aware)
- [x] `lib/forth/runtime.sx`: stack as SX list, push/pop/peek helpers
- [x] Core stack words: `DUP`, `DROP`, `SWAP`, `OVER`, `ROT`, `-ROT`, `NIP`, `TUCK`, `PICK`, `ROLL`, `?DUP`, `DEPTH`, `2DUP`, `2DROP`, `2SWAP`, `2OVER`
- [x] Arithmetic: `+`, `-`, `*`, `/`, `MOD`, `/MOD`, `NEGATE`, `ABS`, `MIN`, `MAX`, `1+`, `1-`, `2+`, `2-`, `2*`, `2/`
- [x] Comparison: `=`, `<>`, `<`, `>`, `<=`, `>=`, `0=`, `0<>`, `0<`, `0>`
- [x] Logical: `AND`, `OR`, `XOR`, `INVERT` (32-bit two's-complement sim)
- [x] I/O: `.` (print), `.S` (show stack), `EMIT`, `CR`, `SPACE`, `SPACES`, `BL`
- [x] Interpreter loop: read word, look up, execute, repeat
- [x] Unit tests in `lib/forth/tests/test-phase1.sx` — 108/108 pass
### Phase 2 — colon definitions + compile mode
- [ ] `:` opens compile mode and starts a definition
- [ ] `;` closes it and installs into the dictionary
- [ ] Compile mode: non-IMMEDIATE words get appended as SX references; numbers get compiled as literals; IMMEDIATE words (like `IF`) run now
- [ ] `VARIABLE`, `CONSTANT`, `VALUE`, `TO`
- [ ] `@` (fetch), `!` (store), `+!`
- [ ] Compile a colon def into an SX lambda that the CEK runs directly
- [ ] Tests: define words, call them, nest definitions
- [x] `:` opens compile mode and starts a definition
- [x] `;` closes it and installs into the dictionary
- [x] Compile mode: non-IMMEDIATE words are compiled as late-binding call thunks; numbers are compiled as pushers; IMMEDIATE words run immediately
- [x] `VARIABLE`, `CONSTANT`, `VALUE`, `TO`, `RECURSE`, `IMMEDIATE`
- [x] `@` (fetch), `!` (store), `+!`
- [x] Colon-def body is `(fn (s) (for-each op body))` — runs on CEK, inherits TCO
- [x] Tests in `lib/forth/tests/test-phase2.sx` — 26/26 pass
### Phase 3 — control flow + first Hayes tests green
- [ ] `IF`, `ELSE`, `THEN` — compile to SX `if`
@@ -99,7 +99,25 @@ Representation:
_Newest first._
- _(not started)_
- **Phase 2 complete — colon defs, compile mode, VARIABLE/CONSTANT/VALUE/TO, @/!/+! (+26).**
`lib/forth/compiler.sx` plus `tests/test-phase2.sx`.
Colon-def body is a list of ops (one per source token) wrapped in a single
lambda. References are late-binding thunks so forward/recursive references
work via `RECURSE`. Redefinitions take effect immediately.
VARIABLE creates a pusher for a symbolic address stored in `state.vars`.
CONSTANT compiles to `(fn (s) (forth-push s v))`. VALUE/TO share the vars dict.
Compiler rewrites `forth-interpret` to drive from a token list stored on
state so parsing words (`:`, `VARIABLE`, `TO` etc.) can consume the next
token with `forth-next-token!`. 134/134 (Phase 1 + 2) green.
- **Phase 1 complete — reader + interpret mode + core words (+108).**
`lib/forth/{runtime,reader,interpreter}.sx` plus `tests/test-phase1.sx`.
Stack as SX list (TOS = first). Dict is `{lowercased-name -> {:kind :body :immediate?}}`.
Data + return stacks both mutable. Output buffered in state (no host IO yet).
BASE-aware number parsing with `$`, `%`, `#` prefixes and `'c'` char literals.
Bitwise AND/OR/XOR/INVERT simulated over 32-bit two's-complement.
Integer `/` is truncated-toward-zero (ANS symmetric), MOD matches.
Case-insensitive lookup. 108/108 tests green.
## Blockers