ff1024ee49
The `is_tail` field on the `BoundKind::Call` node was an intermediate representation for tail-call optimization and is no longer needed as a distinct field. The TCO pass now embeds this information directly into the `RuntimeMetadata` of the `ExecNode`, which is the VM's internal AST. This simplifies the `BoundKind::Call` structure and removes redundant information.
86 lines
2.8 KiB
Rust
86 lines
2.8 KiB
Rust
use std::collections::HashMap;
|
|
use crate::ast::compiler::bound_nodes::{BoundKind, Address, BoundNode};
|
|
|
|
/// A pass that collects all global function definitions (lambdas) into a registry.
|
|
/// This allows the Specializer to retrieve the original AST of a function for monomorphization.
|
|
pub struct LambdaCollector<'a> {
|
|
registry: &'a mut HashMap<u32, BoundNode>,
|
|
}
|
|
|
|
impl<'a> LambdaCollector<'a> {
|
|
/// Performs a full traversal of the AST and populates the provided registry.
|
|
pub fn collect(node: &BoundNode, registry: &'a mut HashMap<u32, BoundNode>) {
|
|
let mut collector = Self { registry };
|
|
collector.visit(node);
|
|
}
|
|
|
|
fn visit(&mut self, node: &BoundNode) {
|
|
match &node.kind {
|
|
BoundKind::Block { exprs } => {
|
|
for expr in exprs {
|
|
self.visit(expr);
|
|
}
|
|
}
|
|
|
|
BoundKind::DefGlobal { global_index, value, .. } => {
|
|
// Register the full Lambda node as the template.
|
|
if let BoundKind::Lambda { .. } = &value.kind {
|
|
self.registry.insert(*global_index, (**value).clone());
|
|
}
|
|
self.visit(value);
|
|
}
|
|
|
|
BoundKind::Set { addr, value } => {
|
|
// Also track assignments to globals if they hold lambdas.
|
|
if let Address::Global(global_index) = addr
|
|
&& let BoundKind::Lambda { .. } = &value.kind
|
|
{
|
|
self.registry.insert(*global_index, (**value).clone());
|
|
}
|
|
self.visit(value);
|
|
}
|
|
|
|
BoundKind::If { cond, then_br, else_br } => {
|
|
self.visit(cond);
|
|
self.visit(then_br);
|
|
if let Some(e) = else_br {
|
|
self.visit(e);
|
|
}
|
|
}
|
|
|
|
BoundKind::Lambda { params, body, .. } => {
|
|
self.visit(params);
|
|
self.visit(body);
|
|
}
|
|
|
|
BoundKind::DefLocal { value, .. } => {
|
|
self.visit(value);
|
|
}
|
|
|
|
BoundKind::Call { callee, args } => {
|
|
self.visit(callee);
|
|
self.visit(args);
|
|
}
|
|
|
|
BoundKind::Tuple { elements } => {
|
|
for el in elements {
|
|
self.visit(el);
|
|
}
|
|
}
|
|
|
|
BoundKind::Record { fields } => {
|
|
for (k, v) in fields {
|
|
self.visit(k);
|
|
self.visit(v);
|
|
}
|
|
}
|
|
|
|
BoundKind::Expansion { bound_expanded, .. } => {
|
|
self.visit(bound_expanded);
|
|
}
|
|
|
|
_ => {} // Leaf nodes
|
|
}
|
|
}
|
|
}
|