Introduce boxing for captured variables

The binder now uses an `UpvalueAnalyzer` to identify local variables
that are captured by nested functions. These identified variables are
marked with `is_boxed: true` during `DefLocal` node creation. The VM
then uses this flag to wrap such variables in a `Value::Cell` (using
`Rc<RefCell<_>>`) to ensure they can be mutated across function calls.
feat: Introduce boxing for captured variables

Add UpvalueAnalyzer to identify variables captured by nested lambdas.
Modify Binder to use the analyzer and mark captured local variables for
boxing.
Update BoundKind::DefLocal to include an `is_boxed` flag.
Update VM to box captured variables when they are defined.
Add tests for upvalue capture detection.
This commit is contained in:
Michael Schimmel
2026-02-18 00:09:46 +01:00
parent 83f6cfb8e1
commit 4d9adccab5
8 changed files with 208 additions and 9 deletions
+2 -3
View File
@@ -116,11 +116,10 @@ impl Environment {
}
// 3. Bind & Type Check
let mut binder = Binder::new(self.global_names.clone());
let mut bound_ast = binder.bind(&untyped_ast)?;
let bound_ast = Binder::bind_root(self.global_names.clone(), &untyped_ast)?;
// 4. Optimize
bound_ast = TCO::optimize(bound_ast);
let bound_ast = TCO::optimize(bound_ast);
// 5. Execute
let mut vm = VM::new(self.global_values.clone());