forth: BEGIN/UNTIL/WHILE/REPEAT/AGAIN (+9)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-24 17:33:25 +00:00
parent b2939c1922
commit bb16477fd4
3 changed files with 127 additions and 1 deletions

View File

@@ -70,7 +70,7 @@ Representation:
### Phase 3 — control flow + first Hayes tests green
- [x] `IF`, `ELSE`, `THEN` — compile to SX `if`
- [ ] `BEGIN`, `UNTIL`, `WHILE`, `REPEAT`, `AGAIN` — compile to loops
- [x] `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@`
- [ ] Vendor John Hayes' test suite to `lib/forth/ans-tests/`
@@ -99,6 +99,20 @@ Representation:
_Newest first._
- **Phase 3 — `BEGIN`/`UNTIL`/`WHILE`/`REPEAT`/`AGAIN` (+9).** Indefinite-loop
constructs built on the same PC-driven body runner introduced for `IF`.
BEGIN records the current body length on `state.cstack` (a plain numeric
back-target). UNTIL/AGAIN pop that back-target and emit a `bif`/`branch`
op whose target cell is set to the recorded PC. WHILE emits a forward
`bif` with a fresh target cell and pushes it on the cstack *above* the
BEGIN marker; REPEAT pops both (while-target first, then back-pc), emits
an unconditional branch back to BEGIN, then patches the while-target to
the current body length — so WHILE's false flag jumps past the REPEAT.
Mixed compile-time layout (numeric back-targets + dict forward targets
on the same cstack) is OK because the immediate words pop them in the
order they expect. AGAIN works structurally but lacks a test without a
usable mid-loop exit; revisit once `EXIT` lands. 161/161 green.
- **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