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
+240 -239
View File
@@ -1,239 +1,240 @@
use crate::ast::compiler::bound_nodes::{BoundKind, CompilerPhase, Node};
/// 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<P: CompilerPhase>(node: &Node<P>) -> 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<P: CompilerPhase>(&mut self, label: &str, node: &Node<P>) {
self.write_indent();
self.output.push_str(label);
self.output
.push_str(&format!(" <Metadata: {:?}>\n", node.ty));
}
fn visit<P: CompilerPhase>(&mut self, node: &Node<P>) {
match &node.kind {
BoundKind::Nop => self.log("Nop", node),
BoundKind::Constant(v) => {
self.log(&format!("Constant: {}", v), node);
}
BoundKind::Get { addr, name } => {
self.log(&format!("Get: {} ({:?})", name.name, addr), node)
}
BoundKind::FieldAccessor(k) => self.log(&format!("FieldAccessor: .{}", k.name()), node),
BoundKind::GetField { rec, field } => {
self.log(&format!("GetField: .{}", field.name()), node);
self.indent += 1;
self.visit(rec);
self.indent -= 1;
}
BoundKind::Set { addr, value } => {
self.log(&format!("Set: {:?}", addr), node);
self.indent += 1;
self.visit(value);
self.indent -= 1;
}
BoundKind::Define {
name,
addr,
kind,
value,
captured_by,
} => {
let k_str = match kind {
crate::ast::compiler::bound_nodes::DeclarationKind::Variable => "Variable",
crate::ast::compiler::bound_nodes::DeclarationKind::Parameter => "Parameter",
};
let capture_info = if captured_by.is_empty() {
String::from("not captured")
} else {
format!("captured by {} lambdas", captured_by.len())
};
self.log(
&format!(
"Define {} (Name: '{}', Address: {:?}, {})",
k_str, name.name, addr, capture_info
),
node,
);
self.indent += 1;
if !captured_by.is_empty() {
for capturer in captured_by {
self.write_indent();
let loc = capturer
.location
.unwrap_or(crate::ast::types::SourceLocation { line: 0, col: 0 });
self.output.push_str(&format!(
"- Capturer: Lambda at line {}, col {}\n",
loc.line, loc.col
));
}
}
self.visit(value);
self.indent -= 1;
}
BoundKind::Destructure { pattern, value } => {
self.log("Destructure", node);
self.indent += 1;
self.write_indent();
self.output.push_str("Pattern:\n");
self.visit(pattern);
self.write_indent();
self.output.push_str("Value:\n");
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 {
params,
upvalues,
body,
..
} => {
self.log(&format!("Lambda (Upvalues: {})", upvalues.len()), node);
self.indent += 1;
self.write_indent();
self.output.push_str("Parameters:\n");
self.visit(params);
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.write_indent();
self.output.push_str("Callee:\n");
self.visit(callee);
self.write_indent();
self.output.push_str("Arguments:\n");
self.visit(args);
self.indent -= 1;
}
BoundKind::Again { args } => {
self.log("Again", node);
self.indent += 1;
self.visit(args);
self.indent -= 1;
}
BoundKind::Pipe { inputs, lambda, .. } => {
self.log("Pipe", node);
self.indent += 1;
for input in inputs {
self.visit(input);
}
self.visit(lambda);
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::Record { layout, values } => {
self.log(
&format!("Record (Layout: {} fields)", layout.fields.len()),
node,
);
self.indent += 1;
for v in values {
self.visit(v);
}
self.indent -= 1;
}
BoundKind::Expansion {
original_call,
bound_expanded,
} => {
self.log(
&format!("Expansion (Original: {:?})", original_call.kind),
node,
);
self.indent += 1;
self.visit(bound_expanded);
self.indent -= 1;
}
BoundKind::Extension(ext) => {
self.log(&ext.display_name(), node);
}
BoundKind::Error => {
self.log("ERROR_NODE", node);
}
}
}
}
use crate::ast::compiler::bound_nodes::{CompilerPhase, Node, NodeKind};
/// 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<P: CompilerPhase>(node: &Node<P>) -> 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<P: CompilerPhase>(&mut self, label: &str, node: &Node<P>) {
self.write_indent();
self.output.push_str(label);
self.output
.push_str(&format!(" <Metadata: {:?}>\n", node.ty));
}
fn visit<P: CompilerPhase>(&mut self, node: &Node<P>) {
match &node.kind {
NodeKind::Nop => self.log("Nop", node),
NodeKind::Constant(v) => {
self.log(&format!("Constant: {}", v), node);
}
NodeKind::Identifier { symbol, binding } => {
self.log(&format!("Identifier: {} ({:?})", symbol.name, binding), node)
}
NodeKind::FieldAccessor(k) => self.log(&format!("FieldAccessor: .{}", k.name()), node),
NodeKind::GetField { rec, field } => {
self.log(&format!("GetField: .{}", field.name()), node);
self.indent += 1;
self.visit(rec);
self.indent -= 1;
}
NodeKind::Assign { target, value, info } => {
self.log(&format!("Assign: {:?}", info), node);
self.indent += 1;
self.write_indent();
self.output.push_str("Target:\n");
self.visit(target);
self.write_indent();
self.output.push_str("Value:\n");
self.visit(value);
self.indent -= 1;
}
NodeKind::Def {
pattern,
value,
info,
} => {
self.log(&format!("Def ({:?})", info), node);
self.indent += 1;
self.write_indent();
self.output.push_str("Pattern:\n");
self.visit(pattern);
self.write_indent();
self.output.push_str("Value:\n");
self.visit(value);
self.indent -= 1;
}
NodeKind::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;
}
NodeKind::Lambda {
params,
body,
info,
} => {
self.log(&format!("Lambda ({:?})", info), node);
self.indent += 1;
self.write_indent();
self.output.push_str("Parameters:\n");
self.visit(params);
self.visit(body);
self.indent -= 1;
}
NodeKind::Call { callee, args } => {
self.log("Call", node);
self.indent += 1;
self.write_indent();
self.output.push_str("Callee:\n");
self.visit(callee);
self.write_indent();
self.output.push_str("Arguments:\n");
self.visit(args);
self.indent -= 1;
}
NodeKind::Again { args } => {
self.log("Again", node);
self.indent += 1;
self.visit(args);
self.indent -= 1;
}
NodeKind::Pipe { inputs, lambda } => {
self.log("Pipe", node);
self.indent += 1;
for input in inputs {
self.visit(input);
}
self.visit(lambda);
self.indent -= 1;
}
NodeKind::Block { exprs } => {
self.log("Block", node);
self.indent += 1;
for expr in exprs {
self.visit(expr);
}
self.indent -= 1;
}
NodeKind::Tuple { elements } => {
self.log("Tuple", node);
self.indent += 1;
for el in elements {
self.visit(el);
}
self.indent -= 1;
}
NodeKind::Record { fields, layout } => {
self.log(
&format!("Record (Layout: {:?})", layout),
node,
);
self.indent += 1;
for (key, val) in fields {
self.write_indent();
self.output.push_str("Key:\n");
self.indent += 1;
self.visit(key);
self.indent -= 1;
self.write_indent();
self.output.push_str("Value:\n");
self.indent += 1;
self.visit(val);
self.indent -= 1;
}
self.indent -= 1;
}
NodeKind::Expansion {
original_call,
expanded,
} => {
self.log(
&format!("Expansion (Original: {:?})", original_call.kind),
node,
);
self.indent += 1;
self.visit(expanded);
self.indent -= 1;
}
NodeKind::MacroDecl { name, params, body } => {
self.log(&format!("MacroDecl: {}", name.name), node);
self.indent += 1;
self.visit(params);
self.visit(body);
self.indent -= 1;
}
NodeKind::Template(inner) => {
self.log("Template", node);
self.indent += 1;
self.visit(inner);
self.indent -= 1;
}
NodeKind::Placeholder(inner) => {
self.log("Placeholder", node);
self.indent += 1;
self.visit(inner);
self.indent -= 1;
}
NodeKind::Splice(inner) => {
self.log("Splice", node);
self.indent += 1;
self.visit(inner);
self.indent -= 1;
}
NodeKind::Extension(ext) => {
self.log(&ext.display_name(), node);
}
NodeKind::Error => {
self.log("ERROR_NODE", node);
}
}
}
}