Refactor: Replace UntypedNode with SyntaxNode

This commit replaces the `UntypedNode` enum with the more accurately
named `SyntaxNode`. This change is primarily for clarity and better
reflects the role of these nodes as representing the structure of the
source code prior to semantic analysis.

The corresponding enum `UntypedKind` has also been renamed to
`SyntaxKind` to maintain consistency.

No functional changes are introduced by this refactoring; it is purely a
renaming and organizational update.
This commit is contained in:
Michael Schimmel
2026-03-13 14:21:28 +01:00
parent 7d72a99fa1
commit 84226f6a16
31 changed files with 8611 additions and 8611 deletions
+42 -42
View File
@@ -2,7 +2,7 @@ use crate::ast::compiler::bound_nodes::{
Address, BoundKind, DeclarationKind, GlobalIdx, LocalSlot, Node, UpvalueIdx,
};
use crate::ast::diagnostics::Diagnostics;
use crate::ast::nodes::{Symbol, UntypedKind, UntypedNode};
use crate::ast::nodes::{Symbol, SyntaxKind, SyntaxNode};
use crate::ast::types::{Identity, StaticType, Purity};
use std::collections::HashMap;
use std::rc::Rc;
@@ -152,7 +152,7 @@ impl Binder {
initial_scopes: Vec<CompilerScope>,
initial_slot_count: u32,
fixed_scope_idx: i32,
node: &UntypedNode,
node: &SyntaxNode,
diagnostics: &mut Diagnostics,
) -> Result<BindingResult, String> {
let mut binder = Self::new(initial_scopes, initial_slot_count, fixed_scope_idx);
@@ -208,17 +208,17 @@ impl Binder {
pub fn bind(
&mut self,
node: &UntypedNode,
node: &SyntaxNode,
ctx: ExprContext,
diag: &mut Diagnostics,
) -> Node {
match &node.kind {
UntypedKind::Nop => self.make_node(node.identity.clone(), BoundKind::Nop),
UntypedKind::Constant(v) => {
SyntaxKind::Nop => self.make_node(node.identity.clone(), BoundKind::Nop),
SyntaxKind::Constant(v) => {
self.make_node(node.identity.clone(), BoundKind::Constant(v.clone()))
}
UntypedKind::Identifier(sym) => {
SyntaxKind::Identifier(sym) => {
if let Some(addr) = self.resolve_variable(sym, diag, &node.identity) {
self.make_node(
node.identity.clone(),
@@ -232,11 +232,11 @@ impl Binder {
}
}
UntypedKind::FieldAccessor(k) => {
SyntaxKind::FieldAccessor(k) => {
self.make_node(node.identity.clone(), BoundKind::FieldAccessor(*k))
}
UntypedKind::If {
SyntaxKind::If {
cond,
then_br,
else_br,
@@ -264,14 +264,14 @@ impl Binder {
)
}
UntypedKind::Def { target, value } => {
SyntaxKind::Def { target, value } => {
if ctx == ExprContext::Expression {
diag.push_error(
"Statement 'def' cannot be used as an expression.",
Some(node.identity.clone()),
);
}
if let UntypedKind::Identifier(ref name) = target.kind {
if let SyntaxKind::Identifier(ref name) = target.kind {
let addr_opt = self.declare_variable(
name,
node.identity.clone(),
@@ -308,10 +308,10 @@ impl Binder {
}
}
UntypedKind::Assign { target, value } => {
SyntaxKind::Assign { target, value } => {
let val_node = self.bind(value, ExprContext::Expression, diag);
if let UntypedKind::Identifier(sym) = &target.kind {
if let SyntaxKind::Identifier(sym) = &target.kind {
if let Some(addr) = self.resolve_variable(sym, diag, &target.identity) {
self.make_node(
node.identity.clone(),
@@ -335,7 +335,7 @@ impl Binder {
}
}
UntypedKind::Pipe { inputs, lambda } => {
SyntaxKind::Pipe { inputs, lambda } => {
let mut bound_inputs = Vec::with_capacity(inputs.len());
for input in inputs {
bound_inputs.push(Rc::new(self.bind(input, ExprContext::Expression, diag)));
@@ -352,7 +352,7 @@ impl Binder {
)
}
UntypedKind::Lambda { params, body } => {
SyntaxKind::Lambda { params, body } => {
let identity = node.identity.clone();
self.functions
.push(FunctionCompiler::new(identity.clone(), vec![], 0));
@@ -392,7 +392,7 @@ impl Binder {
)
}
UntypedKind::Call { callee, args } => {
SyntaxKind::Call { callee, args } => {
let callee = self.bind(callee, ExprContext::Expression, diag);
let args = self.bind(args.as_ref(), ExprContext::Expression, diag);
@@ -405,7 +405,7 @@ impl Binder {
)
}
UntypedKind::Again { args } => {
SyntaxKind::Again { args } => {
if self.functions.len() <= 1 {
diag.push_error(
"'again' is only allowed inside a function or lambda.",
@@ -422,7 +422,7 @@ impl Binder {
)
}
UntypedKind::Block { exprs } => {
SyntaxKind::Block { exprs } => {
self.functions.last_mut().unwrap().push_scope();
let mut bound_exprs = Vec::new();
for (i, expr) in exprs.iter().enumerate() {
@@ -440,7 +440,7 @@ impl Binder {
)
}
UntypedKind::Tuple { elements } => {
SyntaxKind::Tuple { elements } => {
let mut bound_elems = Vec::new();
for e in elements {
bound_elems.push(Rc::new(self.bind(e, ExprContext::Expression, diag)));
@@ -453,7 +453,7 @@ impl Binder {
)
}
UntypedKind::Record { fields } => {
SyntaxKind::Record { fields } => {
let mut bound_values = Vec::new();
let mut layout_fields = Vec::new();
@@ -488,7 +488,7 @@ impl Binder {
)
}
UntypedKind::Expansion { call, expanded } => {
SyntaxKind::Expansion { call, expanded } => {
let bound_expanded = self.bind(expanded.as_ref(), ctx, diag);
self.make_node(
node.identity.clone(),
@@ -499,10 +499,10 @@ impl Binder {
)
}
UntypedKind::Template(_)
| UntypedKind::Placeholder(_)
| UntypedKind::Splice(_)
| UntypedKind::MacroDecl { .. } => {
SyntaxKind::Template(_)
| SyntaxKind::Placeholder(_)
| SyntaxKind::Splice(_)
| SyntaxKind::MacroDecl { .. } => {
diag.push_error(
format!(
"Macro construct {:?} found in Binder. Macros must be expanded before binding.",
@@ -513,14 +513,14 @@ impl Binder {
self.make_node(node.identity.clone(), BoundKind::Error)
}
UntypedKind::Extension(_) => {
SyntaxKind::Extension(_) => {
diag.push_error(
"Custom extensions not supported in Binder yet",
Some(node.identity.clone()),
);
self.make_node(node.identity.clone(), BoundKind::Error)
}
UntypedKind::Error => crate::ast::compiler::bound_nodes::Node {
SyntaxKind::Error => crate::ast::compiler::bound_nodes::Node {
identity: node.identity.clone(),
kind: crate::ast::compiler::bound_nodes::BoundKind::Error,
ty: (),
@@ -600,12 +600,12 @@ impl Binder {
fn bind_pattern(
&mut self,
node: &UntypedNode,
node: &SyntaxNode,
kind: DeclarationKind,
diag: &mut Diagnostics,
) -> Node {
match &node.kind {
UntypedKind::Identifier(sym) => {
SyntaxKind::Identifier(sym) => {
if let Some(addr) = self.declare_variable(sym, node.identity.clone(), kind, diag) {
self.make_node(
node.identity.clone(),
@@ -621,7 +621,7 @@ impl Binder {
self.make_node(node.identity.clone(), BoundKind::Error)
}
}
UntypedKind::Tuple { elements } => {
SyntaxKind::Tuple { elements } => {
let mut bound_elems = Vec::new();
for e in elements {
bound_elems.push(Rc::new(self.bind_pattern(e, kind, diag)));
@@ -645,11 +645,11 @@ impl Binder {
fn bind_assign_pattern(
&mut self,
node: &UntypedNode,
node: &SyntaxNode,
diag: &mut Diagnostics,
) -> Node {
match &node.kind {
UntypedKind::Identifier(sym) => {
SyntaxKind::Identifier(sym) => {
if let Some(addr) = self.resolve_variable(sym, diag, &node.identity) {
self.make_node(
node.identity.clone(),
@@ -662,7 +662,7 @@ impl Binder {
self.make_node(node.identity.clone(), BoundKind::Error)
}
}
UntypedKind::Tuple { elements } => {
SyntaxKind::Tuple { elements } => {
let mut bound_elems = Vec::new();
for e in elements {
bound_elems.push(Rc::new(self.bind_assign_pattern(e, diag)));
@@ -703,10 +703,10 @@ mod tests {
fn test_upvalue_capture_sets_is_boxed() {
let source = "(fn [] (do (def x 10) (def f (fn [] x)) x))";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut diagnostics = Diagnostics::new();
let (bound, captures, _scopes, _slots) = Binder::bind_root(vec![], 0, 0, &untyped, &mut diagnostics).unwrap();
let (bound, captures, _scopes, _slots) = Binder::bind_root(vec![], 0, 0, &syntax, &mut diagnostics).unwrap();
if let BoundKind::Lambda { body, .. } = &bound.kind {
if let BoundKind::Block { exprs } = &body.kind {
@@ -732,10 +732,10 @@ mod tests {
fn test_no_capture_not_boxed() {
let source = "(fn [] (do (def x 10) x))";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut diagnostics = Diagnostics::new();
let (bound, captures, _scopes, _slots) = Binder::bind_root(vec![], 0, 0, &untyped, &mut diagnostics).unwrap();
let (bound, captures, _scopes, _slots) = Binder::bind_root(vec![], 0, 0, &syntax, &mut diagnostics).unwrap();
if let BoundKind::Lambda { body, .. } = &bound.kind {
if let BoundKind::Block { exprs } = &body.kind {
@@ -761,10 +761,10 @@ mod tests {
fn test_redefinition_error() {
let source = "(do (def x 1) (def x 2) x)";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut diagnostics = Diagnostics::new();
let _ = Binder::bind_root(vec![], 0, 0, &untyped, &mut diagnostics);
let _ = Binder::bind_root(vec![], 0, 0, &syntax, &mut diagnostics);
assert!(diagnostics.has_errors());
assert!(diagnostics.items.iter().any(|i| i.message.contains("already defined")));
@@ -773,15 +773,15 @@ mod tests {
#[test]
fn test_repro_global_redefinition() {
let source1 = "(def x 1) 1";
let untyped1 = Parser::new(source1).parse_expression();
let syntax1 = Parser::new(source1).parse_expression();
let mut diagnostics = Diagnostics::new();
let (_, _, scopes1, slots1) = Binder::bind_root(vec![], 0, -1, &untyped1, &mut diagnostics).unwrap();
let (_, _, scopes1, slots1) = Binder::bind_root(vec![], 0, -1, &syntax1, &mut diagnostics).unwrap();
let source2 = "(def x 2) 2";
let untyped2 = Parser::new(source2).parse_expression();
let syntax2 = Parser::new(source2).parse_expression();
let mut diagnostics2 = Diagnostics::new();
// Here we simulate frozen scope by passing fixed_scope_idx = 0
let _ = Binder::bind_root(scopes1, slots1, 0, &untyped2, &mut diagnostics2);
let _ = Binder::bind_root(scopes1, slots1, 0, &syntax2, &mut diagnostics2);
assert!(diagnostics2.has_errors());
assert!(diagnostics2.items.iter().any(|i| i.message.contains("frozen/immutable")));
+352 -352
View File
@@ -1,352 +1,352 @@
use crate::ast::nodes::Symbol;
use crate::ast::types::{Identity, StaticType, Value};
use std::rc::Rc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct LocalSlot(pub u32);
impl std::fmt::Display for LocalSlot {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "L{}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct UpvalueIdx(pub u32);
impl std::fmt::Display for UpvalueIdx {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "U{}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct GlobalIdx(pub u32);
impl std::fmt::Display for GlobalIdx {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "G{}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Address {
Local(LocalSlot), // Stack-Slot index (relative to frame base)
Upvalue(UpvalueIdx), // Index in the closure's upvalue array
Global(GlobalIdx), // Index in the global environment vector
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DeclarationKind {
Variable,
Parameter,
}
/// Trait for DSL-specific AST extensions (like Pipe, Series-Access, etc.)
pub trait BoundExtension<T>: std::fmt::Debug {
fn clone_box(&self) -> Box<dyn BoundExtension<T>>;
fn display_name(&self) -> String;
}
impl<T> Clone for Box<dyn BoundExtension<T>> {
fn clone(&self) -> Self {
self.clone_box()
}
}
/// A bound AST node, decorated with type or metric information T.
#[derive(Debug, Clone, PartialEq)]
pub struct Node<T = ()> {
pub identity: Identity,
pub kind: BoundKind<T>,
pub ty: T,
}
/// Type alias for a node that has been fully type-checked.
pub type TypedNode = Node<StaticType>;
/// Type alias for the global function registry.
pub type GlobalFunctionRegistry = std::collections::HashMap<GlobalIdx, Rc<Node>>;
/// Metrics collected during the analysis phase.
#[derive(Debug, Clone, PartialEq)]
pub struct NodeMetrics {
pub original: Rc<TypedNode>,
pub purity: crate::ast::types::Purity,
pub is_recursive: bool,
}
/// Type alias for a node that has been analyzed.
pub type AnalyzedNode = Node<NodeMetrics>;
/// Type alias for the global analyzed function registry.
pub type GlobalAnalyzedRegistry = std::collections::HashMap<GlobalIdx, Rc<AnalyzedNode>>;
#[derive(Debug, Clone)]
pub enum BoundKind<T = ()> {
Nop,
Constant(Value),
// Variable Access (Resolved)
Get {
addr: Address,
name: Symbol,
},
// Variable Update (Assignment)
Set {
addr: Address,
value: Rc<Node<T>>,
},
// Variable Declaration (Unified for Local/Global/Parameter)
Define {
name: Symbol,
addr: Address,
kind: DeclarationKind,
value: Rc<Node<T>>,
captured_by: Vec<Identity>,
},
/// A first-class field accessor (e.g. .name)
FieldAccessor(crate::ast::types::Keyword),
/// Specialized field access (O(1) via RecordLayout)
GetField {
rec: Rc<Node<T>>,
field: crate::ast::types::Keyword,
},
If {
cond: Rc<Node<T>>,
then_br: Rc<Node<T>>,
else_br: Option<Rc<Node<T>>>,
},
/// A destructuring operation (can be a definition or an assignment depending on the pattern)
Destructure {
pattern: Rc<Node<T>>,
value: Rc<Node<T>>,
},
Lambda {
params: Rc<Node<T>>,
// The list of variables captured from enclosing scopes
upvalues: Vec<Address>,
body: Rc<Node<T>>,
/// Static optimization: number of positional parameters if the pattern is flat.
positional_count: Option<u32>,
},
Call {
callee: Rc<Node<T>>,
args: Rc<Node<T>>,
},
Again {
args: Rc<Node<T>>,
},
Pipe {
inputs: Vec<Rc<Node<T>>>,
lambda: Rc<Node<T>>,
out_type: crate::ast::types::StaticType,
},
Block {
exprs: Vec<Rc<Node<T>>>,
},
Tuple {
elements: Vec<Rc<Node<T>>>,
},
Record {
layout: std::sync::Arc<crate::ast::types::RecordLayout>,
values: Vec<Rc<Node<T>>>,
},
/// An expanded macro call, preserving the original call for debugging and UI.
Expansion {
/// The original call from the untyped AST.
original_call: Rc<crate::ast::nodes::UntypedNode>,
/// The result of binding the expanded AST.
bound_expanded: Rc<Node<T>>,
},
/// A diagnostic poison node, allowing compilation to continue after an error.
Error,
/// DSL-specific extension slot
Extension(Box<dyn BoundExtension<T>>),
}
impl<T> PartialEq for BoundKind<T>
where
T: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(BoundKind::Nop, BoundKind::Nop) => true,
(BoundKind::Constant(a), BoundKind::Constant(b)) => a == b,
(BoundKind::Get { addr: aa, name: na }, BoundKind::Get { addr: ab, name: nb }) => {
aa == ab && na == nb
}
(
BoundKind::Set {
addr: aa,
value: va,
},
BoundKind::Set {
addr: ab,
value: vb,
},
) => aa == ab && Rc::ptr_eq(va, vb),
(
BoundKind::Define {
name: na,
addr: aa,
kind: ka,
value: va,
captured_by: ca,
},
BoundKind::Define {
name: nb,
addr: ab,
kind: kb,
value: vb,
captured_by: cb,
},
) => na == nb && aa == ab && ka == kb && Rc::ptr_eq(va, vb) && ca == cb,
(BoundKind::FieldAccessor(a), BoundKind::FieldAccessor(b)) => a == b,
(
BoundKind::GetField { rec: ra, field: fa },
BoundKind::GetField { rec: rb, field: fb },
) => Rc::ptr_eq(ra, rb) && fa == fb,
(
BoundKind::If {
cond: ca,
then_br: ta,
else_br: ea,
},
BoundKind::If {
cond: cb,
then_br: tb,
else_br: eb,
},
) => Rc::ptr_eq(ca, cb) && Rc::ptr_eq(ta, tb) && match (ea, eb) {
(Some(a), Some(b)) => Rc::ptr_eq(a, b),
(None, None) => true,
_ => false,
},
(
BoundKind::Destructure {
pattern: pa,
value: va,
},
BoundKind::Destructure {
pattern: pb,
value: vb,
},
) => Rc::ptr_eq(pa, pb) && Rc::ptr_eq(va, vb),
(
BoundKind::Lambda {
params: pa,
upvalues: ua,
body: ba,
positional_count: pca,
},
BoundKind::Lambda {
params: pb,
upvalues: ub,
body: bb,
positional_count: pcb,
},
) => Rc::ptr_eq(pa, pb) && ua == ub && Rc::ptr_eq(ba, bb) && pca == pcb,
(
BoundKind::Call {
callee: ca,
args: aa,
},
BoundKind::Call {
callee: cb,
args: ab,
},
) => Rc::ptr_eq(ca, cb) && Rc::ptr_eq(aa, ab),
(BoundKind::Again { args: aa }, BoundKind::Again { args: ab }) => Rc::ptr_eq(aa, ab),
(BoundKind::Block { exprs: ea }, BoundKind::Block { exprs: eb }) => {
ea.len() == eb.len() && ea.iter().zip(eb.iter()).all(|(a, b)| Rc::ptr_eq(a, b))
}
(BoundKind::Tuple { elements: ea }, BoundKind::Tuple { elements: eb }) => {
ea.len() == eb.len() && ea.iter().zip(eb.iter()).all(|(a, b)| Rc::ptr_eq(a, b))
}
(
BoundKind::Record {
layout: la,
values: va,
},
BoundKind::Record {
layout: lb,
values: vb,
},
) => std::sync::Arc::ptr_eq(la, lb) && va.len() == vb.len() && va.iter().zip(vb.iter()).all(|(a, b)| Rc::ptr_eq(a, b)),
(
BoundKind::Expansion {
original_call: ca,
bound_expanded: ea,
},
BoundKind::Expansion {
original_call: cb,
bound_expanded: eb,
},
) => Rc::ptr_eq(ca, cb) && Rc::ptr_eq(ea, eb),
(BoundKind::Error, BoundKind::Error) => true,
(BoundKind::Extension(_), BoundKind::Extension(_)) => false,
_ => false,
}
}
}
/// A single field in a Record literal (Key-Value pair)
pub type RecordField<T> = (Node<T>, Node<T>);
impl<T> BoundKind<T> {
pub fn display_name(&self) -> String {
match self {
BoundKind::Nop => "NOP".to_string(),
BoundKind::Constant(v) => format!("CONST({})", v),
BoundKind::Get { addr, name } => format!("GET({}, {:?})", name.name, addr),
BoundKind::Set { addr, .. } => format!("SET({:?})", addr),
BoundKind::Define {
name, addr, kind, ..
} => {
let k_str = match kind {
DeclarationKind::Variable => "VAR",
DeclarationKind::Parameter => "PARAM",
};
format!("DEF_{}({}, {:?})", k_str, name.name, addr)
}
BoundKind::FieldAccessor(k) => format!("FIELD_ACCESSOR(.{})", k.name()),
BoundKind::GetField { field, .. } => format!("GET_FIELD(.{})", field.name()),
BoundKind::If { .. } => "IF".to_string(),
BoundKind::Destructure { .. } => "DESTRUCTURE".to_string(),
BoundKind::Lambda {
params, upvalues, ..
} => {
let p_str = match &params.kind {
BoundKind::Tuple { elements } => format!("p:{}", elements.len()),
_ => "p:1".to_string(),
};
format!("LAMBDA({}, Captures:{})", p_str, upvalues.len())
}
BoundKind::Call { .. } => "CALL".to_string(),
BoundKind::Again { .. } => "AGAIN".to_string(),
BoundKind::Pipe { .. } => "PIPE".to_string(),
BoundKind::Block { .. } => "BLOCK".to_string(),
BoundKind::Tuple { elements } => format!("TUPLE({})", elements.len()),
BoundKind::Record { values, .. } => format!("RECORD({})", values.len()),
BoundKind::Expansion { .. } => "EXPANSION".to_string(),
BoundKind::Extension(ext) => ext.display_name(),
BoundKind::Error => "ERROR".to_string(),
}
}
}
use crate::ast::nodes::Symbol;
use crate::ast::types::{Identity, StaticType, Value};
use std::rc::Rc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct LocalSlot(pub u32);
impl std::fmt::Display for LocalSlot {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "L{}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct UpvalueIdx(pub u32);
impl std::fmt::Display for UpvalueIdx {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "U{}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct GlobalIdx(pub u32);
impl std::fmt::Display for GlobalIdx {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "G{}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Address {
Local(LocalSlot), // Stack-Slot index (relative to frame base)
Upvalue(UpvalueIdx), // Index in the closure's upvalue array
Global(GlobalIdx), // Index in the global environment vector
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DeclarationKind {
Variable,
Parameter,
}
/// Trait for DSL-specific AST extensions (like Pipe, Series-Access, etc.)
pub trait BoundExtension<T>: std::fmt::Debug {
fn clone_box(&self) -> Box<dyn BoundExtension<T>>;
fn display_name(&self) -> String;
}
impl<T> Clone for Box<dyn BoundExtension<T>> {
fn clone(&self) -> Self {
self.clone_box()
}
}
/// A bound AST node, decorated with type or metric information T.
#[derive(Debug, Clone, PartialEq)]
pub struct Node<T = ()> {
pub identity: Identity,
pub kind: BoundKind<T>,
pub ty: T,
}
/// Type alias for a node that has been fully type-checked.
pub type TypedNode = Node<StaticType>;
/// Type alias for the global function registry.
pub type GlobalFunctionRegistry = std::collections::HashMap<GlobalIdx, Rc<Node>>;
/// Metrics collected during the analysis phase.
#[derive(Debug, Clone, PartialEq)]
pub struct NodeMetrics {
pub original: Rc<TypedNode>,
pub purity: crate::ast::types::Purity,
pub is_recursive: bool,
}
/// Type alias for a node that has been analyzed.
pub type AnalyzedNode = Node<NodeMetrics>;
/// Type alias for the global analyzed function registry.
pub type GlobalAnalyzedRegistry = std::collections::HashMap<GlobalIdx, Rc<AnalyzedNode>>;
#[derive(Debug, Clone)]
pub enum BoundKind<T = ()> {
Nop,
Constant(Value),
// Variable Access (Resolved)
Get {
addr: Address,
name: Symbol,
},
// Variable Update (Assignment)
Set {
addr: Address,
value: Rc<Node<T>>,
},
// Variable Declaration (Unified for Local/Global/Parameter)
Define {
name: Symbol,
addr: Address,
kind: DeclarationKind,
value: Rc<Node<T>>,
captured_by: Vec<Identity>,
},
/// A first-class field accessor (e.g. .name)
FieldAccessor(crate::ast::types::Keyword),
/// Specialized field access (O(1) via RecordLayout)
GetField {
rec: Rc<Node<T>>,
field: crate::ast::types::Keyword,
},
If {
cond: Rc<Node<T>>,
then_br: Rc<Node<T>>,
else_br: Option<Rc<Node<T>>>,
},
/// A destructuring operation (can be a definition or an assignment depending on the pattern)
Destructure {
pattern: Rc<Node<T>>,
value: Rc<Node<T>>,
},
Lambda {
params: Rc<Node<T>>,
// The list of variables captured from enclosing scopes
upvalues: Vec<Address>,
body: Rc<Node<T>>,
/// Static optimization: number of positional parameters if the pattern is flat.
positional_count: Option<u32>,
},
Call {
callee: Rc<Node<T>>,
args: Rc<Node<T>>,
},
Again {
args: Rc<Node<T>>,
},
Pipe {
inputs: Vec<Rc<Node<T>>>,
lambda: Rc<Node<T>>,
out_type: crate::ast::types::StaticType,
},
Block {
exprs: Vec<Rc<Node<T>>>,
},
Tuple {
elements: Vec<Rc<Node<T>>>,
},
Record {
layout: std::sync::Arc<crate::ast::types::RecordLayout>,
values: Vec<Rc<Node<T>>>,
},
/// An expanded macro call, preserving the original call for debugging and UI.
Expansion {
/// The original call from the syntax AST.
original_call: Rc<crate::ast::nodes::SyntaxNode>,
/// The result of binding the expanded AST.
bound_expanded: Rc<Node<T>>,
},
/// A diagnostic poison node, allowing compilation to continue after an error.
Error,
/// DSL-specific extension slot
Extension(Box<dyn BoundExtension<T>>),
}
impl<T> PartialEq for BoundKind<T>
where
T: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(BoundKind::Nop, BoundKind::Nop) => true,
(BoundKind::Constant(a), BoundKind::Constant(b)) => a == b,
(BoundKind::Get { addr: aa, name: na }, BoundKind::Get { addr: ab, name: nb }) => {
aa == ab && na == nb
}
(
BoundKind::Set {
addr: aa,
value: va,
},
BoundKind::Set {
addr: ab,
value: vb,
},
) => aa == ab && Rc::ptr_eq(va, vb),
(
BoundKind::Define {
name: na,
addr: aa,
kind: ka,
value: va,
captured_by: ca,
},
BoundKind::Define {
name: nb,
addr: ab,
kind: kb,
value: vb,
captured_by: cb,
},
) => na == nb && aa == ab && ka == kb && Rc::ptr_eq(va, vb) && ca == cb,
(BoundKind::FieldAccessor(a), BoundKind::FieldAccessor(b)) => a == b,
(
BoundKind::GetField { rec: ra, field: fa },
BoundKind::GetField { rec: rb, field: fb },
) => Rc::ptr_eq(ra, rb) && fa == fb,
(
BoundKind::If {
cond: ca,
then_br: ta,
else_br: ea,
},
BoundKind::If {
cond: cb,
then_br: tb,
else_br: eb,
},
) => Rc::ptr_eq(ca, cb) && Rc::ptr_eq(ta, tb) && match (ea, eb) {
(Some(a), Some(b)) => Rc::ptr_eq(a, b),
(None, None) => true,
_ => false,
},
(
BoundKind::Destructure {
pattern: pa,
value: va,
},
BoundKind::Destructure {
pattern: pb,
value: vb,
},
) => Rc::ptr_eq(pa, pb) && Rc::ptr_eq(va, vb),
(
BoundKind::Lambda {
params: pa,
upvalues: ua,
body: ba,
positional_count: pca,
},
BoundKind::Lambda {
params: pb,
upvalues: ub,
body: bb,
positional_count: pcb,
},
) => Rc::ptr_eq(pa, pb) && ua == ub && Rc::ptr_eq(ba, bb) && pca == pcb,
(
BoundKind::Call {
callee: ca,
args: aa,
},
BoundKind::Call {
callee: cb,
args: ab,
},
) => Rc::ptr_eq(ca, cb) && Rc::ptr_eq(aa, ab),
(BoundKind::Again { args: aa }, BoundKind::Again { args: ab }) => Rc::ptr_eq(aa, ab),
(BoundKind::Block { exprs: ea }, BoundKind::Block { exprs: eb }) => {
ea.len() == eb.len() && ea.iter().zip(eb.iter()).all(|(a, b)| Rc::ptr_eq(a, b))
}
(BoundKind::Tuple { elements: ea }, BoundKind::Tuple { elements: eb }) => {
ea.len() == eb.len() && ea.iter().zip(eb.iter()).all(|(a, b)| Rc::ptr_eq(a, b))
}
(
BoundKind::Record {
layout: la,
values: va,
},
BoundKind::Record {
layout: lb,
values: vb,
},
) => std::sync::Arc::ptr_eq(la, lb) && va.len() == vb.len() && va.iter().zip(vb.iter()).all(|(a, b)| Rc::ptr_eq(a, b)),
(
BoundKind::Expansion {
original_call: ca,
bound_expanded: ea,
},
BoundKind::Expansion {
original_call: cb,
bound_expanded: eb,
},
) => Rc::ptr_eq(ca, cb) && Rc::ptr_eq(ea, eb),
(BoundKind::Error, BoundKind::Error) => true,
(BoundKind::Extension(_), BoundKind::Extension(_)) => false,
_ => false,
}
}
}
/// A single field in a Record literal (Key-Value pair)
pub type RecordField<T> = (Node<T>, Node<T>);
impl<T> BoundKind<T> {
pub fn display_name(&self) -> String {
match self {
BoundKind::Nop => "NOP".to_string(),
BoundKind::Constant(v) => format!("CONST({})", v),
BoundKind::Get { addr, name } => format!("GET({}, {:?})", name.name, addr),
BoundKind::Set { addr, .. } => format!("SET({:?})", addr),
BoundKind::Define {
name, addr, kind, ..
} => {
let k_str = match kind {
DeclarationKind::Variable => "VAR",
DeclarationKind::Parameter => "PARAM",
};
format!("DEF_{}({}, {:?})", k_str, name.name, addr)
}
BoundKind::FieldAccessor(k) => format!("FIELD_ACCESSOR(.{})", k.name()),
BoundKind::GetField { field, .. } => format!("GET_FIELD(.{})", field.name()),
BoundKind::If { .. } => "IF".to_string(),
BoundKind::Destructure { .. } => "DESTRUCTURE".to_string(),
BoundKind::Lambda {
params, upvalues, ..
} => {
let p_str = match &params.kind {
BoundKind::Tuple { elements } => format!("p:{}", elements.len()),
_ => "p:1".to_string(),
};
format!("LAMBDA({}, Captures:{})", p_str, upvalues.len())
}
BoundKind::Call { .. } => "CALL".to_string(),
BoundKind::Again { .. } => "AGAIN".to_string(),
BoundKind::Pipe { .. } => "PIPE".to_string(),
BoundKind::Block { .. } => "BLOCK".to_string(),
BoundKind::Tuple { elements } => format!("TUPLE({})", elements.len()),
BoundKind::Record { values, .. } => format!("RECORD({})", values.len()),
BoundKind::Expansion { .. } => "EXPANSION".to_string(),
BoundKind::Extension(ext) => ext.display_name(),
BoundKind::Error => "ERROR".to_string(),
}
}
}
+154 -154
View File
@@ -1,154 +1,154 @@
use crate::ast::compiler::bound_nodes::{BoundKind, Node};
use crate::ast::types::Identity;
use std::collections::HashMap;
pub struct CapturePass;
impl CapturePass {
pub fn apply<T: Clone>(node: Node<T>, capture_map: &HashMap<Identity, Vec<Identity>>) -> Node<T> {
Self::transform(node, capture_map)
}
fn transform<T: Clone>(mut node: Node<T>, capture_map: &HashMap<Identity, Vec<Identity>>) -> Node<T> {
use std::rc::Rc;
match node.kind {
BoundKind::Define {
name,
addr,
kind,
value,
..
} => {
let captured_by = capture_map.get(&node.identity).cloned().unwrap_or_default();
node.kind = BoundKind::Define {
name,
addr,
kind,
value: Rc::new(Self::transform(value.as_ref().clone(), capture_map)),
captured_by,
};
}
BoundKind::If {
cond,
then_br,
else_br,
} => {
node.kind = BoundKind::If {
cond: Rc::new(Self::transform(cond.as_ref().clone(), capture_map)),
then_br: Rc::new(Self::transform(then_br.as_ref().clone(), capture_map)),
else_br: else_br.map(|e| Rc::new(Self::transform(e.as_ref().clone(), capture_map))),
};
}
BoundKind::Set { addr, value } => {
node.kind = BoundKind::Set {
addr,
value: Rc::new(Self::transform(value.as_ref().clone(), capture_map)),
};
}
BoundKind::FieldAccessor(_) => {}
BoundKind::GetField { rec, field } => {
node.kind = BoundKind::GetField {
rec: Rc::new(Self::transform(rec.as_ref().clone(), capture_map)),
field,
};
}
BoundKind::Destructure { pattern, value } => {
node.kind = BoundKind::Destructure {
pattern: Rc::new(Self::transform(pattern.as_ref().clone(), capture_map)),
value: Rc::new(Self::transform(value.as_ref().clone(), capture_map)),
};
}
BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
} => {
node.kind = BoundKind::Lambda {
params: Rc::new(Self::transform(params.as_ref().clone(), capture_map)),
upvalues,
body: Rc::new(Self::transform(body.as_ref().clone(), capture_map)),
positional_count,
};
}
BoundKind::Call { callee, args } => {
node.kind = BoundKind::Call {
callee: Rc::new(Self::transform(callee.as_ref().clone(), capture_map)),
args: Rc::new(Self::transform(args.as_ref().clone(), capture_map)),
};
}
BoundKind::Again { args } => {
node.kind = BoundKind::Again {
args: Rc::new(Self::transform(args.as_ref().clone(), capture_map)),
};
}
BoundKind::Pipe {
inputs,
lambda,
out_type,
} => {
let mut t_inputs = Vec::with_capacity(inputs.len());
for input in inputs {
t_inputs.push(Rc::new(Self::transform(input.as_ref().clone(), capture_map)));
}
node.kind = BoundKind::Pipe {
inputs: t_inputs,
lambda: Rc::new(Self::transform(lambda.as_ref().clone(), capture_map)),
out_type: out_type.clone(),
};
}
BoundKind::Block { exprs } => {
node.kind = BoundKind::Block {
exprs: exprs
.into_iter()
.map(|e| Rc::new(Self::transform(e.as_ref().clone(), capture_map)))
.collect(),
};
}
BoundKind::Tuple { elements } => {
node.kind = BoundKind::Tuple {
elements: elements
.into_iter()
.map(|e| Rc::new(Self::transform(e.as_ref().clone(), capture_map)))
.collect(),
};
}
BoundKind::Record { layout, values } => {
node.kind = BoundKind::Record {
layout,
values: values
.into_iter()
.map(|v| Rc::new(Self::transform(v.as_ref().clone(), capture_map)))
.collect(),
};
}
BoundKind::Expansion {
original_call,
bound_expanded,
} => {
node.kind = BoundKind::Expansion {
original_call,
bound_expanded: Rc::new(Self::transform(bound_expanded.as_ref().clone(), capture_map)),
};
}
BoundKind::Nop
| BoundKind::Constant(_)
| BoundKind::Get { .. }
| BoundKind::Extension(_)
| BoundKind::Error => {}
}
node
}
}
use crate::ast::compiler::bound_nodes::{BoundKind, Node};
use crate::ast::types::Identity;
use std::collections::HashMap;
pub struct CapturePass;
impl CapturePass {
pub fn apply<T: Clone>(node: Node<T>, capture_map: &HashMap<Identity, Vec<Identity>>) -> Node<T> {
Self::transform(node, capture_map)
}
fn transform<T: Clone>(mut node: Node<T>, capture_map: &HashMap<Identity, Vec<Identity>>) -> Node<T> {
use std::rc::Rc;
match node.kind {
BoundKind::Define {
name,
addr,
kind,
value,
..
} => {
let captured_by = capture_map.get(&node.identity).cloned().unwrap_or_default();
node.kind = BoundKind::Define {
name,
addr,
kind,
value: Rc::new(Self::transform(value.as_ref().clone(), capture_map)),
captured_by,
};
}
BoundKind::If {
cond,
then_br,
else_br,
} => {
node.kind = BoundKind::If {
cond: Rc::new(Self::transform(cond.as_ref().clone(), capture_map)),
then_br: Rc::new(Self::transform(then_br.as_ref().clone(), capture_map)),
else_br: else_br.map(|e| Rc::new(Self::transform(e.as_ref().clone(), capture_map))),
};
}
BoundKind::Set { addr, value } => {
node.kind = BoundKind::Set {
addr,
value: Rc::new(Self::transform(value.as_ref().clone(), capture_map)),
};
}
BoundKind::FieldAccessor(_) => {}
BoundKind::GetField { rec, field } => {
node.kind = BoundKind::GetField {
rec: Rc::new(Self::transform(rec.as_ref().clone(), capture_map)),
field,
};
}
BoundKind::Destructure { pattern, value } => {
node.kind = BoundKind::Destructure {
pattern: Rc::new(Self::transform(pattern.as_ref().clone(), capture_map)),
value: Rc::new(Self::transform(value.as_ref().clone(), capture_map)),
};
}
BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
} => {
node.kind = BoundKind::Lambda {
params: Rc::new(Self::transform(params.as_ref().clone(), capture_map)),
upvalues,
body: Rc::new(Self::transform(body.as_ref().clone(), capture_map)),
positional_count,
};
}
BoundKind::Call { callee, args } => {
node.kind = BoundKind::Call {
callee: Rc::new(Self::transform(callee.as_ref().clone(), capture_map)),
args: Rc::new(Self::transform(args.as_ref().clone(), capture_map)),
};
}
BoundKind::Again { args } => {
node.kind = BoundKind::Again {
args: Rc::new(Self::transform(args.as_ref().clone(), capture_map)),
};
}
BoundKind::Pipe {
inputs,
lambda,
out_type,
} => {
let mut t_inputs = Vec::with_capacity(inputs.len());
for input in inputs {
t_inputs.push(Rc::new(Self::transform(input.as_ref().clone(), capture_map)));
}
node.kind = BoundKind::Pipe {
inputs: t_inputs,
lambda: Rc::new(Self::transform(lambda.as_ref().clone(), capture_map)),
out_type: out_type.clone(),
};
}
BoundKind::Block { exprs } => {
node.kind = BoundKind::Block {
exprs: exprs
.into_iter()
.map(|e| Rc::new(Self::transform(e.as_ref().clone(), capture_map)))
.collect(),
};
}
BoundKind::Tuple { elements } => {
node.kind = BoundKind::Tuple {
elements: elements
.into_iter()
.map(|e| Rc::new(Self::transform(e.as_ref().clone(), capture_map)))
.collect(),
};
}
BoundKind::Record { layout, values } => {
node.kind = BoundKind::Record {
layout,
values: values
.into_iter()
.map(|v| Rc::new(Self::transform(v.as_ref().clone(), capture_map)))
.collect(),
};
}
BoundKind::Expansion {
original_call,
bound_expanded,
} => {
node.kind = BoundKind::Expansion {
original_call,
bound_expanded: Rc::new(Self::transform(bound_expanded.as_ref().clone(), capture_map)),
};
}
BoundKind::Nop
| BoundKind::Constant(_)
| BoundKind::Get { .. }
| BoundKind::Extension(_)
| BoundKind::Error => {}
}
node
}
}
+264 -264
View File
@@ -1,264 +1,264 @@
use crate::ast::compiler::bound_nodes::{BoundKind, Node};
use crate::ast::types::Value;
use crate::ast::vm::Closure;
use std::fmt::Debug;
/// 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<T: Debug>(node: &Node<T>) -> 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<T: Debug>(&mut self, label: &str, node: &Node<T>) {
self.write_indent();
self.output.push_str(label);
self.output
.push_str(&format!(" <Metadata: {:?}>\n", node.ty));
}
fn visit<T: Debug>(&mut self, node: &Node<T>) {
match &node.kind {
BoundKind::Nop => self.log("Nop", node),
BoundKind::Constant(v) => {
self.log(&format!("Constant: {}", v), node);
// Introspect Closure AST if possible
if let Value::Object(obj) = v
&& let Some(closure) = obj.as_any().downcast_ref::<Closure>()
{
self.indent += 1;
self.write_indent();
self.output.push_str("--- Specialized Body ---\n");
// We need to cast the inner TypedNode to the generic T required by visit.
// Since Dumper is generic over T, but Closure stores TypedNode (where T = StaticType),
// we can only fully dump if T is StaticType.
// However, we can hack it by creating a new Dumper for the inner AST string.
// We can't call self.visit because types mismatch if T != StaticType.
// So we just recursively dump to string and append.
let inner_dump = Dumper::dump(&closure.function_node);
for line in inner_dump.lines() {
self.write_indent();
self.output.push_str(line);
self.output.push('\n');
}
self.indent -= 1;
}
}
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::{BoundKind, Node};
use crate::ast::types::Value;
use crate::ast::vm::Closure;
use std::fmt::Debug;
/// 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<T: Debug>(node: &Node<T>) -> 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<T: Debug>(&mut self, label: &str, node: &Node<T>) {
self.write_indent();
self.output.push_str(label);
self.output
.push_str(&format!(" <Metadata: {:?}>\n", node.ty));
}
fn visit<T: Debug>(&mut self, node: &Node<T>) {
match &node.kind {
BoundKind::Nop => self.log("Nop", node),
BoundKind::Constant(v) => {
self.log(&format!("Constant: {}", v), node);
// Introspect Closure AST if possible
if let Value::Object(obj) = v
&& let Some(closure) = obj.as_any().downcast_ref::<Closure>()
{
self.indent += 1;
self.write_indent();
self.output.push_str("--- Specialized Body ---\n");
// We need to cast the inner TypedNode to the generic T required by visit.
// Since Dumper is generic over T, but Closure stores TypedNode (where T = StaticType),
// we can only fully dump if T is StaticType.
// However, we can hack it by creating a new Dumper for the inner AST string.
// We can't call self.visit because types mismatch if T != StaticType.
// So we just recursively dump to string and append.
let inner_dump = Dumper::dump(&closure.function_node);
for line in inner_dump.lines() {
self.write_indent();
self.output.push_str(line);
self.output.push('\n');
}
self.indent -= 1;
}
}
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);
}
}
}
}
+99 -99
View File
@@ -1,99 +1,99 @@
use crate::ast::compiler::bound_nodes::{Address, BoundKind, 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, T> {
registry: &'a mut HashMap<GlobalIdx, Rc<Node<T>>>,
}
impl<'a, T: Clone> LambdaCollector<'a, T> {
/// Performs a full traversal of the AST and populates the provided registry.
pub fn collect(node: &Node<T>, registry: &'a mut HashMap<GlobalIdx, Rc<Node<T>>>) {
let mut collector = Self { registry };
collector.visit(node);
}
fn visit(&mut self, node: &Node<T>) {
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, .. } = &current.kind {
current = bound_expanded;
}
if let BoundKind::Lambda { .. } = &current.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, .. } = &current.kind {
current = bound_expanded;
}
if let BoundKind::Lambda { .. } = &current.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, BoundKind, 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, T> {
registry: &'a mut HashMap<GlobalIdx, Rc<Node<T>>>,
}
impl<'a, T: Clone> LambdaCollector<'a, T> {
/// Performs a full traversal of the AST and populates the provided registry.
pub fn collect(node: &Node<T>, registry: &'a mut HashMap<GlobalIdx, Rc<Node<T>>>) {
let mut collector = Self { registry };
collector.visit(node);
}
fn visit(&mut self, node: &Node<T>) {
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, .. } = &current.kind {
current = bound_expanded;
}
if let BoundKind::Lambda { .. } = &current.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, .. } = &current.kind {
current = bound_expanded;
}
if let BoundKind::Lambda { .. } = &current.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
}
}
}
+144 -144
View File
@@ -1,4 +1,4 @@
use crate::ast::nodes::{Symbol, UntypedKind, UntypedNode};
use crate::ast::nodes::{Symbol, SyntaxKind, SyntaxNode};
use crate::ast::types::{Identity, Value};
use std::collections::HashMap;
use std::rc::Rc;
@@ -7,19 +7,19 @@ use std::rc::Rc;
pub trait MacroEvaluator {
fn evaluate(
&self,
node: &UntypedNode,
bindings: &HashMap<Rc<str>, UntypedNode>,
node: &SyntaxNode,
bindings: &HashMap<Rc<str>, SyntaxNode>,
) -> Result<Value, String>;
}
/// Internal state for template expansion (Hygiene context + Parameter binding)
struct ExpansionState<'a> {
bindings: &'a HashMap<Rc<str>, UntypedNode>,
bindings: &'a HashMap<Rc<str>, SyntaxNode>,
/// The identity of the current expansion instance, used to "color" internal symbols.
expansion_id: Identity,
}
type MacroMap = HashMap<Rc<str>, (Vec<Rc<str>>, UntypedNode)>;
type MacroMap = HashMap<Rc<str>, (Vec<Rc<str>>, SyntaxNode)>;
/// A registry for macro declarations.
#[derive(Clone)]
@@ -50,7 +50,7 @@ impl MacroRegistry {
}
}
pub fn define(&mut self, name: Rc<str>, params: Vec<Rc<str>>, body: UntypedNode) {
pub fn define(&mut self, name: Rc<str>, params: Vec<Rc<str>>, body: SyntaxNode) {
if let Some(current_rc) = self.scopes.last_mut() {
// Copy-on-Write: If the Rc is shared, clone the map before modifying.
let current = Rc::make_mut(current_rc);
@@ -58,7 +58,7 @@ impl MacroRegistry {
}
}
pub fn lookup(&self, name: &str) -> Option<(Vec<Rc<str>>, UntypedNode)> {
pub fn lookup(&self, name: &str) -> Option<(Vec<Rc<str>>, SyntaxNode)> {
for scope in self.scopes.iter().rev() {
if let Some(m) = scope.get(name) {
return Some(m.clone());
@@ -85,30 +85,30 @@ impl<E: MacroEvaluator> MacroExpander<E> {
self.registry
}
pub fn expand(&mut self, node: UntypedNode) -> Result<UntypedNode, String> {
pub fn expand(&mut self, node: SyntaxNode) -> Result<SyntaxNode, String> {
self.expand_recursive(node)
}
fn expand_recursive(&mut self, node: UntypedNode) -> Result<UntypedNode, String> {
fn expand_recursive(&mut self, node: SyntaxNode) -> Result<SyntaxNode, String> {
match node.kind {
UntypedKind::MacroDecl { name, params, body } => {
SyntaxKind::MacroDecl { name, params, body } => {
let p_names = self.extract_param_names(&params)?;
self.registry.define(name.name, p_names, *body);
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Nop,
kind: SyntaxKind::Nop,
})
}
UntypedKind::Call { callee, args } => {
if let UntypedKind::Identifier(ref sym) = callee.kind
SyntaxKind::Call { callee, args } => {
if let SyntaxKind::Identifier(ref sym) = callee.kind
&& let Some((params, body)) = self.registry.lookup(&sym.name)
{
let expanded_args = self.expand_recursive(*args)?;
// Extract argument nodes from the expanded tuple
let arg_elements = if let UntypedKind::Tuple { elements } = expanded_args.kind {
let arg_elements = if let SyntaxKind::Tuple { elements } = expanded_args.kind {
elements
} else {
vec![expanded_args]
@@ -127,9 +127,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
let expanded_callee = self.expand_recursive(*callee)?;
let expanded_args = self.expand_recursive(*args)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Call {
kind: SyntaxKind::Call {
callee: Box::new(expanded_callee),
args: Box::new(expanded_args),
},
@@ -137,7 +137,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Block { exprs } => {
SyntaxKind::Block { exprs } => {
self.registry.push();
let mut expanded_exprs = Vec::new();
for expr in exprs {
@@ -145,24 +145,24 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}
self.registry.pop();
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Block {
kind: SyntaxKind::Block {
exprs: expanded_exprs,
},
})
}
UntypedKind::Pipe { inputs, lambda } => {
SyntaxKind::Pipe { inputs, lambda } => {
let mut expanded_inputs = Vec::with_capacity(inputs.len());
for input in inputs {
expanded_inputs.push(self.expand_recursive(input)?);
}
let expanded_lambda = Box::new(self.expand_recursive(*lambda)?);
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Pipe {
kind: SyntaxKind::Pipe {
inputs: expanded_inputs,
lambda: expanded_lambda,
},
@@ -170,15 +170,15 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Lambda { params, body } => {
SyntaxKind::Lambda { params, body } => {
self.registry.push();
let expanded_params = self.expand_recursive(*params)?;
let expanded_body = self.expand_recursive((*body).clone())?;
self.registry.pop();
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Lambda {
kind: SyntaxKind::Lambda {
params: Box::new(expanded_params),
body: Rc::new(expanded_body),
},
@@ -186,7 +186,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::If {
SyntaxKind::If {
cond,
then_br,
else_br,
@@ -198,9 +198,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} else {
None
};
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::If {
kind: SyntaxKind::If {
cond: Box::new(cond),
then_br: Box::new(then_br),
else_br,
@@ -209,12 +209,12 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Def { target, value } => {
SyntaxKind::Def { target, value } => {
let target = self.expand_recursive(*target)?;
let value = self.expand_recursive(*value)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Def {
kind: SyntaxKind::Def {
target: Box::new(target),
value: Box::new(value),
},
@@ -222,12 +222,12 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Assign { target, value } => {
SyntaxKind::Assign { target, value } => {
let target = self.expand_recursive(*target)?;
let value = self.expand_recursive(*value)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Assign {
kind: SyntaxKind::Assign {
target: Box::new(target),
value: Box::new(value),
},
@@ -235,39 +235,39 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Tuple { elements } => {
SyntaxKind::Tuple { elements } => {
let mut expanded_elements = Vec::new();
for e in elements {
expanded_elements.push(self.expand_recursive(e)?);
}
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Tuple {
kind: SyntaxKind::Tuple {
elements: expanded_elements,
},
})
}
UntypedKind::Record { fields } => {
SyntaxKind::Record { fields } => {
let mut expanded_fields = Vec::new();
for (k, v) in fields {
expanded_fields.push((self.expand_recursive(k)?, self.expand_recursive(v)?));
}
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Record {
kind: SyntaxKind::Record {
fields: expanded_fields,
},
})
}
UntypedKind::Expansion { call, expanded } => {
SyntaxKind::Expansion { call, expanded } => {
let expanded = self.expand_recursive(*expanded)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Expansion {
kind: SyntaxKind::Expansion {
call,
expanded: Box::new(expanded),
},
@@ -275,11 +275,11 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Again { args } => {
SyntaxKind::Again { args } => {
let expanded_args = self.expand_recursive(*args)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Again {
kind: SyntaxKind::Again {
args: Box::new(expanded_args),
},
@@ -295,9 +295,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
identity: Identity,
name: &str,
params: Vec<Rc<str>>,
args: Vec<UntypedNode>,
body: UntypedNode,
) -> Result<UntypedNode, String> {
args: Vec<SyntaxNode>,
body: SyntaxNode,
) -> Result<SyntaxNode, String> {
if params.len() != args.len() {
return Err(format!(
"Macro {} expects {} arguments, but got {}",
@@ -313,7 +313,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}
// AST-Authority: Substitution and Hygiene ONLY happen if there is a Template node.
let expanded_body = if let UntypedKind::Template(inner) = body.kind {
let expanded_body = if let SyntaxKind::Template(inner) = body.kind {
let mut state = ExpansionState {
bindings: &bindings,
expansion_id: identity.clone(),
@@ -324,17 +324,17 @@ impl<E: MacroEvaluator> MacroExpander<E> {
body
};
let original_call = UntypedNode {
let original_call = SyntaxNode {
identity: identity.clone(),
kind: UntypedKind::Call {
callee: Box::new(UntypedNode {
kind: SyntaxKind::Call {
callee: Box::new(SyntaxNode {
identity: identity.clone(),
kind: UntypedKind::Identifier(Symbol::from(name)),
kind: SyntaxKind::Identifier(Symbol::from(name)),
}),
args: Box::new(UntypedNode {
args: Box::new(SyntaxNode {
identity: identity.clone(),
kind: UntypedKind::Tuple {
kind: SyntaxKind::Tuple {
elements: bindings.into_values().collect(),
},
@@ -343,9 +343,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
};
Ok(UntypedNode {
Ok(SyntaxNode {
identity,
kind: UntypedKind::Expansion {
kind: SyntaxKind::Expansion {
call: Box::new(original_call),
expanded: Box::new(expanded_body),
},
@@ -353,10 +353,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
fn extract_param_names(&self, node: &UntypedNode) -> Result<Vec<Rc<str>>, String> {
fn extract_param_names(&self, node: &SyntaxNode) -> Result<Vec<Rc<str>>, String> {
match &node.kind {
UntypedKind::Identifier(sym) => Ok(vec![sym.name.clone()]),
UntypedKind::Tuple { elements } => {
SyntaxKind::Identifier(sym) => Ok(vec![sym.name.clone()]),
SyntaxKind::Tuple { elements } => {
let mut names = Vec::new();
for el in elements {
names.extend(self.extract_param_names(el)?);
@@ -369,23 +369,23 @@ impl<E: MacroEvaluator> MacroExpander<E> {
fn expand_template(
&self,
node: UntypedNode,
node: SyntaxNode,
state: &mut ExpansionState,
) -> Result<UntypedNode, String> {
) -> Result<SyntaxNode, String> {
match node.kind {
UntypedKind::Identifier(mut sym) => {
SyntaxKind::Identifier(mut sym) => {
// Inside a template, all internal identifiers are colored.
sym.context = Some(state.expansion_id.clone());
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Identifier(sym),
kind: SyntaxKind::Identifier(sym),
})
}
UntypedKind::Placeholder(inner) => {
SyntaxKind::Placeholder(inner) => {
// Break out of template for substitution/evaluation
if let UntypedKind::Identifier(ref sym) = inner.kind
if let SyntaxKind::Identifier(ref sym) = inner.kind
&& let Some(arg) = state.bindings.get(&sym.name)
{
return Ok(arg.clone());
@@ -394,9 +394,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
Ok(self.value_to_node(val, node.identity))
}
UntypedKind::Splice(inner) => {
SyntaxKind::Splice(inner) => {
// Splicing is also a form of substitution
if let UntypedKind::Identifier(ref sym) = inner.kind
if let SyntaxKind::Identifier(ref sym) = inner.kind
&& let Some(arg) = state.bindings.get(&sym.name)
{
return Ok(arg.clone());
@@ -405,12 +405,12 @@ impl<E: MacroEvaluator> MacroExpander<E> {
Ok(self.value_to_node(val, node.identity))
}
UntypedKind::Def { target, value } => {
SyntaxKind::Def { target, value } => {
let target = self.expand_template(*target, state)?;
let val = self.expand_template(*value, state)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Def {
kind: SyntaxKind::Def {
target: Box::new(target),
value: Box::new(val),
},
@@ -418,12 +418,12 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Assign { target, value } => {
SyntaxKind::Assign { target, value } => {
let target = self.expand_template(*target, state)?;
let value = self.expand_template(*value, state)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Assign {
kind: SyntaxKind::Assign {
target: Box::new(target),
value: Box::new(value),
},
@@ -431,43 +431,43 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Tuple { elements } => {
SyntaxKind::Tuple { elements } => {
let mut new_elements = Vec::new();
for e in elements {
if let UntypedKind::Splice(ref inner) = e.kind {
if let SyntaxKind::Splice(ref inner) = e.kind {
let val = self.evaluator.evaluate(inner, state.bindings)?;
self.handle_splice_value(val, node.identity.clone(), &mut new_elements)?;
} else {
new_elements.push(self.expand_template(e, state)?);
}
}
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Tuple {
kind: SyntaxKind::Tuple {
elements: new_elements,
},
})
}
UntypedKind::Block { exprs } => {
SyntaxKind::Block { exprs } => {
let mut new_exprs = Vec::new();
for e in exprs {
if let UntypedKind::Splice(ref inner) = e.kind {
if let SyntaxKind::Splice(ref inner) = e.kind {
let val = self.evaluator.evaluate(inner, state.bindings)?;
self.handle_splice_value(val, node.identity.clone(), &mut new_exprs)?;
} else {
new_exprs.push(self.expand_template(e, state)?);
}
}
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Block { exprs: new_exprs },
kind: SyntaxKind::Block { exprs: new_exprs },
})
}
UntypedKind::Record { fields } => {
SyntaxKind::Record { fields } => {
let mut new_fields = Vec::new();
for (k, v) in fields {
new_fields.push((
@@ -475,19 +475,19 @@ impl<E: MacroEvaluator> MacroExpander<E> {
self.expand_template(v, state)?,
));
}
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Record { fields: new_fields },
kind: SyntaxKind::Record { fields: new_fields },
})
}
UntypedKind::Call { callee, args } => {
SyntaxKind::Call { callee, args } => {
let expanded_callee = self.expand_template(*callee, state)?;
let expanded_args = self.expand_template(*args, state)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Call {
kind: SyntaxKind::Call {
callee: Box::new(expanded_callee),
args: Box::new(expanded_args),
},
@@ -495,7 +495,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::If {
SyntaxKind::If {
cond,
then_br,
else_br,
@@ -507,9 +507,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} else {
None
};
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::If {
kind: SyntaxKind::If {
cond: Box::new(cond),
then_br: Box::new(then_br),
else_br,
@@ -518,15 +518,15 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Pipe { inputs, lambda } => {
SyntaxKind::Pipe { inputs, lambda } => {
let mut expanded_inputs = Vec::with_capacity(inputs.len());
for input in inputs {
expanded_inputs.push(self.expand_template(input, state)?);
}
let expanded_lambda = Box::new(self.expand_template(*lambda, state)?);
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Pipe {
kind: SyntaxKind::Pipe {
inputs: expanded_inputs,
lambda: expanded_lambda,
},
@@ -534,12 +534,12 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Lambda { params, body } => {
SyntaxKind::Lambda { params, body } => {
let expanded_params = self.expand_template(*params, state)?;
let body = self.expand_template((*body).clone(), state)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Lambda {
kind: SyntaxKind::Lambda {
params: Box::new(expanded_params),
body: Rc::new(body),
},
@@ -547,11 +547,11 @@ impl<E: MacroEvaluator> MacroExpander<E> {
})
}
UntypedKind::Again { args } => {
SyntaxKind::Again { args } => {
let expanded_args = self.expand_template(*args, state)?;
Ok(UntypedNode {
Ok(SyntaxNode {
identity: node.identity,
kind: UntypedKind::Again {
kind: SyntaxKind::Again {
args: Box::new(expanded_args),
},
@@ -566,19 +566,19 @@ impl<E: MacroEvaluator> MacroExpander<E> {
&self,
val: Value,
_identity: Identity,
target: &mut Vec<UntypedNode>,
target: &mut Vec<SyntaxNode>,
) -> Result<(), String> {
match val {
Value::Object(obj) => {
if let Some(node) = obj.as_any().downcast_ref::<UntypedNode>() {
if let Some(node) = obj.as_any().downcast_ref::<SyntaxNode>() {
match &node.kind {
UntypedKind::Tuple { elements } => {
SyntaxKind::Tuple { elements } => {
for e in elements {
target.push(e.clone());
}
return Ok(());
}
UntypedKind::Block { exprs } => {
SyntaxKind::Block { exprs } => {
for e in exprs {
target.push(e.clone());
}
@@ -599,21 +599,21 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}
}
fn value_to_node(&self, val: Value, identity: Identity) -> UntypedNode {
fn value_to_node(&self, val: Value, identity: Identity) -> SyntaxNode {
match val {
Value::Object(obj) => {
if let Some(node) = obj.as_any().downcast_ref::<UntypedNode>() {
if let Some(node) = obj.as_any().downcast_ref::<SyntaxNode>() {
return node.clone();
}
UntypedNode {
SyntaxNode {
identity,
kind: UntypedKind::Constant(Value::Object(obj)),
kind: SyntaxKind::Constant(Value::Object(obj)),
}
}
_ => UntypedNode {
_ => SyntaxNode {
identity,
kind: UntypedKind::Constant(val),
kind: SyntaxKind::Constant(val),
},
}
@@ -632,10 +632,10 @@ mod tests {
impl MacroEvaluator for SimpleEvaluator {
fn evaluate(
&self,
node: &UntypedNode,
bindings: &HashMap<Rc<str>, UntypedNode>,
node: &SyntaxNode,
bindings: &HashMap<Rc<str>, SyntaxNode>,
) -> Result<Value, String> {
if let UntypedKind::Identifier(ref sym) = node.kind
if let SyntaxKind::Identifier(ref sym) = node.kind
&& let Some(arg) = bindings.get(&sym.name)
{
return Ok(Value::Object(Rc::new(arg.clone()) as Rc<dyn Object>));
@@ -655,19 +655,19 @@ mod tests {
(m))
";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap();
let expanded = expander.expand(syntax).unwrap();
if let UntypedKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Expansion {
if let SyntaxKind::Block { exprs } = &expanded.kind
&& let SyntaxKind::Expansion {
expanded: result,
call,
} = &exprs[1].kind
{
if let UntypedKind::Def { target, .. } = &result.kind {
if let UntypedKind::Identifier(sym) = &target.kind {
if let SyntaxKind::Def { target, .. } = &result.kind {
if let SyntaxKind::Identifier(sym) = &target.kind {
assert_eq!(sym.context, Some(call.identity.clone()));
assert_eq!(sym.name.as_ref(), "y");
} else {
@@ -687,30 +687,30 @@ mod tests {
(unless false 42))
";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap();
let expanded = expander.expand(syntax).unwrap();
if let UntypedKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Expansion {
if let SyntaxKind::Block { exprs } = &expanded.kind
&& let SyntaxKind::Expansion {
call: _,
expanded: result,
} = &exprs[1].kind
&& let UntypedKind::If {
&& let SyntaxKind::If {
cond,
then_br,
else_br,
} = &result.kind
{
if let UntypedKind::Identifier(sym) = &cond.kind {
if let SyntaxKind::Identifier(sym) = &cond.kind {
assert_eq!(sym.name.as_ref(), "false");
} else {
panic!("Expected identifier 'false', got {:?}", cond.kind);
}
assert!(matches!(then_br.kind, UntypedKind::Nop));
assert!(matches!(then_br.kind, SyntaxKind::Nop));
if let Some(eb) = else_br {
if let UntypedKind::Constant(Value::Int(n)) = &eb.kind {
if let SyntaxKind::Constant(Value::Int(n)) = &eb.kind {
assert_eq!(*n, 42);
} else {
panic!("Expected 42, got {:?}", eb.kind);
@@ -729,23 +729,23 @@ mod tests {
(def t (wrap [1 2 3])))
";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap();
let expanded = expander.expand(syntax).unwrap();
if let UntypedKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Def { value, .. } = &exprs[1].kind
&& let UntypedKind::Expansion {
if let SyntaxKind::Block { exprs } = &expanded.kind
&& let SyntaxKind::Def { value, .. } = &exprs[1].kind
&& let SyntaxKind::Expansion {
expanded: result, ..
} = &value.kind
&& let UntypedKind::Tuple { elements } = &result.kind
&& let SyntaxKind::Tuple { elements } = &result.kind
{
assert_eq!(elements.len(), 5);
if let UntypedKind::Constant(Value::Int(n)) = &elements[0].kind {
if let SyntaxKind::Constant(Value::Int(n)) = &elements[0].kind {
assert_eq!(*n, 0);
}
if let UntypedKind::Constant(Value::Int(n)) = &elements[4].kind {
if let SyntaxKind::Constant(Value::Int(n)) = &elements[4].kind {
assert_eq!(*n, 4);
}
}
@@ -759,10 +759,10 @@ mod tests {
(square 3))
";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap();
let expanded = expander.expand(syntax).unwrap();
// Convert test globals into a CompilerScope
let mut locals = HashMap::new();
@@ -800,10 +800,10 @@ mod tests {
y)
";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap();
let expanded = expander.expand(syntax).unwrap();
let mut diag = crate::ast::diagnostics::Diagnostics::new();
let result = Binder::bind_root(vec![], 0, 0, &expanded, &mut diag);
@@ -820,10 +820,10 @@ mod tests {
y)
";
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let syntax = parser.parse_expression();
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap();
let expanded = expander.expand(syntax).unwrap();
let mut diag = crate::ast::diagnostics::Diagnostics::new();
let result = Binder::bind_root(vec![], 0, 0, &expanded, &mut diag);
+21 -21
View File
@@ -1,21 +1,21 @@
pub mod analyzer;
pub mod binder;
pub mod bound_nodes;
pub mod captures;
pub mod dumper;
pub mod lambda_collector;
pub mod macros;
pub mod optimizer;
pub mod specializer;
pub mod lowering;
pub mod type_checker;
pub use binder::*;
pub use bound_nodes::*;
pub use captures::*;
pub use dumper::*;
pub use macros::*;
pub use optimizer::*;
pub use specializer::*;
pub use lowering::*;
pub use type_checker::*;
pub mod analyzer;
pub mod binder;
pub mod bound_nodes;
pub mod captures;
pub mod dumper;
pub mod lambda_collector;
pub mod macros;
pub mod optimizer;
pub mod specializer;
pub mod lowering;
pub mod type_checker;
pub use binder::*;
pub use bound_nodes::*;
pub use captures::*;
pub use dumper::*;
pub use macros::*;
pub use optimizer::*;
pub use specializer::*;
pub use lowering::*;
pub use type_checker::*;
File diff suppressed because it is too large Load Diff
+101 -101
View File
@@ -1,101 +1,101 @@
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, 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))
}
}
+210 -210
View File
@@ -1,210 +1,210 @@
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, LocalSlot, Node, UpvalueIdx};
use crate::ast::types::Value;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
#[derive(Default)]
pub struct SubstitutionMap {
pub values: HashMap<Address, Value>,
pub ast_substitutions: HashMap<Address, Rc<AnalyzedNode>>,
pub slot_mapping: HashMap<LocalSlot, LocalSlot>,
pub assigned: HashSet<Address>,
pub next_slot: u32,
pub used: HashSet<Address>,
pub captured_slots: HashSet<LocalSlot>,
}
impl SubstitutionMap {
pub fn new() -> Self {
Self::default()
}
/// Creates a new SubstitutionMap for an inner scope (like an inlined Lambda).
/// Safely inherits only Global substitutions, because Local and Upvalue
/// addresses are relative to the specific function frame and would overlap.
pub fn new_inner(&self) -> Self {
let mut inner = Self::new();
for (k, v) in &self.values {
if matches!(k, Address::Global(_)) {
inner.values.insert(*k, v.clone());
}
}
for (k, v) in &self.ast_substitutions {
if matches!(k, Address::Global(_)) {
inner.ast_substitutions.insert(*k, v.clone());
}
}
inner
}
pub fn new_for_inlining(&self) -> Self {
let mut inner = self.new_inner();
inner.next_slot = self.next_slot;
inner
}
pub fn add_ast_substitution(&mut self, addr: Address, node: AnalyzedNode) {
self.ast_substitutions.insert(addr, Rc::new(node));
}
pub fn map_slot(&mut self, old_slot: LocalSlot) -> LocalSlot {
if let Some(&new_slot) = self.slot_mapping.get(&old_slot) {
return new_slot;
}
let new_slot = LocalSlot(self.next_slot);
self.slot_mapping.insert(old_slot, new_slot);
self.next_slot += 1;
new_slot
}
pub fn map_address(&mut self, addr: Address) -> Address {
match addr {
Address::Local(slot) => Address::Local(self.map_slot(slot)),
other => other,
}
}
pub fn add_value(&mut self, addr: Address, val: Value) {
self.values.insert(addr, val);
}
pub fn get_value(&self, addr: &Address) -> Option<&Value> {
self.values.get(addr)
}
pub fn remove_value(&mut self, addr: &Address) {
self.values.remove(addr);
}
fn reindex_addr(&self, addr: Address, mapping: &[Option<u32>]) -> Address {
if let Address::Upvalue(idx) = addr
&& let Some(res) = mapping.get(idx.0 as usize)
&& let Some(new_idx) = res
{
Address::Upvalue(UpvalueIdx(*new_idx))
} else {
addr
}
}
pub fn reindex_upvalues(&self, node_rc: Rc<AnalyzedNode>, mapping: &[Option<u32>]) -> Rc<AnalyzedNode> {
let node = &*node_rc;
let (new_kind, metrics) = match &node.kind {
BoundKind::Get { addr, name } => (
BoundKind::Get {
addr: self.reindex_addr(*addr, mapping),
name: name.clone(),
},
node.ty.clone(),
),
BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
} => {
let mut next_upvalues = Vec::new();
for addr in upvalues {
next_upvalues.push(self.reindex_addr(*addr, mapping));
}
(
BoundKind::Lambda {
params: params.clone(),
upvalues: next_upvalues,
body: body.clone(),
positional_count: *positional_count,
},
node.ty.clone(),
)
}
BoundKind::If {
cond,
then_br,
else_br,
} => {
let cond = self.reindex_upvalues(cond.clone(), mapping);
let then_br = self.reindex_upvalues(then_br.clone(), mapping);
let else_br = else_br.as_ref().map(|e| self.reindex_upvalues(e.clone(), mapping));
(
BoundKind::If {
cond,
then_br,
else_br,
},
node.ty.clone(),
)
}
BoundKind::Block { exprs } => {
let exprs = exprs
.iter()
.map(|e| self.reindex_upvalues(e.clone(), mapping))
.collect();
(BoundKind::Block { exprs }, node.ty.clone())
}
BoundKind::Call { callee, args } => {
let callee = self.reindex_upvalues(callee.clone(), mapping);
let args = self.reindex_upvalues(args.clone(), mapping);
(BoundKind::Call { callee, args }, node.ty.clone())
}
BoundKind::Define {
name,
addr,
kind,
value,
captured_by,
} => {
let value = self.reindex_upvalues(value.clone(), mapping);
(
BoundKind::Define {
name: name.clone(),
addr: *addr,
kind: *kind,
value,
captured_by: captured_by.clone(),
},
node.ty.clone(),
)
}
BoundKind::Set { addr, value } => {
let value = self.reindex_upvalues(value.clone(), mapping);
(
BoundKind::Set {
addr: self.reindex_addr(*addr, mapping),
value,
},
node.ty.clone(),
)
}
BoundKind::Tuple { elements } => {
let elements = elements
.iter()
.map(|e| self.reindex_upvalues(e.clone(), mapping))
.collect();
(BoundKind::Tuple { elements }, node.ty.clone())
}
BoundKind::Record { layout, values } => {
let values = values
.iter()
.map(|v| self.reindex_upvalues(v.clone(), mapping))
.collect();
(BoundKind::Record { layout: layout.clone(), values }, node.ty.clone())
}
BoundKind::Expansion { original_call, bound_expanded } => {
let bound_expanded = self.reindex_upvalues(bound_expanded.clone(), mapping);
(
BoundKind::Expansion {
original_call: original_call.clone(),
bound_expanded,
},
node.ty.clone(),
)
}
k => (k.clone(), node.ty.clone()),
};
Rc::new(Node {
identity: node.identity.clone(),
kind: new_kind,
ty: metrics,
})
}
}
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, LocalSlot, Node, UpvalueIdx};
use crate::ast::types::Value;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
#[derive(Default)]
pub struct SubstitutionMap {
pub values: HashMap<Address, Value>,
pub ast_substitutions: HashMap<Address, Rc<AnalyzedNode>>,
pub slot_mapping: HashMap<LocalSlot, LocalSlot>,
pub assigned: HashSet<Address>,
pub next_slot: u32,
pub used: HashSet<Address>,
pub captured_slots: HashSet<LocalSlot>,
}
impl SubstitutionMap {
pub fn new() -> Self {
Self::default()
}
/// Creates a new SubstitutionMap for an inner scope (like an inlined Lambda).
/// Safely inherits only Global substitutions, because Local and Upvalue
/// addresses are relative to the specific function frame and would overlap.
pub fn new_inner(&self) -> Self {
let mut inner = Self::new();
for (k, v) in &self.values {
if matches!(k, Address::Global(_)) {
inner.values.insert(*k, v.clone());
}
}
for (k, v) in &self.ast_substitutions {
if matches!(k, Address::Global(_)) {
inner.ast_substitutions.insert(*k, v.clone());
}
}
inner
}
pub fn new_for_inlining(&self) -> Self {
let mut inner = self.new_inner();
inner.next_slot = self.next_slot;
inner
}
pub fn add_ast_substitution(&mut self, addr: Address, node: AnalyzedNode) {
self.ast_substitutions.insert(addr, Rc::new(node));
}
pub fn map_slot(&mut self, old_slot: LocalSlot) -> LocalSlot {
if let Some(&new_slot) = self.slot_mapping.get(&old_slot) {
return new_slot;
}
let new_slot = LocalSlot(self.next_slot);
self.slot_mapping.insert(old_slot, new_slot);
self.next_slot += 1;
new_slot
}
pub fn map_address(&mut self, addr: Address) -> Address {
match addr {
Address::Local(slot) => Address::Local(self.map_slot(slot)),
other => other,
}
}
pub fn add_value(&mut self, addr: Address, val: Value) {
self.values.insert(addr, val);
}
pub fn get_value(&self, addr: &Address) -> Option<&Value> {
self.values.get(addr)
}
pub fn remove_value(&mut self, addr: &Address) {
self.values.remove(addr);
}
fn reindex_addr(&self, addr: Address, mapping: &[Option<u32>]) -> Address {
if let Address::Upvalue(idx) = addr
&& let Some(res) = mapping.get(idx.0 as usize)
&& let Some(new_idx) = res
{
Address::Upvalue(UpvalueIdx(*new_idx))
} else {
addr
}
}
pub fn reindex_upvalues(&self, node_rc: Rc<AnalyzedNode>, mapping: &[Option<u32>]) -> Rc<AnalyzedNode> {
let node = &*node_rc;
let (new_kind, metrics) = match &node.kind {
BoundKind::Get { addr, name } => (
BoundKind::Get {
addr: self.reindex_addr(*addr, mapping),
name: name.clone(),
},
node.ty.clone(),
),
BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
} => {
let mut next_upvalues = Vec::new();
for addr in upvalues {
next_upvalues.push(self.reindex_addr(*addr, mapping));
}
(
BoundKind::Lambda {
params: params.clone(),
upvalues: next_upvalues,
body: body.clone(),
positional_count: *positional_count,
},
node.ty.clone(),
)
}
BoundKind::If {
cond,
then_br,
else_br,
} => {
let cond = self.reindex_upvalues(cond.clone(), mapping);
let then_br = self.reindex_upvalues(then_br.clone(), mapping);
let else_br = else_br.as_ref().map(|e| self.reindex_upvalues(e.clone(), mapping));
(
BoundKind::If {
cond,
then_br,
else_br,
},
node.ty.clone(),
)
}
BoundKind::Block { exprs } => {
let exprs = exprs
.iter()
.map(|e| self.reindex_upvalues(e.clone(), mapping))
.collect();
(BoundKind::Block { exprs }, node.ty.clone())
}
BoundKind::Call { callee, args } => {
let callee = self.reindex_upvalues(callee.clone(), mapping);
let args = self.reindex_upvalues(args.clone(), mapping);
(BoundKind::Call { callee, args }, node.ty.clone())
}
BoundKind::Define {
name,
addr,
kind,
value,
captured_by,
} => {
let value = self.reindex_upvalues(value.clone(), mapping);
(
BoundKind::Define {
name: name.clone(),
addr: *addr,
kind: *kind,
value,
captured_by: captured_by.clone(),
},
node.ty.clone(),
)
}
BoundKind::Set { addr, value } => {
let value = self.reindex_upvalues(value.clone(), mapping);
(
BoundKind::Set {
addr: self.reindex_addr(*addr, mapping),
value,
},
node.ty.clone(),
)
}
BoundKind::Tuple { elements } => {
let elements = elements
.iter()
.map(|e| self.reindex_upvalues(e.clone(), mapping))
.collect();
(BoundKind::Tuple { elements }, node.ty.clone())
}
BoundKind::Record { layout, values } => {
let values = values
.iter()
.map(|v| self.reindex_upvalues(v.clone(), mapping))
.collect();
(BoundKind::Record { layout: layout.clone(), values }, node.ty.clone())
}
BoundKind::Expansion { original_call, bound_expanded } => {
let bound_expanded = self.reindex_upvalues(bound_expanded.clone(), mapping);
(
BoundKind::Expansion {
original_call: original_call.clone(),
bound_expanded,
},
node.ty.clone(),
)
}
k => (k.clone(), node.ty.clone()),
};
Rc::new(Node {
identity: node.identity.clone(),
kind: new_kind,
ty: metrics,
})
}
}
+183 -183
View File
@@ -1,183 +1,183 @@
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, GlobalIdx};
use crate::ast::types::{Identity, Value};
use crate::ast::vm::Closure;
use std::collections::HashSet;
// --- PathTracker ---
#[derive(Default)]
pub struct PathTracker {
pub inlining_depth: usize,
pub inlining_stack: HashSet<GlobalIdx>,
pub identity_stack: HashSet<Identity>,
}
impl PathTracker {
pub fn new() -> Self {
Self::default()
}
pub fn enter_lambda(&mut self, identity: &Identity) -> bool {
if self.identity_stack.contains(identity) {
return false;
}
self.identity_stack.insert(identity.clone());
true
}
pub fn exit_lambda(&mut self, identity: &Identity) {
self.identity_stack.remove(identity);
}
}
// --- UsageInfo ---
#[derive(Default)]
pub struct UsageInfo {
pub used: HashSet<Address>,
pub assigned: HashSet<Address>,
pub used_identities: HashSet<Identity>,
}
impl UsageInfo {
pub fn is_assigned(&self, addr: &Address) -> bool {
self.assigned.contains(addr)
}
pub fn is_used(&self, addr: &Address) -> bool {
self.used.contains(addr)
}
pub fn collect(&mut self, node: &AnalyzedNode) {
match &node.kind {
BoundKind::Destructure { pattern, value } => {
self.collect_pattern(pattern);
self.collect(value);
}
BoundKind::Constant(v) => {
if let Value::Object(obj) = v
&& let Some(closure) = obj.as_any().downcast_ref::<Closure>()
{
self.used_identities
.insert(closure.function_node.identity.clone());
self.collect(&closure.function_node);
}
}
BoundKind::Get { addr, .. } => {
self.used.insert(*addr);
}
BoundKind::Set { addr, value } => {
self.assigned.insert(*addr);
self.collect(value);
}
BoundKind::GetField { rec, .. } => {
self.collect(rec);
}
BoundKind::Lambda {
params,
body,
upvalues,
..
} => {
self.used_identities.insert(node.identity.clone());
let mut inner_info = UsageInfo::default();
inner_info.collect(params);
inner_info.collect(body);
// Propagate globals and identities
for addr in &inner_info.used {
if let Address::Global(_) = addr {
self.used.insert(*addr);
}
}
for addr in &inner_info.assigned {
if let Address::Global(_) = addr {
self.assigned.insert(*addr);
}
}
self.used_identities.extend(inner_info.used_identities);
// Map used upvalues to parent scope
for addr in &inner_info.used {
if let Address::Upvalue(idx) = addr
&& let Some(parent_addr) = upvalues.get(idx.0 as usize)
{
self.used.insert(*parent_addr);
}
}
// Map assigned upvalues to parent scope
for addr in &inner_info.assigned {
if let Address::Upvalue(idx) = addr
&& let Some(parent_addr) = upvalues.get(idx.0 as usize)
{
self.assigned.insert(*parent_addr);
}
}
}
BoundKind::Block { exprs } => {
for e in exprs {
self.collect(e);
}
}
BoundKind::If {
cond,
then_br,
else_br,
} => {
self.collect(cond);
self.collect(then_br);
if let Some(e) = else_br {
self.collect(e);
}
}
BoundKind::Call { callee, args } => {
self.collect(callee);
self.collect(args);
}
BoundKind::Tuple { elements } => {
for e in elements {
self.collect(e);
}
}
BoundKind::Record { values, .. } => {
for v in values {
self.collect(v);
}
}
BoundKind::Define { addr, value, .. } => {
self.assigned.insert(*addr);
self.collect(value);
}
BoundKind::Expansion { bound_expanded, .. } => {
self.collect(bound_expanded);
}
BoundKind::Again { args } => {
self.collect(args);
}
BoundKind::Pipe { inputs, lambda, .. } => {
for input in inputs {
self.collect(input);
}
self.collect(lambda);
}
BoundKind::Nop
| BoundKind::FieldAccessor(_)
| BoundKind::Extension(_)
| BoundKind::Error => {}
}
}
pub fn collect_pattern(&mut self, node: &AnalyzedNode) {
match &node.kind {
BoundKind::Define { .. } => {}
BoundKind::Set { addr, .. } => {
self.assigned.insert(*addr);
}
BoundKind::Tuple { elements } => {
for el in elements {
self.collect_pattern(el);
}
}
_ => {}
}
}
}
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, GlobalIdx};
use crate::ast::types::{Identity, Value};
use crate::ast::vm::Closure;
use std::collections::HashSet;
// --- PathTracker ---
#[derive(Default)]
pub struct PathTracker {
pub inlining_depth: usize,
pub inlining_stack: HashSet<GlobalIdx>,
pub identity_stack: HashSet<Identity>,
}
impl PathTracker {
pub fn new() -> Self {
Self::default()
}
pub fn enter_lambda(&mut self, identity: &Identity) -> bool {
if self.identity_stack.contains(identity) {
return false;
}
self.identity_stack.insert(identity.clone());
true
}
pub fn exit_lambda(&mut self, identity: &Identity) {
self.identity_stack.remove(identity);
}
}
// --- UsageInfo ---
#[derive(Default)]
pub struct UsageInfo {
pub used: HashSet<Address>,
pub assigned: HashSet<Address>,
pub used_identities: HashSet<Identity>,
}
impl UsageInfo {
pub fn is_assigned(&self, addr: &Address) -> bool {
self.assigned.contains(addr)
}
pub fn is_used(&self, addr: &Address) -> bool {
self.used.contains(addr)
}
pub fn collect(&mut self, node: &AnalyzedNode) {
match &node.kind {
BoundKind::Destructure { pattern, value } => {
self.collect_pattern(pattern);
self.collect(value);
}
BoundKind::Constant(v) => {
if let Value::Object(obj) = v
&& let Some(closure) = obj.as_any().downcast_ref::<Closure>()
{
self.used_identities
.insert(closure.function_node.identity.clone());
self.collect(&closure.function_node);
}
}
BoundKind::Get { addr, .. } => {
self.used.insert(*addr);
}
BoundKind::Set { addr, value } => {
self.assigned.insert(*addr);
self.collect(value);
}
BoundKind::GetField { rec, .. } => {
self.collect(rec);
}
BoundKind::Lambda {
params,
body,
upvalues,
..
} => {
self.used_identities.insert(node.identity.clone());
let mut inner_info = UsageInfo::default();
inner_info.collect(params);
inner_info.collect(body);
// Propagate globals and identities
for addr in &inner_info.used {
if let Address::Global(_) = addr {
self.used.insert(*addr);
}
}
for addr in &inner_info.assigned {
if let Address::Global(_) = addr {
self.assigned.insert(*addr);
}
}
self.used_identities.extend(inner_info.used_identities);
// Map used upvalues to parent scope
for addr in &inner_info.used {
if let Address::Upvalue(idx) = addr
&& let Some(parent_addr) = upvalues.get(idx.0 as usize)
{
self.used.insert(*parent_addr);
}
}
// Map assigned upvalues to parent scope
for addr in &inner_info.assigned {
if let Address::Upvalue(idx) = addr
&& let Some(parent_addr) = upvalues.get(idx.0 as usize)
{
self.assigned.insert(*parent_addr);
}
}
}
BoundKind::Block { exprs } => {
for e in exprs {
self.collect(e);
}
}
BoundKind::If {
cond,
then_br,
else_br,
} => {
self.collect(cond);
self.collect(then_br);
if let Some(e) = else_br {
self.collect(e);
}
}
BoundKind::Call { callee, args } => {
self.collect(callee);
self.collect(args);
}
BoundKind::Tuple { elements } => {
for e in elements {
self.collect(e);
}
}
BoundKind::Record { values, .. } => {
for v in values {
self.collect(v);
}
}
BoundKind::Define { addr, value, .. } => {
self.assigned.insert(*addr);
self.collect(value);
}
BoundKind::Expansion { bound_expanded, .. } => {
self.collect(bound_expanded);
}
BoundKind::Again { args } => {
self.collect(args);
}
BoundKind::Pipe { inputs, lambda, .. } => {
for input in inputs {
self.collect(input);
}
self.collect(lambda);
}
BoundKind::Nop
| BoundKind::FieldAccessor(_)
| BoundKind::Extension(_)
| BoundKind::Error => {}
}
}
pub fn collect_pattern(&mut self, node: &AnalyzedNode) {
match &node.kind {
BoundKind::Define { .. } => {}
BoundKind::Set { addr, .. } => {
self.assigned.insert(*addr);
}
BoundKind::Tuple { elements } => {
for el in elements {
self.collect_pattern(el);
}
}
_ => {}
}
}
}
+276 -276
View File
@@ -1,276 +1,276 @@
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, Node, NodeMetrics};
use crate::ast::types::{Purity, Signature, StaticType, Value};
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MonoCacheKey {
pub address: Address,
pub arg_types: Vec<StaticType>,
}
pub type CompileFunc = Rc<dyn Fn(Rc<Node>, &[StaticType]) -> Result<(Value, StaticType), String>>;
pub type RtlLookupFunc = Rc<dyn Fn(&str, &[StaticType]) -> Option<(Value, StaticType)>>;
pub trait FunctionRegistry {
fn resolve(&self, addr: Address) -> Option<Rc<Node>>;
fn resolve_analyzed(&self, _addr: Address) -> Option<Rc<AnalyzedNode>> {
None
}
}
pub type MonoCache = HashMap<MonoCacheKey, (Value, StaticType)>;
pub struct Specializer {
pub cache: Rc<RefCell<MonoCache>>,
registry: Option<Rc<dyn FunctionRegistry>>,
compiler: Option<CompileFunc>,
rtl_lookup: Option<RtlLookupFunc>,
}
impl Specializer {
pub fn new(
registry: Option<Rc<dyn FunctionRegistry>>,
compiler: Option<CompileFunc>,
rtl_lookup: Option<RtlLookupFunc>,
cache: Option<Rc<RefCell<MonoCache>>>,
) -> Self {
Self {
cache: cache.unwrap_or_else(|| Rc::new(RefCell::new(HashMap::new()))),
registry,
compiler,
rtl_lookup,
}
}
pub fn specialize(&self, node: AnalyzedNode) -> AnalyzedNode {
self.visit_node(node)
}
fn visit_node(&self, node: AnalyzedNode) -> AnalyzedNode {
let (new_kind, metrics) = match node.kind {
BoundKind::Call { callee, args } => {
let (new_callee, new_args, _ret_ty) =
self.specialize_call_logic(callee, args, node.ty.original.ty.clone());
let new_metrics = node.ty.clone();
(
BoundKind::Call {
callee: Rc::new(new_callee),
args: Rc::new(new_args),
},
new_metrics,
)
}
BoundKind::If {
cond,
then_br,
else_br,
} => {
let cond = Rc::new(self.visit_node(cond.as_ref().clone()));
let then_br = Rc::new(self.visit_node(then_br.as_ref().clone()));
let else_br = else_br.map(|e| Rc::new(self.visit_node(e.as_ref().clone())));
(
BoundKind::If {
cond,
then_br,
else_br,
},
node.ty.clone(),
)
}
BoundKind::Block { exprs } => {
let exprs = exprs.into_iter().map(|e| Rc::new(self.visit_node(e.as_ref().clone()))).collect();
(BoundKind::Block { exprs }, node.ty.clone())
}
BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
} => {
let params = Rc::new(self.visit_node(params.as_ref().clone()));
let body = Rc::new(self.visit_node(body.as_ref().clone()));
(
BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
},
node.ty.clone(),
)
}
BoundKind::Define {
name,
addr,
kind,
value,
captured_by,
} => {
let value = Rc::new(self.visit_node(value.as_ref().clone()));
(
BoundKind::Define {
name: name.clone(),
addr,
kind,
value,
captured_by: captured_by.clone(),
},
node.ty.clone(),
)
}
BoundKind::Set { addr, value } => {
let value = Rc::new(self.visit_node(value.as_ref().clone()));
(BoundKind::Set { addr, value }, node.ty.clone())
}
BoundKind::Tuple { elements } => {
let elements = elements.into_iter().map(|e| Rc::new(self.visit_node(e.as_ref().clone()))).collect();
(BoundKind::Tuple { elements }, node.ty.clone())
}
BoundKind::Record { layout, values } => {
let values = values.into_iter().map(|v| Rc::new(self.visit_node(v.as_ref().clone()))).collect();
(BoundKind::Record { layout, values }, node.ty.clone())
}
BoundKind::Expansion {
original_call,
bound_expanded,
} => {
let bound_expanded = Rc::new(self.visit_node(bound_expanded.as_ref().clone()));
(
BoundKind::Expansion {
original_call,
bound_expanded,
},
node.ty.clone(),
)
}
k => (k, node.ty.clone()),
};
Node {
identity: node.identity,
kind: new_kind,
ty: metrics,
}
}
fn specialize_call_logic(
&self,
callee: Rc<AnalyzedNode>,
args: Rc<AnalyzedNode>,
original_ty: StaticType,
) -> (AnalyzedNode, AnalyzedNode, StaticType) {
let new_callee = self.visit_node(callee.as_ref().clone());
let new_args = self.visit_node(args.as_ref().clone());
let address = if let BoundKind::Get { addr, .. } = &new_callee.kind {
*addr
} else {
return (new_callee, new_args, original_ty);
};
let arg_types: Vec<StaticType> =
if let StaticType::Tuple(elements) = &new_args.ty.original.ty {
elements.clone()
} else {
vec![new_args.ty.original.ty.clone()]
};
if arg_types.iter().any(|t| matches!(t, StaticType::Any)) {
return (new_callee, new_args, original_ty);
}
let key = MonoCacheKey {
address,
arg_types: arg_types.clone(),
};
if let Some((val, ret_ty)) = self.cache.borrow().get(&key) {
let specialized_callee = self.make_constant_node(
val.clone(),
StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(arg_types),
ret: ret_ty.clone(),
})),
&new_callee,
);
return (specialized_callee, new_args, ret_ty.clone());
}
if let Some(rtl_lookup) = &self.rtl_lookup
&& let BoundKind::Get { name, .. } = &new_callee.kind
&& let Some((val, ret_ty)) = rtl_lookup(&name.name, &arg_types)
{
self.cache
.borrow_mut()
.insert(key.clone(), (val.clone(), ret_ty.clone()));
let specialized_callee = self.make_constant_node(
val.clone(),
StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(arg_types),
ret: ret_ty.clone(),
})),
&new_callee,
);
return (specialized_callee, new_args, ret_ty);
}
if let Some(registry) = &self.registry
&& let Some(func_node) = registry.resolve_analyzed(address)
&& func_node.ty.is_recursive
{
return (new_callee, new_args, original_ty);
}
if let Some(compiler) = &self.compiler
&& let Some(func_node) = self.registry.as_ref().and_then(|r| r.resolve(address))
&& let Ok((compiled_val, ret_ty)) = compiler(func_node, &arg_types)
{
self.cache
.borrow_mut()
.insert(key, (compiled_val.clone(), ret_ty.clone()));
// Only replace the callee if the compiled value is actually a function/object.
// If it's a scalar (like 30 from folding), we DON'T fold here.
// We keep the Call but update the callee to the specialized version if it's an object.
if let Value::Object(_) | Value::Function(_) = &compiled_val {
let specialized_callee = self.make_constant_node(
compiled_val,
StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(arg_types),
ret: ret_ty.clone(),
})),
&new_callee,
);
return (specialized_callee, new_args, ret_ty);
}
}
(new_callee, new_args, original_ty)
}
fn make_constant_node(
&self,
val: Value,
ty: StaticType,
template: &AnalyzedNode,
) -> AnalyzedNode {
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,
},
}
}
}
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, Node, NodeMetrics};
use crate::ast::types::{Purity, Signature, StaticType, Value};
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MonoCacheKey {
pub address: Address,
pub arg_types: Vec<StaticType>,
}
pub type CompileFunc = Rc<dyn Fn(Rc<Node>, &[StaticType]) -> Result<(Value, StaticType), String>>;
pub type RtlLookupFunc = Rc<dyn Fn(&str, &[StaticType]) -> Option<(Value, StaticType)>>;
pub trait FunctionRegistry {
fn resolve(&self, addr: Address) -> Option<Rc<Node>>;
fn resolve_analyzed(&self, _addr: Address) -> Option<Rc<AnalyzedNode>> {
None
}
}
pub type MonoCache = HashMap<MonoCacheKey, (Value, StaticType)>;
pub struct Specializer {
pub cache: Rc<RefCell<MonoCache>>,
registry: Option<Rc<dyn FunctionRegistry>>,
compiler: Option<CompileFunc>,
rtl_lookup: Option<RtlLookupFunc>,
}
impl Specializer {
pub fn new(
registry: Option<Rc<dyn FunctionRegistry>>,
compiler: Option<CompileFunc>,
rtl_lookup: Option<RtlLookupFunc>,
cache: Option<Rc<RefCell<MonoCache>>>,
) -> Self {
Self {
cache: cache.unwrap_or_else(|| Rc::new(RefCell::new(HashMap::new()))),
registry,
compiler,
rtl_lookup,
}
}
pub fn specialize(&self, node: AnalyzedNode) -> AnalyzedNode {
self.visit_node(node)
}
fn visit_node(&self, node: AnalyzedNode) -> AnalyzedNode {
let (new_kind, metrics) = match node.kind {
BoundKind::Call { callee, args } => {
let (new_callee, new_args, _ret_ty) =
self.specialize_call_logic(callee, args, node.ty.original.ty.clone());
let new_metrics = node.ty.clone();
(
BoundKind::Call {
callee: Rc::new(new_callee),
args: Rc::new(new_args),
},
new_metrics,
)
}
BoundKind::If {
cond,
then_br,
else_br,
} => {
let cond = Rc::new(self.visit_node(cond.as_ref().clone()));
let then_br = Rc::new(self.visit_node(then_br.as_ref().clone()));
let else_br = else_br.map(|e| Rc::new(self.visit_node(e.as_ref().clone())));
(
BoundKind::If {
cond,
then_br,
else_br,
},
node.ty.clone(),
)
}
BoundKind::Block { exprs } => {
let exprs = exprs.into_iter().map(|e| Rc::new(self.visit_node(e.as_ref().clone()))).collect();
(BoundKind::Block { exprs }, node.ty.clone())
}
BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
} => {
let params = Rc::new(self.visit_node(params.as_ref().clone()));
let body = Rc::new(self.visit_node(body.as_ref().clone()));
(
BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
},
node.ty.clone(),
)
}
BoundKind::Define {
name,
addr,
kind,
value,
captured_by,
} => {
let value = Rc::new(self.visit_node(value.as_ref().clone()));
(
BoundKind::Define {
name: name.clone(),
addr,
kind,
value,
captured_by: captured_by.clone(),
},
node.ty.clone(),
)
}
BoundKind::Set { addr, value } => {
let value = Rc::new(self.visit_node(value.as_ref().clone()));
(BoundKind::Set { addr, value }, node.ty.clone())
}
BoundKind::Tuple { elements } => {
let elements = elements.into_iter().map(|e| Rc::new(self.visit_node(e.as_ref().clone()))).collect();
(BoundKind::Tuple { elements }, node.ty.clone())
}
BoundKind::Record { layout, values } => {
let values = values.into_iter().map(|v| Rc::new(self.visit_node(v.as_ref().clone()))).collect();
(BoundKind::Record { layout, values }, node.ty.clone())
}
BoundKind::Expansion {
original_call,
bound_expanded,
} => {
let bound_expanded = Rc::new(self.visit_node(bound_expanded.as_ref().clone()));
(
BoundKind::Expansion {
original_call,
bound_expanded,
},
node.ty.clone(),
)
}
k => (k, node.ty.clone()),
};
Node {
identity: node.identity,
kind: new_kind,
ty: metrics,
}
}
fn specialize_call_logic(
&self,
callee: Rc<AnalyzedNode>,
args: Rc<AnalyzedNode>,
original_ty: StaticType,
) -> (AnalyzedNode, AnalyzedNode, StaticType) {
let new_callee = self.visit_node(callee.as_ref().clone());
let new_args = self.visit_node(args.as_ref().clone());
let address = if let BoundKind::Get { addr, .. } = &new_callee.kind {
*addr
} else {
return (new_callee, new_args, original_ty);
};
let arg_types: Vec<StaticType> =
if let StaticType::Tuple(elements) = &new_args.ty.original.ty {
elements.clone()
} else {
vec![new_args.ty.original.ty.clone()]
};
if arg_types.iter().any(|t| matches!(t, StaticType::Any)) {
return (new_callee, new_args, original_ty);
}
let key = MonoCacheKey {
address,
arg_types: arg_types.clone(),
};
if let Some((val, ret_ty)) = self.cache.borrow().get(&key) {
let specialized_callee = self.make_constant_node(
val.clone(),
StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(arg_types),
ret: ret_ty.clone(),
})),
&new_callee,
);
return (specialized_callee, new_args, ret_ty.clone());
}
if let Some(rtl_lookup) = &self.rtl_lookup
&& let BoundKind::Get { name, .. } = &new_callee.kind
&& let Some((val, ret_ty)) = rtl_lookup(&name.name, &arg_types)
{
self.cache
.borrow_mut()
.insert(key.clone(), (val.clone(), ret_ty.clone()));
let specialized_callee = self.make_constant_node(
val.clone(),
StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(arg_types),
ret: ret_ty.clone(),
})),
&new_callee,
);
return (specialized_callee, new_args, ret_ty);
}
if let Some(registry) = &self.registry
&& let Some(func_node) = registry.resolve_analyzed(address)
&& func_node.ty.is_recursive
{
return (new_callee, new_args, original_ty);
}
if let Some(compiler) = &self.compiler
&& let Some(func_node) = self.registry.as_ref().and_then(|r| r.resolve(address))
&& let Ok((compiled_val, ret_ty)) = compiler(func_node, &arg_types)
{
self.cache
.borrow_mut()
.insert(key, (compiled_val.clone(), ret_ty.clone()));
// Only replace the callee if the compiled value is actually a function/object.
// If it's a scalar (like 30 from folding), we DON'T fold here.
// We keep the Call but update the callee to the specialized version if it's an object.
if let Value::Object(_) | Value::Function(_) = &compiled_val {
let specialized_callee = self.make_constant_node(
compiled_val,
StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(arg_types),
ret: ret_ty.clone(),
})),
&new_callee,
);
return (specialized_callee, new_args, ret_ty);
}
}
(new_callee, new_args, original_ty)
}
fn make_constant_node(
&self,
val: Value,
ty: StaticType,
template: &AnalyzedNode,
) -> AnalyzedNode {
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,
},
}
}
}
File diff suppressed because it is too large Load Diff