forth: TCO at colon-def endings (no extra frame on tail-call ops)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-25 04:29:57 +00:00
parent f6e333dd19
commit 0d6d0bf439
2 changed files with 26 additions and 4 deletions

View File

@@ -274,15 +274,27 @@
(first cs)
(forth-find-do (rest cs))))))
;; Run a colon-def body. The PC is a one-cell dict so step-op can mutate
;; it for branches/loops/exit. As an explicit TCO, when we reach the
;; final op AND it's a plain function (not a branch dict), call it in
;; tail position — no recurse, no post-increment — so chains of
;; colon-def `: A ... B ;` calls don't accumulate continuation frames.
(define
forth-run-body
(fn
(s ops pc n)
(when
(< (get pc "v") n)
(begin
(forth-step-op s (nth ops (get pc "v")) pc)
(forth-run-body s ops pc n)))))
(let
((cur (get pc "v")))
(let
((op (nth ops cur)))
(if
(and (not (dict? op)) (= (+ cur 1) n))
(op s)
(begin
(forth-step-op s op pc)
(forth-run-body s ops pc n))))))))
;; Override forth-interpret-token to branch on compile mode.
(define