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:
2026-03-21 14:12:14 +01:00
parent 99fef2fc86
commit e65402364d
20 changed files with 6523 additions and 6068 deletions
+103 -101
View File
@@ -1,101 +1,103 @@
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, Node, NodeMetrics};
use crate::ast::types::{Purity, RecordLayout, StaticType, Value};
use std::cell::RefCell;
use std::rc::Rc;
pub struct Folder<'a> {
pub globals: &'a Option<Rc<RefCell<Vec<Value>>>>,
}
impl<'a> Folder<'a> {
pub fn new(globals: &'a Option<Rc<RefCell<Vec<Value>>>>) -> Self {
Self { globals }
}
pub fn make_constant_node(&self, val: Value, template: &AnalyzedNode) -> AnalyzedNode {
let ty = val.static_type();
let typed_original = Rc::new(Node {
identity: template.identity.clone(),
kind: BoundKind::Constant(val.clone()),
ty: ty.clone(),
});
Node {
identity: template.identity.clone(),
kind: BoundKind::Constant(val),
ty: NodeMetrics {
original: typed_original,
purity: Purity::Pure,
is_recursive: false,
},
}
}
pub fn make_nop_node(&self, template: &AnalyzedNode) -> AnalyzedNode {
let typed_original = Rc::new(Node {
identity: template.identity.clone(),
kind: BoundKind::Nop,
ty: StaticType::Void,
});
Node {
identity: template.identity.clone(),
kind: BoundKind::Nop,
ty: NodeMetrics {
original: typed_original,
purity: Purity::Pure,
is_recursive: false,
},
}
}
pub fn try_fold_record(
&self,
layout: &std::sync::Arc<RecordLayout>,
values: &[Rc<AnalyzedNode>],
template: &AnalyzedNode,
) -> Option<AnalyzedNode> {
let mut constant_values = Vec::with_capacity(values.len());
for v_node in values {
if let BoundKind::Constant(val) = &v_node.kind {
constant_values.push(val.clone());
} else {
return None;
}
}
let record_val = Value::Record(layout.clone(), Rc::new(constant_values));
Some(self.make_constant_node(record_val, template))
}
pub fn try_fold_pure(
&self,
callee: &AnalyzedNode,
arg_nodes: &[Rc<AnalyzedNode>],
) -> Option<AnalyzedNode> {
if callee.ty.purity < Purity::Pure {
return None;
}
let mut arg_values = Vec::with_capacity(arg_nodes.len());
for node in arg_nodes {
if let BoundKind::Constant(val) = &node.kind {
arg_values.push(val.clone());
} else {
return None;
}
}
let func_val = match &callee.kind {
BoundKind::Get {
addr: Address::Global(idx),
..
} => self.globals.as_ref()?.borrow().get(idx.0 as usize)?.clone(),
BoundKind::Constant(val) => val.clone(),
_ => return None,
};
let result = match func_val {
Value::Function(f) => (f.func)(&arg_values),
_ => return None,
};
Some(self.make_constant_node(result, callee))
}
}
use crate::ast::compiler::bound_nodes::{
Address, AnalyzedNode, IdentifierBinding, Node, NodeKind, NodeMetrics,
};
use crate::ast::types::{Purity, RecordLayout, StaticType, Value};
use std::cell::RefCell;
use std::rc::Rc;
pub struct Folder<'a> {
pub globals: &'a Option<Rc<RefCell<Vec<Value>>>>,
}
impl<'a> Folder<'a> {
pub fn new(globals: &'a Option<Rc<RefCell<Vec<Value>>>>) -> Self {
Self { globals }
}
pub fn make_constant_node(&self, val: Value, template: &AnalyzedNode) -> AnalyzedNode {
let ty = val.static_type();
let typed_original = Rc::new(Node {
identity: template.identity.clone(),
kind: NodeKind::Constant(val.clone()),
ty: ty.clone(),
});
Node {
identity: template.identity.clone(),
kind: NodeKind::Constant(val),
ty: NodeMetrics {
original: typed_original,
purity: Purity::Pure,
is_recursive: false,
},
}
}
pub fn make_nop_node(&self, template: &AnalyzedNode) -> AnalyzedNode {
let typed_original = Rc::new(Node {
identity: template.identity.clone(),
kind: NodeKind::Nop,
ty: StaticType::Void,
});
Node {
identity: template.identity.clone(),
kind: NodeKind::Nop,
ty: NodeMetrics {
original: typed_original,
purity: Purity::Pure,
is_recursive: false,
},
}
}
pub fn try_fold_record(
&self,
layout: &std::sync::Arc<RecordLayout>,
values: &[Rc<AnalyzedNode>],
template: &AnalyzedNode,
) -> Option<AnalyzedNode> {
let mut constant_values = Vec::with_capacity(values.len());
for v_node in values {
if let NodeKind::Constant(val) = &v_node.kind {
constant_values.push(val.clone());
} else {
return None;
}
}
let record_val = Value::Record(layout.clone(), Rc::new(constant_values));
Some(self.make_constant_node(record_val, template))
}
pub fn try_fold_pure(
&self,
callee: &AnalyzedNode,
arg_nodes: &[Rc<AnalyzedNode>],
) -> Option<AnalyzedNode> {
if callee.ty.purity < Purity::Pure {
return None;
}
let mut arg_values = Vec::with_capacity(arg_nodes.len());
for node in arg_nodes {
if let NodeKind::Constant(val) = &node.kind {
arg_values.push(val.clone());
} else {
return None;
}
}
let func_val = match &callee.kind {
NodeKind::Identifier {
binding: IdentifierBinding::Reference(Address::Global(idx)),
..
} => self.globals.as_ref()?.borrow().get(idx.0 as usize)?.clone(),
NodeKind::Constant(val) => val.clone(),
_ => return None,
};
let result = match func_val {
Value::Function(f) => (f.func)(&arg_values),
_ => return None,
};
Some(self.make_constant_node(result, callee))
}
}