Refactor: Implement late stack size calculation

This commit introduces a new mechanism for calculating the required
stack size for closures at bind time, rather than storing it in the AST.
This is achieved by adding a `calculate_stack_size` method to
`BoundNode`.

Key changes include:
- `BoundNode::calculate_stack_size`: Recursively traverses the bound AST
  to find the maximum `LocalSlot` index used.
- Recursion blocker for lambdas: Ensures that nested lambdas are not
  visited during stack size calculation for the outer closure,
  preventing incorrect stack size estimations.
- VM update: The `VM::run` method now accepts and uses the
  pre-calculated stack size for setting up call frames.
- `Closure` struct update: Stores the `stack_size` directly within the
  `Closure` struct.
- Binder and TypeChecker updates: Minor adjustments to accommodate the
  new strategy and error reporting.
- Optimizer enhancements: Constant and pure-lambda propagation for
  `Define` statements within blocks to ensure inlinability.

This refactoring addresses issues related to accurate stack size
management and improves the overall robustness of the binder and VM.
This commit is contained in:
Michael Schimmel
2026-03-10 20:05:32 +01:00
parent cb4d3525c2
commit 8c865681ff
8 changed files with 1063 additions and 896 deletions
+9 -5
View File
@@ -141,9 +141,13 @@ impl FunctionCompiler {
}
}
/// Defines the context in which a node is being bound.
/// This allows the binder to enforce rules like "statements cannot be used as values".
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExprContext {
/// The node must return a value (e.g., RHS of an assignment, function argument).
Expression,
/// The node is used as a standalone instruction (e.g., inside a block).
Statement,
}
@@ -750,7 +754,7 @@ mod tests {
#[test]
fn test_redefinition_error() {
let source = "(do (def x 1) (def x 2))";
let source = "(do (def x 1) (def x 2) x)";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
@@ -759,7 +763,7 @@ mod tests {
let _ = Binder::bind_root(globals, &untyped, &mut diagnostics);
assert!(diagnostics.has_errors());
assert!(diagnostics.items[0].message.contains("already defined"));
assert!(diagnostics.items.iter().any(|i| i.message.contains("already defined")));
}
#[test]
@@ -767,18 +771,18 @@ mod tests {
let globals = Rc::new(RefCell::new(HashMap::new()));
// First run: defines 'x'
let source1 = "(def x 1)";
let source1 = "(def x 1) 1";
let untyped1 = Parser::new(source1).parse_expression();
let mut diagnostics = Diagnostics::new();
assert!(Binder::bind_root(globals.clone(), &untyped1, &mut diagnostics).is_ok());
// Second run: attempts to redefine 'x' in the same global environment
let source2 = "(def x 2)";
let source2 = "(def x 2) 2";
let untyped2 = Parser::new(source2).parse_expression();
let mut diagnostics2 = Diagnostics::new();
let _ = Binder::bind_root(globals.clone(), &untyped2, &mut diagnostics2);
assert!(diagnostics2.has_errors());
assert!(diagnostics2.items[0].message.contains("already defined"));
assert!(diagnostics2.items.iter().any(|i| i.message.contains("already defined")));
}
}