Unify scope mechanism: one world (hashtable stacks everywhere)

Replace continuation-based scope frames with hashtable stacks for all
scope operations. The CEK evaluator's scope/provide/context/emit!/emitted
now use scope-push!/pop!/peek/emit! primitives (registered in
sx_primitives table) instead of walking continuation frames.

This eliminates the two-world problem where the aser used hashtable
stacks (scope-push!/pop!) but eval-expr used continuation frames
(ScopeFrame/ScopeAccFrame). Now both paths share the same mechanism.

Benefits:
- scope/context works inside eval-expr calls (e.g. (str ... (context x)))
- O(1) scope lookup vs O(n) continuation walking
- Simpler — no ScopeFrame/ScopeAccFrame/ProvideFrame creation/dispatch
- VM-compiled code and CEK code both see the same scope state

Also registers scope-push!/pop!/peek/emit!/collect!/collected/
clear-collected! as real primitives (sx_primitives table) so the
transpiled evaluator can call them directly.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-23 09:45:25 +00:00
parent 4734d38f3b
commit 09feb51762
5 changed files with 71 additions and 64 deletions

View File

@@ -315,11 +315,13 @@ let dynamic_wind_call before body after _env =
ignore (sx_call after []);
result
(* Scope stack stubs — delegated to primitives when available *)
let scope_push name value = prim_call "collect!" [name; value]
let scope_pop _name = Nil
let provide_push name value = ignore name; ignore value; Nil
let provide_pop _name = Nil
(* Scope stack — all delegated to primitives registered in sx_server.ml *)
let scope_push name value = prim_call "scope-push!" [name; value]
let scope_pop name = prim_call "scope-pop!" [name]
let scope_peek name = prim_call "scope-peek" [name]
let scope_emit name value = prim_call "scope-emit!" [name; value]
let provide_push name value = prim_call "scope-push!" [name; value]
let provide_pop name = prim_call "scope-pop!" [name]
(* Custom special forms registry — mutable dict *)
let custom_special_forms = Dict (Hashtbl.create 4)