go: defer + LIFO drain → eval 86/86, total 503/503 [shapes-scheduler]
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 32s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 32s
Phase 6 first slice. New :defer stmt dispatch, go-eval-defer-stmt captures (callee, eagerly-evaluated args) onto a frame-local __go-defer-stack mutable list. go-eval-call installs the stack and drains LIFO before returning; go-eval-program does the same for the implicit main frame. New :quoted-value AST node lets defer re-invoke calls with the frozen arg values. 6 eval tests: single defer, multi-LIFO, args-eager-at-defer-time, fires-on-early-return, frame-local (no bleed to outer), defer-in-loop. Shape: defer is a per-frame cleanup queue (LIFO on frame exit) that the scheduler kit will reuse for panic-unwind + clean-exit + select- case-rollback paths. Distinct from the scheduler's ready-queue — diary updated to keep that distinction explicit. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -467,6 +467,55 @@
|
||||
(go-eval env (go-parse "find(nums, 99)")))
|
||||
-1)
|
||||
|
||||
(go-eval-test
|
||||
"defer: single defer runs after surrounding fn body returns"
|
||||
(let
|
||||
((env (go-eval-program go-env-builtins (list (go-parse "ch := make()") (go-parse "func push2(c chan int) { c <- 2 }") (go-parse "func run(c chan int) { defer push2(c) ; c <- 1 }") (go-parse "run(ch)") (go-parse "first := <-ch") (go-parse "second := <-ch")))))
|
||||
(list (go-env-lookup env "first") (go-env-lookup env "second")))
|
||||
(list 1 2))
|
||||
|
||||
(go-eval-test
|
||||
"defer: multiple defers run LIFO"
|
||||
(let
|
||||
((env (go-eval-program go-env-builtins (list (go-parse "ch := make()") (go-parse "func p2(c chan int) { c <- 2 }") (go-parse "func p3(c chan int) { c <- 3 }") (go-parse "func run(c chan int) { defer p2(c) ; defer p3(c) ; c <- 1 }") (go-parse "run(ch)") (go-parse "a := <-ch") (go-parse "b := <-ch") (go-parse "d := <-ch")))))
|
||||
(list
|
||||
(go-env-lookup env "a")
|
||||
(go-env-lookup env "b")
|
||||
(go-env-lookup env "d")))
|
||||
(list 1 3 2))
|
||||
|
||||
(go-eval-test
|
||||
"defer: arguments are evaluated at defer-time (not call-time)"
|
||||
(let
|
||||
((env (go-eval-program go-env-builtins (list (go-parse "ch := make()") (go-parse "func pushN(c chan int, v int) { c <- v }") (go-parse "func run(c chan int) { x := 7 ; defer pushN(c, x) ; x = 99 }") (go-parse "run(ch)") (go-parse "got := <-ch")))))
|
||||
(go-env-lookup env "got"))
|
||||
7)
|
||||
|
||||
(go-eval-test
|
||||
"defer: runs even when fn returns early via return"
|
||||
(let
|
||||
((env (go-eval-program go-env-builtins (list (go-parse "ch := make()") (go-parse "func note(c chan int) { c <- 42 }") (go-parse "func run(c chan int) int { defer note(c) ; return 1 }") (go-parse "r := run(ch)") (go-parse "n := <-ch")))))
|
||||
(list (go-env-lookup env "r") (go-env-lookup env "n")))
|
||||
(list 1 42))
|
||||
|
||||
(go-eval-test
|
||||
"defer: stack is frame-local — outer defers don't run on inner return"
|
||||
(let
|
||||
((env (go-eval-program go-env-builtins (list (go-parse "ch := make()") (go-parse "func push1(c chan int) { c <- 1 }") (go-parse "func push2(c chan int) { c <- 2 }") (go-parse "func inner(c chan int) { defer push2(c) }") (go-parse "func outer(c chan int) { defer push1(c) ; inner(c) }") (go-parse "outer(ch)") (go-parse "a := <-ch") (go-parse "b := <-ch")))))
|
||||
(list (go-env-lookup env "a") (go-env-lookup env "b")))
|
||||
(list 2 1))
|
||||
|
||||
(go-eval-test
|
||||
"defer: in a loop, all defers fire on fn return (not loop iter)"
|
||||
(let
|
||||
((env (go-eval-program go-env-builtins (list (go-parse "ch := make()") (go-parse "func pushI(c chan int, v int) { c <- v }") (go-parse "func loop(c chan int) { for i := 0; i < 4; i = i + 1 { defer pushI(c, i) } }") (go-parse "loop(ch)") (go-parse "a := <-ch") (go-parse "b := <-ch") (go-parse "d := <-ch") (go-parse "e := <-ch")))))
|
||||
(list
|
||||
(go-env-lookup env "a")
|
||||
(go-env-lookup env "b")
|
||||
(go-env-lookup env "d")
|
||||
(go-env-lookup env "e")))
|
||||
(list 3 2 1 0))
|
||||
|
||||
(define
|
||||
go-eval-test-summary
|
||||
(str "eval " go-eval-test-pass "/" go-eval-test-count))
|
||||
|
||||
Reference in New Issue
Block a user