forth: IF/ELSE/THEN + PC-driven body runner (+18)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-24 17:03:41 +00:00
parent 30d76537d1
commit b2939c1922
4 changed files with 238 additions and 3 deletions

View File

@@ -69,7 +69,7 @@ Representation:
- [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`
- [x] `IF`, `ELSE`, `THEN` — compile to SX `if`
- [ ] `BEGIN`, `UNTIL`, `WHILE`, `REPEAT`, `AGAIN` — compile to loops
- [ ] `DO`, `LOOP`, `+LOOP`, `I`, `J`, `LEAVE` — counted loops (needs a return stack)
- [ ] Return stack: `>R`, `R>`, `R@`, `2>R`, `2R>`, `2R@`
@@ -99,6 +99,19 @@ Representation:
_Newest first._
- **Phase 3 start — `IF`/`ELSE`/`THEN` (+18).** `lib/forth/compiler.sx`
+ `tests/test-phase3.sx`. Colon-def body switched from `for-each` to
a PC-driven runner so branch ops can jump: ops now include dict tags
`{"kind" "bif"|"branch" "target" cell}` alongside the existing
`(fn (s) ...)` shape. IF compiles a `bif` with a fresh target cell
pushed to `state.cstack`; ELSE emits an unconditional `branch`,
patches the IF's target to the instruction after this branch, and
pushes the new target; THEN patches the most recent target to the
current body length. Nested IF/ELSE/THEN works via the cstack.
Also fixed `EMIT`: `code-char``char-from-code` (spec-correct
primitive name) so Phase 1/2 tests run green on sx_server.
152/152 (Phase 1 + 2 + 3) green.
- **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