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:
+42
-42
@@ -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
@@ -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 ¶ms.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 ¶ms.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
@@ -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
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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, .. } = ¤t.kind {
|
||||
current = bound_expanded;
|
||||
}
|
||||
|
||||
if let BoundKind::Lambda { .. } = ¤t.kind {
|
||||
self.registry
|
||||
.insert(*global_index, (*current).clone());
|
||||
}
|
||||
}
|
||||
self.visit(value);
|
||||
}
|
||||
|
||||
BoundKind::Set { addr, value } => {
|
||||
// Also track assignments to globals if they hold lambdas.
|
||||
if let Address::Global(global_index) = addr {
|
||||
let mut current = value;
|
||||
while let BoundKind::Expansion { bound_expanded, .. } = ¤t.kind {
|
||||
current = bound_expanded;
|
||||
}
|
||||
|
||||
if let BoundKind::Lambda { .. } = ¤t.kind {
|
||||
self.registry
|
||||
.insert(*global_index, (*current).clone());
|
||||
}
|
||||
}
|
||||
self.visit(value);
|
||||
}
|
||||
|
||||
BoundKind::If {
|
||||
cond,
|
||||
then_br,
|
||||
else_br,
|
||||
} => {
|
||||
self.visit(cond);
|
||||
self.visit(then_br);
|
||||
if let Some(e) = else_br {
|
||||
self.visit(e);
|
||||
}
|
||||
}
|
||||
|
||||
BoundKind::Lambda { params, body, .. } => {
|
||||
self.visit(params);
|
||||
self.visit(body);
|
||||
}
|
||||
|
||||
BoundKind::Call { callee, args } => {
|
||||
self.visit(callee);
|
||||
self.visit(args);
|
||||
}
|
||||
|
||||
BoundKind::Tuple { elements } => {
|
||||
for el in elements {
|
||||
self.visit(el);
|
||||
}
|
||||
}
|
||||
|
||||
BoundKind::Record { values, .. } => {
|
||||
for v in values {
|
||||
self.visit(v);
|
||||
}
|
||||
}
|
||||
|
||||
BoundKind::Expansion { bound_expanded, .. } => {
|
||||
self.visit(bound_expanded);
|
||||
}
|
||||
|
||||
_ => {} // Leaf nodes
|
||||
}
|
||||
}
|
||||
}
|
||||
use crate::ast::compiler::bound_nodes::{Address, 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, .. } = ¤t.kind {
|
||||
current = bound_expanded;
|
||||
}
|
||||
|
||||
if let BoundKind::Lambda { .. } = ¤t.kind {
|
||||
self.registry
|
||||
.insert(*global_index, (*current).clone());
|
||||
}
|
||||
}
|
||||
self.visit(value);
|
||||
}
|
||||
|
||||
BoundKind::Set { addr, value } => {
|
||||
// Also track assignments to globals if they hold lambdas.
|
||||
if let Address::Global(global_index) = addr {
|
||||
let mut current = value;
|
||||
while let BoundKind::Expansion { bound_expanded, .. } = ¤t.kind {
|
||||
current = bound_expanded;
|
||||
}
|
||||
|
||||
if let BoundKind::Lambda { .. } = ¤t.kind {
|
||||
self.registry
|
||||
.insert(*global_index, (*current).clone());
|
||||
}
|
||||
}
|
||||
self.visit(value);
|
||||
}
|
||||
|
||||
BoundKind::If {
|
||||
cond,
|
||||
then_br,
|
||||
else_br,
|
||||
} => {
|
||||
self.visit(cond);
|
||||
self.visit(then_br);
|
||||
if let Some(e) = else_br {
|
||||
self.visit(e);
|
||||
}
|
||||
}
|
||||
|
||||
BoundKind::Lambda { params, body, .. } => {
|
||||
self.visit(params);
|
||||
self.visit(body);
|
||||
}
|
||||
|
||||
BoundKind::Call { callee, args } => {
|
||||
self.visit(callee);
|
||||
self.visit(args);
|
||||
}
|
||||
|
||||
BoundKind::Tuple { elements } => {
|
||||
for el in elements {
|
||||
self.visit(el);
|
||||
}
|
||||
}
|
||||
|
||||
BoundKind::Record { values, .. } => {
|
||||
for v in values {
|
||||
self.visit(v);
|
||||
}
|
||||
}
|
||||
|
||||
BoundKind::Expansion { bound_expanded, .. } => {
|
||||
self.visit(bound_expanded);
|
||||
}
|
||||
|
||||
_ => {} // Leaf nodes
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+144
-144
@@ -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(¶ms)?;
|
||||
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
@@ -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
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
@@ -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
@@ -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,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+931
-931
File diff suppressed because it is too large
Load Diff
+30
-30
@@ -1,7 +1,7 @@
|
||||
use crate::ast::compiler::analyzer::Analyzer;
|
||||
use crate::ast::compiler::binder::Binder;
|
||||
use crate::ast::compiler::{TypeChecker, TypedNode};
|
||||
use crate::ast::nodes::{Symbol, UntypedKind, UntypedNode};
|
||||
use crate::ast::nodes::{Symbol, SyntaxKind, SyntaxNode};
|
||||
use crate::ast::parser::Parser;
|
||||
use crate::ast::vm::{TracingObserver, VM};
|
||||
use std::cell::RefCell;
|
||||
@@ -122,10 +122,10 @@ struct RuntimeMacroEvaluator {
|
||||
impl MacroEvaluator for RuntimeMacroEvaluator {
|
||||
fn evaluate(
|
||||
&self,
|
||||
node: &UntypedNode,
|
||||
bindings: &HashMap<Rc<str>, UntypedNode>,
|
||||
node: &SyntaxNode,
|
||||
bindings: &HashMap<Rc<str>, SyntaxNode>,
|
||||
) -> Result<Value, String> {
|
||||
if let UntypedKind::Identifier(sym) = &node.kind
|
||||
if let SyntaxKind::Identifier(sym) = &node.kind
|
||||
&& let Some(arg_node) = bindings.get(&sym.name)
|
||||
{
|
||||
return Ok(Value::Object(Rc::new(arg_node.clone()) as Rc<dyn Object>));
|
||||
@@ -290,7 +290,7 @@ impl Environment {
|
||||
&self,
|
||||
source: &str,
|
||||
base_path: &Path,
|
||||
all_files: &mut Vec<(PathBuf, UntypedNode)>,
|
||||
all_files: &mut Vec<(PathBuf, SyntaxNode)>,
|
||||
) -> Result<(), String> {
|
||||
let directives = self.extract_use_directives(source);
|
||||
|
||||
@@ -312,7 +312,7 @@ impl Environment {
|
||||
self.collect_dependencies(&lib_source, lib_base, all_files)?;
|
||||
|
||||
let mut parser = Parser::new(&lib_source);
|
||||
let untyped_ast = parser.parse_expression();
|
||||
let syntax_ast = parser.parse_expression();
|
||||
|
||||
if parser.diagnostics.has_errors() {
|
||||
return Err(format!(
|
||||
@@ -322,7 +322,7 @@ impl Environment {
|
||||
));
|
||||
}
|
||||
|
||||
all_files.push((abs_path, untyped_ast));
|
||||
all_files.push((abs_path, syntax_ast));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -358,34 +358,34 @@ impl Environment {
|
||||
/// It returns the base path which can be used to resolve further things if necessary.
|
||||
pub fn preload_dependencies(&self, source: &str, file_path: Option<&Path>) -> Result<(), String> {
|
||||
let base_path = file_path.and_then(|p| p.parent()).unwrap_or_else(|| Path::new("."));
|
||||
let mut files: Vec<(PathBuf, UntypedNode)> = Vec::new();
|
||||
let mut files: Vec<(PathBuf, SyntaxNode)> = Vec::new();
|
||||
|
||||
// 1. Always load the embedded system library first as a virtual module
|
||||
let system_path = PathBuf::from(SYSTEM_LIB_PATH);
|
||||
if !self.loaded_modules.borrow().contains(&system_path) {
|
||||
self.loaded_modules.borrow_mut().insert(system_path.clone());
|
||||
let mut parser = Parser::new(SYSTEM_LIB_SOURCE);
|
||||
let untyped_ast = parser.parse_expression();
|
||||
let syntax_ast = parser.parse_expression();
|
||||
if parser.diagnostics.has_errors() {
|
||||
return Err(format!(
|
||||
"Failed to parse embedded system library:\n{}",
|
||||
parser.diagnostics.items[0].message
|
||||
));
|
||||
}
|
||||
files.push((system_path, untyped_ast));
|
||||
files.push((system_path, syntax_ast));
|
||||
}
|
||||
|
||||
// 2. Collect dependencies from the main source
|
||||
self.collect_dependencies(source, base_path, &mut files)?;
|
||||
|
||||
// Pass 1: Discovery (Globals and Macros)
|
||||
for (_, untyped_ast) in &files {
|
||||
self.discover_globals(untyped_ast);
|
||||
for (_, syntax_ast) in &files {
|
||||
self.discover_globals(syntax_ast);
|
||||
}
|
||||
|
||||
// Pass 2: Compilation and Initialization
|
||||
for (path, untyped_ast) in files {
|
||||
let typed_ast = self.compile_untyped(untyped_ast).map_err(|e: String| {
|
||||
for (path, syntax_ast) in files {
|
||||
let typed_ast = self.compile_syntax(syntax_ast).map_err(|e: String| {
|
||||
format!("Compilation error in {}:\n{}", path.display(), e)
|
||||
})?;
|
||||
self.run_script_compiled(typed_ast).map_err(|e: String| {
|
||||
@@ -396,10 +396,10 @@ impl Environment {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn discover_globals(&self, node: &UntypedNode) {
|
||||
fn discover_globals(&self, node: &SyntaxNode) {
|
||||
match &node.kind {
|
||||
UntypedKind::Def { target, .. } => {
|
||||
if let UntypedKind::Identifier(sym) = &target.kind {
|
||||
SyntaxKind::Def { target, .. } => {
|
||||
if let SyntaxKind::Identifier(sym) = &target.kind {
|
||||
let mut root_scopes = self.root_scopes.borrow_mut();
|
||||
let last_idx = root_scopes.len() - 1;
|
||||
let current_scope = &mut root_scopes[last_idx];
|
||||
@@ -420,13 +420,13 @@ impl Environment {
|
||||
}
|
||||
}
|
||||
}
|
||||
UntypedKind::MacroDecl { name, params, body } => {
|
||||
SyntaxKind::MacroDecl { name, params, body } => {
|
||||
let mut registry = self.macro_registry.borrow_mut();
|
||||
|
||||
fn extract_names(node: &UntypedNode) -> Vec<Rc<str>> {
|
||||
fn extract_names(node: &SyntaxNode) -> Vec<Rc<str>> {
|
||||
match &node.kind {
|
||||
UntypedKind::Identifier(sym) => vec![sym.name.clone()],
|
||||
UntypedKind::Tuple { elements } => {
|
||||
SyntaxKind::Identifier(sym) => vec![sym.name.clone()],
|
||||
SyntaxKind::Tuple { elements } => {
|
||||
elements.iter().flat_map(extract_names).collect()
|
||||
}
|
||||
_ => vec![],
|
||||
@@ -436,7 +436,7 @@ impl Environment {
|
||||
let p_names = extract_names(params);
|
||||
registry.define(name.name.clone(), p_names, body.as_ref().clone());
|
||||
}
|
||||
UntypedKind::Block { exprs } => {
|
||||
SyntaxKind::Block { exprs } => {
|
||||
for expr in exprs {
|
||||
self.discover_globals(expr);
|
||||
}
|
||||
@@ -445,8 +445,8 @@ impl Environment {
|
||||
}
|
||||
}
|
||||
|
||||
fn compile_pipeline(&self, untyped_ast: UntypedNode, diagnostics: &mut Diagnostics) -> Option<TypedNode> {
|
||||
let expanded_ast = match self.get_expander().expand(untyped_ast) {
|
||||
fn compile_pipeline(&self, syntax_ast: SyntaxNode, diagnostics: &mut Diagnostics) -> Option<TypedNode> {
|
||||
let expanded_ast = match self.get_expander().expand(syntax_ast) {
|
||||
Ok(ast) => ast,
|
||||
Err(e) => {
|
||||
diagnostics.push_error(e, None);
|
||||
@@ -505,10 +505,10 @@ impl Environment {
|
||||
Some(checker.check(&wrapped_ast, &[], diagnostics))
|
||||
}
|
||||
|
||||
fn compile_untyped(&self, untyped_ast: UntypedNode) -> Result<TypedNode, String> {
|
||||
fn compile_syntax(&self, syntax_ast: SyntaxNode) -> Result<TypedNode, String> {
|
||||
let mut diagnostics = Diagnostics::new();
|
||||
|
||||
let typed_ast_opt = self.compile_pipeline(untyped_ast, &mut diagnostics);
|
||||
let typed_ast_opt = self.compile_pipeline(syntax_ast, &mut diagnostics);
|
||||
|
||||
if diagnostics.has_errors() || typed_ast_opt.is_none() {
|
||||
return Err(diagnostics
|
||||
@@ -627,7 +627,7 @@ impl Environment {
|
||||
}
|
||||
|
||||
let mut parser = Parser::new(source);
|
||||
let untyped_ast = parser.parse_expression();
|
||||
let syntax_ast = parser.parse_expression();
|
||||
|
||||
if !parser.at_eof() {
|
||||
parser
|
||||
@@ -640,7 +640,7 @@ impl Environment {
|
||||
}
|
||||
let mut diagnostics = parser.diagnostics;
|
||||
|
||||
let typed_ast = self.compile_pipeline(untyped_ast, &mut diagnostics);
|
||||
let typed_ast = self.compile_pipeline(syntax_ast, &mut diagnostics);
|
||||
|
||||
CompilationResult {
|
||||
ast: typed_ast,
|
||||
@@ -737,7 +737,7 @@ impl Environment {
|
||||
|
||||
let rtl_lookup = Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args));
|
||||
let typed_reg = self.typed_function_registry.clone();
|
||||
let untyped_reg = self.function_registry.clone();
|
||||
let syntax_reg = self.function_registry.clone();
|
||||
let mono_cache = self.monomorph_cache.clone();
|
||||
let root_values = self.root_values.clone();
|
||||
let root_types = self.root_types.clone();
|
||||
@@ -764,7 +764,7 @@ impl Environment {
|
||||
let analyzed = Analyzer::analyze(&retyped_ast, &root_purity.borrow());
|
||||
|
||||
let sub_registry = Rc::new(EnvFunctionRegistry {
|
||||
registry: untyped_reg.clone(),
|
||||
registry: syntax_reg.clone(),
|
||||
analyzed_registry: typed_reg.clone(),
|
||||
});
|
||||
let sub_rtl_lookup =
|
||||
|
||||
+219
-219
@@ -1,219 +1,219 @@
|
||||
use crate::ast::types::SourceLocation;
|
||||
use std::iter::Peekable;
|
||||
use std::rc::Rc;
|
||||
use std::str::Chars;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum TokenKind {
|
||||
LeftParen,
|
||||
RightParen,
|
||||
LeftBracket,
|
||||
RightBracket,
|
||||
LeftBrace,
|
||||
RightBrace,
|
||||
Quote,
|
||||
Backtick,
|
||||
Tilde,
|
||||
At,
|
||||
Identifier(Rc<str>),
|
||||
Keyword(Rc<str>),
|
||||
Integer(i64),
|
||||
Float(f64),
|
||||
String(Rc<str>),
|
||||
EOF,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Token {
|
||||
pub kind: TokenKind,
|
||||
pub location: SourceLocation,
|
||||
}
|
||||
|
||||
pub struct Lexer<'a> {
|
||||
input: Peekable<Chars<'a>>,
|
||||
line: u32,
|
||||
col: u32,
|
||||
}
|
||||
|
||||
impl<'a> Lexer<'a> {
|
||||
pub fn new(input: &'a str) -> Self {
|
||||
Self {
|
||||
input: input.chars().peekable(),
|
||||
line: 1,
|
||||
col: 1,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_token(&mut self) -> Result<Token, String> {
|
||||
self.skip_whitespace();
|
||||
|
||||
let start_location = SourceLocation {
|
||||
line: self.line,
|
||||
col: self.col,
|
||||
};
|
||||
|
||||
let char = match self.input.next() {
|
||||
Some(c) => {
|
||||
let current_char = c;
|
||||
self.col += 1;
|
||||
current_char
|
||||
}
|
||||
None => {
|
||||
return Ok(Token {
|
||||
kind: TokenKind::EOF,
|
||||
location: start_location,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
let kind = match char {
|
||||
'(' => TokenKind::LeftParen,
|
||||
')' => TokenKind::RightParen,
|
||||
'[' => TokenKind::LeftBracket,
|
||||
']' => TokenKind::RightBracket,
|
||||
'{' => TokenKind::LeftBrace,
|
||||
'}' => TokenKind::RightBrace,
|
||||
'\'' => TokenKind::Quote,
|
||||
'`' => TokenKind::Backtick,
|
||||
'~' => TokenKind::Tilde,
|
||||
'@' => TokenKind::At,
|
||||
':' => {
|
||||
let id = self.read_while(|c| !c.is_whitespace() && !"()[]{}\"'`~@;".contains(c));
|
||||
TokenKind::Keyword(Rc::from(id))
|
||||
}
|
||||
'"' => TokenKind::String(Rc::from(self.read_string()?)),
|
||||
c if c.is_ascii_digit()
|
||||
|| (c == '-' && self.input.peek().is_some_and(|p| p.is_ascii_digit())) =>
|
||||
{
|
||||
let mut num_str = String::from(c);
|
||||
while let Some(&next) = self.peek() {
|
||||
if next.is_ascii_digit() || next == '.' {
|
||||
num_str.push(self.input.next().unwrap());
|
||||
self.col += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if num_str.contains('.') {
|
||||
TokenKind::Float(num_str.parse().map_err(|_| "Invalid float format")?)
|
||||
} else {
|
||||
TokenKind::Integer(num_str.parse().map_err(|_| "Invalid integer format")?)
|
||||
}
|
||||
}
|
||||
c if is_ident_start(c) => {
|
||||
let mut id = String::from(c);
|
||||
id.push_str(&self.read_while(is_ident_char));
|
||||
TokenKind::Identifier(Rc::from(id))
|
||||
}
|
||||
_ => {
|
||||
return Err(format!(
|
||||
"Unexpected character: {} at {}:{}",
|
||||
char,
|
||||
self.line,
|
||||
self.col - 1
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Token {
|
||||
kind,
|
||||
location: start_location,
|
||||
})
|
||||
}
|
||||
|
||||
fn peek(&mut self) -> Option<&char> {
|
||||
self.input.peek()
|
||||
}
|
||||
|
||||
fn skip_whitespace(&mut self) {
|
||||
while let Some(&c) = self.peek() {
|
||||
if c.is_whitespace() || is_invisible(c) || c == ',' {
|
||||
if c == '\n' {
|
||||
self.line += 1;
|
||||
self.col = 1;
|
||||
} else {
|
||||
self.col += 1;
|
||||
}
|
||||
self.input.next();
|
||||
} else if c == ';' || c == '#' {
|
||||
for c in self.input.by_ref() {
|
||||
if c == '\n' {
|
||||
self.line += 1;
|
||||
self.col = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_while<F>(&mut self, mut predicate: F) -> String
|
||||
where
|
||||
F: FnMut(char) -> bool,
|
||||
{
|
||||
let mut s = String::new();
|
||||
while let Some(&c) = self.peek() {
|
||||
if predicate(c) {
|
||||
s.push(self.input.next().unwrap());
|
||||
self.col += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn read_string(&mut self) -> Result<String, String> {
|
||||
let mut s = String::new();
|
||||
while let Some(c) = self.input.next() {
|
||||
self.col += 1;
|
||||
match c {
|
||||
'"' => return Ok(s),
|
||||
'\\' => {
|
||||
let next = self.input.next().ok_or("Unterminated string escape")?;
|
||||
self.col += 1;
|
||||
match next {
|
||||
'n' => s.push('\n'),
|
||||
'r' => s.push('\r'),
|
||||
't' => s.push('\t'),
|
||||
'\\' => s.push('\\'),
|
||||
'"' => s.push('"'),
|
||||
_ => s.push(next),
|
||||
}
|
||||
}
|
||||
'\n' => {
|
||||
self.line += 1;
|
||||
self.col = 1;
|
||||
s.push(c);
|
||||
}
|
||||
_ => s.push(c),
|
||||
}
|
||||
}
|
||||
Err("Unterminated string".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn is_ident_start(c: char) -> bool {
|
||||
c.is_alphabetic() || "+-*/<=>!$%&_?.".contains(c)
|
||||
}
|
||||
|
||||
fn is_ident_char(c: char) -> bool {
|
||||
is_ident_start(c) || c.is_ascii_digit()
|
||||
}
|
||||
|
||||
/// Returns true if the character is an invisible control character
|
||||
/// that should be ignored by the lexer (like BOM or Zero Width Space).
|
||||
fn is_invisible(c: char) -> bool {
|
||||
match c {
|
||||
'\u{FEFF}' | // BOM
|
||||
'\u{200B}' | // Zero Width Space
|
||||
'\u{200C}' | // Zero Width Non-Joiner
|
||||
'\u{200D}' | // Zero Width Joiner
|
||||
'\u{2060}' // Word Joiner
|
||||
=> true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
use crate::ast::types::SourceLocation;
|
||||
use std::iter::Peekable;
|
||||
use std::rc::Rc;
|
||||
use std::str::Chars;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum TokenKind {
|
||||
LeftParen,
|
||||
RightParen,
|
||||
LeftBracket,
|
||||
RightBracket,
|
||||
LeftBrace,
|
||||
RightBrace,
|
||||
Quote,
|
||||
Backtick,
|
||||
Tilde,
|
||||
At,
|
||||
Identifier(Rc<str>),
|
||||
Keyword(Rc<str>),
|
||||
Integer(i64),
|
||||
Float(f64),
|
||||
String(Rc<str>),
|
||||
EOF,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Token {
|
||||
pub kind: TokenKind,
|
||||
pub location: SourceLocation,
|
||||
}
|
||||
|
||||
pub struct Lexer<'a> {
|
||||
input: Peekable<Chars<'a>>,
|
||||
line: u32,
|
||||
col: u32,
|
||||
}
|
||||
|
||||
impl<'a> Lexer<'a> {
|
||||
pub fn new(input: &'a str) -> Self {
|
||||
Self {
|
||||
input: input.chars().peekable(),
|
||||
line: 1,
|
||||
col: 1,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_token(&mut self) -> Result<Token, String> {
|
||||
self.skip_whitespace();
|
||||
|
||||
let start_location = SourceLocation {
|
||||
line: self.line,
|
||||
col: self.col,
|
||||
};
|
||||
|
||||
let char = match self.input.next() {
|
||||
Some(c) => {
|
||||
let current_char = c;
|
||||
self.col += 1;
|
||||
current_char
|
||||
}
|
||||
None => {
|
||||
return Ok(Token {
|
||||
kind: TokenKind::EOF,
|
||||
location: start_location,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
let kind = match char {
|
||||
'(' => TokenKind::LeftParen,
|
||||
')' => TokenKind::RightParen,
|
||||
'[' => TokenKind::LeftBracket,
|
||||
']' => TokenKind::RightBracket,
|
||||
'{' => TokenKind::LeftBrace,
|
||||
'}' => TokenKind::RightBrace,
|
||||
'\'' => TokenKind::Quote,
|
||||
'`' => TokenKind::Backtick,
|
||||
'~' => TokenKind::Tilde,
|
||||
'@' => TokenKind::At,
|
||||
':' => {
|
||||
let id = self.read_while(|c| !c.is_whitespace() && !"()[]{}\"'`~@;".contains(c));
|
||||
TokenKind::Keyword(Rc::from(id))
|
||||
}
|
||||
'"' => TokenKind::String(Rc::from(self.read_string()?)),
|
||||
c if c.is_ascii_digit()
|
||||
|| (c == '-' && self.input.peek().is_some_and(|p| p.is_ascii_digit())) =>
|
||||
{
|
||||
let mut num_str = String::from(c);
|
||||
while let Some(&next) = self.peek() {
|
||||
if next.is_ascii_digit() || next == '.' {
|
||||
num_str.push(self.input.next().unwrap());
|
||||
self.col += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if num_str.contains('.') {
|
||||
TokenKind::Float(num_str.parse().map_err(|_| "Invalid float format")?)
|
||||
} else {
|
||||
TokenKind::Integer(num_str.parse().map_err(|_| "Invalid integer format")?)
|
||||
}
|
||||
}
|
||||
c if is_ident_start(c) => {
|
||||
let mut id = String::from(c);
|
||||
id.push_str(&self.read_while(is_ident_char));
|
||||
TokenKind::Identifier(Rc::from(id))
|
||||
}
|
||||
_ => {
|
||||
return Err(format!(
|
||||
"Unexpected character: {} at {}:{}",
|
||||
char,
|
||||
self.line,
|
||||
self.col - 1
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Token {
|
||||
kind,
|
||||
location: start_location,
|
||||
})
|
||||
}
|
||||
|
||||
fn peek(&mut self) -> Option<&char> {
|
||||
self.input.peek()
|
||||
}
|
||||
|
||||
fn skip_whitespace(&mut self) {
|
||||
while let Some(&c) = self.peek() {
|
||||
if c.is_whitespace() || is_invisible(c) || c == ',' {
|
||||
if c == '\n' {
|
||||
self.line += 1;
|
||||
self.col = 1;
|
||||
} else {
|
||||
self.col += 1;
|
||||
}
|
||||
self.input.next();
|
||||
} else if c == ';' || c == '#' {
|
||||
for c in self.input.by_ref() {
|
||||
if c == '\n' {
|
||||
self.line += 1;
|
||||
self.col = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_while<F>(&mut self, mut predicate: F) -> String
|
||||
where
|
||||
F: FnMut(char) -> bool,
|
||||
{
|
||||
let mut s = String::new();
|
||||
while let Some(&c) = self.peek() {
|
||||
if predicate(c) {
|
||||
s.push(self.input.next().unwrap());
|
||||
self.col += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn read_string(&mut self) -> Result<String, String> {
|
||||
let mut s = String::new();
|
||||
while let Some(c) = self.input.next() {
|
||||
self.col += 1;
|
||||
match c {
|
||||
'"' => return Ok(s),
|
||||
'\\' => {
|
||||
let next = self.input.next().ok_or("Unterminated string escape")?;
|
||||
self.col += 1;
|
||||
match next {
|
||||
'n' => s.push('\n'),
|
||||
'r' => s.push('\r'),
|
||||
't' => s.push('\t'),
|
||||
'\\' => s.push('\\'),
|
||||
'"' => s.push('"'),
|
||||
_ => s.push(next),
|
||||
}
|
||||
}
|
||||
'\n' => {
|
||||
self.line += 1;
|
||||
self.col = 1;
|
||||
s.push(c);
|
||||
}
|
||||
_ => s.push(c),
|
||||
}
|
||||
}
|
||||
Err("Unterminated string".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn is_ident_start(c: char) -> bool {
|
||||
c.is_alphabetic() || "+-*/<=>!$%&_?.".contains(c)
|
||||
}
|
||||
|
||||
fn is_ident_char(c: char) -> bool {
|
||||
is_ident_start(c) || c.is_ascii_digit()
|
||||
}
|
||||
|
||||
/// Returns true if the character is an invisible control character
|
||||
/// that should be ignored by the lexer (like BOM or Zero Width Space).
|
||||
fn is_invisible(c: char) -> bool {
|
||||
match c {
|
||||
'\u{FEFF}' | // BOM
|
||||
'\u{200B}' | // Zero Width Space
|
||||
'\u{200C}' | // Zero Width Non-Joiner
|
||||
'\u{200D}' | // Zero Width Joiner
|
||||
'\u{2060}' // Word Joiner
|
||||
=> true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
+9
-9
@@ -1,9 +1,9 @@
|
||||
pub mod compiler;
|
||||
pub mod diagnostics;
|
||||
pub mod environment;
|
||||
pub mod lexer;
|
||||
pub mod nodes;
|
||||
pub mod parser;
|
||||
pub mod rtl;
|
||||
pub mod types;
|
||||
pub mod vm;
|
||||
pub mod compiler;
|
||||
pub mod diagnostics;
|
||||
pub mod environment;
|
||||
pub mod lexer;
|
||||
pub mod nodes;
|
||||
pub mod parser;
|
||||
pub mod rtl;
|
||||
pub mod types;
|
||||
pub mod vm;
|
||||
|
||||
+134
-134
@@ -1,134 +1,134 @@
|
||||
use crate::ast::types::{Identity, Object, Value};
|
||||
use std::any::Any;
|
||||
use std::fmt::Debug;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A name with an optional context for macro hygiene.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Symbol {
|
||||
pub name: Rc<str>,
|
||||
/// Points to the identity of the Expansion node if this symbol
|
||||
/// was created/referenced inside a macro expansion.
|
||||
pub context: Option<Identity>,
|
||||
}
|
||||
|
||||
impl From<Rc<str>> for Symbol {
|
||||
fn from(name: Rc<str>) -> Self {
|
||||
Self {
|
||||
name,
|
||||
context: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Symbol {
|
||||
fn from(name: &str) -> Self {
|
||||
Self {
|
||||
name: Rc::from(name),
|
||||
context: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A parser AST Node wrapper to preserve identity and metadata
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct UntypedNode {
|
||||
pub identity: Identity,
|
||||
pub kind: UntypedKind,
|
||||
}
|
||||
|
||||
impl Object for UntypedNode {
|
||||
fn type_name(&self) -> &'static str {
|
||||
"ast-node"
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// The base for custom node types (extensions)
|
||||
pub trait CustomNode: Debug {
|
||||
fn display_name(&self) -> &'static str;
|
||||
fn clone_box(&self) -> Box<dyn CustomNode>;
|
||||
}
|
||||
|
||||
impl Clone for Box<dyn CustomNode> {
|
||||
fn clone(&self) -> Self {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum UntypedKind {
|
||||
Nop,
|
||||
Constant(Value),
|
||||
/// A general identifier (used for both references and declarations in the untyped AST).
|
||||
Identifier(Symbol),
|
||||
/// A first-class field accessor (e.g. .name)
|
||||
FieldAccessor(crate::ast::types::Keyword),
|
||||
If {
|
||||
cond: Box<UntypedNode>,
|
||||
then_br: Box<UntypedNode>,
|
||||
else_br: Option<Box<UntypedNode>>,
|
||||
},
|
||||
Def {
|
||||
target: Box<UntypedNode>,
|
||||
value: Box<UntypedNode>,
|
||||
},
|
||||
Assign {
|
||||
target: Box<UntypedNode>,
|
||||
value: Box<UntypedNode>,
|
||||
},
|
||||
Lambda {
|
||||
params: Box<UntypedNode>,
|
||||
body: Rc<UntypedNode>,
|
||||
},
|
||||
Call {
|
||||
callee: Box<UntypedNode>,
|
||||
args: Box<UntypedNode>,
|
||||
},
|
||||
Again {
|
||||
args: Box<UntypedNode>,
|
||||
},
|
||||
Pipe {
|
||||
inputs: Vec<UntypedNode>,
|
||||
lambda: Box<UntypedNode>,
|
||||
},
|
||||
Block {
|
||||
exprs: Vec<UntypedNode>,
|
||||
},
|
||||
Tuple {
|
||||
elements: Vec<UntypedNode>,
|
||||
},
|
||||
Record {
|
||||
fields: Vec<(UntypedNode, UntypedNode)>,
|
||||
},
|
||||
/// A macro declaration that can be expanded at compile time.
|
||||
MacroDecl {
|
||||
name: Symbol,
|
||||
params: Box<UntypedNode>,
|
||||
body: Box<UntypedNode>,
|
||||
},
|
||||
/// A template for AST nodes, allowing for substitutions. (Quasiquote)
|
||||
Template(Box<UntypedNode>),
|
||||
/// A placeholder inside a template to be replaced by a node or value. (Unquote)
|
||||
Placeholder(Box<UntypedNode>),
|
||||
/// A placeholder that flattens a sequence or merges a record into its surroundings. (Splice)
|
||||
Splice(Box<UntypedNode>),
|
||||
/// Represents an expanded macro call, preserving the original call for debugging.
|
||||
Expansion {
|
||||
/// The original call from the source AST.
|
||||
call: Box<UntypedNode>,
|
||||
/// The resulting AST after macro expansion.
|
||||
expanded: Box<UntypedNode>,
|
||||
},
|
||||
/// A diagnostic poison node, allowing compilation to continue after an error.
|
||||
Error,
|
||||
Extension(Box<dyn CustomNode>),
|
||||
}
|
||||
|
||||
impl PartialEq for Box<dyn CustomNode> {
|
||||
fn eq(&self, _other: &Self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
use crate::ast::types::{Identity, Object, Value};
|
||||
use std::any::Any;
|
||||
use std::fmt::Debug;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A name with an optional context for macro hygiene.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Symbol {
|
||||
pub name: Rc<str>,
|
||||
/// Points to the identity of the Expansion node if this symbol
|
||||
/// was created/referenced inside a macro expansion.
|
||||
pub context: Option<Identity>,
|
||||
}
|
||||
|
||||
impl From<Rc<str>> for Symbol {
|
||||
fn from(name: Rc<str>) -> Self {
|
||||
Self {
|
||||
name,
|
||||
context: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Symbol {
|
||||
fn from(name: &str) -> Self {
|
||||
Self {
|
||||
name: Rc::from(name),
|
||||
context: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A parser AST Node wrapper to preserve identity and metadata
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct SyntaxNode {
|
||||
pub identity: Identity,
|
||||
pub kind: SyntaxKind,
|
||||
}
|
||||
|
||||
impl Object for SyntaxNode {
|
||||
fn type_name(&self) -> &'static str {
|
||||
"ast-node"
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// The base for custom node types (extensions)
|
||||
pub trait CustomNode: Debug {
|
||||
fn display_name(&self) -> &'static str;
|
||||
fn clone_box(&self) -> Box<dyn CustomNode>;
|
||||
}
|
||||
|
||||
impl Clone for Box<dyn CustomNode> {
|
||||
fn clone(&self) -> Self {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum SyntaxKind {
|
||||
Nop,
|
||||
Constant(Value),
|
||||
/// A general identifier (used for both references and declarations in the syntax AST).
|
||||
Identifier(Symbol),
|
||||
/// A first-class field accessor (e.g. .name)
|
||||
FieldAccessor(crate::ast::types::Keyword),
|
||||
If {
|
||||
cond: Box<SyntaxNode>,
|
||||
then_br: Box<SyntaxNode>,
|
||||
else_br: Option<Box<SyntaxNode>>,
|
||||
},
|
||||
Def {
|
||||
target: Box<SyntaxNode>,
|
||||
value: Box<SyntaxNode>,
|
||||
},
|
||||
Assign {
|
||||
target: Box<SyntaxNode>,
|
||||
value: Box<SyntaxNode>,
|
||||
},
|
||||
Lambda {
|
||||
params: Box<SyntaxNode>,
|
||||
body: Rc<SyntaxNode>,
|
||||
},
|
||||
Call {
|
||||
callee: Box<SyntaxNode>,
|
||||
args: Box<SyntaxNode>,
|
||||
},
|
||||
Again {
|
||||
args: Box<SyntaxNode>,
|
||||
},
|
||||
Pipe {
|
||||
inputs: Vec<SyntaxNode>,
|
||||
lambda: Box<SyntaxNode>,
|
||||
},
|
||||
Block {
|
||||
exprs: Vec<SyntaxNode>,
|
||||
},
|
||||
Tuple {
|
||||
elements: Vec<SyntaxNode>,
|
||||
},
|
||||
Record {
|
||||
fields: Vec<(SyntaxNode, SyntaxNode)>,
|
||||
},
|
||||
/// A macro declaration that can be expanded at compile time.
|
||||
MacroDecl {
|
||||
name: Symbol,
|
||||
params: Box<SyntaxNode>,
|
||||
body: Box<SyntaxNode>,
|
||||
},
|
||||
/// A template for AST nodes, allowing for substitutions. (Quasiquote)
|
||||
Template(Box<SyntaxNode>),
|
||||
/// A placeholder inside a template to be replaced by a node or value. (Unquote)
|
||||
Placeholder(Box<SyntaxNode>),
|
||||
/// A placeholder that flattens a sequence or merges a record into its surroundings. (Splice)
|
||||
Splice(Box<SyntaxNode>),
|
||||
/// Represents an expanded macro call, preserving the original call for debugging.
|
||||
Expansion {
|
||||
/// The original call from the source AST.
|
||||
call: Box<SyntaxNode>,
|
||||
/// The resulting AST after macro expansion.
|
||||
expanded: Box<SyntaxNode>,
|
||||
},
|
||||
/// A diagnostic poison node, allowing compilation to continue after an error.
|
||||
Error,
|
||||
Extension(Box<dyn CustomNode>),
|
||||
}
|
||||
|
||||
impl PartialEq for Box<dyn CustomNode> {
|
||||
fn eq(&self, _other: &Self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
+522
-522
File diff suppressed because it is too large
Load Diff
+509
-509
File diff suppressed because it is too large
Load Diff
+39
-39
@@ -1,39 +1,39 @@
|
||||
use crate::ast::environment::Environment;
|
||||
use crate::ast::types::{Purity, Signature, StaticType, Value};
|
||||
use chrono::{NaiveDate, NaiveDateTime};
|
||||
|
||||
pub fn register(env: &Environment) {
|
||||
let date_ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Text]),
|
||||
ret: StaticType::DateTime,
|
||||
}));
|
||||
|
||||
env.register_native_fn("date", date_ty, Purity::Pure, |args| {
|
||||
if let Value::Text(s) = &args[0] {
|
||||
// Try parse YYYY-MM-DD
|
||||
if let Ok(dt) = NaiveDate::parse_from_str(s, "%Y-%m-%d") {
|
||||
let ts = dt
|
||||
.and_hms_opt(0, 0, 0)
|
||||
.unwrap()
|
||||
.and_utc()
|
||||
.timestamp_millis();
|
||||
return Value::DateTime(ts);
|
||||
}
|
||||
// Try parse YYYY-MM-DD HH:MM:SS
|
||||
if let Ok(dt) = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S") {
|
||||
return Value::DateTime(dt.and_utc().timestamp_millis());
|
||||
}
|
||||
}
|
||||
Value::Void
|
||||
});
|
||||
|
||||
let now_ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![]),
|
||||
ret: StaticType::DateTime,
|
||||
}));
|
||||
|
||||
env.register_native_fn("now", now_ty, Purity::SideEffectFree, |_| {
|
||||
let ts = chrono::Utc::now().timestamp_millis();
|
||||
Value::DateTime(ts)
|
||||
});
|
||||
}
|
||||
use crate::ast::environment::Environment;
|
||||
use crate::ast::types::{Purity, Signature, StaticType, Value};
|
||||
use chrono::{NaiveDate, NaiveDateTime};
|
||||
|
||||
pub fn register(env: &Environment) {
|
||||
let date_ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Text]),
|
||||
ret: StaticType::DateTime,
|
||||
}));
|
||||
|
||||
env.register_native_fn("date", date_ty, Purity::Pure, |args| {
|
||||
if let Value::Text(s) = &args[0] {
|
||||
// Try parse YYYY-MM-DD
|
||||
if let Ok(dt) = NaiveDate::parse_from_str(s, "%Y-%m-%d") {
|
||||
let ts = dt
|
||||
.and_hms_opt(0, 0, 0)
|
||||
.unwrap()
|
||||
.and_utc()
|
||||
.timestamp_millis();
|
||||
return Value::DateTime(ts);
|
||||
}
|
||||
// Try parse YYYY-MM-DD HH:MM:SS
|
||||
if let Ok(dt) = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S") {
|
||||
return Value::DateTime(dt.and_utc().timestamp_millis());
|
||||
}
|
||||
}
|
||||
Value::Void
|
||||
});
|
||||
|
||||
let now_ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![]),
|
||||
ret: StaticType::DateTime,
|
||||
}));
|
||||
|
||||
env.register_native_fn("now", now_ty, Purity::SideEffectFree, |_| {
|
||||
let ts = chrono::Utc::now().timestamp_millis();
|
||||
Value::DateTime(ts)
|
||||
});
|
||||
}
|
||||
|
||||
+115
-115
@@ -1,115 +1,115 @@
|
||||
use crate::ast::types::{Purity, StaticType, Value};
|
||||
|
||||
/// Looks up a specialized intrinsic function for the given operator and argument types.
|
||||
/// Returns (Executable Value, Return Type) if a fast-path exists.
|
||||
pub fn lookup(name: &str, args: &[StaticType]) -> Option<(Value, StaticType)> {
|
||||
match (name, args) {
|
||||
// --- Integer Arithmetic ---
|
||||
("+", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Int(a + b)
|
||||
} else {
|
||||
Value::Int(0) // Should not happen if type checker works
|
||||
}
|
||||
}),
|
||||
StaticType::Int,
|
||||
)),
|
||||
("-", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Int(a - b)
|
||||
} else {
|
||||
Value::Int(0)
|
||||
}
|
||||
}),
|
||||
StaticType::Int,
|
||||
)),
|
||||
("-", [StaticType::Int, StaticType::Int, StaticType::Int]) => Some((
|
||||
// Variadic optimization for 3 args (common in some Lisp dialects, though - usually is binary/unary)
|
||||
// MyC's core.rs supports variadic subtraction.
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
let a = match args[0] {
|
||||
Value::Int(i) => i,
|
||||
_ => 0,
|
||||
};
|
||||
let b = match args[1] {
|
||||
Value::Int(i) => i,
|
||||
_ => 0,
|
||||
};
|
||||
let c = match args[2] {
|
||||
Value::Int(i) => i,
|
||||
_ => 0,
|
||||
};
|
||||
Value::Int(a - b - c)
|
||||
}),
|
||||
StaticType::Int,
|
||||
)),
|
||||
("*", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Int(a * b)
|
||||
} else {
|
||||
Value::Int(0)
|
||||
}
|
||||
}),
|
||||
StaticType::Int,
|
||||
)),
|
||||
|
||||
// --- Integer Comparison ---
|
||||
("<=", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a <= b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
}),
|
||||
StaticType::Bool,
|
||||
)),
|
||||
("<", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a < b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
}),
|
||||
StaticType::Bool,
|
||||
)),
|
||||
(">", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a > b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
}),
|
||||
StaticType::Bool,
|
||||
)),
|
||||
(">=", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a >= b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
}),
|
||||
StaticType::Bool,
|
||||
)),
|
||||
("=", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a == b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
}),
|
||||
StaticType::Bool,
|
||||
)),
|
||||
|
||||
// --- Constant Unary for -1 (decrement optimization) ---
|
||||
// Special case: tak uses (- x 1). The specializer sees Call("-", [Int, Int]).
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
use crate::ast::types::{Purity, StaticType, Value};
|
||||
|
||||
/// Looks up a specialized intrinsic function for the given operator and argument types.
|
||||
/// Returns (Executable Value, Return Type) if a fast-path exists.
|
||||
pub fn lookup(name: &str, args: &[StaticType]) -> Option<(Value, StaticType)> {
|
||||
match (name, args) {
|
||||
// --- Integer Arithmetic ---
|
||||
("+", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Int(a + b)
|
||||
} else {
|
||||
Value::Int(0) // Should not happen if type checker works
|
||||
}
|
||||
}),
|
||||
StaticType::Int,
|
||||
)),
|
||||
("-", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Int(a - b)
|
||||
} else {
|
||||
Value::Int(0)
|
||||
}
|
||||
}),
|
||||
StaticType::Int,
|
||||
)),
|
||||
("-", [StaticType::Int, StaticType::Int, StaticType::Int]) => Some((
|
||||
// Variadic optimization for 3 args (common in some Lisp dialects, though - usually is binary/unary)
|
||||
// MyC's core.rs supports variadic subtraction.
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
let a = match args[0] {
|
||||
Value::Int(i) => i,
|
||||
_ => 0,
|
||||
};
|
||||
let b = match args[1] {
|
||||
Value::Int(i) => i,
|
||||
_ => 0,
|
||||
};
|
||||
let c = match args[2] {
|
||||
Value::Int(i) => i,
|
||||
_ => 0,
|
||||
};
|
||||
Value::Int(a - b - c)
|
||||
}),
|
||||
StaticType::Int,
|
||||
)),
|
||||
("*", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Int(a * b)
|
||||
} else {
|
||||
Value::Int(0)
|
||||
}
|
||||
}),
|
||||
StaticType::Int,
|
||||
)),
|
||||
|
||||
// --- Integer Comparison ---
|
||||
("<=", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a <= b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
}),
|
||||
StaticType::Bool,
|
||||
)),
|
||||
("<", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a < b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
}),
|
||||
StaticType::Bool,
|
||||
)),
|
||||
(">", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a > b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
}),
|
||||
StaticType::Bool,
|
||||
)),
|
||||
(">=", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a >= b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
}),
|
||||
StaticType::Bool,
|
||||
)),
|
||||
("=", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::make_function(Purity::Pure, |args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a == b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
}),
|
||||
StaticType::Bool,
|
||||
)),
|
||||
|
||||
// --- Constant Unary for -1 (decrement optimization) ---
|
||||
// Special case: tak uses (- x 1). The specializer sees Call("-", [Int, Int]).
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
+176
-176
@@ -1,176 +1,176 @@
|
||||
use crate::ast::environment::Environment;
|
||||
use crate::ast::types::{Purity, Signature, StaticType, Value};
|
||||
use std::f64::consts;
|
||||
|
||||
pub fn register(env: &Environment) {
|
||||
// Constants
|
||||
env.register_constant("PI", StaticType::Float, Value::Float(consts::PI));
|
||||
env.register_constant("E", StaticType::Float, Value::Float(consts::E));
|
||||
|
||||
// Helper to get f64 from Value (Int or Float)
|
||||
fn to_f64(v: &Value) -> Option<f64> {
|
||||
match v {
|
||||
Value::Float(f) => Some(*f),
|
||||
Value::Int(i) => Some(*i as f64),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
// Unary functions (float -> float)
|
||||
let register_unary = |name: &'static str, f: fn(f64) -> f64| {
|
||||
let ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Float]),
|
||||
ret: StaticType::Float,
|
||||
}));
|
||||
env.register_native_fn(name, ty, Purity::Pure, move |args| {
|
||||
if let Some(x) = to_f64(&args[0]) {
|
||||
Value::Float(f(x))
|
||||
} else {
|
||||
Value::Void
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Unary functions (float -> int)
|
||||
let register_to_int = |name: &'static str, f: fn(f64) -> f64| {
|
||||
let ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Float]),
|
||||
ret: StaticType::Int,
|
||||
}));
|
||||
env.register_native_fn(name, ty, Purity::Pure, move |args| {
|
||||
if let Some(x) = to_f64(&args[0]) {
|
||||
Value::Int(f(x) as i64)
|
||||
} else {
|
||||
Value::Void
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
register_unary("sqrt", f64::sqrt);
|
||||
register_unary("sin", f64::sin);
|
||||
register_unary("cos", f64::cos);
|
||||
register_unary("tan", f64::tan);
|
||||
register_unary("asin", f64::asin);
|
||||
register_unary("acos", f64::acos);
|
||||
register_unary("atan", f64::atan);
|
||||
register_unary("exp", f64::exp);
|
||||
register_unary("ln", f64::ln);
|
||||
register_unary("log10", f64::log10);
|
||||
register_unary("fract", f64::fract);
|
||||
|
||||
// Rounding functions returning Int
|
||||
register_to_int("round", f64::round);
|
||||
register_to_int("floor", f64::floor);
|
||||
register_to_int("ceil", f64::ceil);
|
||||
register_to_int("trunc", f64::trunc);
|
||||
|
||||
// Binary functions (float, float -> float)
|
||||
let register_binary = |name: &'static str, f: fn(f64, f64) -> f64| {
|
||||
let ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
|
||||
ret: StaticType::Float,
|
||||
}));
|
||||
env.register_native_fn(name, ty, Purity::Pure, move |args| {
|
||||
if let (Some(a), Some(b)) = (to_f64(&args[0]), to_f64(&args[1])) {
|
||||
Value::Float(f(a, b))
|
||||
} else {
|
||||
Value::Void
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
register_binary("atan2", f64::atan2);
|
||||
register_binary("pow", f64::powf);
|
||||
register_binary("log", f64::log);
|
||||
|
||||
// Specialized abs to handle Int -> Int, Float -> Float
|
||||
let abs_ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Any]),
|
||||
ret: StaticType::Any,
|
||||
}));
|
||||
env.register_native_fn("abs", abs_ty, Purity::Pure, |args| {
|
||||
if args.len() != 1 {
|
||||
return Value::Void;
|
||||
}
|
||||
match args[0] {
|
||||
Value::Int(i) => Value::Int(i.abs()),
|
||||
Value::Float(f) => Value::Float(f.abs()),
|
||||
_ => Value::Void,
|
||||
}
|
||||
});
|
||||
|
||||
// Variadic min / max
|
||||
let variadic_ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Any,
|
||||
ret: StaticType::Any,
|
||||
}));
|
||||
|
||||
env.register_native_fn("min", variadic_ty.clone(), Purity::Pure, |args| {
|
||||
if args.is_empty() {
|
||||
return Value::Void;
|
||||
}
|
||||
let mut best = args[0].clone();
|
||||
for arg in &args[1..] {
|
||||
if let Some(ord) = arg.partial_cmp(&best)
|
||||
&& ord == std::cmp::Ordering::Less
|
||||
{
|
||||
best = arg.clone();
|
||||
}
|
||||
}
|
||||
best
|
||||
});
|
||||
|
||||
env.register_native_fn("max", variadic_ty, Purity::Pure, |args| {
|
||||
if args.is_empty() {
|
||||
return Value::Void;
|
||||
}
|
||||
let mut best = args[0].clone();
|
||||
for arg in &args[1..] {
|
||||
if let Some(ord) = arg.partial_cmp(&best)
|
||||
&& ord == std::cmp::Ordering::Greater
|
||||
{
|
||||
best = arg.clone();
|
||||
}
|
||||
}
|
||||
best
|
||||
});
|
||||
|
||||
// Random generator factory (Closure-based)
|
||||
let generator_sig = Signature {
|
||||
params: StaticType::Tuple(vec![]),
|
||||
ret: StaticType::Float,
|
||||
};
|
||||
|
||||
let make_random_ty = StaticType::FunctionOverloads(vec![
|
||||
Signature {
|
||||
params: StaticType::Tuple(vec![]),
|
||||
ret: StaticType::Function(Box::new(generator_sig.clone())),
|
||||
},
|
||||
Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Int]),
|
||||
ret: StaticType::Function(Box::new(generator_sig)),
|
||||
},
|
||||
]);
|
||||
|
||||
env.register_native_fn(
|
||||
"make-random",
|
||||
make_random_ty,
|
||||
Purity::SideEffectFree,
|
||||
|args| {
|
||||
let rng = if args.is_empty() {
|
||||
fastrand::Rng::new()
|
||||
} else if let Value::Int(s) = args[0] {
|
||||
fastrand::Rng::with_seed(s as u64)
|
||||
} else {
|
||||
fastrand::Rng::new()
|
||||
};
|
||||
|
||||
let prng = std::rc::Rc::new(std::cell::RefCell::new(rng));
|
||||
|
||||
Value::Function(std::rc::Rc::new(crate::ast::types::NativeFunction {
|
||||
func: std::rc::Rc::new(move |_args| Value::Float(prng.borrow_mut().f64())),
|
||||
purity: Purity::Impure,
|
||||
}))
|
||||
},
|
||||
);
|
||||
}
|
||||
use crate::ast::environment::Environment;
|
||||
use crate::ast::types::{Purity, Signature, StaticType, Value};
|
||||
use std::f64::consts;
|
||||
|
||||
pub fn register(env: &Environment) {
|
||||
// Constants
|
||||
env.register_constant("PI", StaticType::Float, Value::Float(consts::PI));
|
||||
env.register_constant("E", StaticType::Float, Value::Float(consts::E));
|
||||
|
||||
// Helper to get f64 from Value (Int or Float)
|
||||
fn to_f64(v: &Value) -> Option<f64> {
|
||||
match v {
|
||||
Value::Float(f) => Some(*f),
|
||||
Value::Int(i) => Some(*i as f64),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
// Unary functions (float -> float)
|
||||
let register_unary = |name: &'static str, f: fn(f64) -> f64| {
|
||||
let ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Float]),
|
||||
ret: StaticType::Float,
|
||||
}));
|
||||
env.register_native_fn(name, ty, Purity::Pure, move |args| {
|
||||
if let Some(x) = to_f64(&args[0]) {
|
||||
Value::Float(f(x))
|
||||
} else {
|
||||
Value::Void
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Unary functions (float -> int)
|
||||
let register_to_int = |name: &'static str, f: fn(f64) -> f64| {
|
||||
let ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Float]),
|
||||
ret: StaticType::Int,
|
||||
}));
|
||||
env.register_native_fn(name, ty, Purity::Pure, move |args| {
|
||||
if let Some(x) = to_f64(&args[0]) {
|
||||
Value::Int(f(x) as i64)
|
||||
} else {
|
||||
Value::Void
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
register_unary("sqrt", f64::sqrt);
|
||||
register_unary("sin", f64::sin);
|
||||
register_unary("cos", f64::cos);
|
||||
register_unary("tan", f64::tan);
|
||||
register_unary("asin", f64::asin);
|
||||
register_unary("acos", f64::acos);
|
||||
register_unary("atan", f64::atan);
|
||||
register_unary("exp", f64::exp);
|
||||
register_unary("ln", f64::ln);
|
||||
register_unary("log10", f64::log10);
|
||||
register_unary("fract", f64::fract);
|
||||
|
||||
// Rounding functions returning Int
|
||||
register_to_int("round", f64::round);
|
||||
register_to_int("floor", f64::floor);
|
||||
register_to_int("ceil", f64::ceil);
|
||||
register_to_int("trunc", f64::trunc);
|
||||
|
||||
// Binary functions (float, float -> float)
|
||||
let register_binary = |name: &'static str, f: fn(f64, f64) -> f64| {
|
||||
let ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
|
||||
ret: StaticType::Float,
|
||||
}));
|
||||
env.register_native_fn(name, ty, Purity::Pure, move |args| {
|
||||
if let (Some(a), Some(b)) = (to_f64(&args[0]), to_f64(&args[1])) {
|
||||
Value::Float(f(a, b))
|
||||
} else {
|
||||
Value::Void
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
register_binary("atan2", f64::atan2);
|
||||
register_binary("pow", f64::powf);
|
||||
register_binary("log", f64::log);
|
||||
|
||||
// Specialized abs to handle Int -> Int, Float -> Float
|
||||
let abs_ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Any]),
|
||||
ret: StaticType::Any,
|
||||
}));
|
||||
env.register_native_fn("abs", abs_ty, Purity::Pure, |args| {
|
||||
if args.len() != 1 {
|
||||
return Value::Void;
|
||||
}
|
||||
match args[0] {
|
||||
Value::Int(i) => Value::Int(i.abs()),
|
||||
Value::Float(f) => Value::Float(f.abs()),
|
||||
_ => Value::Void,
|
||||
}
|
||||
});
|
||||
|
||||
// Variadic min / max
|
||||
let variadic_ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Any,
|
||||
ret: StaticType::Any,
|
||||
}));
|
||||
|
||||
env.register_native_fn("min", variadic_ty.clone(), Purity::Pure, |args| {
|
||||
if args.is_empty() {
|
||||
return Value::Void;
|
||||
}
|
||||
let mut best = args[0].clone();
|
||||
for arg in &args[1..] {
|
||||
if let Some(ord) = arg.partial_cmp(&best)
|
||||
&& ord == std::cmp::Ordering::Less
|
||||
{
|
||||
best = arg.clone();
|
||||
}
|
||||
}
|
||||
best
|
||||
});
|
||||
|
||||
env.register_native_fn("max", variadic_ty, Purity::Pure, |args| {
|
||||
if args.is_empty() {
|
||||
return Value::Void;
|
||||
}
|
||||
let mut best = args[0].clone();
|
||||
for arg in &args[1..] {
|
||||
if let Some(ord) = arg.partial_cmp(&best)
|
||||
&& ord == std::cmp::Ordering::Greater
|
||||
{
|
||||
best = arg.clone();
|
||||
}
|
||||
}
|
||||
best
|
||||
});
|
||||
|
||||
// Random generator factory (Closure-based)
|
||||
let generator_sig = Signature {
|
||||
params: StaticType::Tuple(vec![]),
|
||||
ret: StaticType::Float,
|
||||
};
|
||||
|
||||
let make_random_ty = StaticType::FunctionOverloads(vec![
|
||||
Signature {
|
||||
params: StaticType::Tuple(vec![]),
|
||||
ret: StaticType::Function(Box::new(generator_sig.clone())),
|
||||
},
|
||||
Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Int]),
|
||||
ret: StaticType::Function(Box::new(generator_sig)),
|
||||
},
|
||||
]);
|
||||
|
||||
env.register_native_fn(
|
||||
"make-random",
|
||||
make_random_ty,
|
||||
Purity::SideEffectFree,
|
||||
|args| {
|
||||
let rng = if args.is_empty() {
|
||||
fastrand::Rng::new()
|
||||
} else if let Value::Int(s) = args[0] {
|
||||
fastrand::Rng::with_seed(s as u64)
|
||||
} else {
|
||||
fastrand::Rng::new()
|
||||
};
|
||||
|
||||
let prng = std::rc::Rc::new(std::cell::RefCell::new(rng));
|
||||
|
||||
Value::Function(std::rc::Rc::new(crate::ast::types::NativeFunction {
|
||||
func: std::rc::Rc::new(move |_args| Value::Float(prng.borrow_mut().f64())),
|
||||
purity: Purity::Impure,
|
||||
}))
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
+17
-17
@@ -1,17 +1,17 @@
|
||||
pub mod core;
|
||||
pub mod datetime;
|
||||
pub mod intrinsics;
|
||||
pub mod math;
|
||||
pub mod series;
|
||||
pub mod streams;
|
||||
pub mod type_registry;
|
||||
|
||||
use crate::ast::environment::Environment;
|
||||
|
||||
pub fn register(env: &Environment) {
|
||||
core::register(env);
|
||||
datetime::register(env);
|
||||
math::register(env);
|
||||
series::register(env);
|
||||
streams::register(env);
|
||||
}
|
||||
pub mod core;
|
||||
pub mod datetime;
|
||||
pub mod intrinsics;
|
||||
pub mod math;
|
||||
pub mod series;
|
||||
pub mod streams;
|
||||
pub mod type_registry;
|
||||
|
||||
use crate::ast::environment::Environment;
|
||||
|
||||
pub fn register(env: &Environment) {
|
||||
core::register(env);
|
||||
datetime::register(env);
|
||||
math::register(env);
|
||||
series::register(env);
|
||||
streams::register(env);
|
||||
}
|
||||
|
||||
+648
-648
File diff suppressed because it is too large
Load Diff
+536
-536
File diff suppressed because it is too large
Load Diff
+273
-273
@@ -1,273 +1,273 @@
|
||||
use crate::ast::types::{Keyword, Purity, Signature, StaticType, Value};
|
||||
use std::any::TypeId;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Represents a Rust type that can be exposed to the script environment.
|
||||
pub trait Scriptable: 'static + Sized {
|
||||
/// Returns the name of the type for debugging/AST.
|
||||
fn type_name() -> &'static str;
|
||||
|
||||
/// Returns the static type definition (methods, properties) for the AST.
|
||||
fn static_type() -> StaticType;
|
||||
|
||||
/// Wraps the instance into a Value (usually a Record of closures).
|
||||
fn wrap(self) -> Value;
|
||||
}
|
||||
|
||||
/// A registry for tracking registered types and their static definitions.
|
||||
pub struct TypeRegistry {
|
||||
known_types: HashMap<TypeId, StaticType>,
|
||||
}
|
||||
|
||||
impl Default for TypeRegistry {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeRegistry {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
known_types: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Registers a type T. Equivalent to `RegisterType<T>` in Delphi.
|
||||
pub fn register<T: Scriptable>(&mut self) {
|
||||
let id = TypeId::of::<T>();
|
||||
self.known_types.entry(id).or_insert_with(T::static_type);
|
||||
}
|
||||
|
||||
/// Resolves the static type for a Rust type.
|
||||
pub fn resolve_type<T: 'static>(&self) -> StaticType {
|
||||
self.known_types
|
||||
.get(&TypeId::of::<T>())
|
||||
.cloned()
|
||||
.unwrap_or(StaticType::Any)
|
||||
}
|
||||
|
||||
/// Creates a factory function value that can be bound in the environment.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `factory_func` - A Rust closure that takes script arguments and returns a Result<T, String>.
|
||||
pub fn create_factory<T, F>(factory_func: F) -> Value
|
||||
where
|
||||
T: Scriptable,
|
||||
F: Fn(&[Value]) -> Result<T, String> + 'static,
|
||||
{
|
||||
// The factory is a script function that calls the Rust factory, gets T, then wraps it.
|
||||
let closure = move |args: &[Value]| -> Value {
|
||||
match factory_func(args) {
|
||||
Ok(instance) => instance.wrap(),
|
||||
Err(msg) => {
|
||||
// In a real system, we'd propagate this error. For now, panic or return Void.
|
||||
// Delphi returns Void if nil, but raises exception on error.
|
||||
// Since Value doesn't have Error, we panic to stop execution.
|
||||
panic!("Runtime Error in Factory for {}: {}", T::type_name(), msg);
|
||||
}
|
||||
}
|
||||
};
|
||||
Value::make_function(Purity::Impure, closure)
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper to build the shadow record for an instance.
|
||||
/// This is used inside `Scriptable::wrap`.
|
||||
pub struct RecordBuilder {
|
||||
fields: Vec<(Keyword, Value)>,
|
||||
}
|
||||
|
||||
impl Default for RecordBuilder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl RecordBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self { fields: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn method<F>(mut self, name: &str, func: F) -> Self
|
||||
where
|
||||
F: Fn(&[Value]) -> Value + 'static,
|
||||
{
|
||||
let key = Keyword::intern(name);
|
||||
self.fields
|
||||
.push((key, Value::make_function(Purity::Impure, func)));
|
||||
self
|
||||
}
|
||||
|
||||
// Helper for methods that return Result (propagating panics for now)
|
||||
pub fn method_checked<F>(self, name: &str, func: F) -> Self
|
||||
where
|
||||
F: Fn(&[Value]) -> Result<Value, String> + 'static,
|
||||
{
|
||||
let closure = move |args: &[Value]| match func(args) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("Method call error: {}", e),
|
||||
};
|
||||
self.method(name, closure)
|
||||
}
|
||||
|
||||
pub fn build(self) -> Value {
|
||||
let mut keys = Vec::with_capacity(self.fields.len());
|
||||
let mut values = Vec::with_capacity(self.fields.len());
|
||||
for (k, v) in self.fields {
|
||||
keys.push(k);
|
||||
values.push(v);
|
||||
}
|
||||
Value::make_record(keys, values)
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper to build the StaticType definition.
|
||||
pub struct TypeBuilder {
|
||||
fields: Vec<(Keyword, StaticType)>,
|
||||
}
|
||||
|
||||
impl Default for TypeBuilder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self { fields: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn method(mut self, name: &str, params: Vec<StaticType>, ret: StaticType) -> Self {
|
||||
let key = Keyword::intern(name);
|
||||
let sig = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(params),
|
||||
ret,
|
||||
}));
|
||||
self.fields.push((key, sig));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> StaticType {
|
||||
StaticType::Record(crate::ast::types::RecordLayout::get_or_create(self.fields))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ast::types::{StaticType, Value};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Person {
|
||||
name: String,
|
||||
age: u32,
|
||||
}
|
||||
|
||||
impl Scriptable for Person {
|
||||
fn type_name() -> &'static str {
|
||||
"Person"
|
||||
}
|
||||
|
||||
fn static_type() -> StaticType {
|
||||
TypeBuilder::new()
|
||||
.method("greet", vec![], StaticType::Text)
|
||||
.method("older", vec![], StaticType::Int)
|
||||
.build()
|
||||
}
|
||||
|
||||
fn wrap(self) -> Value {
|
||||
let name = self.name.clone();
|
||||
let age = self.age;
|
||||
|
||||
RecordBuilder::new()
|
||||
.method("greet", move |_| {
|
||||
Value::Text(format!("Hello {}", name).into())
|
||||
})
|
||||
.method("older", move |_| Value::Int((age + 1) as i64))
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_register_and_wrap() {
|
||||
let mut registry = TypeRegistry::new();
|
||||
registry.register::<Person>();
|
||||
|
||||
let p = Person {
|
||||
name: "Alice".to_string(),
|
||||
age: 30,
|
||||
};
|
||||
let wrapped = p.wrap();
|
||||
|
||||
// Check static type
|
||||
let st = TypeRegistry::resolve_type::<Person>(®istry);
|
||||
if let StaticType::Record(layout) = st {
|
||||
assert!(
|
||||
layout
|
||||
.fields
|
||||
.iter()
|
||||
.any(|(k, _)| *k == Keyword::intern("greet"))
|
||||
);
|
||||
} else {
|
||||
panic!("Expected Record type");
|
||||
}
|
||||
|
||||
// Check runtime behavior
|
||||
if let Value::Record(layout, values) = wrapped {
|
||||
let idx = layout.index_of(Keyword::intern("greet")).unwrap();
|
||||
let greet_fn = &values[idx];
|
||||
|
||||
if let Value::Function(f) = greet_fn {
|
||||
let res = (f.func)(&[]);
|
||||
if let Value::Text(s) = res {
|
||||
assert_eq!(&*s, "Hello Alice");
|
||||
} else {
|
||||
panic!("Expected Text result");
|
||||
}
|
||||
} else {
|
||||
panic!("Expected Function value for method");
|
||||
}
|
||||
} else {
|
||||
panic!("Expected Record value");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_factory() {
|
||||
let factory_val = TypeRegistry::create_factory(|args: &[Value]| {
|
||||
if args.len() != 2 {
|
||||
return Err("Expected 2 args".to_string());
|
||||
}
|
||||
let name = match &args[0] {
|
||||
Value::Text(t) => t.to_string(),
|
||||
_ => return Err("Name must be text".to_string()),
|
||||
};
|
||||
let age = match &args[1] {
|
||||
Value::Int(i) => *i as u32,
|
||||
_ => return Err("Age must be int".to_string()),
|
||||
};
|
||||
Ok(Person { name, age })
|
||||
});
|
||||
|
||||
if let Value::Function(f) = factory_val {
|
||||
let instance = (f.func)(&[Value::Text("Bob".into()), Value::Int(40)]);
|
||||
if let Value::Record(layout, values) = instance {
|
||||
let idx = layout.index_of(Keyword::intern("greet")).unwrap();
|
||||
let greet_fn = &values[idx];
|
||||
|
||||
if let Value::Function(gf) = greet_fn {
|
||||
let res = (gf.func)(&[]);
|
||||
if let Value::Text(s) = res {
|
||||
assert_eq!(&*s, "Hello Bob");
|
||||
} else {
|
||||
panic!("Wrong return type");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
panic!("Factory should return Record");
|
||||
}
|
||||
} else {
|
||||
panic!("Factory is not a function");
|
||||
}
|
||||
}
|
||||
}
|
||||
use crate::ast::types::{Keyword, Purity, Signature, StaticType, Value};
|
||||
use std::any::TypeId;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Represents a Rust type that can be exposed to the script environment.
|
||||
pub trait Scriptable: 'static + Sized {
|
||||
/// Returns the name of the type for debugging/AST.
|
||||
fn type_name() -> &'static str;
|
||||
|
||||
/// Returns the static type definition (methods, properties) for the AST.
|
||||
fn static_type() -> StaticType;
|
||||
|
||||
/// Wraps the instance into a Value (usually a Record of closures).
|
||||
fn wrap(self) -> Value;
|
||||
}
|
||||
|
||||
/// A registry for tracking registered types and their static definitions.
|
||||
pub struct TypeRegistry {
|
||||
known_types: HashMap<TypeId, StaticType>,
|
||||
}
|
||||
|
||||
impl Default for TypeRegistry {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeRegistry {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
known_types: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Registers a type T. Equivalent to `RegisterType<T>` in Delphi.
|
||||
pub fn register<T: Scriptable>(&mut self) {
|
||||
let id = TypeId::of::<T>();
|
||||
self.known_types.entry(id).or_insert_with(T::static_type);
|
||||
}
|
||||
|
||||
/// Resolves the static type for a Rust type.
|
||||
pub fn resolve_type<T: 'static>(&self) -> StaticType {
|
||||
self.known_types
|
||||
.get(&TypeId::of::<T>())
|
||||
.cloned()
|
||||
.unwrap_or(StaticType::Any)
|
||||
}
|
||||
|
||||
/// Creates a factory function value that can be bound in the environment.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `factory_func` - A Rust closure that takes script arguments and returns a Result<T, String>.
|
||||
pub fn create_factory<T, F>(factory_func: F) -> Value
|
||||
where
|
||||
T: Scriptable,
|
||||
F: Fn(&[Value]) -> Result<T, String> + 'static,
|
||||
{
|
||||
// The factory is a script function that calls the Rust factory, gets T, then wraps it.
|
||||
let closure = move |args: &[Value]| -> Value {
|
||||
match factory_func(args) {
|
||||
Ok(instance) => instance.wrap(),
|
||||
Err(msg) => {
|
||||
// In a real system, we'd propagate this error. For now, panic or return Void.
|
||||
// Delphi returns Void if nil, but raises exception on error.
|
||||
// Since Value doesn't have Error, we panic to stop execution.
|
||||
panic!("Runtime Error in Factory for {}: {}", T::type_name(), msg);
|
||||
}
|
||||
}
|
||||
};
|
||||
Value::make_function(Purity::Impure, closure)
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper to build the shadow record for an instance.
|
||||
/// This is used inside `Scriptable::wrap`.
|
||||
pub struct RecordBuilder {
|
||||
fields: Vec<(Keyword, Value)>,
|
||||
}
|
||||
|
||||
impl Default for RecordBuilder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl RecordBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self { fields: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn method<F>(mut self, name: &str, func: F) -> Self
|
||||
where
|
||||
F: Fn(&[Value]) -> Value + 'static,
|
||||
{
|
||||
let key = Keyword::intern(name);
|
||||
self.fields
|
||||
.push((key, Value::make_function(Purity::Impure, func)));
|
||||
self
|
||||
}
|
||||
|
||||
// Helper for methods that return Result (propagating panics for now)
|
||||
pub fn method_checked<F>(self, name: &str, func: F) -> Self
|
||||
where
|
||||
F: Fn(&[Value]) -> Result<Value, String> + 'static,
|
||||
{
|
||||
let closure = move |args: &[Value]| match func(args) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("Method call error: {}", e),
|
||||
};
|
||||
self.method(name, closure)
|
||||
}
|
||||
|
||||
pub fn build(self) -> Value {
|
||||
let mut keys = Vec::with_capacity(self.fields.len());
|
||||
let mut values = Vec::with_capacity(self.fields.len());
|
||||
for (k, v) in self.fields {
|
||||
keys.push(k);
|
||||
values.push(v);
|
||||
}
|
||||
Value::make_record(keys, values)
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper to build the StaticType definition.
|
||||
pub struct TypeBuilder {
|
||||
fields: Vec<(Keyword, StaticType)>,
|
||||
}
|
||||
|
||||
impl Default for TypeBuilder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self { fields: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn method(mut self, name: &str, params: Vec<StaticType>, ret: StaticType) -> Self {
|
||||
let key = Keyword::intern(name);
|
||||
let sig = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(params),
|
||||
ret,
|
||||
}));
|
||||
self.fields.push((key, sig));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> StaticType {
|
||||
StaticType::Record(crate::ast::types::RecordLayout::get_or_create(self.fields))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ast::types::{StaticType, Value};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Person {
|
||||
name: String,
|
||||
age: u32,
|
||||
}
|
||||
|
||||
impl Scriptable for Person {
|
||||
fn type_name() -> &'static str {
|
||||
"Person"
|
||||
}
|
||||
|
||||
fn static_type() -> StaticType {
|
||||
TypeBuilder::new()
|
||||
.method("greet", vec![], StaticType::Text)
|
||||
.method("older", vec![], StaticType::Int)
|
||||
.build()
|
||||
}
|
||||
|
||||
fn wrap(self) -> Value {
|
||||
let name = self.name.clone();
|
||||
let age = self.age;
|
||||
|
||||
RecordBuilder::new()
|
||||
.method("greet", move |_| {
|
||||
Value::Text(format!("Hello {}", name).into())
|
||||
})
|
||||
.method("older", move |_| Value::Int((age + 1) as i64))
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_register_and_wrap() {
|
||||
let mut registry = TypeRegistry::new();
|
||||
registry.register::<Person>();
|
||||
|
||||
let p = Person {
|
||||
name: "Alice".to_string(),
|
||||
age: 30,
|
||||
};
|
||||
let wrapped = p.wrap();
|
||||
|
||||
// Check static type
|
||||
let st = TypeRegistry::resolve_type::<Person>(®istry);
|
||||
if let StaticType::Record(layout) = st {
|
||||
assert!(
|
||||
layout
|
||||
.fields
|
||||
.iter()
|
||||
.any(|(k, _)| *k == Keyword::intern("greet"))
|
||||
);
|
||||
} else {
|
||||
panic!("Expected Record type");
|
||||
}
|
||||
|
||||
// Check runtime behavior
|
||||
if let Value::Record(layout, values) = wrapped {
|
||||
let idx = layout.index_of(Keyword::intern("greet")).unwrap();
|
||||
let greet_fn = &values[idx];
|
||||
|
||||
if let Value::Function(f) = greet_fn {
|
||||
let res = (f.func)(&[]);
|
||||
if let Value::Text(s) = res {
|
||||
assert_eq!(&*s, "Hello Alice");
|
||||
} else {
|
||||
panic!("Expected Text result");
|
||||
}
|
||||
} else {
|
||||
panic!("Expected Function value for method");
|
||||
}
|
||||
} else {
|
||||
panic!("Expected Record value");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_factory() {
|
||||
let factory_val = TypeRegistry::create_factory(|args: &[Value]| {
|
||||
if args.len() != 2 {
|
||||
return Err("Expected 2 args".to_string());
|
||||
}
|
||||
let name = match &args[0] {
|
||||
Value::Text(t) => t.to_string(),
|
||||
_ => return Err("Name must be text".to_string()),
|
||||
};
|
||||
let age = match &args[1] {
|
||||
Value::Int(i) => *i as u32,
|
||||
_ => return Err("Age must be int".to_string()),
|
||||
};
|
||||
Ok(Person { name, age })
|
||||
});
|
||||
|
||||
if let Value::Function(f) = factory_val {
|
||||
let instance = (f.func)(&[Value::Text("Bob".into()), Value::Int(40)]);
|
||||
if let Value::Record(layout, values) = instance {
|
||||
let idx = layout.index_of(Keyword::intern("greet")).unwrap();
|
||||
let greet_fn = &values[idx];
|
||||
|
||||
if let Value::Function(gf) = greet_fn {
|
||||
let res = (gf.func)(&[]);
|
||||
if let Value::Text(s) = res {
|
||||
assert_eq!(&*s, "Hello Bob");
|
||||
} else {
|
||||
panic!("Wrong return type");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
panic!("Factory should return Record");
|
||||
}
|
||||
} else {
|
||||
panic!("Factory is not a function");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+682
-682
File diff suppressed because it is too large
Load Diff
+171
-171
@@ -1,171 +1,171 @@
|
||||
use clap::Parser;
|
||||
use myc::ast::environment::Environment;
|
||||
use myc::utils::tester;
|
||||
use std::fs;
|
||||
use std::io::{self, IsTerminal, Read};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about = "MYC AST Compiler & Benchmarker", long_about = None)]
|
||||
struct Cli {
|
||||
/// The script file to run or benchmark
|
||||
#[arg(value_name = "FILE")]
|
||||
file: Option<PathBuf>,
|
||||
|
||||
/// Run a script string directly
|
||||
#[arg(short, long)]
|
||||
eval: Option<String>,
|
||||
|
||||
/// Run benchmarks (Only allowed in Release mode)
|
||||
#[arg(short, long)]
|
||||
bench: bool,
|
||||
|
||||
/// Update the benchmark baseline (Only allowed in Release mode)
|
||||
#[arg(short, long)]
|
||||
update_bench: bool,
|
||||
|
||||
/// Dump the compiled AST
|
||||
#[arg(short, long)]
|
||||
dump: bool,
|
||||
|
||||
/// Run with TracingObserver enabled
|
||||
#[arg(short, long)]
|
||||
trace: bool,
|
||||
|
||||
/// Library directories to load before execution
|
||||
#[arg(short, long)]
|
||||
lib: Vec<PathBuf>,
|
||||
|
||||
/// Disable optimization
|
||||
#[arg(long)]
|
||||
no_opt: bool,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
let mut env = Environment::new();
|
||||
env.optimization = !cli.no_opt;
|
||||
|
||||
// Load libraries (Now just search paths)
|
||||
for lib_path in &cli.lib {
|
||||
env.add_search_path(lib_path);
|
||||
}
|
||||
|
||||
if cli.bench || cli.update_bench {
|
||||
if cfg!(debug_assertions) {
|
||||
eprintln!("❌ ERROR: Benchmarks must be run in Release mode!");
|
||||
eprintln!(" Use: cargo run --release --bin ast -- --bench");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
println!("🚀 Running benchmarks in RELEASE mode...\n");
|
||||
let filter = cli
|
||||
.file
|
||||
.as_ref()
|
||||
.and_then(|p| p.file_name())
|
||||
.map(|n| n.to_string_lossy().to_string());
|
||||
let results = tester::run_benchmarks(cli.update_bench, filter.as_deref());
|
||||
for res in results {
|
||||
let diff = res
|
||||
.diff_pct
|
||||
.map_or(String::new(), |d| format!(" ({:+.1}%)", d));
|
||||
println!("{}: {} - {:?}{}", res.status, res.name, res.median, diff);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Determine the source code to execute
|
||||
let source = if let Some(script_str) = cli.eval {
|
||||
Some(script_str)
|
||||
} else if let Some(file_path) = cli.file {
|
||||
if file_path.to_str() == Some("-") {
|
||||
read_stdin()
|
||||
} else {
|
||||
match fs::read_to_string(&file_path) {
|
||||
Ok(content) => Some(content),
|
||||
Err(e) => {
|
||||
eprintln!("Error reading file {:?}: {}", file_path, e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if !io::stdin().is_terminal() {
|
||||
read_stdin()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if let Some(content) = source {
|
||||
if cli.dump {
|
||||
match env.dump_ast(&content) {
|
||||
Ok(dump) => println!("{}", dump),
|
||||
Err(e) => eprintln!("Error dumping AST: {}", e),
|
||||
}
|
||||
} else if cli.trace {
|
||||
execute_trace(&env, &content);
|
||||
} else {
|
||||
execute(&env, &content);
|
||||
}
|
||||
} else {
|
||||
println!("MYC AST Compiler CLI. Use --help for usage.");
|
||||
}
|
||||
}
|
||||
|
||||
fn read_stdin() -> Option<String> {
|
||||
let mut buffer = String::new();
|
||||
match io::stdin().read_to_string(&mut buffer) {
|
||||
Ok(_) => Some(buffer),
|
||||
Err(e) => {
|
||||
eprintln!("Error reading from stdin: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn execute(env: &Environment, source: &str) {
|
||||
let result = env.compile(source);
|
||||
for diag in &result.diagnostics.items {
|
||||
let level = format!("{:?}", diag.level).to_uppercase();
|
||||
let loc = diag
|
||||
.identity
|
||||
.as_ref()
|
||||
.and_then(|id| id.location)
|
||||
.map(|l| format!(" at {}:{}", l.line, l.col))
|
||||
.unwrap_or_default();
|
||||
eprintln!("{}{} : {}", level, loc, diag.message);
|
||||
}
|
||||
|
||||
if result.diagnostics.has_errors() {
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
match env.run_script_compiled(result.ast.unwrap()) {
|
||||
Ok(res) => println!("{}", res),
|
||||
Err(e) => {
|
||||
eprintln!("Runtime Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn execute_trace(env: &Environment, source: &str) {
|
||||
match env.run_debug(source) {
|
||||
Ok((Ok(result), logs)) => {
|
||||
for line in logs {
|
||||
println!("{}", line);
|
||||
}
|
||||
println!("Result: {}", result);
|
||||
}
|
||||
Ok((Err(e), logs)) => {
|
||||
for line in logs {
|
||||
println!("{}", line);
|
||||
}
|
||||
eprintln!("Runtime Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Compilation Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
use clap::Parser;
|
||||
use myc::ast::environment::Environment;
|
||||
use myc::utils::tester;
|
||||
use std::fs;
|
||||
use std::io::{self, IsTerminal, Read};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about = "MYC AST Compiler & Benchmarker", long_about = None)]
|
||||
struct Cli {
|
||||
/// The script file to run or benchmark
|
||||
#[arg(value_name = "FILE")]
|
||||
file: Option<PathBuf>,
|
||||
|
||||
/// Run a script string directly
|
||||
#[arg(short, long)]
|
||||
eval: Option<String>,
|
||||
|
||||
/// Run benchmarks (Only allowed in Release mode)
|
||||
#[arg(short, long)]
|
||||
bench: bool,
|
||||
|
||||
/// Update the benchmark baseline (Only allowed in Release mode)
|
||||
#[arg(short, long)]
|
||||
update_bench: bool,
|
||||
|
||||
/// Dump the compiled AST
|
||||
#[arg(short, long)]
|
||||
dump: bool,
|
||||
|
||||
/// Run with TracingObserver enabled
|
||||
#[arg(short, long)]
|
||||
trace: bool,
|
||||
|
||||
/// Library directories to load before execution
|
||||
#[arg(short, long)]
|
||||
lib: Vec<PathBuf>,
|
||||
|
||||
/// Disable optimization
|
||||
#[arg(long)]
|
||||
no_opt: bool,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
let mut env = Environment::new();
|
||||
env.optimization = !cli.no_opt;
|
||||
|
||||
// Load libraries (Now just search paths)
|
||||
for lib_path in &cli.lib {
|
||||
env.add_search_path(lib_path);
|
||||
}
|
||||
|
||||
if cli.bench || cli.update_bench {
|
||||
if cfg!(debug_assertions) {
|
||||
eprintln!("❌ ERROR: Benchmarks must be run in Release mode!");
|
||||
eprintln!(" Use: cargo run --release --bin ast -- --bench");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
println!("🚀 Running benchmarks in RELEASE mode...\n");
|
||||
let filter = cli
|
||||
.file
|
||||
.as_ref()
|
||||
.and_then(|p| p.file_name())
|
||||
.map(|n| n.to_string_lossy().to_string());
|
||||
let results = tester::run_benchmarks(cli.update_bench, filter.as_deref());
|
||||
for res in results {
|
||||
let diff = res
|
||||
.diff_pct
|
||||
.map_or(String::new(), |d| format!(" ({:+.1}%)", d));
|
||||
println!("{}: {} - {:?}{}", res.status, res.name, res.median, diff);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Determine the source code to execute
|
||||
let source = if let Some(script_str) = cli.eval {
|
||||
Some(script_str)
|
||||
} else if let Some(file_path) = cli.file {
|
||||
if file_path.to_str() == Some("-") {
|
||||
read_stdin()
|
||||
} else {
|
||||
match fs::read_to_string(&file_path) {
|
||||
Ok(content) => Some(content),
|
||||
Err(e) => {
|
||||
eprintln!("Error reading file {:?}: {}", file_path, e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if !io::stdin().is_terminal() {
|
||||
read_stdin()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if let Some(content) = source {
|
||||
if cli.dump {
|
||||
match env.dump_ast(&content) {
|
||||
Ok(dump) => println!("{}", dump),
|
||||
Err(e) => eprintln!("Error dumping AST: {}", e),
|
||||
}
|
||||
} else if cli.trace {
|
||||
execute_trace(&env, &content);
|
||||
} else {
|
||||
execute(&env, &content);
|
||||
}
|
||||
} else {
|
||||
println!("MYC AST Compiler CLI. Use --help for usage.");
|
||||
}
|
||||
}
|
||||
|
||||
fn read_stdin() -> Option<String> {
|
||||
let mut buffer = String::new();
|
||||
match io::stdin().read_to_string(&mut buffer) {
|
||||
Ok(_) => Some(buffer),
|
||||
Err(e) => {
|
||||
eprintln!("Error reading from stdin: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn execute(env: &Environment, source: &str) {
|
||||
let result = env.compile(source);
|
||||
for diag in &result.diagnostics.items {
|
||||
let level = format!("{:?}", diag.level).to_uppercase();
|
||||
let loc = diag
|
||||
.identity
|
||||
.as_ref()
|
||||
.and_then(|id| id.location)
|
||||
.map(|l| format!(" at {}:{}", l.line, l.col))
|
||||
.unwrap_or_default();
|
||||
eprintln!("{}{} : {}", level, loc, diag.message);
|
||||
}
|
||||
|
||||
if result.diagnostics.has_errors() {
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
match env.run_script_compiled(result.ast.unwrap()) {
|
||||
Ok(res) => println!("{}", res),
|
||||
Err(e) => {
|
||||
eprintln!("Runtime Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn execute_trace(env: &Environment, source: &str) {
|
||||
match env.run_debug(source) {
|
||||
Ok((Ok(result), logs)) => {
|
||||
for line in logs {
|
||||
println!("{}", line);
|
||||
}
|
||||
println!("Result: {}", result);
|
||||
}
|
||||
Ok((Err(e), logs)) => {
|
||||
for line in logs {
|
||||
println!("{}", line);
|
||||
}
|
||||
eprintln!("Runtime Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Compilation Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::ast::environment::Environment;
|
||||
use crate::ast::nodes::UntypedKind;
|
||||
use crate::ast::nodes::SyntaxKind;
|
||||
use crate::ast::parser::Parser;
|
||||
use crate::ast::types::Value;
|
||||
|
||||
@@ -11,7 +11,7 @@ mod tests {
|
||||
let mut parser = Parser::new(source);
|
||||
let ast = parser.parse_expression();
|
||||
|
||||
if let UntypedKind::Constant(Value::Int(val)) = ast.kind {
|
||||
if let SyntaxKind::Constant(Value::Int(val)) = ast.kind {
|
||||
assert_eq!(val, 123);
|
||||
} else {
|
||||
panic!("Expected Integer constant, got {:?}", ast.kind);
|
||||
@@ -24,7 +24,7 @@ mod tests {
|
||||
let mut parser = Parser::new(source);
|
||||
let ast = parser.parse_expression();
|
||||
|
||||
if let UntypedKind::Constant(Value::Int(val)) = ast.kind {
|
||||
if let SyntaxKind::Constant(Value::Int(val)) = ast.kind {
|
||||
assert_eq!(val, -42);
|
||||
} else {
|
||||
panic!("Expected Integer constant, got {:?}", ast.kind);
|
||||
@@ -37,7 +37,7 @@ mod tests {
|
||||
let mut parser = Parser::new(source);
|
||||
let ast = parser.parse_expression();
|
||||
|
||||
if let UntypedKind::Constant(Value::Float(val)) = ast.kind {
|
||||
if let SyntaxKind::Constant(Value::Float(val)) = ast.kind {
|
||||
assert_eq!(val, 123.45);
|
||||
} else {
|
||||
panic!("Expected Float constant, got {:?}", ast.kind);
|
||||
@@ -50,7 +50,7 @@ mod tests {
|
||||
let mut parser = Parser::new(source);
|
||||
let ast = parser.parse_expression();
|
||||
|
||||
if let UntypedKind::Constant(Value::Float(val)) = ast.kind {
|
||||
if let SyntaxKind::Constant(Value::Float(val)) = ast.kind {
|
||||
assert_eq!(val, -10.5);
|
||||
} else {
|
||||
panic!("Expected Float constant, got {:?}", ast.kind);
|
||||
|
||||
+696
-696
File diff suppressed because it is too large
Load Diff
+337
-337
@@ -1,337 +1,337 @@
|
||||
use crate::ast::environment::Environment;
|
||||
use regex::Regex;
|
||||
use std::fs;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
pub struct TestResult {
|
||||
pub name: String,
|
||||
pub success: bool,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
pub struct BenchmarkResult {
|
||||
pub name: String,
|
||||
pub median: Duration,
|
||||
pub baseline: Option<Duration>,
|
||||
pub diff_pct: Option<f64>,
|
||||
pub status: String,
|
||||
}
|
||||
|
||||
pub fn run_functional_tests() -> Vec<TestResult> {
|
||||
run_functional_tests_with_optimization(false)
|
||||
}
|
||||
|
||||
pub fn run_functional_tests_with_optimization(enabled: bool) -> Vec<TestResult> {
|
||||
let mut results = Vec::new();
|
||||
let entries = fs::read_dir("examples").unwrap();
|
||||
let output_re = Regex::new(r";; Output: (.*)").unwrap();
|
||||
|
||||
for entry in entries.filter_map(|e| e.ok()) {
|
||||
let mut env = Environment::new(); // Fresh environment per test file
|
||||
env.optimization = enabled;
|
||||
let path = entry.path();
|
||||
if path.extension().is_some_and(|ext| ext == "myc") {
|
||||
let content = fs::read_to_string(&path).unwrap();
|
||||
let name = path.file_name().unwrap().to_string_lossy().to_string();
|
||||
|
||||
let expected_output = output_re
|
||||
.captures(&content)
|
||||
.map(|m| m.get(1).unwrap().as_str().trim().to_string());
|
||||
|
||||
if let Some(expected) = expected_output {
|
||||
match env.run_script(&content) {
|
||||
Ok(val) => {
|
||||
let val_str = format!("{}", val);
|
||||
if val_str == expected {
|
||||
results.push(TestResult {
|
||||
name,
|
||||
success: true,
|
||||
message: format!("OK: {}", val_str),
|
||||
});
|
||||
} else {
|
||||
results.push(TestResult {
|
||||
name,
|
||||
success: false,
|
||||
message: format!(
|
||||
"Opt {}: Expected {}, got {}",
|
||||
if enabled { "ON" } else { "OFF" },
|
||||
expected,
|
||||
val_str
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
Err(e) => results.push(TestResult {
|
||||
name,
|
||||
success: false,
|
||||
message: format!(
|
||||
"Opt {}: Error: {}",
|
||||
if enabled { "ON" } else { "OFF" },
|
||||
e
|
||||
),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
results
|
||||
}
|
||||
|
||||
pub fn run_benchmarks(update: bool, filter: Option<&str>) -> Vec<BenchmarkResult> {
|
||||
let mut results = Vec::new();
|
||||
let entries = fs::read_dir("examples").unwrap();
|
||||
let is_release = !cfg!(debug_assertions);
|
||||
let baseline_re = Regex::new(r";; Benchmark: ([\d\.]+\w+)").unwrap();
|
||||
let repeat_re = Regex::new(r";; Benchmark-Repeat: (\d+)").unwrap();
|
||||
|
||||
for entry in entries.filter_map(|e| e.ok()) {
|
||||
let path = entry.path();
|
||||
if path.extension().is_none_or(|ext| ext != "myc") {
|
||||
continue;
|
||||
}
|
||||
|
||||
let name = path.file_name().unwrap().to_string_lossy().to_string();
|
||||
if let Some(f) = filter
|
||||
&& name != f
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
let content = fs::read_to_string(&path).unwrap();
|
||||
|
||||
let baseline_match = baseline_re.captures(&content);
|
||||
if !update && baseline_match.is_none() {
|
||||
continue;
|
||||
}
|
||||
let repeat_match = repeat_re.captures(&content);
|
||||
|
||||
let mut repeats = repeat_match
|
||||
.and_then(|m| m.get(1))
|
||||
.and_then(|m| m.as_str().parse::<u32>().ok())
|
||||
.unwrap_or(1);
|
||||
|
||||
// Compile once for this file (symbols/types are compatible with all fresh environments)
|
||||
let env = Environment::new();
|
||||
let compiled_once = match env.compile(&content).into_result() {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: Duration::ZERO,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: format!("COMPILE ERROR: {}", e),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
// Helper to measure sum of VM execution times over N executions in one environment
|
||||
let measure_sum =
|
||||
|n: u32, node: &crate::ast::compiler::TypedNode| -> Result<Duration, String> {
|
||||
let env = Environment::new();
|
||||
|
||||
// Link once per sample
|
||||
let linked = env.link(node.clone());
|
||||
let func = env.instantiate(linked);
|
||||
let mut total = Duration::ZERO;
|
||||
for _ in 0..n {
|
||||
let start = Instant::now();
|
||||
let _ = (func.func)(&[]);
|
||||
total += start.elapsed();
|
||||
}
|
||||
Ok(total)
|
||||
};
|
||||
|
||||
if update {
|
||||
repeats = 1;
|
||||
loop {
|
||||
match measure_sum(repeats, &compiled_once) {
|
||||
Ok(total) => {
|
||||
if total >= Duration::from_millis(2) || repeats >= 100_000 {
|
||||
break;
|
||||
}
|
||||
let nanos = total.as_nanos().max(1) as f64;
|
||||
let factor = 2_000_000.0 / nanos;
|
||||
repeats = (repeats as f64 * factor).ceil() as u32;
|
||||
repeats = repeats.max(repeats + 1);
|
||||
}
|
||||
Err(e) => {
|
||||
results.push(BenchmarkResult {
|
||||
name: name.clone(),
|
||||
median: Duration::ZERO,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: format!("ERROR: {}", e),
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if results.last().is_some_and(|r| r.name == name) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let mut runs = Vec::new();
|
||||
let mut error = None;
|
||||
// Adaptive samples: High repeats need fewer samples for stable median
|
||||
let num_samples = if repeats > 1000 {
|
||||
10
|
||||
} else if repeats > 100 {
|
||||
30
|
||||
} else {
|
||||
100
|
||||
};
|
||||
|
||||
for _ in 0..num_samples {
|
||||
match measure_sum(repeats, &compiled_once) {
|
||||
Ok(d) => runs.push(d),
|
||||
Err(e) => {
|
||||
error = Some(e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(e) = error {
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: Duration::ZERO,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: format!("ERROR: {}", e),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
runs.sort();
|
||||
let median_total = runs[runs.len() / 2];
|
||||
let median_single = median_total / repeats;
|
||||
|
||||
if update {
|
||||
let new_val = format_duration(median_single);
|
||||
let mut updated_content = content.clone();
|
||||
|
||||
// 1. Update/Insert Benchmark
|
||||
let bench_line = format!(";; Benchmark: {}", new_val);
|
||||
if let Some(m) = baseline_match {
|
||||
updated_content = updated_content.replace(m.get(0).unwrap().as_str(), &bench_line);
|
||||
} else {
|
||||
updated_content = format!("{}\n{}", bench_line, updated_content);
|
||||
}
|
||||
|
||||
// 2. Update/Insert/Remove Benchmark-Repeat
|
||||
let repeat_line = if repeats > 1 {
|
||||
Some(format!(";; Benchmark-Repeat: {}", repeats))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let current_repeat_str = repeat_re
|
||||
.captures(&updated_content)
|
||||
.map(|m| m.get(0).unwrap().as_str().to_string());
|
||||
|
||||
if let Some(line) = repeat_line {
|
||||
if let Some(old_line) = current_repeat_str {
|
||||
updated_content = updated_content.replace(&old_line, &line);
|
||||
} else if let Some(m) = baseline_re.captures(&updated_content) {
|
||||
let b_str = m.get(0).unwrap().as_str().to_string();
|
||||
if let Some(pos) = updated_content.find(&b_str) {
|
||||
updated_content.insert_str(pos + b_str.len(), &format!("\n{}", line));
|
||||
}
|
||||
}
|
||||
} else if let Some(old_line) = current_repeat_str {
|
||||
updated_content = updated_content.replace(&format!("{}\n", old_line), "");
|
||||
updated_content = updated_content.replace(&old_line, "");
|
||||
}
|
||||
|
||||
fs::write(&path, updated_content).unwrap();
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: median_single,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: format!("UPDATED: {}", new_val),
|
||||
});
|
||||
} else if let Some(m) = baseline_match {
|
||||
let baseline_str = m.get(1).unwrap().as_str();
|
||||
let baseline = parse_duration(baseline_str).unwrap();
|
||||
|
||||
let diff = (median_single.as_nanos() as f64 / baseline.as_nanos() as f64) - 1.0;
|
||||
|
||||
let threshold = if is_release { 0.15 } else { 0.30 };
|
||||
let status = if median_single > baseline && diff > threshold {
|
||||
"FAILED"
|
||||
} else {
|
||||
"OK"
|
||||
};
|
||||
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: median_single,
|
||||
baseline: Some(baseline),
|
||||
diff_pct: Some(diff * 100.0),
|
||||
status: status.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
results
|
||||
}
|
||||
|
||||
fn format_duration(d: Duration) -> String {
|
||||
if d.as_nanos() < 1000 {
|
||||
format!("{}ns", d.as_nanos())
|
||||
} else if d.as_micros() < 1000 {
|
||||
format!("{:.1}us", d.as_nanos() as f64 / 1000.0)
|
||||
} else if d.as_millis() < 1000 {
|
||||
format!("{:.1}ms", d.as_micros() as f64 / 1000.0)
|
||||
} else {
|
||||
format!("{:.1}s", d.as_millis() as f64 / 1000.0)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_duration(s: &str) -> Option<Duration> {
|
||||
use std::sync::OnceLock;
|
||||
static RE: OnceLock<Regex> = OnceLock::new();
|
||||
let re = RE.get_or_init(|| Regex::new(r"([\d\.]+)(\w+)").unwrap());
|
||||
|
||||
let caps = re.captures(s)?;
|
||||
let val: f64 = caps[1].parse().ok()?;
|
||||
let unit = &caps[2];
|
||||
match unit {
|
||||
"ns" => Some(Duration::from_nanos(val as u64)),
|
||||
"us" => Some(Duration::from_nanos((val * 1000.0) as u64)),
|
||||
"ms" => Some(Duration::from_nanos((val * 1_000_000.0) as u64)),
|
||||
"s" => Some(Duration::from_nanos((val * 1_000_000_000.0) as u64)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn benchmark_regression_test() {
|
||||
use super::*;
|
||||
let results = run_benchmarks(false, None);
|
||||
let failures: Vec<_> = results.iter().filter(|r| r.status == "FAILED").collect();
|
||||
|
||||
if !failures.is_empty() {
|
||||
let error_msg = failures
|
||||
.iter()
|
||||
.map(|r| {
|
||||
format!(
|
||||
"{}: Median {:?}, Baseline {:?}, Diff {:.2}%",
|
||||
r.name,
|
||||
r.median,
|
||||
r.baseline.unwrap_or_default(),
|
||||
r.diff_pct.unwrap_or(0.0)
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
panic!("Performance regression detected:\n{}", error_msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
use crate::ast::environment::Environment;
|
||||
use regex::Regex;
|
||||
use std::fs;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
pub struct TestResult {
|
||||
pub name: String,
|
||||
pub success: bool,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
pub struct BenchmarkResult {
|
||||
pub name: String,
|
||||
pub median: Duration,
|
||||
pub baseline: Option<Duration>,
|
||||
pub diff_pct: Option<f64>,
|
||||
pub status: String,
|
||||
}
|
||||
|
||||
pub fn run_functional_tests() -> Vec<TestResult> {
|
||||
run_functional_tests_with_optimization(false)
|
||||
}
|
||||
|
||||
pub fn run_functional_tests_with_optimization(enabled: bool) -> Vec<TestResult> {
|
||||
let mut results = Vec::new();
|
||||
let entries = fs::read_dir("examples").unwrap();
|
||||
let output_re = Regex::new(r";; Output: (.*)").unwrap();
|
||||
|
||||
for entry in entries.filter_map(|e| e.ok()) {
|
||||
let mut env = Environment::new(); // Fresh environment per test file
|
||||
env.optimization = enabled;
|
||||
let path = entry.path();
|
||||
if path.extension().is_some_and(|ext| ext == "myc") {
|
||||
let content = fs::read_to_string(&path).unwrap();
|
||||
let name = path.file_name().unwrap().to_string_lossy().to_string();
|
||||
|
||||
let expected_output = output_re
|
||||
.captures(&content)
|
||||
.map(|m| m.get(1).unwrap().as_str().trim().to_string());
|
||||
|
||||
if let Some(expected) = expected_output {
|
||||
match env.run_script(&content) {
|
||||
Ok(val) => {
|
||||
let val_str = format!("{}", val);
|
||||
if val_str == expected {
|
||||
results.push(TestResult {
|
||||
name,
|
||||
success: true,
|
||||
message: format!("OK: {}", val_str),
|
||||
});
|
||||
} else {
|
||||
results.push(TestResult {
|
||||
name,
|
||||
success: false,
|
||||
message: format!(
|
||||
"Opt {}: Expected {}, got {}",
|
||||
if enabled { "ON" } else { "OFF" },
|
||||
expected,
|
||||
val_str
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
Err(e) => results.push(TestResult {
|
||||
name,
|
||||
success: false,
|
||||
message: format!(
|
||||
"Opt {}: Error: {}",
|
||||
if enabled { "ON" } else { "OFF" },
|
||||
e
|
||||
),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
results
|
||||
}
|
||||
|
||||
pub fn run_benchmarks(update: bool, filter: Option<&str>) -> Vec<BenchmarkResult> {
|
||||
let mut results = Vec::new();
|
||||
let entries = fs::read_dir("examples").unwrap();
|
||||
let is_release = !cfg!(debug_assertions);
|
||||
let baseline_re = Regex::new(r";; Benchmark: ([\d\.]+\w+)").unwrap();
|
||||
let repeat_re = Regex::new(r";; Benchmark-Repeat: (\d+)").unwrap();
|
||||
|
||||
for entry in entries.filter_map(|e| e.ok()) {
|
||||
let path = entry.path();
|
||||
if path.extension().is_none_or(|ext| ext != "myc") {
|
||||
continue;
|
||||
}
|
||||
|
||||
let name = path.file_name().unwrap().to_string_lossy().to_string();
|
||||
if let Some(f) = filter
|
||||
&& name != f
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
let content = fs::read_to_string(&path).unwrap();
|
||||
|
||||
let baseline_match = baseline_re.captures(&content);
|
||||
if !update && baseline_match.is_none() {
|
||||
continue;
|
||||
}
|
||||
let repeat_match = repeat_re.captures(&content);
|
||||
|
||||
let mut repeats = repeat_match
|
||||
.and_then(|m| m.get(1))
|
||||
.and_then(|m| m.as_str().parse::<u32>().ok())
|
||||
.unwrap_or(1);
|
||||
|
||||
// Compile once for this file (symbols/types are compatible with all fresh environments)
|
||||
let env = Environment::new();
|
||||
let compiled_once = match env.compile(&content).into_result() {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: Duration::ZERO,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: format!("COMPILE ERROR: {}", e),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
// Helper to measure sum of VM execution times over N executions in one environment
|
||||
let measure_sum =
|
||||
|n: u32, node: &crate::ast::compiler::TypedNode| -> Result<Duration, String> {
|
||||
let env = Environment::new();
|
||||
|
||||
// Link once per sample
|
||||
let linked = env.link(node.clone());
|
||||
let func = env.instantiate(linked);
|
||||
let mut total = Duration::ZERO;
|
||||
for _ in 0..n {
|
||||
let start = Instant::now();
|
||||
let _ = (func.func)(&[]);
|
||||
total += start.elapsed();
|
||||
}
|
||||
Ok(total)
|
||||
};
|
||||
|
||||
if update {
|
||||
repeats = 1;
|
||||
loop {
|
||||
match measure_sum(repeats, &compiled_once) {
|
||||
Ok(total) => {
|
||||
if total >= Duration::from_millis(2) || repeats >= 100_000 {
|
||||
break;
|
||||
}
|
||||
let nanos = total.as_nanos().max(1) as f64;
|
||||
let factor = 2_000_000.0 / nanos;
|
||||
repeats = (repeats as f64 * factor).ceil() as u32;
|
||||
repeats = repeats.max(repeats + 1);
|
||||
}
|
||||
Err(e) => {
|
||||
results.push(BenchmarkResult {
|
||||
name: name.clone(),
|
||||
median: Duration::ZERO,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: format!("ERROR: {}", e),
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if results.last().is_some_and(|r| r.name == name) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let mut runs = Vec::new();
|
||||
let mut error = None;
|
||||
// Adaptive samples: High repeats need fewer samples for stable median
|
||||
let num_samples = if repeats > 1000 {
|
||||
10
|
||||
} else if repeats > 100 {
|
||||
30
|
||||
} else {
|
||||
100
|
||||
};
|
||||
|
||||
for _ in 0..num_samples {
|
||||
match measure_sum(repeats, &compiled_once) {
|
||||
Ok(d) => runs.push(d),
|
||||
Err(e) => {
|
||||
error = Some(e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(e) = error {
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: Duration::ZERO,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: format!("ERROR: {}", e),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
runs.sort();
|
||||
let median_total = runs[runs.len() / 2];
|
||||
let median_single = median_total / repeats;
|
||||
|
||||
if update {
|
||||
let new_val = format_duration(median_single);
|
||||
let mut updated_content = content.clone();
|
||||
|
||||
// 1. Update/Insert Benchmark
|
||||
let bench_line = format!(";; Benchmark: {}", new_val);
|
||||
if let Some(m) = baseline_match {
|
||||
updated_content = updated_content.replace(m.get(0).unwrap().as_str(), &bench_line);
|
||||
} else {
|
||||
updated_content = format!("{}\n{}", bench_line, updated_content);
|
||||
}
|
||||
|
||||
// 2. Update/Insert/Remove Benchmark-Repeat
|
||||
let repeat_line = if repeats > 1 {
|
||||
Some(format!(";; Benchmark-Repeat: {}", repeats))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let current_repeat_str = repeat_re
|
||||
.captures(&updated_content)
|
||||
.map(|m| m.get(0).unwrap().as_str().to_string());
|
||||
|
||||
if let Some(line) = repeat_line {
|
||||
if let Some(old_line) = current_repeat_str {
|
||||
updated_content = updated_content.replace(&old_line, &line);
|
||||
} else if let Some(m) = baseline_re.captures(&updated_content) {
|
||||
let b_str = m.get(0).unwrap().as_str().to_string();
|
||||
if let Some(pos) = updated_content.find(&b_str) {
|
||||
updated_content.insert_str(pos + b_str.len(), &format!("\n{}", line));
|
||||
}
|
||||
}
|
||||
} else if let Some(old_line) = current_repeat_str {
|
||||
updated_content = updated_content.replace(&format!("{}\n", old_line), "");
|
||||
updated_content = updated_content.replace(&old_line, "");
|
||||
}
|
||||
|
||||
fs::write(&path, updated_content).unwrap();
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: median_single,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: format!("UPDATED: {}", new_val),
|
||||
});
|
||||
} else if let Some(m) = baseline_match {
|
||||
let baseline_str = m.get(1).unwrap().as_str();
|
||||
let baseline = parse_duration(baseline_str).unwrap();
|
||||
|
||||
let diff = (median_single.as_nanos() as f64 / baseline.as_nanos() as f64) - 1.0;
|
||||
|
||||
let threshold = if is_release { 0.15 } else { 0.30 };
|
||||
let status = if median_single > baseline && diff > threshold {
|
||||
"FAILED"
|
||||
} else {
|
||||
"OK"
|
||||
};
|
||||
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: median_single,
|
||||
baseline: Some(baseline),
|
||||
diff_pct: Some(diff * 100.0),
|
||||
status: status.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
results
|
||||
}
|
||||
|
||||
fn format_duration(d: Duration) -> String {
|
||||
if d.as_nanos() < 1000 {
|
||||
format!("{}ns", d.as_nanos())
|
||||
} else if d.as_micros() < 1000 {
|
||||
format!("{:.1}us", d.as_nanos() as f64 / 1000.0)
|
||||
} else if d.as_millis() < 1000 {
|
||||
format!("{:.1}ms", d.as_micros() as f64 / 1000.0)
|
||||
} else {
|
||||
format!("{:.1}s", d.as_millis() as f64 / 1000.0)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_duration(s: &str) -> Option<Duration> {
|
||||
use std::sync::OnceLock;
|
||||
static RE: OnceLock<Regex> = OnceLock::new();
|
||||
let re = RE.get_or_init(|| Regex::new(r"([\d\.]+)(\w+)").unwrap());
|
||||
|
||||
let caps = re.captures(s)?;
|
||||
let val: f64 = caps[1].parse().ok()?;
|
||||
let unit = &caps[2];
|
||||
match unit {
|
||||
"ns" => Some(Duration::from_nanos(val as u64)),
|
||||
"us" => Some(Duration::from_nanos((val * 1000.0) as u64)),
|
||||
"ms" => Some(Duration::from_nanos((val * 1_000_000.0) as u64)),
|
||||
"s" => Some(Duration::from_nanos((val * 1_000_000_000.0) as u64)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn benchmark_regression_test() {
|
||||
use super::*;
|
||||
let results = run_benchmarks(false, None);
|
||||
let failures: Vec<_> = results.iter().filter(|r| r.status == "FAILED").collect();
|
||||
|
||||
if !failures.is_empty() {
|
||||
let error_msg = failures
|
||||
.iter()
|
||||
.map(|r| {
|
||||
format!(
|
||||
"{}: Median {:?}, Baseline {:?}, Diff {:.2}%",
|
||||
r.name,
|
||||
r.median,
|
||||
r.baseline.unwrap_or_default(),
|
||||
r.diff_pct.unwrap_or(0.0)
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
panic!("Performance regression detected:\n{}", error_msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user