Add AST dumper and improve upvalue analysis
The `UpvalueAnalyzer` now correctly identifies which lambdas capture which variable declarations, rather than just marking declarations that need boxing. This information is stored in a `capture_map`. The `Binder` has been updated to use this new `capture_map` instead of a `HashSet` of boxed declarations. The `DefLocal` node in `bound_nodes.rs` now stores a `captured_by` field, which is a list of lambda identities that capture the local variable. A new `dumper` module has been added to provide a human-readable representation of the bound AST, including information about captured variables. The `VM` has been updated to use the `captured_by` field to determine if a local variable needs to be stored in a `Cell`, rather than relying on a boolean `is_boxed` flag. An example script `extreme_capture.myc` has been added to test deep nesting and variable capture. A new "Dump AST" button has been added to the UI, which uses the new `Dumper` to display the bound AST.
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
use crate::ast::nodes::Node;
|
||||
use crate::ast::compiler::bound_nodes::BoundKind;
|
||||
use crate::ast::types::StaticType;
|
||||
|
||||
/// Human-readable AST dumper for the bound AST.
|
||||
pub struct Dumper {
|
||||
output: String,
|
||||
indent: usize,
|
||||
}
|
||||
|
||||
impl Dumper {
|
||||
/// Produces a formatted string representation of the given bound AST node and its children.
|
||||
pub fn dump(node: &Node<BoundKind, StaticType>) -> String {
|
||||
let mut dumper = Self {
|
||||
output: String::new(),
|
||||
indent: 0,
|
||||
};
|
||||
dumper.visit(node);
|
||||
dumper.output
|
||||
}
|
||||
|
||||
fn write_indent(&mut self) {
|
||||
for _ in 0..self.indent {
|
||||
self.output.push_str(" ");
|
||||
}
|
||||
}
|
||||
|
||||
fn log(&mut self, label: &str, node: &Node<BoundKind, StaticType>) {
|
||||
self.write_indent();
|
||||
self.output.push_str(label);
|
||||
self.output.push_str(&format!(" <Type: {:?}>\n", node.ty));
|
||||
}
|
||||
|
||||
fn visit(&mut self, node: &Node<BoundKind, StaticType>) {
|
||||
match &node.kind {
|
||||
BoundKind::Nop => self.log("Nop", node),
|
||||
BoundKind::Constant(v) => self.log(&format!("Constant: {}", v), node),
|
||||
BoundKind::Get(addr) => self.log(&format!("Get: {:?}", addr), node),
|
||||
|
||||
BoundKind::Set { addr, value } => {
|
||||
self.log(&format!("Set: {:?}", addr), node);
|
||||
self.indent += 1;
|
||||
self.visit(value);
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::DefLocal { slot, value, captured_by } => {
|
||||
let capture_info = if captured_by.is_empty() {
|
||||
String::from("not captured")
|
||||
} else {
|
||||
format!("captured by {} lambdas", captured_by.len())
|
||||
};
|
||||
self.log(&format!("DefLocal (Slot: {}, {})", slot, capture_info), node);
|
||||
|
||||
self.indent += 1;
|
||||
if !captured_by.is_empty() {
|
||||
for capturer in captured_by {
|
||||
self.write_indent();
|
||||
self.output.push_str(&format!("- Capturer: Lambda at line {}, col {}\n",
|
||||
capturer.location.line, capturer.location.col));
|
||||
}
|
||||
}
|
||||
self.visit(value);
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::DefGlobal { global_index, value } => {
|
||||
self.log(&format!("DefGlobal (Index: {})", global_index), node);
|
||||
self.indent += 1;
|
||||
self.visit(value);
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::If { cond, then_br, else_br } => {
|
||||
self.log("If", node);
|
||||
self.indent += 1;
|
||||
|
||||
self.write_indent();
|
||||
self.output.push_str("Condition:\n");
|
||||
self.visit(cond);
|
||||
|
||||
self.write_indent();
|
||||
self.output.push_str("Then:\n");
|
||||
self.visit(then_br);
|
||||
|
||||
if let Some(e) = else_br {
|
||||
self.write_indent();
|
||||
self.output.push_str("Else:\n");
|
||||
self.visit(e);
|
||||
}
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Lambda { param_count, upvalues, body } => {
|
||||
self.log(&format!("Lambda (Params: {}, Upvalues: {})", param_count, upvalues.len()), node);
|
||||
self.indent += 1;
|
||||
if !upvalues.is_empty() {
|
||||
self.write_indent();
|
||||
self.output.push_str(&format!("Upvalues: {:?}\n", upvalues));
|
||||
}
|
||||
self.visit(body);
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Call { callee, args } => {
|
||||
self.log("Call", node);
|
||||
self.indent += 1;
|
||||
self.visit(callee);
|
||||
for arg in args {
|
||||
self.visit(arg);
|
||||
}
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::TailCall { callee, args } => {
|
||||
self.log("TailCall (TCO)", node);
|
||||
self.indent += 1;
|
||||
self.visit(callee);
|
||||
for arg in args {
|
||||
self.visit(arg);
|
||||
}
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Block { exprs } => {
|
||||
self.log("Block", node);
|
||||
self.indent += 1;
|
||||
for expr in exprs {
|
||||
self.visit(expr);
|
||||
}
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Tuple { elements } => {
|
||||
self.log("Tuple", node);
|
||||
self.indent += 1;
|
||||
for el in elements {
|
||||
self.visit(el);
|
||||
}
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Map { entries } => {
|
||||
self.log("Map", node);
|
||||
self.indent += 1;
|
||||
for (k, v) in entries {
|
||||
self.visit(k);
|
||||
self.visit(v);
|
||||
}
|
||||
self.indent -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user