Refactor: Use new NodeKind and clean up Binder definitions
The Binder and related types have been refactored to use the new `NodeKind` enum instead of the previous `BoundKind`. This commit updates all references to use the new structure, ensuring consistency across the compiler's AST representation. Key changes include: - Replacing `BoundKind` with `NodeKind` in the Binder's `bind` and `visit` methods. - Updating pattern matching and field access to reflect the new enum variants (e.g., `NodeKind::Def` instead of `BoundKind::Define`). - Adjusting identifier bindings to use the new `IdentifierBinding` enum. - Reflecting changes in `DefBinding`, `AssignBinding`, and `LambdaBinding` structures. - Ensuring all newly created nodes use `NodeKind` and the appropriate metadata.
This commit is contained in:
@@ -1,99 +1,111 @@
|
||||
use crate::ast::compiler::bound_nodes::{Address, BoundKind, CompilerPhase, GlobalIdx, Node};
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// 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, P: CompilerPhase> {
|
||||
registry: &'a mut HashMap<GlobalIdx, Rc<Node<P>>>,
|
||||
}
|
||||
|
||||
impl<'a, P: CompilerPhase> LambdaCollector<'a, P> {
|
||||
/// Performs a full traversal of the AST and populates the provided registry.
|
||||
pub fn collect(node: &Node<P>, registry: &'a mut HashMap<GlobalIdx, Rc<Node<P>>>) {
|
||||
let mut collector = Self { registry };
|
||||
collector.visit(node);
|
||||
}
|
||||
|
||||
fn visit(&mut self, node: &Node<P>) {
|
||||
match &node.kind {
|
||||
BoundKind::Block { exprs } => {
|
||||
for expr in exprs {
|
||||
self.visit(expr);
|
||||
}
|
||||
}
|
||||
|
||||
BoundKind::Define { addr, value, .. } => {
|
||||
// Register global function definitions (lambdas)
|
||||
if let Address::Global(global_index) = addr {
|
||||
let mut current = value;
|
||||
while let BoundKind::Expansion { bound_expanded, .. } = ¤t.kind {
|
||||
current = bound_expanded;
|
||||
}
|
||||
|
||||
if let BoundKind::Lambda { .. } = ¤t.kind {
|
||||
self.registry
|
||||
.insert(*global_index, (*current).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 mut current = value;
|
||||
while let BoundKind::Expansion { bound_expanded, .. } = ¤t.kind {
|
||||
current = bound_expanded;
|
||||
}
|
||||
|
||||
if let BoundKind::Lambda { .. } = ¤t.kind {
|
||||
self.registry
|
||||
.insert(*global_index, (*current).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::Call { callee, args } => {
|
||||
self.visit(callee);
|
||||
self.visit(args);
|
||||
}
|
||||
|
||||
BoundKind::Tuple { elements } => {
|
||||
for el in elements {
|
||||
self.visit(el);
|
||||
}
|
||||
}
|
||||
|
||||
BoundKind::Record { values, .. } => {
|
||||
for v in values {
|
||||
self.visit(v);
|
||||
}
|
||||
}
|
||||
|
||||
BoundKind::Expansion { bound_expanded, .. } => {
|
||||
self.visit(bound_expanded);
|
||||
}
|
||||
|
||||
_ => {} // Leaf nodes
|
||||
}
|
||||
}
|
||||
}
|
||||
use crate::ast::compiler::bound_nodes::{
|
||||
Address, BoundLike, GlobalIdx, IdentifierBinding, Node, NodeKind,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// 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, P: BoundLike> {
|
||||
registry: &'a mut HashMap<GlobalIdx, Rc<Node<P>>>,
|
||||
}
|
||||
|
||||
impl<'a, P> LambdaCollector<'a, P>
|
||||
where
|
||||
P: BoundLike,
|
||||
{
|
||||
/// Performs a full traversal of the AST and populates the provided registry.
|
||||
pub fn collect(node: &Node<P>, registry: &'a mut HashMap<GlobalIdx, Rc<Node<P>>>) {
|
||||
let mut collector = Self { registry };
|
||||
collector.visit(node);
|
||||
}
|
||||
|
||||
fn visit(&mut self, node: &Node<P>) {
|
||||
match &node.kind {
|
||||
NodeKind::Block { exprs } => {
|
||||
for expr in exprs {
|
||||
self.visit(expr);
|
||||
}
|
||||
}
|
||||
|
||||
NodeKind::Def { pattern, value, .. } => {
|
||||
// Register global function definitions (lambdas)
|
||||
if let NodeKind::Identifier {
|
||||
binding: IdentifierBinding::Declaration {
|
||||
addr: Address::Global(global_index),
|
||||
..
|
||||
},
|
||||
..
|
||||
} = &pattern.kind
|
||||
{
|
||||
let mut current = value;
|
||||
while let NodeKind::Expansion { expanded, .. } = ¤t.kind {
|
||||
current = expanded;
|
||||
}
|
||||
|
||||
if let NodeKind::Lambda { .. } = ¤t.kind {
|
||||
self.registry
|
||||
.insert(*global_index, (*current).clone());
|
||||
}
|
||||
}
|
||||
self.visit(value);
|
||||
}
|
||||
|
||||
NodeKind::Assign { value, info, .. } => {
|
||||
// Also track assignments to globals if they hold lambdas.
|
||||
if let Some(Address::Global(global_index)) = &info.addr {
|
||||
let mut current = value;
|
||||
while let NodeKind::Expansion { expanded, .. } = ¤t.kind {
|
||||
current = expanded;
|
||||
}
|
||||
|
||||
if let NodeKind::Lambda { .. } = ¤t.kind {
|
||||
self.registry
|
||||
.insert(*global_index, (*current).clone());
|
||||
}
|
||||
}
|
||||
self.visit(value);
|
||||
}
|
||||
|
||||
NodeKind::If {
|
||||
cond,
|
||||
then_br,
|
||||
else_br,
|
||||
} => {
|
||||
self.visit(cond);
|
||||
self.visit(then_br);
|
||||
if let Some(e) = else_br {
|
||||
self.visit(e);
|
||||
}
|
||||
}
|
||||
|
||||
NodeKind::Lambda { params, body, .. } => {
|
||||
self.visit(params);
|
||||
self.visit(body);
|
||||
}
|
||||
|
||||
NodeKind::Call { callee, args } => {
|
||||
self.visit(callee);
|
||||
self.visit(args);
|
||||
}
|
||||
|
||||
NodeKind::Tuple { elements } => {
|
||||
for el in elements {
|
||||
self.visit(el);
|
||||
}
|
||||
}
|
||||
|
||||
NodeKind::Record { fields, .. } => {
|
||||
for (_, v) in fields {
|
||||
self.visit(v);
|
||||
}
|
||||
}
|
||||
|
||||
NodeKind::Expansion { expanded, .. } => {
|
||||
self.visit(expanded);
|
||||
}
|
||||
|
||||
_ => {} // Leaf nodes
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user