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
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user