Refactor lambda binding and parameter handling
Introduce `BoundKind::Parameter` to represent function parameters. Modify `Binder` to correctly handle parameters within lambda definitions, ensuring they are only defined within function scopes. Update `LambdaCollector` to register the body of lambdas as templates for global definitions. Adjust `Dumper` to accurately represent lambda parameters. Update `Specializer` and `TCO` to handle the new `BoundKind::Parameter`. Refactor `Call` and `TailCall` to use a single `args` node, often a tuple. Adjust type signatures in RTL to use `StaticType::Tuple` for function parameters.
This commit is contained in:
@@ -19,6 +19,20 @@ impl UpvalueAnalyzer {
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn visit_params(node: &Node<UntypedKind>, scope: &mut HashMap<Symbol, Identity>) {
|
||||
match &node.kind {
|
||||
UntypedKind::Parameter(sym) => {
|
||||
scope.insert(sym.clone(), node.identity.clone());
|
||||
}
|
||||
UntypedKind::Tuple { elements } => {
|
||||
for el in elements {
|
||||
Self::visit_params(el, scope);
|
||||
}
|
||||
}
|
||||
_ => {} // Ignore other nodes in parameter patterns for now
|
||||
}
|
||||
}
|
||||
|
||||
fn visit(
|
||||
node: &Node<UntypedKind>,
|
||||
scopes: &mut Vec<HashMap<Symbol, Identity>>,
|
||||
@@ -50,15 +64,20 @@ impl UpvalueAnalyzer {
|
||||
}
|
||||
UntypedKind::Lambda { params, body } => {
|
||||
let mut new_scope = HashMap::new();
|
||||
for p in params {
|
||||
new_scope.insert(p.clone(), node.identity.clone());
|
||||
}
|
||||
Self::visit_params(params, &mut new_scope);
|
||||
|
||||
scopes.push(new_scope);
|
||||
// The current node is the lambda causing captures in its body
|
||||
Self::visit(body, scopes, capture_map, Some(node.identity.clone()));
|
||||
scopes.pop();
|
||||
}
|
||||
UntypedKind::MacroDecl { params, body, .. } => {
|
||||
let mut new_scope = HashMap::new();
|
||||
Self::visit_params(params, &mut new_scope);
|
||||
scopes.push(new_scope);
|
||||
Self::visit(body, scopes, capture_map, current_lambda);
|
||||
scopes.pop();
|
||||
}
|
||||
UntypedKind::If { cond, then_br, else_br } => {
|
||||
Self::visit(cond, scopes, capture_map, current_lambda.clone());
|
||||
Self::visit(then_br, scopes, capture_map, current_lambda.clone());
|
||||
@@ -72,9 +91,7 @@ impl UpvalueAnalyzer {
|
||||
}
|
||||
UntypedKind::Call { callee, args } => {
|
||||
Self::visit(callee, scopes, capture_map, current_lambda.clone());
|
||||
for arg in args {
|
||||
Self::visit(arg, scopes, capture_map, current_lambda.clone());
|
||||
}
|
||||
Self::visit(args, scopes, capture_map, current_lambda.clone());
|
||||
}
|
||||
UntypedKind::Block { exprs } => {
|
||||
for expr in exprs {
|
||||
@@ -95,10 +112,10 @@ impl UpvalueAnalyzer {
|
||||
UntypedKind::Expansion { call: _, expanded } => {
|
||||
Self::visit(expanded, scopes, capture_map, current_lambda);
|
||||
}
|
||||
UntypedKind::MacroDecl { body, .. } | UntypedKind::Template(body) | UntypedKind::Placeholder(body) | UntypedKind::Splice(body) => {
|
||||
UntypedKind::Template(body) | UntypedKind::Placeholder(body) | UntypedKind::Splice(body) => {
|
||||
Self::visit(body, scopes, capture_map, current_lambda);
|
||||
}
|
||||
UntypedKind::Nop | UntypedKind::Constant(_) | UntypedKind::Extension(_) => {}
|
||||
UntypedKind::Nop | UntypedKind::Constant(_) | UntypedKind::Extension(_) | UntypedKind::Parameter(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user