Refactor: Replace UntypedNode with SyntaxNode

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

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

No functional changes are introduced by this refactoring; it is purely a
renaming and organizational update.
This commit is contained in:
Michael Schimmel
2026-03-13 14:21:28 +01:00
parent 7d72a99fa1
commit 84226f6a16
31 changed files with 8611 additions and 8611 deletions
+42 -42
View File
@@ -2,7 +2,7 @@ use crate::ast::compiler::bound_nodes::{
Address, BoundKind, DeclarationKind, GlobalIdx, LocalSlot, Node, UpvalueIdx, Address, BoundKind, DeclarationKind, GlobalIdx, LocalSlot, Node, UpvalueIdx,
}; };
use crate::ast::diagnostics::Diagnostics; 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 crate::ast::types::{Identity, StaticType, Purity};
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
@@ -152,7 +152,7 @@ impl Binder {
initial_scopes: Vec<CompilerScope>, initial_scopes: Vec<CompilerScope>,
initial_slot_count: u32, initial_slot_count: u32,
fixed_scope_idx: i32, fixed_scope_idx: i32,
node: &UntypedNode, node: &SyntaxNode,
diagnostics: &mut Diagnostics, diagnostics: &mut Diagnostics,
) -> Result<BindingResult, String> { ) -> Result<BindingResult, String> {
let mut binder = Self::new(initial_scopes, initial_slot_count, fixed_scope_idx); let mut binder = Self::new(initial_scopes, initial_slot_count, fixed_scope_idx);
@@ -208,17 +208,17 @@ impl Binder {
pub fn bind( pub fn bind(
&mut self, &mut self,
node: &UntypedNode, node: &SyntaxNode,
ctx: ExprContext, ctx: ExprContext,
diag: &mut Diagnostics, diag: &mut Diagnostics,
) -> Node { ) -> Node {
match &node.kind { match &node.kind {
UntypedKind::Nop => self.make_node(node.identity.clone(), BoundKind::Nop), SyntaxKind::Nop => self.make_node(node.identity.clone(), BoundKind::Nop),
UntypedKind::Constant(v) => { SyntaxKind::Constant(v) => {
self.make_node(node.identity.clone(), BoundKind::Constant(v.clone())) 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) { if let Some(addr) = self.resolve_variable(sym, diag, &node.identity) {
self.make_node( self.make_node(
node.identity.clone(), node.identity.clone(),
@@ -232,11 +232,11 @@ impl Binder {
} }
} }
UntypedKind::FieldAccessor(k) => { SyntaxKind::FieldAccessor(k) => {
self.make_node(node.identity.clone(), BoundKind::FieldAccessor(*k)) self.make_node(node.identity.clone(), BoundKind::FieldAccessor(*k))
} }
UntypedKind::If { SyntaxKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
@@ -264,14 +264,14 @@ impl Binder {
) )
} }
UntypedKind::Def { target, value } => { SyntaxKind::Def { target, value } => {
if ctx == ExprContext::Expression { if ctx == ExprContext::Expression {
diag.push_error( diag.push_error(
"Statement 'def' cannot be used as an expression.", "Statement 'def' cannot be used as an expression.",
Some(node.identity.clone()), 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( let addr_opt = self.declare_variable(
name, name,
node.identity.clone(), 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); 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) { if let Some(addr) = self.resolve_variable(sym, diag, &target.identity) {
self.make_node( self.make_node(
node.identity.clone(), 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()); let mut bound_inputs = Vec::with_capacity(inputs.len());
for input in inputs { for input in inputs {
bound_inputs.push(Rc::new(self.bind(input, ExprContext::Expression, diag))); 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(); let identity = node.identity.clone();
self.functions self.functions
.push(FunctionCompiler::new(identity.clone(), vec![], 0)); .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 callee = self.bind(callee, ExprContext::Expression, diag);
let args = self.bind(args.as_ref(), 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 { if self.functions.len() <= 1 {
diag.push_error( diag.push_error(
"'again' is only allowed inside a function or lambda.", "'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(); self.functions.last_mut().unwrap().push_scope();
let mut bound_exprs = Vec::new(); let mut bound_exprs = Vec::new();
for (i, expr) in exprs.iter().enumerate() { 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(); let mut bound_elems = Vec::new();
for e in elements { for e in elements {
bound_elems.push(Rc::new(self.bind(e, ExprContext::Expression, diag))); 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 bound_values = Vec::new();
let mut layout_fields = 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); let bound_expanded = self.bind(expanded.as_ref(), ctx, diag);
self.make_node( self.make_node(
node.identity.clone(), node.identity.clone(),
@@ -499,10 +499,10 @@ impl Binder {
) )
} }
UntypedKind::Template(_) SyntaxKind::Template(_)
| UntypedKind::Placeholder(_) | SyntaxKind::Placeholder(_)
| UntypedKind::Splice(_) | SyntaxKind::Splice(_)
| UntypedKind::MacroDecl { .. } => { | SyntaxKind::MacroDecl { .. } => {
diag.push_error( diag.push_error(
format!( format!(
"Macro construct {:?} found in Binder. Macros must be expanded before binding.", "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) self.make_node(node.identity.clone(), BoundKind::Error)
} }
UntypedKind::Extension(_) => { SyntaxKind::Extension(_) => {
diag.push_error( diag.push_error(
"Custom extensions not supported in Binder yet", "Custom extensions not supported in Binder yet",
Some(node.identity.clone()), Some(node.identity.clone()),
); );
self.make_node(node.identity.clone(), BoundKind::Error) 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(), identity: node.identity.clone(),
kind: crate::ast::compiler::bound_nodes::BoundKind::Error, kind: crate::ast::compiler::bound_nodes::BoundKind::Error,
ty: (), ty: (),
@@ -600,12 +600,12 @@ impl Binder {
fn bind_pattern( fn bind_pattern(
&mut self, &mut self,
node: &UntypedNode, node: &SyntaxNode,
kind: DeclarationKind, kind: DeclarationKind,
diag: &mut Diagnostics, diag: &mut Diagnostics,
) -> Node { ) -> Node {
match &node.kind { match &node.kind {
UntypedKind::Identifier(sym) => { SyntaxKind::Identifier(sym) => {
if let Some(addr) = self.declare_variable(sym, node.identity.clone(), kind, diag) { if let Some(addr) = self.declare_variable(sym, node.identity.clone(), kind, diag) {
self.make_node( self.make_node(
node.identity.clone(), node.identity.clone(),
@@ -621,7 +621,7 @@ impl Binder {
self.make_node(node.identity.clone(), BoundKind::Error) self.make_node(node.identity.clone(), BoundKind::Error)
} }
} }
UntypedKind::Tuple { elements } => { SyntaxKind::Tuple { elements } => {
let mut bound_elems = Vec::new(); let mut bound_elems = Vec::new();
for e in elements { for e in elements {
bound_elems.push(Rc::new(self.bind_pattern(e, kind, diag))); bound_elems.push(Rc::new(self.bind_pattern(e, kind, diag)));
@@ -645,11 +645,11 @@ impl Binder {
fn bind_assign_pattern( fn bind_assign_pattern(
&mut self, &mut self,
node: &UntypedNode, node: &SyntaxNode,
diag: &mut Diagnostics, diag: &mut Diagnostics,
) -> Node { ) -> Node {
match &node.kind { match &node.kind {
UntypedKind::Identifier(sym) => { SyntaxKind::Identifier(sym) => {
if let Some(addr) = self.resolve_variable(sym, diag, &node.identity) { if let Some(addr) = self.resolve_variable(sym, diag, &node.identity) {
self.make_node( self.make_node(
node.identity.clone(), node.identity.clone(),
@@ -662,7 +662,7 @@ impl Binder {
self.make_node(node.identity.clone(), BoundKind::Error) self.make_node(node.identity.clone(), BoundKind::Error)
} }
} }
UntypedKind::Tuple { elements } => { SyntaxKind::Tuple { elements } => {
let mut bound_elems = Vec::new(); let mut bound_elems = Vec::new();
for e in elements { for e in elements {
bound_elems.push(Rc::new(self.bind_assign_pattern(e, diag))); bound_elems.push(Rc::new(self.bind_assign_pattern(e, diag)));
@@ -703,10 +703,10 @@ mod tests {
fn test_upvalue_capture_sets_is_boxed() { fn test_upvalue_capture_sets_is_boxed() {
let source = "(fn [] (do (def x 10) (def f (fn [] x)) x))"; let source = "(fn [] (do (def x 10) (def f (fn [] x)) x))";
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let untyped = parser.parse_expression(); let syntax = parser.parse_expression();
let mut diagnostics = Diagnostics::new(); 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::Lambda { body, .. } = &bound.kind {
if let BoundKind::Block { exprs } = &body.kind { if let BoundKind::Block { exprs } = &body.kind {
@@ -732,10 +732,10 @@ mod tests {
fn test_no_capture_not_boxed() { fn test_no_capture_not_boxed() {
let source = "(fn [] (do (def x 10) x))"; let source = "(fn [] (do (def x 10) x))";
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let untyped = parser.parse_expression(); let syntax = parser.parse_expression();
let mut diagnostics = Diagnostics::new(); 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::Lambda { body, .. } = &bound.kind {
if let BoundKind::Block { exprs } = &body.kind { if let BoundKind::Block { exprs } = &body.kind {
@@ -761,10 +761,10 @@ mod tests {
fn test_redefinition_error() { fn test_redefinition_error() {
let source = "(do (def x 1) (def x 2) x)"; let source = "(do (def x 1) (def x 2) x)";
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let untyped = parser.parse_expression(); let syntax = parser.parse_expression();
let mut diagnostics = Diagnostics::new(); 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.has_errors());
assert!(diagnostics.items.iter().any(|i| i.message.contains("already defined"))); assert!(diagnostics.items.iter().any(|i| i.message.contains("already defined")));
@@ -773,15 +773,15 @@ mod tests {
#[test] #[test]
fn test_repro_global_redefinition() { fn test_repro_global_redefinition() {
let source1 = "(def x 1) 1"; 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 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 source2 = "(def x 2) 2";
let untyped2 = Parser::new(source2).parse_expression(); let syntax2 = Parser::new(source2).parse_expression();
let mut diagnostics2 = Diagnostics::new(); let mut diagnostics2 = Diagnostics::new();
// Here we simulate frozen scope by passing fixed_scope_idx = 0 // 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.has_errors());
assert!(diagnostics2.items.iter().any(|i| i.message.contains("frozen/immutable"))); assert!(diagnostics2.items.iter().any(|i| i.message.contains("frozen/immutable")));
+352 -352
View File
@@ -1,352 +1,352 @@
use crate::ast::nodes::Symbol; use crate::ast::nodes::Symbol;
use crate::ast::types::{Identity, StaticType, Value}; use crate::ast::types::{Identity, StaticType, Value};
use std::rc::Rc; use std::rc::Rc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct LocalSlot(pub u32); pub struct LocalSlot(pub u32);
impl std::fmt::Display for LocalSlot { impl std::fmt::Display for LocalSlot {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "L{}", self.0) write!(f, "L{}", self.0)
} }
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct UpvalueIdx(pub u32); pub struct UpvalueIdx(pub u32);
impl std::fmt::Display for UpvalueIdx { impl std::fmt::Display for UpvalueIdx {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "U{}", self.0) write!(f, "U{}", self.0)
} }
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct GlobalIdx(pub u32); pub struct GlobalIdx(pub u32);
impl std::fmt::Display for GlobalIdx { impl std::fmt::Display for GlobalIdx {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "G{}", self.0) write!(f, "G{}", self.0)
} }
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Address { pub enum Address {
Local(LocalSlot), // Stack-Slot index (relative to frame base) Local(LocalSlot), // Stack-Slot index (relative to frame base)
Upvalue(UpvalueIdx), // Index in the closure's upvalue array Upvalue(UpvalueIdx), // Index in the closure's upvalue array
Global(GlobalIdx), // Index in the global environment vector Global(GlobalIdx), // Index in the global environment vector
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DeclarationKind { pub enum DeclarationKind {
Variable, Variable,
Parameter, Parameter,
} }
/// Trait for DSL-specific AST extensions (like Pipe, Series-Access, etc.) /// Trait for DSL-specific AST extensions (like Pipe, Series-Access, etc.)
pub trait BoundExtension<T>: std::fmt::Debug { pub trait BoundExtension<T>: std::fmt::Debug {
fn clone_box(&self) -> Box<dyn BoundExtension<T>>; fn clone_box(&self) -> Box<dyn BoundExtension<T>>;
fn display_name(&self) -> String; fn display_name(&self) -> String;
} }
impl<T> Clone for Box<dyn BoundExtension<T>> { impl<T> Clone for Box<dyn BoundExtension<T>> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
self.clone_box() self.clone_box()
} }
} }
/// A bound AST node, decorated with type or metric information T. /// A bound AST node, decorated with type or metric information T.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct Node<T = ()> { pub struct Node<T = ()> {
pub identity: Identity, pub identity: Identity,
pub kind: BoundKind<T>, pub kind: BoundKind<T>,
pub ty: T, pub ty: T,
} }
/// Type alias for a node that has been fully type-checked. /// Type alias for a node that has been fully type-checked.
pub type TypedNode = Node<StaticType>; pub type TypedNode = Node<StaticType>;
/// Type alias for the global function registry. /// Type alias for the global function registry.
pub type GlobalFunctionRegistry = std::collections::HashMap<GlobalIdx, Rc<Node>>; pub type GlobalFunctionRegistry = std::collections::HashMap<GlobalIdx, Rc<Node>>;
/// Metrics collected during the analysis phase. /// Metrics collected during the analysis phase.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct NodeMetrics { pub struct NodeMetrics {
pub original: Rc<TypedNode>, pub original: Rc<TypedNode>,
pub purity: crate::ast::types::Purity, pub purity: crate::ast::types::Purity,
pub is_recursive: bool, pub is_recursive: bool,
} }
/// Type alias for a node that has been analyzed. /// Type alias for a node that has been analyzed.
pub type AnalyzedNode = Node<NodeMetrics>; pub type AnalyzedNode = Node<NodeMetrics>;
/// Type alias for the global analyzed function registry. /// Type alias for the global analyzed function registry.
pub type GlobalAnalyzedRegistry = std::collections::HashMap<GlobalIdx, Rc<AnalyzedNode>>; pub type GlobalAnalyzedRegistry = std::collections::HashMap<GlobalIdx, Rc<AnalyzedNode>>;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum BoundKind<T = ()> { pub enum BoundKind<T = ()> {
Nop, Nop,
Constant(Value), Constant(Value),
// Variable Access (Resolved) // Variable Access (Resolved)
Get { Get {
addr: Address, addr: Address,
name: Symbol, name: Symbol,
}, },
// Variable Update (Assignment) // Variable Update (Assignment)
Set { Set {
addr: Address, addr: Address,
value: Rc<Node<T>>, value: Rc<Node<T>>,
}, },
// Variable Declaration (Unified for Local/Global/Parameter) // Variable Declaration (Unified for Local/Global/Parameter)
Define { Define {
name: Symbol, name: Symbol,
addr: Address, addr: Address,
kind: DeclarationKind, kind: DeclarationKind,
value: Rc<Node<T>>, value: Rc<Node<T>>,
captured_by: Vec<Identity>, captured_by: Vec<Identity>,
}, },
/// A first-class field accessor (e.g. .name) /// A first-class field accessor (e.g. .name)
FieldAccessor(crate::ast::types::Keyword), FieldAccessor(crate::ast::types::Keyword),
/// Specialized field access (O(1) via RecordLayout) /// Specialized field access (O(1) via RecordLayout)
GetField { GetField {
rec: Rc<Node<T>>, rec: Rc<Node<T>>,
field: crate::ast::types::Keyword, field: crate::ast::types::Keyword,
}, },
If { If {
cond: Rc<Node<T>>, cond: Rc<Node<T>>,
then_br: Rc<Node<T>>, then_br: Rc<Node<T>>,
else_br: Option<Rc<Node<T>>>, else_br: Option<Rc<Node<T>>>,
}, },
/// A destructuring operation (can be a definition or an assignment depending on the pattern) /// A destructuring operation (can be a definition or an assignment depending on the pattern)
Destructure { Destructure {
pattern: Rc<Node<T>>, pattern: Rc<Node<T>>,
value: Rc<Node<T>>, value: Rc<Node<T>>,
}, },
Lambda { Lambda {
params: Rc<Node<T>>, params: Rc<Node<T>>,
// The list of variables captured from enclosing scopes // The list of variables captured from enclosing scopes
upvalues: Vec<Address>, upvalues: Vec<Address>,
body: Rc<Node<T>>, body: Rc<Node<T>>,
/// Static optimization: number of positional parameters if the pattern is flat. /// Static optimization: number of positional parameters if the pattern is flat.
positional_count: Option<u32>, positional_count: Option<u32>,
}, },
Call { Call {
callee: Rc<Node<T>>, callee: Rc<Node<T>>,
args: Rc<Node<T>>, args: Rc<Node<T>>,
}, },
Again { Again {
args: Rc<Node<T>>, args: Rc<Node<T>>,
}, },
Pipe { Pipe {
inputs: Vec<Rc<Node<T>>>, inputs: Vec<Rc<Node<T>>>,
lambda: Rc<Node<T>>, lambda: Rc<Node<T>>,
out_type: crate::ast::types::StaticType, out_type: crate::ast::types::StaticType,
}, },
Block { Block {
exprs: Vec<Rc<Node<T>>>, exprs: Vec<Rc<Node<T>>>,
}, },
Tuple { Tuple {
elements: Vec<Rc<Node<T>>>, elements: Vec<Rc<Node<T>>>,
}, },
Record { Record {
layout: std::sync::Arc<crate::ast::types::RecordLayout>, layout: std::sync::Arc<crate::ast::types::RecordLayout>,
values: Vec<Rc<Node<T>>>, values: Vec<Rc<Node<T>>>,
}, },
/// An expanded macro call, preserving the original call for debugging and UI. /// An expanded macro call, preserving the original call for debugging and UI.
Expansion { Expansion {
/// The original call from the untyped AST. /// The original call from the syntax AST.
original_call: Rc<crate::ast::nodes::UntypedNode>, original_call: Rc<crate::ast::nodes::SyntaxNode>,
/// The result of binding the expanded AST. /// The result of binding the expanded AST.
bound_expanded: Rc<Node<T>>, bound_expanded: Rc<Node<T>>,
}, },
/// A diagnostic poison node, allowing compilation to continue after an error. /// A diagnostic poison node, allowing compilation to continue after an error.
Error, Error,
/// DSL-specific extension slot /// DSL-specific extension slot
Extension(Box<dyn BoundExtension<T>>), Extension(Box<dyn BoundExtension<T>>),
} }
impl<T> PartialEq for BoundKind<T> impl<T> PartialEq for BoundKind<T>
where where
T: PartialEq, T: PartialEq,
{ {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
match (self, other) { match (self, other) {
(BoundKind::Nop, BoundKind::Nop) => true, (BoundKind::Nop, BoundKind::Nop) => true,
(BoundKind::Constant(a), BoundKind::Constant(b)) => a == b, (BoundKind::Constant(a), BoundKind::Constant(b)) => a == b,
(BoundKind::Get { addr: aa, name: na }, BoundKind::Get { addr: ab, name: nb }) => { (BoundKind::Get { addr: aa, name: na }, BoundKind::Get { addr: ab, name: nb }) => {
aa == ab && na == nb aa == ab && na == nb
} }
( (
BoundKind::Set { BoundKind::Set {
addr: aa, addr: aa,
value: va, value: va,
}, },
BoundKind::Set { BoundKind::Set {
addr: ab, addr: ab,
value: vb, value: vb,
}, },
) => aa == ab && Rc::ptr_eq(va, vb), ) => aa == ab && Rc::ptr_eq(va, vb),
( (
BoundKind::Define { BoundKind::Define {
name: na, name: na,
addr: aa, addr: aa,
kind: ka, kind: ka,
value: va, value: va,
captured_by: ca, captured_by: ca,
}, },
BoundKind::Define { BoundKind::Define {
name: nb, name: nb,
addr: ab, addr: ab,
kind: kb, kind: kb,
value: vb, value: vb,
captured_by: cb, captured_by: cb,
}, },
) => na == nb && aa == ab && ka == kb && Rc::ptr_eq(va, vb) && ca == cb, ) => na == nb && aa == ab && ka == kb && Rc::ptr_eq(va, vb) && ca == cb,
(BoundKind::FieldAccessor(a), BoundKind::FieldAccessor(b)) => a == b, (BoundKind::FieldAccessor(a), BoundKind::FieldAccessor(b)) => a == b,
( (
BoundKind::GetField { rec: ra, field: fa }, BoundKind::GetField { rec: ra, field: fa },
BoundKind::GetField { rec: rb, field: fb }, BoundKind::GetField { rec: rb, field: fb },
) => Rc::ptr_eq(ra, rb) && fa == fb, ) => Rc::ptr_eq(ra, rb) && fa == fb,
( (
BoundKind::If { BoundKind::If {
cond: ca, cond: ca,
then_br: ta, then_br: ta,
else_br: ea, else_br: ea,
}, },
BoundKind::If { BoundKind::If {
cond: cb, cond: cb,
then_br: tb, then_br: tb,
else_br: eb, else_br: eb,
}, },
) => Rc::ptr_eq(ca, cb) && Rc::ptr_eq(ta, tb) && match (ea, eb) { ) => Rc::ptr_eq(ca, cb) && Rc::ptr_eq(ta, tb) && match (ea, eb) {
(Some(a), Some(b)) => Rc::ptr_eq(a, b), (Some(a), Some(b)) => Rc::ptr_eq(a, b),
(None, None) => true, (None, None) => true,
_ => false, _ => false,
}, },
( (
BoundKind::Destructure { BoundKind::Destructure {
pattern: pa, pattern: pa,
value: va, value: va,
}, },
BoundKind::Destructure { BoundKind::Destructure {
pattern: pb, pattern: pb,
value: vb, value: vb,
}, },
) => Rc::ptr_eq(pa, pb) && Rc::ptr_eq(va, vb), ) => Rc::ptr_eq(pa, pb) && Rc::ptr_eq(va, vb),
( (
BoundKind::Lambda { BoundKind::Lambda {
params: pa, params: pa,
upvalues: ua, upvalues: ua,
body: ba, body: ba,
positional_count: pca, positional_count: pca,
}, },
BoundKind::Lambda { BoundKind::Lambda {
params: pb, params: pb,
upvalues: ub, upvalues: ub,
body: bb, body: bb,
positional_count: pcb, positional_count: pcb,
}, },
) => Rc::ptr_eq(pa, pb) && ua == ub && Rc::ptr_eq(ba, bb) && pca == pcb, ) => Rc::ptr_eq(pa, pb) && ua == ub && Rc::ptr_eq(ba, bb) && pca == pcb,
( (
BoundKind::Call { BoundKind::Call {
callee: ca, callee: ca,
args: aa, args: aa,
}, },
BoundKind::Call { BoundKind::Call {
callee: cb, callee: cb,
args: ab, args: ab,
}, },
) => Rc::ptr_eq(ca, cb) && Rc::ptr_eq(aa, 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::Again { args: aa }, BoundKind::Again { args: ab }) => Rc::ptr_eq(aa, ab),
(BoundKind::Block { exprs: ea }, BoundKind::Block { exprs: eb }) => { (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)) 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 }) => { (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)) ea.len() == eb.len() && ea.iter().zip(eb.iter()).all(|(a, b)| Rc::ptr_eq(a, b))
} }
( (
BoundKind::Record { BoundKind::Record {
layout: la, layout: la,
values: va, values: va,
}, },
BoundKind::Record { BoundKind::Record {
layout: lb, layout: lb,
values: vb, 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)), ) => 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 { BoundKind::Expansion {
original_call: ca, original_call: ca,
bound_expanded: ea, bound_expanded: ea,
}, },
BoundKind::Expansion { BoundKind::Expansion {
original_call: cb, original_call: cb,
bound_expanded: eb, bound_expanded: eb,
}, },
) => Rc::ptr_eq(ca, cb) && Rc::ptr_eq(ea, eb), ) => Rc::ptr_eq(ca, cb) && Rc::ptr_eq(ea, eb),
(BoundKind::Error, BoundKind::Error) => true, (BoundKind::Error, BoundKind::Error) => true,
(BoundKind::Extension(_), BoundKind::Extension(_)) => false, (BoundKind::Extension(_), BoundKind::Extension(_)) => false,
_ => false, _ => false,
} }
} }
} }
/// A single field in a Record literal (Key-Value pair) /// A single field in a Record literal (Key-Value pair)
pub type RecordField<T> = (Node<T>, Node<T>); pub type RecordField<T> = (Node<T>, Node<T>);
impl<T> BoundKind<T> { impl<T> BoundKind<T> {
pub fn display_name(&self) -> String { pub fn display_name(&self) -> String {
match self { match self {
BoundKind::Nop => "NOP".to_string(), BoundKind::Nop => "NOP".to_string(),
BoundKind::Constant(v) => format!("CONST({})", v), BoundKind::Constant(v) => format!("CONST({})", v),
BoundKind::Get { addr, name } => format!("GET({}, {:?})", name.name, addr), BoundKind::Get { addr, name } => format!("GET({}, {:?})", name.name, addr),
BoundKind::Set { addr, .. } => format!("SET({:?})", addr), BoundKind::Set { addr, .. } => format!("SET({:?})", addr),
BoundKind::Define { BoundKind::Define {
name, addr, kind, .. name, addr, kind, ..
} => { } => {
let k_str = match kind { let k_str = match kind {
DeclarationKind::Variable => "VAR", DeclarationKind::Variable => "VAR",
DeclarationKind::Parameter => "PARAM", DeclarationKind::Parameter => "PARAM",
}; };
format!("DEF_{}({}, {:?})", k_str, name.name, addr) format!("DEF_{}({}, {:?})", k_str, name.name, addr)
} }
BoundKind::FieldAccessor(k) => format!("FIELD_ACCESSOR(.{})", k.name()), BoundKind::FieldAccessor(k) => format!("FIELD_ACCESSOR(.{})", k.name()),
BoundKind::GetField { field, .. } => format!("GET_FIELD(.{})", field.name()), BoundKind::GetField { field, .. } => format!("GET_FIELD(.{})", field.name()),
BoundKind::If { .. } => "IF".to_string(), BoundKind::If { .. } => "IF".to_string(),
BoundKind::Destructure { .. } => "DESTRUCTURE".to_string(), BoundKind::Destructure { .. } => "DESTRUCTURE".to_string(),
BoundKind::Lambda { BoundKind::Lambda {
params, upvalues, .. params, upvalues, ..
} => { } => {
let p_str = match &params.kind { let p_str = match &params.kind {
BoundKind::Tuple { elements } => format!("p:{}", elements.len()), BoundKind::Tuple { elements } => format!("p:{}", elements.len()),
_ => "p:1".to_string(), _ => "p:1".to_string(),
}; };
format!("LAMBDA({}, Captures:{})", p_str, upvalues.len()) format!("LAMBDA({}, Captures:{})", p_str, upvalues.len())
} }
BoundKind::Call { .. } => "CALL".to_string(), BoundKind::Call { .. } => "CALL".to_string(),
BoundKind::Again { .. } => "AGAIN".to_string(), BoundKind::Again { .. } => "AGAIN".to_string(),
BoundKind::Pipe { .. } => "PIPE".to_string(), BoundKind::Pipe { .. } => "PIPE".to_string(),
BoundKind::Block { .. } => "BLOCK".to_string(), BoundKind::Block { .. } => "BLOCK".to_string(),
BoundKind::Tuple { elements } => format!("TUPLE({})", elements.len()), BoundKind::Tuple { elements } => format!("TUPLE({})", elements.len()),
BoundKind::Record { values, .. } => format!("RECORD({})", values.len()), BoundKind::Record { values, .. } => format!("RECORD({})", values.len()),
BoundKind::Expansion { .. } => "EXPANSION".to_string(), BoundKind::Expansion { .. } => "EXPANSION".to_string(),
BoundKind::Extension(ext) => ext.display_name(), BoundKind::Extension(ext) => ext.display_name(),
BoundKind::Error => "ERROR".to_string(), BoundKind::Error => "ERROR".to_string(),
} }
} }
} }
+154 -154
View File
@@ -1,154 +1,154 @@
use crate::ast::compiler::bound_nodes::{BoundKind, Node}; use crate::ast::compiler::bound_nodes::{BoundKind, Node};
use crate::ast::types::Identity; use crate::ast::types::Identity;
use std::collections::HashMap; use std::collections::HashMap;
pub struct CapturePass; pub struct CapturePass;
impl CapturePass { impl CapturePass {
pub fn apply<T: Clone>(node: Node<T>, capture_map: &HashMap<Identity, Vec<Identity>>) -> Node<T> { pub fn apply<T: Clone>(node: Node<T>, capture_map: &HashMap<Identity, Vec<Identity>>) -> Node<T> {
Self::transform(node, capture_map) Self::transform(node, capture_map)
} }
fn transform<T: Clone>(mut node: Node<T>, capture_map: &HashMap<Identity, Vec<Identity>>) -> Node<T> { fn transform<T: Clone>(mut node: Node<T>, capture_map: &HashMap<Identity, Vec<Identity>>) -> Node<T> {
use std::rc::Rc; use std::rc::Rc;
match node.kind { match node.kind {
BoundKind::Define { BoundKind::Define {
name, name,
addr, addr,
kind, kind,
value, value,
.. ..
} => { } => {
let captured_by = capture_map.get(&node.identity).cloned().unwrap_or_default(); let captured_by = capture_map.get(&node.identity).cloned().unwrap_or_default();
node.kind = BoundKind::Define { node.kind = BoundKind::Define {
name, name,
addr, addr,
kind, kind,
value: Rc::new(Self::transform(value.as_ref().clone(), capture_map)), value: Rc::new(Self::transform(value.as_ref().clone(), capture_map)),
captured_by, captured_by,
}; };
} }
BoundKind::If { BoundKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
} => { } => {
node.kind = BoundKind::If { node.kind = BoundKind::If {
cond: Rc::new(Self::transform(cond.as_ref().clone(), capture_map)), cond: Rc::new(Self::transform(cond.as_ref().clone(), capture_map)),
then_br: Rc::new(Self::transform(then_br.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))), else_br: else_br.map(|e| Rc::new(Self::transform(e.as_ref().clone(), capture_map))),
}; };
} }
BoundKind::Set { addr, value } => { BoundKind::Set { addr, value } => {
node.kind = BoundKind::Set { node.kind = BoundKind::Set {
addr, addr,
value: Rc::new(Self::transform(value.as_ref().clone(), capture_map)), value: Rc::new(Self::transform(value.as_ref().clone(), capture_map)),
}; };
} }
BoundKind::FieldAccessor(_) => {} BoundKind::FieldAccessor(_) => {}
BoundKind::GetField { rec, field } => { BoundKind::GetField { rec, field } => {
node.kind = BoundKind::GetField { node.kind = BoundKind::GetField {
rec: Rc::new(Self::transform(rec.as_ref().clone(), capture_map)), rec: Rc::new(Self::transform(rec.as_ref().clone(), capture_map)),
field, field,
}; };
} }
BoundKind::Destructure { pattern, value } => { BoundKind::Destructure { pattern, value } => {
node.kind = BoundKind::Destructure { node.kind = BoundKind::Destructure {
pattern: Rc::new(Self::transform(pattern.as_ref().clone(), capture_map)), pattern: Rc::new(Self::transform(pattern.as_ref().clone(), capture_map)),
value: Rc::new(Self::transform(value.as_ref().clone(), capture_map)), value: Rc::new(Self::transform(value.as_ref().clone(), capture_map)),
}; };
} }
BoundKind::Lambda { BoundKind::Lambda {
params, params,
upvalues, upvalues,
body, body,
positional_count, positional_count,
} => { } => {
node.kind = BoundKind::Lambda { node.kind = BoundKind::Lambda {
params: Rc::new(Self::transform(params.as_ref().clone(), capture_map)), params: Rc::new(Self::transform(params.as_ref().clone(), capture_map)),
upvalues, upvalues,
body: Rc::new(Self::transform(body.as_ref().clone(), capture_map)), body: Rc::new(Self::transform(body.as_ref().clone(), capture_map)),
positional_count, positional_count,
}; };
} }
BoundKind::Call { callee, args } => { BoundKind::Call { callee, args } => {
node.kind = BoundKind::Call { node.kind = BoundKind::Call {
callee: Rc::new(Self::transform(callee.as_ref().clone(), capture_map)), callee: Rc::new(Self::transform(callee.as_ref().clone(), capture_map)),
args: Rc::new(Self::transform(args.as_ref().clone(), capture_map)), args: Rc::new(Self::transform(args.as_ref().clone(), capture_map)),
}; };
} }
BoundKind::Again { args } => { BoundKind::Again { args } => {
node.kind = BoundKind::Again { node.kind = BoundKind::Again {
args: Rc::new(Self::transform(args.as_ref().clone(), capture_map)), args: Rc::new(Self::transform(args.as_ref().clone(), capture_map)),
}; };
} }
BoundKind::Pipe { BoundKind::Pipe {
inputs, inputs,
lambda, lambda,
out_type, out_type,
} => { } => {
let mut t_inputs = Vec::with_capacity(inputs.len()); let mut t_inputs = Vec::with_capacity(inputs.len());
for input in inputs { for input in inputs {
t_inputs.push(Rc::new(Self::transform(input.as_ref().clone(), capture_map))); t_inputs.push(Rc::new(Self::transform(input.as_ref().clone(), capture_map)));
} }
node.kind = BoundKind::Pipe { node.kind = BoundKind::Pipe {
inputs: t_inputs, inputs: t_inputs,
lambda: Rc::new(Self::transform(lambda.as_ref().clone(), capture_map)), lambda: Rc::new(Self::transform(lambda.as_ref().clone(), capture_map)),
out_type: out_type.clone(), out_type: out_type.clone(),
}; };
} }
BoundKind::Block { exprs } => { BoundKind::Block { exprs } => {
node.kind = BoundKind::Block { node.kind = BoundKind::Block {
exprs: exprs exprs: exprs
.into_iter() .into_iter()
.map(|e| Rc::new(Self::transform(e.as_ref().clone(), capture_map))) .map(|e| Rc::new(Self::transform(e.as_ref().clone(), capture_map)))
.collect(), .collect(),
}; };
} }
BoundKind::Tuple { elements } => { BoundKind::Tuple { elements } => {
node.kind = BoundKind::Tuple { node.kind = BoundKind::Tuple {
elements: elements elements: elements
.into_iter() .into_iter()
.map(|e| Rc::new(Self::transform(e.as_ref().clone(), capture_map))) .map(|e| Rc::new(Self::transform(e.as_ref().clone(), capture_map)))
.collect(), .collect(),
}; };
} }
BoundKind::Record { layout, values } => { BoundKind::Record { layout, values } => {
node.kind = BoundKind::Record { node.kind = BoundKind::Record {
layout, layout,
values: values values: values
.into_iter() .into_iter()
.map(|v| Rc::new(Self::transform(v.as_ref().clone(), capture_map))) .map(|v| Rc::new(Self::transform(v.as_ref().clone(), capture_map)))
.collect(), .collect(),
}; };
} }
BoundKind::Expansion { BoundKind::Expansion {
original_call, original_call,
bound_expanded, bound_expanded,
} => { } => {
node.kind = BoundKind::Expansion { node.kind = BoundKind::Expansion {
original_call, original_call,
bound_expanded: Rc::new(Self::transform(bound_expanded.as_ref().clone(), capture_map)), bound_expanded: Rc::new(Self::transform(bound_expanded.as_ref().clone(), capture_map)),
}; };
} }
BoundKind::Nop BoundKind::Nop
| BoundKind::Constant(_) | BoundKind::Constant(_)
| BoundKind::Get { .. } | BoundKind::Get { .. }
| BoundKind::Extension(_) | BoundKind::Extension(_)
| BoundKind::Error => {} | BoundKind::Error => {}
} }
node node
} }
} }
+264 -264
View File
@@ -1,264 +1,264 @@
use crate::ast::compiler::bound_nodes::{BoundKind, Node}; use crate::ast::compiler::bound_nodes::{BoundKind, Node};
use crate::ast::types::Value; use crate::ast::types::Value;
use crate::ast::vm::Closure; use crate::ast::vm::Closure;
use std::fmt::Debug; use std::fmt::Debug;
/// Human-readable AST dumper for the bound AST. /// Human-readable AST dumper for the bound AST.
pub struct Dumper { pub struct Dumper {
output: String, output: String,
indent: usize, indent: usize,
} }
impl Dumper { impl Dumper {
/// Produces a formatted string representation of the given bound AST node and its children. /// Produces a formatted string representation of the given bound AST node and its children.
pub fn dump<T: Debug>(node: &Node<T>) -> String { pub fn dump<T: Debug>(node: &Node<T>) -> String {
let mut dumper = Self { let mut dumper = Self {
output: String::new(), output: String::new(),
indent: 0, indent: 0,
}; };
dumper.visit(node); dumper.visit(node);
dumper.output dumper.output
} }
fn write_indent(&mut self) { fn write_indent(&mut self) {
for _ in 0..self.indent { for _ in 0..self.indent {
self.output.push_str(" "); self.output.push_str(" ");
} }
} }
fn log<T: Debug>(&mut self, label: &str, node: &Node<T>) { fn log<T: Debug>(&mut self, label: &str, node: &Node<T>) {
self.write_indent(); self.write_indent();
self.output.push_str(label); self.output.push_str(label);
self.output self.output
.push_str(&format!(" <Metadata: {:?}>\n", node.ty)); .push_str(&format!(" <Metadata: {:?}>\n", node.ty));
} }
fn visit<T: Debug>(&mut self, node: &Node<T>) { fn visit<T: Debug>(&mut self, node: &Node<T>) {
match &node.kind { match &node.kind {
BoundKind::Nop => self.log("Nop", node), BoundKind::Nop => self.log("Nop", node),
BoundKind::Constant(v) => { BoundKind::Constant(v) => {
self.log(&format!("Constant: {}", v), node); self.log(&format!("Constant: {}", v), node);
// Introspect Closure AST if possible // Introspect Closure AST if possible
if let Value::Object(obj) = v if let Value::Object(obj) = v
&& let Some(closure) = obj.as_any().downcast_ref::<Closure>() && let Some(closure) = obj.as_any().downcast_ref::<Closure>()
{ {
self.indent += 1; self.indent += 1;
self.write_indent(); self.write_indent();
self.output.push_str("--- Specialized Body ---\n"); self.output.push_str("--- Specialized Body ---\n");
// We need to cast the inner TypedNode to the generic T required by visit. // 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), // Since Dumper is generic over T, but Closure stores TypedNode (where T = StaticType),
// we can only fully dump if T is 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. // 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. // We can't call self.visit because types mismatch if T != StaticType.
// So we just recursively dump to string and append. // So we just recursively dump to string and append.
let inner_dump = Dumper::dump(&closure.function_node); let inner_dump = Dumper::dump(&closure.function_node);
for line in inner_dump.lines() { for line in inner_dump.lines() {
self.write_indent(); self.write_indent();
self.output.push_str(line); self.output.push_str(line);
self.output.push('\n'); self.output.push('\n');
} }
self.indent -= 1; self.indent -= 1;
} }
} }
BoundKind::Get { addr, name } => { BoundKind::Get { addr, name } => {
self.log(&format!("Get: {} ({:?})", name.name, addr), node) self.log(&format!("Get: {} ({:?})", name.name, addr), node)
} }
BoundKind::FieldAccessor(k) => self.log(&format!("FieldAccessor: .{}", k.name()), node), BoundKind::FieldAccessor(k) => self.log(&format!("FieldAccessor: .{}", k.name()), node),
BoundKind::GetField { rec, field } => { BoundKind::GetField { rec, field } => {
self.log(&format!("GetField: .{}", field.name()), node); self.log(&format!("GetField: .{}", field.name()), node);
self.indent += 1; self.indent += 1;
self.visit(rec); self.visit(rec);
self.indent -= 1; self.indent -= 1;
} }
BoundKind::Set { addr, value } => { BoundKind::Set { addr, value } => {
self.log(&format!("Set: {:?}", addr), node); self.log(&format!("Set: {:?}", addr), node);
self.indent += 1; self.indent += 1;
self.visit(value); self.visit(value);
self.indent -= 1; self.indent -= 1;
} }
BoundKind::Define { BoundKind::Define {
name, name,
addr, addr,
kind, kind,
value, value,
captured_by, captured_by,
} => { } => {
let k_str = match kind { let k_str = match kind {
crate::ast::compiler::bound_nodes::DeclarationKind::Variable => "Variable", crate::ast::compiler::bound_nodes::DeclarationKind::Variable => "Variable",
crate::ast::compiler::bound_nodes::DeclarationKind::Parameter => "Parameter", crate::ast::compiler::bound_nodes::DeclarationKind::Parameter => "Parameter",
}; };
let capture_info = if captured_by.is_empty() { let capture_info = if captured_by.is_empty() {
String::from("not captured") String::from("not captured")
} else { } else {
format!("captured by {} lambdas", captured_by.len()) format!("captured by {} lambdas", captured_by.len())
}; };
self.log( self.log(
&format!( &format!(
"Define {} (Name: '{}', Address: {:?}, {})", "Define {} (Name: '{}', Address: {:?}, {})",
k_str, name.name, addr, capture_info k_str, name.name, addr, capture_info
), ),
node, node,
); );
self.indent += 1; self.indent += 1;
if !captured_by.is_empty() { if !captured_by.is_empty() {
for capturer in captured_by { for capturer in captured_by {
self.write_indent(); self.write_indent();
let loc = capturer let loc = capturer
.location .location
.unwrap_or(crate::ast::types::SourceLocation { line: 0, col: 0 }); .unwrap_or(crate::ast::types::SourceLocation { line: 0, col: 0 });
self.output.push_str(&format!( self.output.push_str(&format!(
"- Capturer: Lambda at line {}, col {}\n", "- Capturer: Lambda at line {}, col {}\n",
loc.line, loc.col loc.line, loc.col
)); ));
} }
} }
self.visit(value); self.visit(value);
self.indent -= 1; self.indent -= 1;
} }
BoundKind::Destructure { pattern, value } => { BoundKind::Destructure { pattern, value } => {
self.log("Destructure", node); self.log("Destructure", node);
self.indent += 1; self.indent += 1;
self.write_indent(); self.write_indent();
self.output.push_str("Pattern:\n"); self.output.push_str("Pattern:\n");
self.visit(pattern); self.visit(pattern);
self.write_indent(); self.write_indent();
self.output.push_str("Value:\n"); self.output.push_str("Value:\n");
self.visit(value); self.visit(value);
self.indent -= 1; self.indent -= 1;
} }
BoundKind::If { BoundKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
} => { } => {
self.log("If", node); self.log("If", node);
self.indent += 1; self.indent += 1;
self.write_indent(); self.write_indent();
self.output.push_str("Condition:\n"); self.output.push_str("Condition:\n");
self.visit(cond); self.visit(cond);
self.write_indent(); self.write_indent();
self.output.push_str("Then:\n"); self.output.push_str("Then:\n");
self.visit(then_br); self.visit(then_br);
if let Some(e) = else_br { if let Some(e) = else_br {
self.write_indent(); self.write_indent();
self.output.push_str("Else:\n"); self.output.push_str("Else:\n");
self.visit(e); self.visit(e);
} }
self.indent -= 1; self.indent -= 1;
} }
BoundKind::Lambda { BoundKind::Lambda {
params, params,
upvalues, upvalues,
body, body,
.. ..
} => { } => {
self.log(&format!("Lambda (Upvalues: {})", upvalues.len()), node); self.log(&format!("Lambda (Upvalues: {})", upvalues.len()), node);
self.indent += 1; self.indent += 1;
self.write_indent(); self.write_indent();
self.output.push_str("Parameters:\n"); self.output.push_str("Parameters:\n");
self.visit(params); self.visit(params);
if !upvalues.is_empty() { if !upvalues.is_empty() {
self.write_indent(); self.write_indent();
self.output.push_str(&format!("Upvalues: {:?}\n", upvalues)); self.output.push_str(&format!("Upvalues: {:?}\n", upvalues));
} }
self.visit(body); self.visit(body);
self.indent -= 1; self.indent -= 1;
} }
BoundKind::Call { callee, args } => { BoundKind::Call { callee, args } => {
self.log("Call", node); self.log("Call", node);
self.indent += 1; self.indent += 1;
self.write_indent(); self.write_indent();
self.output.push_str("Callee:\n"); self.output.push_str("Callee:\n");
self.visit(callee); self.visit(callee);
self.write_indent(); self.write_indent();
self.output.push_str("Arguments:\n"); self.output.push_str("Arguments:\n");
self.visit(args); self.visit(args);
self.indent -= 1; self.indent -= 1;
} }
BoundKind::Again { args } => { BoundKind::Again { args } => {
self.log("Again", node); self.log("Again", node);
self.indent += 1; self.indent += 1;
self.visit(args); self.visit(args);
self.indent -= 1; self.indent -= 1;
} }
BoundKind::Pipe { inputs, lambda, .. } => { BoundKind::Pipe { inputs, lambda, .. } => {
self.log("Pipe", node); self.log("Pipe", node);
self.indent += 1; self.indent += 1;
for input in inputs { for input in inputs {
self.visit(input); self.visit(input);
} }
self.visit(lambda); self.visit(lambda);
self.indent -= 1; self.indent -= 1;
} }
BoundKind::Block { exprs } => { BoundKind::Block { exprs } => {
self.log("Block", node); self.log("Block", node);
self.indent += 1; self.indent += 1;
for expr in exprs { for expr in exprs {
self.visit(expr); self.visit(expr);
} }
self.indent -= 1; self.indent -= 1;
} }
BoundKind::Tuple { elements } => { BoundKind::Tuple { elements } => {
self.log("Tuple", node); self.log("Tuple", node);
self.indent += 1; self.indent += 1;
for el in elements { for el in elements {
self.visit(el); self.visit(el);
} }
self.indent -= 1; self.indent -= 1;
} }
BoundKind::Record { layout, values } => { BoundKind::Record { layout, values } => {
self.log( self.log(
&format!("Record (Layout: {} fields)", layout.fields.len()), &format!("Record (Layout: {} fields)", layout.fields.len()),
node, node,
); );
self.indent += 1; self.indent += 1;
for v in values { for v in values {
self.visit(v); self.visit(v);
} }
self.indent -= 1; self.indent -= 1;
} }
BoundKind::Expansion { BoundKind::Expansion {
original_call, original_call,
bound_expanded, bound_expanded,
} => { } => {
self.log( self.log(
&format!("Expansion (Original: {:?})", original_call.kind), &format!("Expansion (Original: {:?})", original_call.kind),
node, node,
); );
self.indent += 1; self.indent += 1;
self.visit(bound_expanded); self.visit(bound_expanded);
self.indent -= 1; self.indent -= 1;
} }
BoundKind::Extension(ext) => { BoundKind::Extension(ext) => {
self.log(&ext.display_name(), node); self.log(&ext.display_name(), node);
} }
BoundKind::Error => { BoundKind::Error => {
self.log("ERROR_NODE", node); self.log("ERROR_NODE", node);
} }
} }
} }
} }
+99 -99
View File
@@ -1,99 +1,99 @@
use crate::ast::compiler::bound_nodes::{Address, BoundKind, GlobalIdx, Node}; use crate::ast::compiler::bound_nodes::{Address, BoundKind, GlobalIdx, Node};
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
/// A pass that collects all global function definitions (lambdas) into a registry. /// 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. /// This allows the Specializer to retrieve the original AST of a function for monomorphization.
pub struct LambdaCollector<'a, T> { pub struct LambdaCollector<'a, T> {
registry: &'a mut HashMap<GlobalIdx, Rc<Node<T>>>, registry: &'a mut HashMap<GlobalIdx, Rc<Node<T>>>,
} }
impl<'a, T: Clone> LambdaCollector<'a, T> { impl<'a, T: Clone> LambdaCollector<'a, T> {
/// Performs a full traversal of the AST and populates the provided registry. /// 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>>>) { pub fn collect(node: &Node<T>, registry: &'a mut HashMap<GlobalIdx, Rc<Node<T>>>) {
let mut collector = Self { registry }; let mut collector = Self { registry };
collector.visit(node); collector.visit(node);
} }
fn visit(&mut self, node: &Node<T>) { fn visit(&mut self, node: &Node<T>) {
match &node.kind { match &node.kind {
BoundKind::Block { exprs } => { BoundKind::Block { exprs } => {
for expr in exprs { for expr in exprs {
self.visit(expr); self.visit(expr);
} }
} }
BoundKind::Define { addr, value, .. } => { BoundKind::Define { addr, value, .. } => {
// Register global function definitions (lambdas) // Register global function definitions (lambdas)
if let Address::Global(global_index) = addr { if let Address::Global(global_index) = addr {
let mut current = value; let mut current = value;
while let BoundKind::Expansion { bound_expanded, .. } = &current.kind { while let BoundKind::Expansion { bound_expanded, .. } = &current.kind {
current = bound_expanded; current = bound_expanded;
} }
if let BoundKind::Lambda { .. } = &current.kind { if let BoundKind::Lambda { .. } = &current.kind {
self.registry self.registry
.insert(*global_index, (*current).clone()); .insert(*global_index, (*current).clone());
} }
} }
self.visit(value); self.visit(value);
} }
BoundKind::Set { addr, value } => { BoundKind::Set { addr, value } => {
// Also track assignments to globals if they hold lambdas. // Also track assignments to globals if they hold lambdas.
if let Address::Global(global_index) = addr { if let Address::Global(global_index) = addr {
let mut current = value; let mut current = value;
while let BoundKind::Expansion { bound_expanded, .. } = &current.kind { while let BoundKind::Expansion { bound_expanded, .. } = &current.kind {
current = bound_expanded; current = bound_expanded;
} }
if let BoundKind::Lambda { .. } = &current.kind { if let BoundKind::Lambda { .. } = &current.kind {
self.registry self.registry
.insert(*global_index, (*current).clone()); .insert(*global_index, (*current).clone());
} }
} }
self.visit(value); self.visit(value);
} }
BoundKind::If { BoundKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
} => { } => {
self.visit(cond); self.visit(cond);
self.visit(then_br); self.visit(then_br);
if let Some(e) = else_br { if let Some(e) = else_br {
self.visit(e); self.visit(e);
} }
} }
BoundKind::Lambda { params, body, .. } => { BoundKind::Lambda { params, body, .. } => {
self.visit(params); self.visit(params);
self.visit(body); self.visit(body);
} }
BoundKind::Call { callee, args } => { BoundKind::Call { callee, args } => {
self.visit(callee); self.visit(callee);
self.visit(args); self.visit(args);
} }
BoundKind::Tuple { elements } => { BoundKind::Tuple { elements } => {
for el in elements { for el in elements {
self.visit(el); self.visit(el);
} }
} }
BoundKind::Record { values, .. } => { BoundKind::Record { values, .. } => {
for v in values { for v in values {
self.visit(v); self.visit(v);
} }
} }
BoundKind::Expansion { bound_expanded, .. } => { BoundKind::Expansion { bound_expanded, .. } => {
self.visit(bound_expanded); self.visit(bound_expanded);
} }
_ => {} // Leaf nodes _ => {} // Leaf nodes
} }
} }
} }
+144 -144
View File
@@ -1,4 +1,4 @@
use crate::ast::nodes::{Symbol, UntypedKind, UntypedNode}; use crate::ast::nodes::{Symbol, SyntaxKind, SyntaxNode};
use crate::ast::types::{Identity, Value}; use crate::ast::types::{Identity, Value};
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
@@ -7,19 +7,19 @@ use std::rc::Rc;
pub trait MacroEvaluator { pub trait MacroEvaluator {
fn evaluate( fn evaluate(
&self, &self,
node: &UntypedNode, node: &SyntaxNode,
bindings: &HashMap<Rc<str>, UntypedNode>, bindings: &HashMap<Rc<str>, SyntaxNode>,
) -> Result<Value, String>; ) -> Result<Value, String>;
} }
/// Internal state for template expansion (Hygiene context + Parameter binding) /// Internal state for template expansion (Hygiene context + Parameter binding)
struct ExpansionState<'a> { 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. /// The identity of the current expansion instance, used to "color" internal symbols.
expansion_id: Identity, 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. /// A registry for macro declarations.
#[derive(Clone)] #[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() { if let Some(current_rc) = self.scopes.last_mut() {
// Copy-on-Write: If the Rc is shared, clone the map before modifying. // Copy-on-Write: If the Rc is shared, clone the map before modifying.
let current = Rc::make_mut(current_rc); 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() { for scope in self.scopes.iter().rev() {
if let Some(m) = scope.get(name) { if let Some(m) = scope.get(name) {
return Some(m.clone()); return Some(m.clone());
@@ -85,30 +85,30 @@ impl<E: MacroEvaluator> MacroExpander<E> {
self.registry 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) 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 { match node.kind {
UntypedKind::MacroDecl { name, params, body } => { SyntaxKind::MacroDecl { name, params, body } => {
let p_names = self.extract_param_names(&params)?; let p_names = self.extract_param_names(&params)?;
self.registry.define(name.name, p_names, *body); self.registry.define(name.name, p_names, *body);
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Nop, kind: SyntaxKind::Nop,
}) })
} }
UntypedKind::Call { callee, args } => { SyntaxKind::Call { callee, args } => {
if let UntypedKind::Identifier(ref sym) = callee.kind if let SyntaxKind::Identifier(ref sym) = callee.kind
&& let Some((params, body)) = self.registry.lookup(&sym.name) && let Some((params, body)) = self.registry.lookup(&sym.name)
{ {
let expanded_args = self.expand_recursive(*args)?; let expanded_args = self.expand_recursive(*args)?;
// Extract argument nodes from the expanded tuple // 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 elements
} else { } else {
vec![expanded_args] vec![expanded_args]
@@ -127,9 +127,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
let expanded_callee = self.expand_recursive(*callee)?; let expanded_callee = self.expand_recursive(*callee)?;
let expanded_args = self.expand_recursive(*args)?; let expanded_args = self.expand_recursive(*args)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Call { kind: SyntaxKind::Call {
callee: Box::new(expanded_callee), callee: Box::new(expanded_callee),
args: Box::new(expanded_args), args: Box::new(expanded_args),
}, },
@@ -137,7 +137,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::Block { exprs } => { SyntaxKind::Block { exprs } => {
self.registry.push(); self.registry.push();
let mut expanded_exprs = Vec::new(); let mut expanded_exprs = Vec::new();
for expr in exprs { for expr in exprs {
@@ -145,24 +145,24 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} }
self.registry.pop(); self.registry.pop();
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Block { kind: SyntaxKind::Block {
exprs: expanded_exprs, exprs: expanded_exprs,
}, },
}) })
} }
UntypedKind::Pipe { inputs, lambda } => { SyntaxKind::Pipe { inputs, lambda } => {
let mut expanded_inputs = Vec::with_capacity(inputs.len()); let mut expanded_inputs = Vec::with_capacity(inputs.len());
for input in inputs { for input in inputs {
expanded_inputs.push(self.expand_recursive(input)?); expanded_inputs.push(self.expand_recursive(input)?);
} }
let expanded_lambda = Box::new(self.expand_recursive(*lambda)?); let expanded_lambda = Box::new(self.expand_recursive(*lambda)?);
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Pipe { kind: SyntaxKind::Pipe {
inputs: expanded_inputs, inputs: expanded_inputs,
lambda: expanded_lambda, lambda: expanded_lambda,
}, },
@@ -170,15 +170,15 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::Lambda { params, body } => { SyntaxKind::Lambda { params, body } => {
self.registry.push(); self.registry.push();
let expanded_params = self.expand_recursive(*params)?; let expanded_params = self.expand_recursive(*params)?;
let expanded_body = self.expand_recursive((*body).clone())?; let expanded_body = self.expand_recursive((*body).clone())?;
self.registry.pop(); self.registry.pop();
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Lambda { kind: SyntaxKind::Lambda {
params: Box::new(expanded_params), params: Box::new(expanded_params),
body: Rc::new(expanded_body), body: Rc::new(expanded_body),
}, },
@@ -186,7 +186,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::If { SyntaxKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
@@ -198,9 +198,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} else { } else {
None None
}; };
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::If { kind: SyntaxKind::If {
cond: Box::new(cond), cond: Box::new(cond),
then_br: Box::new(then_br), then_br: Box::new(then_br),
else_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 target = self.expand_recursive(*target)?;
let value = self.expand_recursive(*value)?; let value = self.expand_recursive(*value)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Def { kind: SyntaxKind::Def {
target: Box::new(target), target: Box::new(target),
value: Box::new(value), 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 target = self.expand_recursive(*target)?;
let value = self.expand_recursive(*value)?; let value = self.expand_recursive(*value)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Assign { kind: SyntaxKind::Assign {
target: Box::new(target), target: Box::new(target),
value: Box::new(value), 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(); let mut expanded_elements = Vec::new();
for e in elements { for e in elements {
expanded_elements.push(self.expand_recursive(e)?); expanded_elements.push(self.expand_recursive(e)?);
} }
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Tuple { kind: SyntaxKind::Tuple {
elements: expanded_elements, elements: expanded_elements,
}, },
}) })
} }
UntypedKind::Record { fields } => { SyntaxKind::Record { fields } => {
let mut expanded_fields = Vec::new(); let mut expanded_fields = Vec::new();
for (k, v) in fields { for (k, v) in fields {
expanded_fields.push((self.expand_recursive(k)?, self.expand_recursive(v)?)); expanded_fields.push((self.expand_recursive(k)?, self.expand_recursive(v)?));
} }
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Record { kind: SyntaxKind::Record {
fields: expanded_fields, fields: expanded_fields,
}, },
}) })
} }
UntypedKind::Expansion { call, expanded } => { SyntaxKind::Expansion { call, expanded } => {
let expanded = self.expand_recursive(*expanded)?; let expanded = self.expand_recursive(*expanded)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Expansion { kind: SyntaxKind::Expansion {
call, call,
expanded: Box::new(expanded), 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)?; let expanded_args = self.expand_recursive(*args)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Again { kind: SyntaxKind::Again {
args: Box::new(expanded_args), args: Box::new(expanded_args),
}, },
@@ -295,9 +295,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
identity: Identity, identity: Identity,
name: &str, name: &str,
params: Vec<Rc<str>>, params: Vec<Rc<str>>,
args: Vec<UntypedNode>, args: Vec<SyntaxNode>,
body: UntypedNode, body: SyntaxNode,
) -> Result<UntypedNode, String> { ) -> Result<SyntaxNode, String> {
if params.len() != args.len() { if params.len() != args.len() {
return Err(format!( return Err(format!(
"Macro {} expects {} arguments, but got {}", "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. // 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 { let mut state = ExpansionState {
bindings: &bindings, bindings: &bindings,
expansion_id: identity.clone(), expansion_id: identity.clone(),
@@ -324,17 +324,17 @@ impl<E: MacroEvaluator> MacroExpander<E> {
body body
}; };
let original_call = UntypedNode { let original_call = SyntaxNode {
identity: identity.clone(), identity: identity.clone(),
kind: UntypedKind::Call { kind: SyntaxKind::Call {
callee: Box::new(UntypedNode { callee: Box::new(SyntaxNode {
identity: identity.clone(), 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(), identity: identity.clone(),
kind: UntypedKind::Tuple { kind: SyntaxKind::Tuple {
elements: bindings.into_values().collect(), elements: bindings.into_values().collect(),
}, },
@@ -343,9 +343,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}; };
Ok(UntypedNode { Ok(SyntaxNode {
identity, identity,
kind: UntypedKind::Expansion { kind: SyntaxKind::Expansion {
call: Box::new(original_call), call: Box::new(original_call),
expanded: Box::new(expanded_body), 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 { match &node.kind {
UntypedKind::Identifier(sym) => Ok(vec![sym.name.clone()]), SyntaxKind::Identifier(sym) => Ok(vec![sym.name.clone()]),
UntypedKind::Tuple { elements } => { SyntaxKind::Tuple { elements } => {
let mut names = Vec::new(); let mut names = Vec::new();
for el in elements { for el in elements {
names.extend(self.extract_param_names(el)?); names.extend(self.extract_param_names(el)?);
@@ -369,23 +369,23 @@ impl<E: MacroEvaluator> MacroExpander<E> {
fn expand_template( fn expand_template(
&self, &self,
node: UntypedNode, node: SyntaxNode,
state: &mut ExpansionState, state: &mut ExpansionState,
) -> Result<UntypedNode, String> { ) -> Result<SyntaxNode, String> {
match node.kind { match node.kind {
UntypedKind::Identifier(mut sym) => { SyntaxKind::Identifier(mut sym) => {
// Inside a template, all internal identifiers are colored. // Inside a template, all internal identifiers are colored.
sym.context = Some(state.expansion_id.clone()); sym.context = Some(state.expansion_id.clone());
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Identifier(sym), kind: SyntaxKind::Identifier(sym),
}) })
} }
UntypedKind::Placeholder(inner) => { SyntaxKind::Placeholder(inner) => {
// Break out of template for substitution/evaluation // 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) && let Some(arg) = state.bindings.get(&sym.name)
{ {
return Ok(arg.clone()); return Ok(arg.clone());
@@ -394,9 +394,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
Ok(self.value_to_node(val, node.identity)) Ok(self.value_to_node(val, node.identity))
} }
UntypedKind::Splice(inner) => { SyntaxKind::Splice(inner) => {
// Splicing is also a form of substitution // 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) && let Some(arg) = state.bindings.get(&sym.name)
{ {
return Ok(arg.clone()); return Ok(arg.clone());
@@ -405,12 +405,12 @@ impl<E: MacroEvaluator> MacroExpander<E> {
Ok(self.value_to_node(val, node.identity)) Ok(self.value_to_node(val, node.identity))
} }
UntypedKind::Def { target, value } => { SyntaxKind::Def { target, value } => {
let target = self.expand_template(*target, state)?; let target = self.expand_template(*target, state)?;
let val = self.expand_template(*value, state)?; let val = self.expand_template(*value, state)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Def { kind: SyntaxKind::Def {
target: Box::new(target), target: Box::new(target),
value: Box::new(val), 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 target = self.expand_template(*target, state)?;
let value = self.expand_template(*value, state)?; let value = self.expand_template(*value, state)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Assign { kind: SyntaxKind::Assign {
target: Box::new(target), target: Box::new(target),
value: Box::new(value), 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(); let mut new_elements = Vec::new();
for e in elements { 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)?; let val = self.evaluator.evaluate(inner, state.bindings)?;
self.handle_splice_value(val, node.identity.clone(), &mut new_elements)?; self.handle_splice_value(val, node.identity.clone(), &mut new_elements)?;
} else { } else {
new_elements.push(self.expand_template(e, state)?); new_elements.push(self.expand_template(e, state)?);
} }
} }
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Tuple { kind: SyntaxKind::Tuple {
elements: new_elements, elements: new_elements,
}, },
}) })
} }
UntypedKind::Block { exprs } => { SyntaxKind::Block { exprs } => {
let mut new_exprs = Vec::new(); let mut new_exprs = Vec::new();
for e in exprs { 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)?; let val = self.evaluator.evaluate(inner, state.bindings)?;
self.handle_splice_value(val, node.identity.clone(), &mut new_exprs)?; self.handle_splice_value(val, node.identity.clone(), &mut new_exprs)?;
} else { } else {
new_exprs.push(self.expand_template(e, state)?); new_exprs.push(self.expand_template(e, state)?);
} }
} }
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, 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(); let mut new_fields = Vec::new();
for (k, v) in fields { for (k, v) in fields {
new_fields.push(( new_fields.push((
@@ -475,19 +475,19 @@ impl<E: MacroEvaluator> MacroExpander<E> {
self.expand_template(v, state)?, self.expand_template(v, state)?,
)); ));
} }
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, 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_callee = self.expand_template(*callee, state)?;
let expanded_args = self.expand_template(*args, state)?; let expanded_args = self.expand_template(*args, state)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Call { kind: SyntaxKind::Call {
callee: Box::new(expanded_callee), callee: Box::new(expanded_callee),
args: Box::new(expanded_args), args: Box::new(expanded_args),
}, },
@@ -495,7 +495,7 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}) })
} }
UntypedKind::If { SyntaxKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
@@ -507,9 +507,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} else { } else {
None None
}; };
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::If { kind: SyntaxKind::If {
cond: Box::new(cond), cond: Box::new(cond),
then_br: Box::new(then_br), then_br: Box::new(then_br),
else_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()); let mut expanded_inputs = Vec::with_capacity(inputs.len());
for input in inputs { for input in inputs {
expanded_inputs.push(self.expand_template(input, state)?); expanded_inputs.push(self.expand_template(input, state)?);
} }
let expanded_lambda = Box::new(self.expand_template(*lambda, state)?); let expanded_lambda = Box::new(self.expand_template(*lambda, state)?);
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Pipe { kind: SyntaxKind::Pipe {
inputs: expanded_inputs, inputs: expanded_inputs,
lambda: expanded_lambda, 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 expanded_params = self.expand_template(*params, state)?;
let body = self.expand_template((*body).clone(), state)?; let body = self.expand_template((*body).clone(), state)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Lambda { kind: SyntaxKind::Lambda {
params: Box::new(expanded_params), params: Box::new(expanded_params),
body: Rc::new(body), 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)?; let expanded_args = self.expand_template(*args, state)?;
Ok(UntypedNode { Ok(SyntaxNode {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Again { kind: SyntaxKind::Again {
args: Box::new(expanded_args), args: Box::new(expanded_args),
}, },
@@ -566,19 +566,19 @@ impl<E: MacroEvaluator> MacroExpander<E> {
&self, &self,
val: Value, val: Value,
_identity: Identity, _identity: Identity,
target: &mut Vec<UntypedNode>, target: &mut Vec<SyntaxNode>,
) -> Result<(), String> { ) -> Result<(), String> {
match val { match val {
Value::Object(obj) => { 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 { match &node.kind {
UntypedKind::Tuple { elements } => { SyntaxKind::Tuple { elements } => {
for e in elements { for e in elements {
target.push(e.clone()); target.push(e.clone());
} }
return Ok(()); return Ok(());
} }
UntypedKind::Block { exprs } => { SyntaxKind::Block { exprs } => {
for e in exprs { for e in exprs {
target.push(e.clone()); 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 { match val {
Value::Object(obj) => { 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(); return node.clone();
} }
UntypedNode { SyntaxNode {
identity, identity,
kind: UntypedKind::Constant(Value::Object(obj)), kind: SyntaxKind::Constant(Value::Object(obj)),
} }
} }
_ => UntypedNode { _ => SyntaxNode {
identity, identity,
kind: UntypedKind::Constant(val), kind: SyntaxKind::Constant(val),
}, },
} }
@@ -632,10 +632,10 @@ mod tests {
impl MacroEvaluator for SimpleEvaluator { impl MacroEvaluator for SimpleEvaluator {
fn evaluate( fn evaluate(
&self, &self,
node: &UntypedNode, node: &SyntaxNode,
bindings: &HashMap<Rc<str>, UntypedNode>, bindings: &HashMap<Rc<str>, SyntaxNode>,
) -> Result<Value, String> { ) -> 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) && let Some(arg) = bindings.get(&sym.name)
{ {
return Ok(Value::Object(Rc::new(arg.clone()) as Rc<dyn Object>)); return Ok(Value::Object(Rc::new(arg.clone()) as Rc<dyn Object>));
@@ -655,19 +655,19 @@ mod tests {
(m)) (m))
"; ";
let mut parser = Parser::new(source); 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 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 if let SyntaxKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Expansion { && let SyntaxKind::Expansion {
expanded: result, expanded: result,
call, call,
} = &exprs[1].kind } = &exprs[1].kind
{ {
if let UntypedKind::Def { target, .. } = &result.kind { if let SyntaxKind::Def { target, .. } = &result.kind {
if let UntypedKind::Identifier(sym) = &target.kind { if let SyntaxKind::Identifier(sym) = &target.kind {
assert_eq!(sym.context, Some(call.identity.clone())); assert_eq!(sym.context, Some(call.identity.clone()));
assert_eq!(sym.name.as_ref(), "y"); assert_eq!(sym.name.as_ref(), "y");
} else { } else {
@@ -687,30 +687,30 @@ mod tests {
(unless false 42)) (unless false 42))
"; ";
let mut parser = Parser::new(source); 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 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 if let SyntaxKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Expansion { && let SyntaxKind::Expansion {
call: _, call: _,
expanded: result, expanded: result,
} = &exprs[1].kind } = &exprs[1].kind
&& let UntypedKind::If { && let SyntaxKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
} = &result.kind } = &result.kind
{ {
if let UntypedKind::Identifier(sym) = &cond.kind { if let SyntaxKind::Identifier(sym) = &cond.kind {
assert_eq!(sym.name.as_ref(), "false"); assert_eq!(sym.name.as_ref(), "false");
} else { } else {
panic!("Expected identifier 'false', got {:?}", cond.kind); 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 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); assert_eq!(*n, 42);
} else { } else {
panic!("Expected 42, got {:?}", eb.kind); panic!("Expected 42, got {:?}", eb.kind);
@@ -729,23 +729,23 @@ mod tests {
(def t (wrap [1 2 3]))) (def t (wrap [1 2 3])))
"; ";
let mut parser = Parser::new(source); 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 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 if let SyntaxKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Def { value, .. } = &exprs[1].kind && let SyntaxKind::Def { value, .. } = &exprs[1].kind
&& let UntypedKind::Expansion { && let SyntaxKind::Expansion {
expanded: result, .. expanded: result, ..
} = &value.kind } = &value.kind
&& let UntypedKind::Tuple { elements } = &result.kind && let SyntaxKind::Tuple { elements } = &result.kind
{ {
assert_eq!(elements.len(), 5); 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); 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); assert_eq!(*n, 4);
} }
} }
@@ -759,10 +759,10 @@ mod tests {
(square 3)) (square 3))
"; ";
let mut parser = Parser::new(source); 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 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 // Convert test globals into a CompilerScope
let mut locals = HashMap::new(); let mut locals = HashMap::new();
@@ -800,10 +800,10 @@ mod tests {
y) y)
"; ";
let mut parser = Parser::new(source); 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 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 mut diag = crate::ast::diagnostics::Diagnostics::new();
let result = Binder::bind_root(vec![], 0, 0, &expanded, &mut diag); let result = Binder::bind_root(vec![], 0, 0, &expanded, &mut diag);
@@ -820,10 +820,10 @@ mod tests {
y) y)
"; ";
let mut parser = Parser::new(source); 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 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 mut diag = crate::ast::diagnostics::Diagnostics::new();
let result = Binder::bind_root(vec![], 0, 0, &expanded, &mut diag); let result = Binder::bind_root(vec![], 0, 0, &expanded, &mut diag);
+21 -21
View File
@@ -1,21 +1,21 @@
pub mod analyzer; pub mod analyzer;
pub mod binder; pub mod binder;
pub mod bound_nodes; pub mod bound_nodes;
pub mod captures; pub mod captures;
pub mod dumper; pub mod dumper;
pub mod lambda_collector; pub mod lambda_collector;
pub mod macros; pub mod macros;
pub mod optimizer; pub mod optimizer;
pub mod specializer; pub mod specializer;
pub mod lowering; pub mod lowering;
pub mod type_checker; pub mod type_checker;
pub use binder::*; pub use binder::*;
pub use bound_nodes::*; pub use bound_nodes::*;
pub use captures::*; pub use captures::*;
pub use dumper::*; pub use dumper::*;
pub use macros::*; pub use macros::*;
pub use optimizer::*; pub use optimizer::*;
pub use specializer::*; pub use specializer::*;
pub use lowering::*; pub use lowering::*;
pub use type_checker::*; pub use type_checker::*;
File diff suppressed because it is too large Load Diff
+101 -101
View File
@@ -1,101 +1,101 @@
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, Node, NodeMetrics}; use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, Node, NodeMetrics};
use crate::ast::types::{Purity, RecordLayout, StaticType, Value}; use crate::ast::types::{Purity, RecordLayout, StaticType, Value};
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
pub struct Folder<'a> { pub struct Folder<'a> {
pub globals: &'a Option<Rc<RefCell<Vec<Value>>>>, pub globals: &'a Option<Rc<RefCell<Vec<Value>>>>,
} }
impl<'a> Folder<'a> { impl<'a> Folder<'a> {
pub fn new(globals: &'a Option<Rc<RefCell<Vec<Value>>>>) -> Self { pub fn new(globals: &'a Option<Rc<RefCell<Vec<Value>>>>) -> Self {
Self { globals } Self { globals }
} }
pub fn make_constant_node(&self, val: Value, template: &AnalyzedNode) -> AnalyzedNode { pub fn make_constant_node(&self, val: Value, template: &AnalyzedNode) -> AnalyzedNode {
let ty = val.static_type(); let ty = val.static_type();
let typed_original = Rc::new(Node { let typed_original = Rc::new(Node {
identity: template.identity.clone(), identity: template.identity.clone(),
kind: BoundKind::Constant(val.clone()), kind: BoundKind::Constant(val.clone()),
ty: ty.clone(), ty: ty.clone(),
}); });
Node { Node {
identity: template.identity.clone(), identity: template.identity.clone(),
kind: BoundKind::Constant(val), kind: BoundKind::Constant(val),
ty: NodeMetrics { ty: NodeMetrics {
original: typed_original, original: typed_original,
purity: Purity::Pure, purity: Purity::Pure,
is_recursive: false, is_recursive: false,
}, },
} }
} }
pub fn make_nop_node(&self, template: &AnalyzedNode) -> AnalyzedNode { pub fn make_nop_node(&self, template: &AnalyzedNode) -> AnalyzedNode {
let typed_original = Rc::new(Node { let typed_original = Rc::new(Node {
identity: template.identity.clone(), identity: template.identity.clone(),
kind: BoundKind::Nop, kind: BoundKind::Nop,
ty: StaticType::Void, ty: StaticType::Void,
}); });
Node { Node {
identity: template.identity.clone(), identity: template.identity.clone(),
kind: BoundKind::Nop, kind: BoundKind::Nop,
ty: NodeMetrics { ty: NodeMetrics {
original: typed_original, original: typed_original,
purity: Purity::Pure, purity: Purity::Pure,
is_recursive: false, is_recursive: false,
}, },
} }
} }
pub fn try_fold_record( pub fn try_fold_record(
&self, &self,
layout: &std::sync::Arc<RecordLayout>, layout: &std::sync::Arc<RecordLayout>,
values: &[Rc<AnalyzedNode>], values: &[Rc<AnalyzedNode>],
template: &AnalyzedNode, template: &AnalyzedNode,
) -> Option<AnalyzedNode> { ) -> Option<AnalyzedNode> {
let mut constant_values = Vec::with_capacity(values.len()); let mut constant_values = Vec::with_capacity(values.len());
for v_node in values { for v_node in values {
if let BoundKind::Constant(val) = &v_node.kind { if let BoundKind::Constant(val) = &v_node.kind {
constant_values.push(val.clone()); constant_values.push(val.clone());
} else { } else {
return None; return None;
} }
} }
let record_val = Value::Record(layout.clone(), Rc::new(constant_values)); let record_val = Value::Record(layout.clone(), Rc::new(constant_values));
Some(self.make_constant_node(record_val, template)) Some(self.make_constant_node(record_val, template))
} }
pub fn try_fold_pure( pub fn try_fold_pure(
&self, &self,
callee: &AnalyzedNode, callee: &AnalyzedNode,
arg_nodes: &[Rc<AnalyzedNode>], arg_nodes: &[Rc<AnalyzedNode>],
) -> Option<AnalyzedNode> { ) -> Option<AnalyzedNode> {
if callee.ty.purity < Purity::Pure { if callee.ty.purity < Purity::Pure {
return None; return None;
} }
let mut arg_values = Vec::with_capacity(arg_nodes.len()); let mut arg_values = Vec::with_capacity(arg_nodes.len());
for node in arg_nodes { for node in arg_nodes {
if let BoundKind::Constant(val) = &node.kind { if let BoundKind::Constant(val) = &node.kind {
arg_values.push(val.clone()); arg_values.push(val.clone());
} else { } else {
return None; return None;
} }
} }
let func_val = match &callee.kind { let func_val = match &callee.kind {
BoundKind::Get { BoundKind::Get {
addr: Address::Global(idx), addr: Address::Global(idx),
.. ..
} => self.globals.as_ref()?.borrow().get(idx.0 as usize)?.clone(), } => self.globals.as_ref()?.borrow().get(idx.0 as usize)?.clone(),
BoundKind::Constant(val) => val.clone(), BoundKind::Constant(val) => val.clone(),
_ => return None, _ => return None,
}; };
let result = match func_val { let result = match func_val {
Value::Function(f) => (f.func)(&arg_values), Value::Function(f) => (f.func)(&arg_values),
_ => return None, _ => return None,
}; };
Some(self.make_constant_node(result, callee)) Some(self.make_constant_node(result, callee))
} }
} }
+210 -210
View File
@@ -1,210 +1,210 @@
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, LocalSlot, Node, UpvalueIdx}; use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, LocalSlot, Node, UpvalueIdx};
use crate::ast::types::Value; use crate::ast::types::Value;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::rc::Rc; use std::rc::Rc;
#[derive(Default)] #[derive(Default)]
pub struct SubstitutionMap { pub struct SubstitutionMap {
pub values: HashMap<Address, Value>, pub values: HashMap<Address, Value>,
pub ast_substitutions: HashMap<Address, Rc<AnalyzedNode>>, pub ast_substitutions: HashMap<Address, Rc<AnalyzedNode>>,
pub slot_mapping: HashMap<LocalSlot, LocalSlot>, pub slot_mapping: HashMap<LocalSlot, LocalSlot>,
pub assigned: HashSet<Address>, pub assigned: HashSet<Address>,
pub next_slot: u32, pub next_slot: u32,
pub used: HashSet<Address>, pub used: HashSet<Address>,
pub captured_slots: HashSet<LocalSlot>, pub captured_slots: HashSet<LocalSlot>,
} }
impl SubstitutionMap { impl SubstitutionMap {
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()
} }
/// Creates a new SubstitutionMap for an inner scope (like an inlined Lambda). /// Creates a new SubstitutionMap for an inner scope (like an inlined Lambda).
/// Safely inherits only Global substitutions, because Local and Upvalue /// Safely inherits only Global substitutions, because Local and Upvalue
/// addresses are relative to the specific function frame and would overlap. /// addresses are relative to the specific function frame and would overlap.
pub fn new_inner(&self) -> Self { pub fn new_inner(&self) -> Self {
let mut inner = Self::new(); let mut inner = Self::new();
for (k, v) in &self.values { for (k, v) in &self.values {
if matches!(k, Address::Global(_)) { if matches!(k, Address::Global(_)) {
inner.values.insert(*k, v.clone()); inner.values.insert(*k, v.clone());
} }
} }
for (k, v) in &self.ast_substitutions { for (k, v) in &self.ast_substitutions {
if matches!(k, Address::Global(_)) { if matches!(k, Address::Global(_)) {
inner.ast_substitutions.insert(*k, v.clone()); inner.ast_substitutions.insert(*k, v.clone());
} }
} }
inner inner
} }
pub fn new_for_inlining(&self) -> Self { pub fn new_for_inlining(&self) -> Self {
let mut inner = self.new_inner(); let mut inner = self.new_inner();
inner.next_slot = self.next_slot; inner.next_slot = self.next_slot;
inner inner
} }
pub fn add_ast_substitution(&mut self, addr: Address, node: AnalyzedNode) { pub fn add_ast_substitution(&mut self, addr: Address, node: AnalyzedNode) {
self.ast_substitutions.insert(addr, Rc::new(node)); self.ast_substitutions.insert(addr, Rc::new(node));
} }
pub fn map_slot(&mut self, old_slot: LocalSlot) -> LocalSlot { pub fn map_slot(&mut self, old_slot: LocalSlot) -> LocalSlot {
if let Some(&new_slot) = self.slot_mapping.get(&old_slot) { if let Some(&new_slot) = self.slot_mapping.get(&old_slot) {
return new_slot; return new_slot;
} }
let new_slot = LocalSlot(self.next_slot); let new_slot = LocalSlot(self.next_slot);
self.slot_mapping.insert(old_slot, new_slot); self.slot_mapping.insert(old_slot, new_slot);
self.next_slot += 1; self.next_slot += 1;
new_slot new_slot
} }
pub fn map_address(&mut self, addr: Address) -> Address { pub fn map_address(&mut self, addr: Address) -> Address {
match addr { match addr {
Address::Local(slot) => Address::Local(self.map_slot(slot)), Address::Local(slot) => Address::Local(self.map_slot(slot)),
other => other, other => other,
} }
} }
pub fn add_value(&mut self, addr: Address, val: Value) { pub fn add_value(&mut self, addr: Address, val: Value) {
self.values.insert(addr, val); self.values.insert(addr, val);
} }
pub fn get_value(&self, addr: &Address) -> Option<&Value> { pub fn get_value(&self, addr: &Address) -> Option<&Value> {
self.values.get(addr) self.values.get(addr)
} }
pub fn remove_value(&mut self, addr: &Address) { pub fn remove_value(&mut self, addr: &Address) {
self.values.remove(addr); self.values.remove(addr);
} }
fn reindex_addr(&self, addr: Address, mapping: &[Option<u32>]) -> Address { fn reindex_addr(&self, addr: Address, mapping: &[Option<u32>]) -> Address {
if let Address::Upvalue(idx) = addr if let Address::Upvalue(idx) = addr
&& let Some(res) = mapping.get(idx.0 as usize) && let Some(res) = mapping.get(idx.0 as usize)
&& let Some(new_idx) = res && let Some(new_idx) = res
{ {
Address::Upvalue(UpvalueIdx(*new_idx)) Address::Upvalue(UpvalueIdx(*new_idx))
} else { } else {
addr addr
} }
} }
pub fn reindex_upvalues(&self, node_rc: Rc<AnalyzedNode>, mapping: &[Option<u32>]) -> Rc<AnalyzedNode> { pub fn reindex_upvalues(&self, node_rc: Rc<AnalyzedNode>, mapping: &[Option<u32>]) -> Rc<AnalyzedNode> {
let node = &*node_rc; let node = &*node_rc;
let (new_kind, metrics) = match &node.kind { let (new_kind, metrics) = match &node.kind {
BoundKind::Get { addr, name } => ( BoundKind::Get { addr, name } => (
BoundKind::Get { BoundKind::Get {
addr: self.reindex_addr(*addr, mapping), addr: self.reindex_addr(*addr, mapping),
name: name.clone(), name: name.clone(),
}, },
node.ty.clone(), node.ty.clone(),
), ),
BoundKind::Lambda { BoundKind::Lambda {
params, params,
upvalues, upvalues,
body, body,
positional_count, positional_count,
} => { } => {
let mut next_upvalues = Vec::new(); let mut next_upvalues = Vec::new();
for addr in upvalues { for addr in upvalues {
next_upvalues.push(self.reindex_addr(*addr, mapping)); next_upvalues.push(self.reindex_addr(*addr, mapping));
} }
( (
BoundKind::Lambda { BoundKind::Lambda {
params: params.clone(), params: params.clone(),
upvalues: next_upvalues, upvalues: next_upvalues,
body: body.clone(), body: body.clone(),
positional_count: *positional_count, positional_count: *positional_count,
}, },
node.ty.clone(), node.ty.clone(),
) )
} }
BoundKind::If { BoundKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
} => { } => {
let cond = self.reindex_upvalues(cond.clone(), mapping); let cond = self.reindex_upvalues(cond.clone(), mapping);
let then_br = self.reindex_upvalues(then_br.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)); let else_br = else_br.as_ref().map(|e| self.reindex_upvalues(e.clone(), mapping));
( (
BoundKind::If { BoundKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
}, },
node.ty.clone(), node.ty.clone(),
) )
} }
BoundKind::Block { exprs } => { BoundKind::Block { exprs } => {
let exprs = exprs let exprs = exprs
.iter() .iter()
.map(|e| self.reindex_upvalues(e.clone(), mapping)) .map(|e| self.reindex_upvalues(e.clone(), mapping))
.collect(); .collect();
(BoundKind::Block { exprs }, node.ty.clone()) (BoundKind::Block { exprs }, node.ty.clone())
} }
BoundKind::Call { callee, args } => { BoundKind::Call { callee, args } => {
let callee = self.reindex_upvalues(callee.clone(), mapping); let callee = self.reindex_upvalues(callee.clone(), mapping);
let args = self.reindex_upvalues(args.clone(), mapping); let args = self.reindex_upvalues(args.clone(), mapping);
(BoundKind::Call { callee, args }, node.ty.clone()) (BoundKind::Call { callee, args }, node.ty.clone())
} }
BoundKind::Define { BoundKind::Define {
name, name,
addr, addr,
kind, kind,
value, value,
captured_by, captured_by,
} => { } => {
let value = self.reindex_upvalues(value.clone(), mapping); let value = self.reindex_upvalues(value.clone(), mapping);
( (
BoundKind::Define { BoundKind::Define {
name: name.clone(), name: name.clone(),
addr: *addr, addr: *addr,
kind: *kind, kind: *kind,
value, value,
captured_by: captured_by.clone(), captured_by: captured_by.clone(),
}, },
node.ty.clone(), node.ty.clone(),
) )
} }
BoundKind::Set { addr, value } => { BoundKind::Set { addr, value } => {
let value = self.reindex_upvalues(value.clone(), mapping); let value = self.reindex_upvalues(value.clone(), mapping);
( (
BoundKind::Set { BoundKind::Set {
addr: self.reindex_addr(*addr, mapping), addr: self.reindex_addr(*addr, mapping),
value, value,
}, },
node.ty.clone(), node.ty.clone(),
) )
} }
BoundKind::Tuple { elements } => { BoundKind::Tuple { elements } => {
let elements = elements let elements = elements
.iter() .iter()
.map(|e| self.reindex_upvalues(e.clone(), mapping)) .map(|e| self.reindex_upvalues(e.clone(), mapping))
.collect(); .collect();
(BoundKind::Tuple { elements }, node.ty.clone()) (BoundKind::Tuple { elements }, node.ty.clone())
} }
BoundKind::Record { layout, values } => { BoundKind::Record { layout, values } => {
let values = values let values = values
.iter() .iter()
.map(|v| self.reindex_upvalues(v.clone(), mapping)) .map(|v| self.reindex_upvalues(v.clone(), mapping))
.collect(); .collect();
(BoundKind::Record { layout: layout.clone(), values }, node.ty.clone()) (BoundKind::Record { layout: layout.clone(), values }, node.ty.clone())
} }
BoundKind::Expansion { original_call, bound_expanded } => { BoundKind::Expansion { original_call, bound_expanded } => {
let bound_expanded = self.reindex_upvalues(bound_expanded.clone(), mapping); let bound_expanded = self.reindex_upvalues(bound_expanded.clone(), mapping);
( (
BoundKind::Expansion { BoundKind::Expansion {
original_call: original_call.clone(), original_call: original_call.clone(),
bound_expanded, bound_expanded,
}, },
node.ty.clone(), node.ty.clone(),
) )
} }
k => (k.clone(), node.ty.clone()), k => (k.clone(), node.ty.clone()),
}; };
Rc::new(Node { Rc::new(Node {
identity: node.identity.clone(), identity: node.identity.clone(),
kind: new_kind, kind: new_kind,
ty: metrics, ty: metrics,
}) })
} }
} }
+183 -183
View File
@@ -1,183 +1,183 @@
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, GlobalIdx}; use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, GlobalIdx};
use crate::ast::types::{Identity, Value}; use crate::ast::types::{Identity, Value};
use crate::ast::vm::Closure; use crate::ast::vm::Closure;
use std::collections::HashSet; use std::collections::HashSet;
// --- PathTracker --- // --- PathTracker ---
#[derive(Default)] #[derive(Default)]
pub struct PathTracker { pub struct PathTracker {
pub inlining_depth: usize, pub inlining_depth: usize,
pub inlining_stack: HashSet<GlobalIdx>, pub inlining_stack: HashSet<GlobalIdx>,
pub identity_stack: HashSet<Identity>, pub identity_stack: HashSet<Identity>,
} }
impl PathTracker { impl PathTracker {
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()
} }
pub fn enter_lambda(&mut self, identity: &Identity) -> bool { pub fn enter_lambda(&mut self, identity: &Identity) -> bool {
if self.identity_stack.contains(identity) { if self.identity_stack.contains(identity) {
return false; return false;
} }
self.identity_stack.insert(identity.clone()); self.identity_stack.insert(identity.clone());
true true
} }
pub fn exit_lambda(&mut self, identity: &Identity) { pub fn exit_lambda(&mut self, identity: &Identity) {
self.identity_stack.remove(identity); self.identity_stack.remove(identity);
} }
} }
// --- UsageInfo --- // --- UsageInfo ---
#[derive(Default)] #[derive(Default)]
pub struct UsageInfo { pub struct UsageInfo {
pub used: HashSet<Address>, pub used: HashSet<Address>,
pub assigned: HashSet<Address>, pub assigned: HashSet<Address>,
pub used_identities: HashSet<Identity>, pub used_identities: HashSet<Identity>,
} }
impl UsageInfo { impl UsageInfo {
pub fn is_assigned(&self, addr: &Address) -> bool { pub fn is_assigned(&self, addr: &Address) -> bool {
self.assigned.contains(addr) self.assigned.contains(addr)
} }
pub fn is_used(&self, addr: &Address) -> bool { pub fn is_used(&self, addr: &Address) -> bool {
self.used.contains(addr) self.used.contains(addr)
} }
pub fn collect(&mut self, node: &AnalyzedNode) { pub fn collect(&mut self, node: &AnalyzedNode) {
match &node.kind { match &node.kind {
BoundKind::Destructure { pattern, value } => { BoundKind::Destructure { pattern, value } => {
self.collect_pattern(pattern); self.collect_pattern(pattern);
self.collect(value); self.collect(value);
} }
BoundKind::Constant(v) => { BoundKind::Constant(v) => {
if let Value::Object(obj) = v if let Value::Object(obj) = v
&& let Some(closure) = obj.as_any().downcast_ref::<Closure>() && let Some(closure) = obj.as_any().downcast_ref::<Closure>()
{ {
self.used_identities self.used_identities
.insert(closure.function_node.identity.clone()); .insert(closure.function_node.identity.clone());
self.collect(&closure.function_node); self.collect(&closure.function_node);
} }
} }
BoundKind::Get { addr, .. } => { BoundKind::Get { addr, .. } => {
self.used.insert(*addr); self.used.insert(*addr);
} }
BoundKind::Set { addr, value } => { BoundKind::Set { addr, value } => {
self.assigned.insert(*addr); self.assigned.insert(*addr);
self.collect(value); self.collect(value);
} }
BoundKind::GetField { rec, .. } => { BoundKind::GetField { rec, .. } => {
self.collect(rec); self.collect(rec);
} }
BoundKind::Lambda { BoundKind::Lambda {
params, params,
body, body,
upvalues, upvalues,
.. ..
} => { } => {
self.used_identities.insert(node.identity.clone()); self.used_identities.insert(node.identity.clone());
let mut inner_info = UsageInfo::default(); let mut inner_info = UsageInfo::default();
inner_info.collect(params); inner_info.collect(params);
inner_info.collect(body); inner_info.collect(body);
// Propagate globals and identities // Propagate globals and identities
for addr in &inner_info.used { for addr in &inner_info.used {
if let Address::Global(_) = addr { if let Address::Global(_) = addr {
self.used.insert(*addr); self.used.insert(*addr);
} }
} }
for addr in &inner_info.assigned { for addr in &inner_info.assigned {
if let Address::Global(_) = addr { if let Address::Global(_) = addr {
self.assigned.insert(*addr); self.assigned.insert(*addr);
} }
} }
self.used_identities.extend(inner_info.used_identities); self.used_identities.extend(inner_info.used_identities);
// Map used upvalues to parent scope // Map used upvalues to parent scope
for addr in &inner_info.used { for addr in &inner_info.used {
if let Address::Upvalue(idx) = addr if let Address::Upvalue(idx) = addr
&& let Some(parent_addr) = upvalues.get(idx.0 as usize) && let Some(parent_addr) = upvalues.get(idx.0 as usize)
{ {
self.used.insert(*parent_addr); self.used.insert(*parent_addr);
} }
} }
// Map assigned upvalues to parent scope // Map assigned upvalues to parent scope
for addr in &inner_info.assigned { for addr in &inner_info.assigned {
if let Address::Upvalue(idx) = addr if let Address::Upvalue(idx) = addr
&& let Some(parent_addr) = upvalues.get(idx.0 as usize) && let Some(parent_addr) = upvalues.get(idx.0 as usize)
{ {
self.assigned.insert(*parent_addr); self.assigned.insert(*parent_addr);
} }
} }
} }
BoundKind::Block { exprs } => { BoundKind::Block { exprs } => {
for e in exprs { for e in exprs {
self.collect(e); self.collect(e);
} }
} }
BoundKind::If { BoundKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
} => { } => {
self.collect(cond); self.collect(cond);
self.collect(then_br); self.collect(then_br);
if let Some(e) = else_br { if let Some(e) = else_br {
self.collect(e); self.collect(e);
} }
} }
BoundKind::Call { callee, args } => { BoundKind::Call { callee, args } => {
self.collect(callee); self.collect(callee);
self.collect(args); self.collect(args);
} }
BoundKind::Tuple { elements } => { BoundKind::Tuple { elements } => {
for e in elements { for e in elements {
self.collect(e); self.collect(e);
} }
} }
BoundKind::Record { values, .. } => { BoundKind::Record { values, .. } => {
for v in values { for v in values {
self.collect(v); self.collect(v);
} }
} }
BoundKind::Define { addr, value, .. } => { BoundKind::Define { addr, value, .. } => {
self.assigned.insert(*addr); self.assigned.insert(*addr);
self.collect(value); self.collect(value);
} }
BoundKind::Expansion { bound_expanded, .. } => { BoundKind::Expansion { bound_expanded, .. } => {
self.collect(bound_expanded); self.collect(bound_expanded);
} }
BoundKind::Again { args } => { BoundKind::Again { args } => {
self.collect(args); self.collect(args);
} }
BoundKind::Pipe { inputs, lambda, .. } => { BoundKind::Pipe { inputs, lambda, .. } => {
for input in inputs { for input in inputs {
self.collect(input); self.collect(input);
} }
self.collect(lambda); self.collect(lambda);
} }
BoundKind::Nop BoundKind::Nop
| BoundKind::FieldAccessor(_) | BoundKind::FieldAccessor(_)
| BoundKind::Extension(_) | BoundKind::Extension(_)
| BoundKind::Error => {} | BoundKind::Error => {}
} }
} }
pub fn collect_pattern(&mut self, node: &AnalyzedNode) { pub fn collect_pattern(&mut self, node: &AnalyzedNode) {
match &node.kind { match &node.kind {
BoundKind::Define { .. } => {} BoundKind::Define { .. } => {}
BoundKind::Set { addr, .. } => { BoundKind::Set { addr, .. } => {
self.assigned.insert(*addr); self.assigned.insert(*addr);
} }
BoundKind::Tuple { elements } => { BoundKind::Tuple { elements } => {
for el in elements { for el in elements {
self.collect_pattern(el); self.collect_pattern(el);
} }
} }
_ => {} _ => {}
} }
} }
} }
+276 -276
View File
@@ -1,276 +1,276 @@
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, Node, NodeMetrics}; use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, Node, NodeMetrics};
use crate::ast::types::{Purity, Signature, StaticType, Value}; use crate::ast::types::{Purity, Signature, StaticType, Value};
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MonoCacheKey { pub struct MonoCacheKey {
pub address: Address, pub address: Address,
pub arg_types: Vec<StaticType>, pub arg_types: Vec<StaticType>,
} }
pub type CompileFunc = Rc<dyn Fn(Rc<Node>, &[StaticType]) -> Result<(Value, StaticType), String>>; 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 type RtlLookupFunc = Rc<dyn Fn(&str, &[StaticType]) -> Option<(Value, StaticType)>>;
pub trait FunctionRegistry { pub trait FunctionRegistry {
fn resolve(&self, addr: Address) -> Option<Rc<Node>>; fn resolve(&self, addr: Address) -> Option<Rc<Node>>;
fn resolve_analyzed(&self, _addr: Address) -> Option<Rc<AnalyzedNode>> { fn resolve_analyzed(&self, _addr: Address) -> Option<Rc<AnalyzedNode>> {
None None
} }
} }
pub type MonoCache = HashMap<MonoCacheKey, (Value, StaticType)>; pub type MonoCache = HashMap<MonoCacheKey, (Value, StaticType)>;
pub struct Specializer { pub struct Specializer {
pub cache: Rc<RefCell<MonoCache>>, pub cache: Rc<RefCell<MonoCache>>,
registry: Option<Rc<dyn FunctionRegistry>>, registry: Option<Rc<dyn FunctionRegistry>>,
compiler: Option<CompileFunc>, compiler: Option<CompileFunc>,
rtl_lookup: Option<RtlLookupFunc>, rtl_lookup: Option<RtlLookupFunc>,
} }
impl Specializer { impl Specializer {
pub fn new( pub fn new(
registry: Option<Rc<dyn FunctionRegistry>>, registry: Option<Rc<dyn FunctionRegistry>>,
compiler: Option<CompileFunc>, compiler: Option<CompileFunc>,
rtl_lookup: Option<RtlLookupFunc>, rtl_lookup: Option<RtlLookupFunc>,
cache: Option<Rc<RefCell<MonoCache>>>, cache: Option<Rc<RefCell<MonoCache>>>,
) -> Self { ) -> Self {
Self { Self {
cache: cache.unwrap_or_else(|| Rc::new(RefCell::new(HashMap::new()))), cache: cache.unwrap_or_else(|| Rc::new(RefCell::new(HashMap::new()))),
registry, registry,
compiler, compiler,
rtl_lookup, rtl_lookup,
} }
} }
pub fn specialize(&self, node: AnalyzedNode) -> AnalyzedNode { pub fn specialize(&self, node: AnalyzedNode) -> AnalyzedNode {
self.visit_node(node) self.visit_node(node)
} }
fn visit_node(&self, node: AnalyzedNode) -> AnalyzedNode { fn visit_node(&self, node: AnalyzedNode) -> AnalyzedNode {
let (new_kind, metrics) = match node.kind { let (new_kind, metrics) = match node.kind {
BoundKind::Call { callee, args } => { BoundKind::Call { callee, args } => {
let (new_callee, new_args, _ret_ty) = let (new_callee, new_args, _ret_ty) =
self.specialize_call_logic(callee, args, node.ty.original.ty.clone()); self.specialize_call_logic(callee, args, node.ty.original.ty.clone());
let new_metrics = node.ty.clone(); let new_metrics = node.ty.clone();
( (
BoundKind::Call { BoundKind::Call {
callee: Rc::new(new_callee), callee: Rc::new(new_callee),
args: Rc::new(new_args), args: Rc::new(new_args),
}, },
new_metrics, new_metrics,
) )
} }
BoundKind::If { BoundKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
} => { } => {
let cond = Rc::new(self.visit_node(cond.as_ref().clone())); 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 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()))); let else_br = else_br.map(|e| Rc::new(self.visit_node(e.as_ref().clone())));
( (
BoundKind::If { BoundKind::If {
cond, cond,
then_br, then_br,
else_br, else_br,
}, },
node.ty.clone(), node.ty.clone(),
) )
} }
BoundKind::Block { exprs } => { BoundKind::Block { exprs } => {
let exprs = exprs.into_iter().map(|e| Rc::new(self.visit_node(e.as_ref().clone()))).collect(); let exprs = exprs.into_iter().map(|e| Rc::new(self.visit_node(e.as_ref().clone()))).collect();
(BoundKind::Block { exprs }, node.ty.clone()) (BoundKind::Block { exprs }, node.ty.clone())
} }
BoundKind::Lambda { BoundKind::Lambda {
params, params,
upvalues, upvalues,
body, body,
positional_count, positional_count,
} => { } => {
let params = Rc::new(self.visit_node(params.as_ref().clone())); let params = Rc::new(self.visit_node(params.as_ref().clone()));
let body = Rc::new(self.visit_node(body.as_ref().clone())); let body = Rc::new(self.visit_node(body.as_ref().clone()));
( (
BoundKind::Lambda { BoundKind::Lambda {
params, params,
upvalues, upvalues,
body, body,
positional_count, positional_count,
}, },
node.ty.clone(), node.ty.clone(),
) )
} }
BoundKind::Define { BoundKind::Define {
name, name,
addr, addr,
kind, kind,
value, value,
captured_by, captured_by,
} => { } => {
let value = Rc::new(self.visit_node(value.as_ref().clone())); let value = Rc::new(self.visit_node(value.as_ref().clone()));
( (
BoundKind::Define { BoundKind::Define {
name: name.clone(), name: name.clone(),
addr, addr,
kind, kind,
value, value,
captured_by: captured_by.clone(), captured_by: captured_by.clone(),
}, },
node.ty.clone(), node.ty.clone(),
) )
} }
BoundKind::Set { addr, value } => { BoundKind::Set { addr, value } => {
let value = Rc::new(self.visit_node(value.as_ref().clone())); let value = Rc::new(self.visit_node(value.as_ref().clone()));
(BoundKind::Set { addr, value }, node.ty.clone()) (BoundKind::Set { addr, value }, node.ty.clone())
} }
BoundKind::Tuple { elements } => { BoundKind::Tuple { elements } => {
let elements = elements.into_iter().map(|e| Rc::new(self.visit_node(e.as_ref().clone()))).collect(); let elements = elements.into_iter().map(|e| Rc::new(self.visit_node(e.as_ref().clone()))).collect();
(BoundKind::Tuple { elements }, node.ty.clone()) (BoundKind::Tuple { elements }, node.ty.clone())
} }
BoundKind::Record { layout, values } => { BoundKind::Record { layout, values } => {
let values = values.into_iter().map(|v| Rc::new(self.visit_node(v.as_ref().clone()))).collect(); 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::Record { layout, values }, node.ty.clone())
} }
BoundKind::Expansion { BoundKind::Expansion {
original_call, original_call,
bound_expanded, bound_expanded,
} => { } => {
let bound_expanded = Rc::new(self.visit_node(bound_expanded.as_ref().clone())); let bound_expanded = Rc::new(self.visit_node(bound_expanded.as_ref().clone()));
( (
BoundKind::Expansion { BoundKind::Expansion {
original_call, original_call,
bound_expanded, bound_expanded,
}, },
node.ty.clone(), node.ty.clone(),
) )
} }
k => (k, node.ty.clone()), k => (k, node.ty.clone()),
}; };
Node { Node {
identity: node.identity, identity: node.identity,
kind: new_kind, kind: new_kind,
ty: metrics, ty: metrics,
} }
} }
fn specialize_call_logic( fn specialize_call_logic(
&self, &self,
callee: Rc<AnalyzedNode>, callee: Rc<AnalyzedNode>,
args: Rc<AnalyzedNode>, args: Rc<AnalyzedNode>,
original_ty: StaticType, original_ty: StaticType,
) -> (AnalyzedNode, AnalyzedNode, StaticType) { ) -> (AnalyzedNode, AnalyzedNode, StaticType) {
let new_callee = self.visit_node(callee.as_ref().clone()); let new_callee = self.visit_node(callee.as_ref().clone());
let new_args = self.visit_node(args.as_ref().clone()); let new_args = self.visit_node(args.as_ref().clone());
let address = if let BoundKind::Get { addr, .. } = &new_callee.kind { let address = if let BoundKind::Get { addr, .. } = &new_callee.kind {
*addr *addr
} else { } else {
return (new_callee, new_args, original_ty); return (new_callee, new_args, original_ty);
}; };
let arg_types: Vec<StaticType> = let arg_types: Vec<StaticType> =
if let StaticType::Tuple(elements) = &new_args.ty.original.ty { if let StaticType::Tuple(elements) = &new_args.ty.original.ty {
elements.clone() elements.clone()
} else { } else {
vec![new_args.ty.original.ty.clone()] vec![new_args.ty.original.ty.clone()]
}; };
if arg_types.iter().any(|t| matches!(t, StaticType::Any)) { if arg_types.iter().any(|t| matches!(t, StaticType::Any)) {
return (new_callee, new_args, original_ty); return (new_callee, new_args, original_ty);
} }
let key = MonoCacheKey { let key = MonoCacheKey {
address, address,
arg_types: arg_types.clone(), arg_types: arg_types.clone(),
}; };
if let Some((val, ret_ty)) = self.cache.borrow().get(&key) { if let Some((val, ret_ty)) = self.cache.borrow().get(&key) {
let specialized_callee = self.make_constant_node( let specialized_callee = self.make_constant_node(
val.clone(), val.clone(),
StaticType::Function(Box::new(Signature { StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(arg_types), params: StaticType::Tuple(arg_types),
ret: ret_ty.clone(), ret: ret_ty.clone(),
})), })),
&new_callee, &new_callee,
); );
return (specialized_callee, new_args, ret_ty.clone()); return (specialized_callee, new_args, ret_ty.clone());
} }
if let Some(rtl_lookup) = &self.rtl_lookup if let Some(rtl_lookup) = &self.rtl_lookup
&& let BoundKind::Get { name, .. } = &new_callee.kind && let BoundKind::Get { name, .. } = &new_callee.kind
&& let Some((val, ret_ty)) = rtl_lookup(&name.name, &arg_types) && let Some((val, ret_ty)) = rtl_lookup(&name.name, &arg_types)
{ {
self.cache self.cache
.borrow_mut() .borrow_mut()
.insert(key.clone(), (val.clone(), ret_ty.clone())); .insert(key.clone(), (val.clone(), ret_ty.clone()));
let specialized_callee = self.make_constant_node( let specialized_callee = self.make_constant_node(
val.clone(), val.clone(),
StaticType::Function(Box::new(Signature { StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(arg_types), params: StaticType::Tuple(arg_types),
ret: ret_ty.clone(), ret: ret_ty.clone(),
})), })),
&new_callee, &new_callee,
); );
return (specialized_callee, new_args, ret_ty); return (specialized_callee, new_args, ret_ty);
} }
if let Some(registry) = &self.registry if let Some(registry) = &self.registry
&& let Some(func_node) = registry.resolve_analyzed(address) && let Some(func_node) = registry.resolve_analyzed(address)
&& func_node.ty.is_recursive && func_node.ty.is_recursive
{ {
return (new_callee, new_args, original_ty); return (new_callee, new_args, original_ty);
} }
if let Some(compiler) = &self.compiler if let Some(compiler) = &self.compiler
&& let Some(func_node) = self.registry.as_ref().and_then(|r| r.resolve(address)) && 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) && let Ok((compiled_val, ret_ty)) = compiler(func_node, &arg_types)
{ {
self.cache self.cache
.borrow_mut() .borrow_mut()
.insert(key, (compiled_val.clone(), ret_ty.clone())); .insert(key, (compiled_val.clone(), ret_ty.clone()));
// Only replace the callee if the compiled value is actually a function/object. // 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. // 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. // 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 { if let Value::Object(_) | Value::Function(_) = &compiled_val {
let specialized_callee = self.make_constant_node( let specialized_callee = self.make_constant_node(
compiled_val, compiled_val,
StaticType::Function(Box::new(Signature { StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(arg_types), params: StaticType::Tuple(arg_types),
ret: ret_ty.clone(), ret: ret_ty.clone(),
})), })),
&new_callee, &new_callee,
); );
return (specialized_callee, new_args, ret_ty); return (specialized_callee, new_args, ret_ty);
} }
} }
(new_callee, new_args, original_ty) (new_callee, new_args, original_ty)
} }
fn make_constant_node( fn make_constant_node(
&self, &self,
val: Value, val: Value,
ty: StaticType, ty: StaticType,
template: &AnalyzedNode, template: &AnalyzedNode,
) -> AnalyzedNode { ) -> AnalyzedNode {
let typed_original = Rc::new(Node { let typed_original = Rc::new(Node {
identity: template.identity.clone(), identity: template.identity.clone(),
kind: BoundKind::Constant(val.clone()), kind: BoundKind::Constant(val.clone()),
ty: ty.clone(), ty: ty.clone(),
}); });
Node { Node {
identity: template.identity.clone(), identity: template.identity.clone(),
kind: BoundKind::Constant(val), kind: BoundKind::Constant(val),
ty: NodeMetrics { ty: NodeMetrics {
original: typed_original, original: typed_original,
purity: Purity::Pure, purity: Purity::Pure,
is_recursive: false, is_recursive: false,
}, },
} }
} }
} }
File diff suppressed because it is too large Load Diff
+30 -30
View File
@@ -1,7 +1,7 @@
use crate::ast::compiler::analyzer::Analyzer; use crate::ast::compiler::analyzer::Analyzer;
use crate::ast::compiler::binder::Binder; use crate::ast::compiler::binder::Binder;
use crate::ast::compiler::{TypeChecker, TypedNode}; 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::parser::Parser;
use crate::ast::vm::{TracingObserver, VM}; use crate::ast::vm::{TracingObserver, VM};
use std::cell::RefCell; use std::cell::RefCell;
@@ -122,10 +122,10 @@ struct RuntimeMacroEvaluator {
impl MacroEvaluator for RuntimeMacroEvaluator { impl MacroEvaluator for RuntimeMacroEvaluator {
fn evaluate( fn evaluate(
&self, &self,
node: &UntypedNode, node: &SyntaxNode,
bindings: &HashMap<Rc<str>, UntypedNode>, bindings: &HashMap<Rc<str>, SyntaxNode>,
) -> Result<Value, String> { ) -> 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) && let Some(arg_node) = bindings.get(&sym.name)
{ {
return Ok(Value::Object(Rc::new(arg_node.clone()) as Rc<dyn Object>)); return Ok(Value::Object(Rc::new(arg_node.clone()) as Rc<dyn Object>));
@@ -290,7 +290,7 @@ impl Environment {
&self, &self,
source: &str, source: &str,
base_path: &Path, base_path: &Path,
all_files: &mut Vec<(PathBuf, UntypedNode)>, all_files: &mut Vec<(PathBuf, SyntaxNode)>,
) -> Result<(), String> { ) -> Result<(), String> {
let directives = self.extract_use_directives(source); let directives = self.extract_use_directives(source);
@@ -312,7 +312,7 @@ impl Environment {
self.collect_dependencies(&lib_source, lib_base, all_files)?; self.collect_dependencies(&lib_source, lib_base, all_files)?;
let mut parser = Parser::new(&lib_source); let mut parser = Parser::new(&lib_source);
let untyped_ast = parser.parse_expression(); let syntax_ast = parser.parse_expression();
if parser.diagnostics.has_errors() { if parser.diagnostics.has_errors() {
return Err(format!( return Err(format!(
@@ -322,7 +322,7 @@ impl Environment {
)); ));
} }
all_files.push((abs_path, untyped_ast)); all_files.push((abs_path, syntax_ast));
} }
} }
Ok(()) Ok(())
@@ -358,34 +358,34 @@ impl Environment {
/// It returns the base path which can be used to resolve further things if necessary. /// 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> { 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 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 // 1. Always load the embedded system library first as a virtual module
let system_path = PathBuf::from(SYSTEM_LIB_PATH); let system_path = PathBuf::from(SYSTEM_LIB_PATH);
if !self.loaded_modules.borrow().contains(&system_path) { if !self.loaded_modules.borrow().contains(&system_path) {
self.loaded_modules.borrow_mut().insert(system_path.clone()); self.loaded_modules.borrow_mut().insert(system_path.clone());
let mut parser = Parser::new(SYSTEM_LIB_SOURCE); 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() { if parser.diagnostics.has_errors() {
return Err(format!( return Err(format!(
"Failed to parse embedded system library:\n{}", "Failed to parse embedded system library:\n{}",
parser.diagnostics.items[0].message parser.diagnostics.items[0].message
)); ));
} }
files.push((system_path, untyped_ast)); files.push((system_path, syntax_ast));
} }
// 2. Collect dependencies from the main source // 2. Collect dependencies from the main source
self.collect_dependencies(source, base_path, &mut files)?; self.collect_dependencies(source, base_path, &mut files)?;
// Pass 1: Discovery (Globals and Macros) // Pass 1: Discovery (Globals and Macros)
for (_, untyped_ast) in &files { for (_, syntax_ast) in &files {
self.discover_globals(untyped_ast); self.discover_globals(syntax_ast);
} }
// Pass 2: Compilation and Initialization // Pass 2: Compilation and Initialization
for (path, untyped_ast) in files { for (path, syntax_ast) in files {
let typed_ast = self.compile_untyped(untyped_ast).map_err(|e: String| { let typed_ast = self.compile_syntax(syntax_ast).map_err(|e: String| {
format!("Compilation error in {}:\n{}", path.display(), e) format!("Compilation error in {}:\n{}", path.display(), e)
})?; })?;
self.run_script_compiled(typed_ast).map_err(|e: String| { self.run_script_compiled(typed_ast).map_err(|e: String| {
@@ -396,10 +396,10 @@ impl Environment {
Ok(()) Ok(())
} }
fn discover_globals(&self, node: &UntypedNode) { fn discover_globals(&self, node: &SyntaxNode) {
match &node.kind { match &node.kind {
UntypedKind::Def { target, .. } => { SyntaxKind::Def { target, .. } => {
if let UntypedKind::Identifier(sym) = &target.kind { if let SyntaxKind::Identifier(sym) = &target.kind {
let mut root_scopes = self.root_scopes.borrow_mut(); let mut root_scopes = self.root_scopes.borrow_mut();
let last_idx = root_scopes.len() - 1; let last_idx = root_scopes.len() - 1;
let current_scope = &mut root_scopes[last_idx]; 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(); 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 { match &node.kind {
UntypedKind::Identifier(sym) => vec![sym.name.clone()], SyntaxKind::Identifier(sym) => vec![sym.name.clone()],
UntypedKind::Tuple { elements } => { SyntaxKind::Tuple { elements } => {
elements.iter().flat_map(extract_names).collect() elements.iter().flat_map(extract_names).collect()
} }
_ => vec![], _ => vec![],
@@ -436,7 +436,7 @@ impl Environment {
let p_names = extract_names(params); let p_names = extract_names(params);
registry.define(name.name.clone(), p_names, body.as_ref().clone()); registry.define(name.name.clone(), p_names, body.as_ref().clone());
} }
UntypedKind::Block { exprs } => { SyntaxKind::Block { exprs } => {
for expr in exprs { for expr in exprs {
self.discover_globals(expr); self.discover_globals(expr);
} }
@@ -445,8 +445,8 @@ impl Environment {
} }
} }
fn compile_pipeline(&self, untyped_ast: UntypedNode, diagnostics: &mut Diagnostics) -> Option<TypedNode> { fn compile_pipeline(&self, syntax_ast: SyntaxNode, diagnostics: &mut Diagnostics) -> Option<TypedNode> {
let expanded_ast = match self.get_expander().expand(untyped_ast) { let expanded_ast = match self.get_expander().expand(syntax_ast) {
Ok(ast) => ast, Ok(ast) => ast,
Err(e) => { Err(e) => {
diagnostics.push_error(e, None); diagnostics.push_error(e, None);
@@ -505,10 +505,10 @@ impl Environment {
Some(checker.check(&wrapped_ast, &[], diagnostics)) 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 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() { if diagnostics.has_errors() || typed_ast_opt.is_none() {
return Err(diagnostics return Err(diagnostics
@@ -627,7 +627,7 @@ impl Environment {
} }
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let untyped_ast = parser.parse_expression(); let syntax_ast = parser.parse_expression();
if !parser.at_eof() { if !parser.at_eof() {
parser parser
@@ -640,7 +640,7 @@ impl Environment {
} }
let mut diagnostics = parser.diagnostics; 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 { CompilationResult {
ast: typed_ast, ast: typed_ast,
@@ -737,7 +737,7 @@ impl Environment {
let rtl_lookup = Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args)); let rtl_lookup = Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args));
let typed_reg = self.typed_function_registry.clone(); 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 mono_cache = self.monomorph_cache.clone();
let root_values = self.root_values.clone(); let root_values = self.root_values.clone();
let root_types = self.root_types.clone(); let root_types = self.root_types.clone();
@@ -764,7 +764,7 @@ impl Environment {
let analyzed = Analyzer::analyze(&retyped_ast, &root_purity.borrow()); let analyzed = Analyzer::analyze(&retyped_ast, &root_purity.borrow());
let sub_registry = Rc::new(EnvFunctionRegistry { let sub_registry = Rc::new(EnvFunctionRegistry {
registry: untyped_reg.clone(), registry: syntax_reg.clone(),
analyzed_registry: typed_reg.clone(), analyzed_registry: typed_reg.clone(),
}); });
let sub_rtl_lookup = let sub_rtl_lookup =
+219 -219
View File
@@ -1,219 +1,219 @@
use crate::ast::types::SourceLocation; use crate::ast::types::SourceLocation;
use std::iter::Peekable; use std::iter::Peekable;
use std::rc::Rc; use std::rc::Rc;
use std::str::Chars; use std::str::Chars;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum TokenKind { pub enum TokenKind {
LeftParen, LeftParen,
RightParen, RightParen,
LeftBracket, LeftBracket,
RightBracket, RightBracket,
LeftBrace, LeftBrace,
RightBrace, RightBrace,
Quote, Quote,
Backtick, Backtick,
Tilde, Tilde,
At, At,
Identifier(Rc<str>), Identifier(Rc<str>),
Keyword(Rc<str>), Keyword(Rc<str>),
Integer(i64), Integer(i64),
Float(f64), Float(f64),
String(Rc<str>), String(Rc<str>),
EOF, EOF,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Token { pub struct Token {
pub kind: TokenKind, pub kind: TokenKind,
pub location: SourceLocation, pub location: SourceLocation,
} }
pub struct Lexer<'a> { pub struct Lexer<'a> {
input: Peekable<Chars<'a>>, input: Peekable<Chars<'a>>,
line: u32, line: u32,
col: u32, col: u32,
} }
impl<'a> Lexer<'a> { impl<'a> Lexer<'a> {
pub fn new(input: &'a str) -> Self { pub fn new(input: &'a str) -> Self {
Self { Self {
input: input.chars().peekable(), input: input.chars().peekable(),
line: 1, line: 1,
col: 1, col: 1,
} }
} }
pub fn next_token(&mut self) -> Result<Token, String> { pub fn next_token(&mut self) -> Result<Token, String> {
self.skip_whitespace(); self.skip_whitespace();
let start_location = SourceLocation { let start_location = SourceLocation {
line: self.line, line: self.line,
col: self.col, col: self.col,
}; };
let char = match self.input.next() { let char = match self.input.next() {
Some(c) => { Some(c) => {
let current_char = c; let current_char = c;
self.col += 1; self.col += 1;
current_char current_char
} }
None => { None => {
return Ok(Token { return Ok(Token {
kind: TokenKind::EOF, kind: TokenKind::EOF,
location: start_location, location: start_location,
}); });
} }
}; };
let kind = match char { let kind = match char {
'(' => TokenKind::LeftParen, '(' => TokenKind::LeftParen,
')' => TokenKind::RightParen, ')' => TokenKind::RightParen,
'[' => TokenKind::LeftBracket, '[' => TokenKind::LeftBracket,
']' => TokenKind::RightBracket, ']' => TokenKind::RightBracket,
'{' => TokenKind::LeftBrace, '{' => TokenKind::LeftBrace,
'}' => TokenKind::RightBrace, '}' => TokenKind::RightBrace,
'\'' => TokenKind::Quote, '\'' => TokenKind::Quote,
'`' => TokenKind::Backtick, '`' => TokenKind::Backtick,
'~' => TokenKind::Tilde, '~' => TokenKind::Tilde,
'@' => TokenKind::At, '@' => TokenKind::At,
':' => { ':' => {
let id = self.read_while(|c| !c.is_whitespace() && !"()[]{}\"'`~@;".contains(c)); let id = self.read_while(|c| !c.is_whitespace() && !"()[]{}\"'`~@;".contains(c));
TokenKind::Keyword(Rc::from(id)) TokenKind::Keyword(Rc::from(id))
} }
'"' => TokenKind::String(Rc::from(self.read_string()?)), '"' => TokenKind::String(Rc::from(self.read_string()?)),
c if c.is_ascii_digit() c if c.is_ascii_digit()
|| (c == '-' && self.input.peek().is_some_and(|p| p.is_ascii_digit())) => || (c == '-' && self.input.peek().is_some_and(|p| p.is_ascii_digit())) =>
{ {
let mut num_str = String::from(c); let mut num_str = String::from(c);
while let Some(&next) = self.peek() { while let Some(&next) = self.peek() {
if next.is_ascii_digit() || next == '.' { if next.is_ascii_digit() || next == '.' {
num_str.push(self.input.next().unwrap()); num_str.push(self.input.next().unwrap());
self.col += 1; self.col += 1;
} else { } else {
break; break;
} }
} }
if num_str.contains('.') { if num_str.contains('.') {
TokenKind::Float(num_str.parse().map_err(|_| "Invalid float format")?) TokenKind::Float(num_str.parse().map_err(|_| "Invalid float format")?)
} else { } else {
TokenKind::Integer(num_str.parse().map_err(|_| "Invalid integer format")?) TokenKind::Integer(num_str.parse().map_err(|_| "Invalid integer format")?)
} }
} }
c if is_ident_start(c) => { c if is_ident_start(c) => {
let mut id = String::from(c); let mut id = String::from(c);
id.push_str(&self.read_while(is_ident_char)); id.push_str(&self.read_while(is_ident_char));
TokenKind::Identifier(Rc::from(id)) TokenKind::Identifier(Rc::from(id))
} }
_ => { _ => {
return Err(format!( return Err(format!(
"Unexpected character: {} at {}:{}", "Unexpected character: {} at {}:{}",
char, char,
self.line, self.line,
self.col - 1 self.col - 1
)); ));
} }
}; };
Ok(Token { Ok(Token {
kind, kind,
location: start_location, location: start_location,
}) })
} }
fn peek(&mut self) -> Option<&char> { fn peek(&mut self) -> Option<&char> {
self.input.peek() self.input.peek()
} }
fn skip_whitespace(&mut self) { fn skip_whitespace(&mut self) {
while let Some(&c) = self.peek() { while let Some(&c) = self.peek() {
if c.is_whitespace() || is_invisible(c) || c == ',' { if c.is_whitespace() || is_invisible(c) || c == ',' {
if c == '\n' { if c == '\n' {
self.line += 1; self.line += 1;
self.col = 1; self.col = 1;
} else { } else {
self.col += 1; self.col += 1;
} }
self.input.next(); self.input.next();
} else if c == ';' || c == '#' { } else if c == ';' || c == '#' {
for c in self.input.by_ref() { for c in self.input.by_ref() {
if c == '\n' { if c == '\n' {
self.line += 1; self.line += 1;
self.col = 1; self.col = 1;
break; break;
} }
} }
} else { } else {
break; break;
} }
} }
} }
fn read_while<F>(&mut self, mut predicate: F) -> String fn read_while<F>(&mut self, mut predicate: F) -> String
where where
F: FnMut(char) -> bool, F: FnMut(char) -> bool,
{ {
let mut s = String::new(); let mut s = String::new();
while let Some(&c) = self.peek() { while let Some(&c) = self.peek() {
if predicate(c) { if predicate(c) {
s.push(self.input.next().unwrap()); s.push(self.input.next().unwrap());
self.col += 1; self.col += 1;
} else { } else {
break; break;
} }
} }
s s
} }
fn read_string(&mut self) -> Result<String, String> { fn read_string(&mut self) -> Result<String, String> {
let mut s = String::new(); let mut s = String::new();
while let Some(c) = self.input.next() { while let Some(c) = self.input.next() {
self.col += 1; self.col += 1;
match c { match c {
'"' => return Ok(s), '"' => return Ok(s),
'\\' => { '\\' => {
let next = self.input.next().ok_or("Unterminated string escape")?; let next = self.input.next().ok_or("Unterminated string escape")?;
self.col += 1; self.col += 1;
match next { match next {
'n' => s.push('\n'), 'n' => s.push('\n'),
'r' => s.push('\r'), 'r' => s.push('\r'),
't' => s.push('\t'), 't' => s.push('\t'),
'\\' => s.push('\\'), '\\' => s.push('\\'),
'"' => s.push('"'), '"' => s.push('"'),
_ => s.push(next), _ => s.push(next),
} }
} }
'\n' => { '\n' => {
self.line += 1; self.line += 1;
self.col = 1; self.col = 1;
s.push(c); s.push(c);
} }
_ => s.push(c), _ => s.push(c),
} }
} }
Err("Unterminated string".to_string()) Err("Unterminated string".to_string())
} }
} }
fn is_ident_start(c: char) -> bool { fn is_ident_start(c: char) -> bool {
c.is_alphabetic() || "+-*/<=>!$%&_?.".contains(c) c.is_alphabetic() || "+-*/<=>!$%&_?.".contains(c)
} }
fn is_ident_char(c: char) -> bool { fn is_ident_char(c: char) -> bool {
is_ident_start(c) || c.is_ascii_digit() is_ident_start(c) || c.is_ascii_digit()
} }
/// Returns true if the character is an invisible control character /// Returns true if the character is an invisible control character
/// that should be ignored by the lexer (like BOM or Zero Width Space). /// that should be ignored by the lexer (like BOM or Zero Width Space).
fn is_invisible(c: char) -> bool { fn is_invisible(c: char) -> bool {
match c { match c {
'\u{FEFF}' | // BOM '\u{FEFF}' | // BOM
'\u{200B}' | // Zero Width Space '\u{200B}' | // Zero Width Space
'\u{200C}' | // Zero Width Non-Joiner '\u{200C}' | // Zero Width Non-Joiner
'\u{200D}' | // Zero Width Joiner '\u{200D}' | // Zero Width Joiner
'\u{2060}' // Word Joiner '\u{2060}' // Word Joiner
=> true, => true,
_ => false, _ => false,
} }
} }
+9 -9
View File
@@ -1,9 +1,9 @@
pub mod compiler; pub mod compiler;
pub mod diagnostics; pub mod diagnostics;
pub mod environment; pub mod environment;
pub mod lexer; pub mod lexer;
pub mod nodes; pub mod nodes;
pub mod parser; pub mod parser;
pub mod rtl; pub mod rtl;
pub mod types; pub mod types;
pub mod vm; pub mod vm;
+134 -134
View File
@@ -1,134 +1,134 @@
use crate::ast::types::{Identity, Object, Value}; use crate::ast::types::{Identity, Object, Value};
use std::any::Any; use std::any::Any;
use std::fmt::Debug; use std::fmt::Debug;
use std::rc::Rc; use std::rc::Rc;
/// A name with an optional context for macro hygiene. /// A name with an optional context for macro hygiene.
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Symbol { pub struct Symbol {
pub name: Rc<str>, pub name: Rc<str>,
/// Points to the identity of the Expansion node if this symbol /// Points to the identity of the Expansion node if this symbol
/// was created/referenced inside a macro expansion. /// was created/referenced inside a macro expansion.
pub context: Option<Identity>, pub context: Option<Identity>,
} }
impl From<Rc<str>> for Symbol { impl From<Rc<str>> for Symbol {
fn from(name: Rc<str>) -> Self { fn from(name: Rc<str>) -> Self {
Self { Self {
name, name,
context: None, context: None,
} }
} }
} }
impl From<&str> for Symbol { impl From<&str> for Symbol {
fn from(name: &str) -> Self { fn from(name: &str) -> Self {
Self { Self {
name: Rc::from(name), name: Rc::from(name),
context: None, context: None,
} }
} }
} }
/// A parser AST Node wrapper to preserve identity and metadata /// A parser AST Node wrapper to preserve identity and metadata
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct UntypedNode { pub struct SyntaxNode {
pub identity: Identity, pub identity: Identity,
pub kind: UntypedKind, pub kind: SyntaxKind,
} }
impl Object for UntypedNode { impl Object for SyntaxNode {
fn type_name(&self) -> &'static str { fn type_name(&self) -> &'static str {
"ast-node" "ast-node"
} }
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn Any {
self self
} }
} }
/// The base for custom node types (extensions) /// The base for custom node types (extensions)
pub trait CustomNode: Debug { pub trait CustomNode: Debug {
fn display_name(&self) -> &'static str; fn display_name(&self) -> &'static str;
fn clone_box(&self) -> Box<dyn CustomNode>; fn clone_box(&self) -> Box<dyn CustomNode>;
} }
impl Clone for Box<dyn CustomNode> { impl Clone for Box<dyn CustomNode> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
self.clone_box() self.clone_box()
} }
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum UntypedKind { pub enum SyntaxKind {
Nop, Nop,
Constant(Value), Constant(Value),
/// A general identifier (used for both references and declarations in the untyped AST). /// A general identifier (used for both references and declarations in the syntax AST).
Identifier(Symbol), Identifier(Symbol),
/// A first-class field accessor (e.g. .name) /// A first-class field accessor (e.g. .name)
FieldAccessor(crate::ast::types::Keyword), FieldAccessor(crate::ast::types::Keyword),
If { If {
cond: Box<UntypedNode>, cond: Box<SyntaxNode>,
then_br: Box<UntypedNode>, then_br: Box<SyntaxNode>,
else_br: Option<Box<UntypedNode>>, else_br: Option<Box<SyntaxNode>>,
}, },
Def { Def {
target: Box<UntypedNode>, target: Box<SyntaxNode>,
value: Box<UntypedNode>, value: Box<SyntaxNode>,
}, },
Assign { Assign {
target: Box<UntypedNode>, target: Box<SyntaxNode>,
value: Box<UntypedNode>, value: Box<SyntaxNode>,
}, },
Lambda { Lambda {
params: Box<UntypedNode>, params: Box<SyntaxNode>,
body: Rc<UntypedNode>, body: Rc<SyntaxNode>,
}, },
Call { Call {
callee: Box<UntypedNode>, callee: Box<SyntaxNode>,
args: Box<UntypedNode>, args: Box<SyntaxNode>,
}, },
Again { Again {
args: Box<UntypedNode>, args: Box<SyntaxNode>,
}, },
Pipe { Pipe {
inputs: Vec<UntypedNode>, inputs: Vec<SyntaxNode>,
lambda: Box<UntypedNode>, lambda: Box<SyntaxNode>,
}, },
Block { Block {
exprs: Vec<UntypedNode>, exprs: Vec<SyntaxNode>,
}, },
Tuple { Tuple {
elements: Vec<UntypedNode>, elements: Vec<SyntaxNode>,
}, },
Record { Record {
fields: Vec<(UntypedNode, UntypedNode)>, fields: Vec<(SyntaxNode, SyntaxNode)>,
}, },
/// A macro declaration that can be expanded at compile time. /// A macro declaration that can be expanded at compile time.
MacroDecl { MacroDecl {
name: Symbol, name: Symbol,
params: Box<UntypedNode>, params: Box<SyntaxNode>,
body: Box<UntypedNode>, body: Box<SyntaxNode>,
}, },
/// A template for AST nodes, allowing for substitutions. (Quasiquote) /// A template for AST nodes, allowing for substitutions. (Quasiquote)
Template(Box<UntypedNode>), Template(Box<SyntaxNode>),
/// A placeholder inside a template to be replaced by a node or value. (Unquote) /// A placeholder inside a template to be replaced by a node or value. (Unquote)
Placeholder(Box<UntypedNode>), Placeholder(Box<SyntaxNode>),
/// A placeholder that flattens a sequence or merges a record into its surroundings. (Splice) /// A placeholder that flattens a sequence or merges a record into its surroundings. (Splice)
Splice(Box<UntypedNode>), Splice(Box<SyntaxNode>),
/// Represents an expanded macro call, preserving the original call for debugging. /// Represents an expanded macro call, preserving the original call for debugging.
Expansion { Expansion {
/// The original call from the source AST. /// The original call from the source AST.
call: Box<UntypedNode>, call: Box<SyntaxNode>,
/// The resulting AST after macro expansion. /// The resulting AST after macro expansion.
expanded: Box<UntypedNode>, expanded: Box<SyntaxNode>,
}, },
/// A diagnostic poison node, allowing compilation to continue after an error. /// A diagnostic poison node, allowing compilation to continue after an error.
Error, Error,
Extension(Box<dyn CustomNode>), Extension(Box<dyn CustomNode>),
} }
impl PartialEq for Box<dyn CustomNode> { impl PartialEq for Box<dyn CustomNode> {
fn eq(&self, _other: &Self) -> bool { fn eq(&self, _other: &Self) -> bool {
false false
} }
} }
+522 -522
View File
File diff suppressed because it is too large Load Diff
+509 -509
View File
File diff suppressed because it is too large Load Diff
+39 -39
View File
@@ -1,39 +1,39 @@
use crate::ast::environment::Environment; use crate::ast::environment::Environment;
use crate::ast::types::{Purity, Signature, StaticType, Value}; use crate::ast::types::{Purity, Signature, StaticType, Value};
use chrono::{NaiveDate, NaiveDateTime}; use chrono::{NaiveDate, NaiveDateTime};
pub fn register(env: &Environment) { pub fn register(env: &Environment) {
let date_ty = StaticType::Function(Box::new(Signature { let date_ty = StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(vec![StaticType::Text]), params: StaticType::Tuple(vec![StaticType::Text]),
ret: StaticType::DateTime, ret: StaticType::DateTime,
})); }));
env.register_native_fn("date", date_ty, Purity::Pure, |args| { env.register_native_fn("date", date_ty, Purity::Pure, |args| {
if let Value::Text(s) = &args[0] { if let Value::Text(s) = &args[0] {
// Try parse YYYY-MM-DD // Try parse YYYY-MM-DD
if let Ok(dt) = NaiveDate::parse_from_str(s, "%Y-%m-%d") { if let Ok(dt) = NaiveDate::parse_from_str(s, "%Y-%m-%d") {
let ts = dt let ts = dt
.and_hms_opt(0, 0, 0) .and_hms_opt(0, 0, 0)
.unwrap() .unwrap()
.and_utc() .and_utc()
.timestamp_millis(); .timestamp_millis();
return Value::DateTime(ts); return Value::DateTime(ts);
} }
// Try parse YYYY-MM-DD HH:MM:SS // Try parse YYYY-MM-DD HH:MM:SS
if let Ok(dt) = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S") { if let Ok(dt) = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S") {
return Value::DateTime(dt.and_utc().timestamp_millis()); return Value::DateTime(dt.and_utc().timestamp_millis());
} }
} }
Value::Void Value::Void
}); });
let now_ty = StaticType::Function(Box::new(Signature { let now_ty = StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(vec![]), params: StaticType::Tuple(vec![]),
ret: StaticType::DateTime, ret: StaticType::DateTime,
})); }));
env.register_native_fn("now", now_ty, Purity::SideEffectFree, |_| { env.register_native_fn("now", now_ty, Purity::SideEffectFree, |_| {
let ts = chrono::Utc::now().timestamp_millis(); let ts = chrono::Utc::now().timestamp_millis();
Value::DateTime(ts) Value::DateTime(ts)
}); });
} }
+115 -115
View File
@@ -1,115 +1,115 @@
use crate::ast::types::{Purity, StaticType, Value}; use crate::ast::types::{Purity, StaticType, Value};
/// Looks up a specialized intrinsic function for the given operator and argument types. /// Looks up a specialized intrinsic function for the given operator and argument types.
/// Returns (Executable Value, Return Type) if a fast-path exists. /// Returns (Executable Value, Return Type) if a fast-path exists.
pub fn lookup(name: &str, args: &[StaticType]) -> Option<(Value, StaticType)> { pub fn lookup(name: &str, args: &[StaticType]) -> Option<(Value, StaticType)> {
match (name, args) { match (name, args) {
// --- Integer Arithmetic --- // --- Integer Arithmetic ---
("+", [StaticType::Int, StaticType::Int]) => Some(( ("+", [StaticType::Int, StaticType::Int]) => Some((
Value::make_function(Purity::Pure, |args| { Value::make_function(Purity::Pure, |args| {
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) { if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
Value::Int(a + b) Value::Int(a + b)
} else { } else {
Value::Int(0) // Should not happen if type checker works Value::Int(0) // Should not happen if type checker works
} }
}), }),
StaticType::Int, StaticType::Int,
)), )),
("-", [StaticType::Int, StaticType::Int]) => Some(( ("-", [StaticType::Int, StaticType::Int]) => Some((
Value::make_function(Purity::Pure, |args| { Value::make_function(Purity::Pure, |args| {
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) { if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
Value::Int(a - b) Value::Int(a - b)
} else { } else {
Value::Int(0) Value::Int(0)
} }
}), }),
StaticType::Int, StaticType::Int,
)), )),
("-", [StaticType::Int, StaticType::Int, StaticType::Int]) => Some(( ("-", [StaticType::Int, StaticType::Int, StaticType::Int]) => Some((
// Variadic optimization for 3 args (common in some Lisp dialects, though - usually is binary/unary) // Variadic optimization for 3 args (common in some Lisp dialects, though - usually is binary/unary)
// MyC's core.rs supports variadic subtraction. // MyC's core.rs supports variadic subtraction.
Value::make_function(Purity::Pure, |args| { Value::make_function(Purity::Pure, |args| {
let a = match args[0] { let a = match args[0] {
Value::Int(i) => i, Value::Int(i) => i,
_ => 0, _ => 0,
}; };
let b = match args[1] { let b = match args[1] {
Value::Int(i) => i, Value::Int(i) => i,
_ => 0, _ => 0,
}; };
let c = match args[2] { let c = match args[2] {
Value::Int(i) => i, Value::Int(i) => i,
_ => 0, _ => 0,
}; };
Value::Int(a - b - c) Value::Int(a - b - c)
}), }),
StaticType::Int, StaticType::Int,
)), )),
("*", [StaticType::Int, StaticType::Int]) => Some(( ("*", [StaticType::Int, StaticType::Int]) => Some((
Value::make_function(Purity::Pure, |args| { Value::make_function(Purity::Pure, |args| {
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) { if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
Value::Int(a * b) Value::Int(a * b)
} else { } else {
Value::Int(0) Value::Int(0)
} }
}), }),
StaticType::Int, StaticType::Int,
)), )),
// --- Integer Comparison --- // --- Integer Comparison ---
("<=", [StaticType::Int, StaticType::Int]) => Some(( ("<=", [StaticType::Int, StaticType::Int]) => Some((
Value::make_function(Purity::Pure, |args| { Value::make_function(Purity::Pure, |args| {
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) { if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
Value::Bool(a <= b) Value::Bool(a <= b)
} else { } else {
Value::Bool(false) Value::Bool(false)
} }
}), }),
StaticType::Bool, StaticType::Bool,
)), )),
("<", [StaticType::Int, StaticType::Int]) => Some(( ("<", [StaticType::Int, StaticType::Int]) => Some((
Value::make_function(Purity::Pure, |args| { Value::make_function(Purity::Pure, |args| {
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) { if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
Value::Bool(a < b) Value::Bool(a < b)
} else { } else {
Value::Bool(false) Value::Bool(false)
} }
}), }),
StaticType::Bool, StaticType::Bool,
)), )),
(">", [StaticType::Int, StaticType::Int]) => Some(( (">", [StaticType::Int, StaticType::Int]) => Some((
Value::make_function(Purity::Pure, |args| { Value::make_function(Purity::Pure, |args| {
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) { if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
Value::Bool(a > b) Value::Bool(a > b)
} else { } else {
Value::Bool(false) Value::Bool(false)
} }
}), }),
StaticType::Bool, StaticType::Bool,
)), )),
(">=", [StaticType::Int, StaticType::Int]) => Some(( (">=", [StaticType::Int, StaticType::Int]) => Some((
Value::make_function(Purity::Pure, |args| { Value::make_function(Purity::Pure, |args| {
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) { if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
Value::Bool(a >= b) Value::Bool(a >= b)
} else { } else {
Value::Bool(false) Value::Bool(false)
} }
}), }),
StaticType::Bool, StaticType::Bool,
)), )),
("=", [StaticType::Int, StaticType::Int]) => Some(( ("=", [StaticType::Int, StaticType::Int]) => Some((
Value::make_function(Purity::Pure, |args| { Value::make_function(Purity::Pure, |args| {
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) { if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
Value::Bool(a == b) Value::Bool(a == b)
} else { } else {
Value::Bool(false) Value::Bool(false)
} }
}), }),
StaticType::Bool, StaticType::Bool,
)), )),
// --- Constant Unary for -1 (decrement optimization) --- // --- Constant Unary for -1 (decrement optimization) ---
// Special case: tak uses (- x 1). The specializer sees Call("-", [Int, Int]). // Special case: tak uses (- x 1). The specializer sees Call("-", [Int, Int]).
_ => None, _ => None,
} }
} }
+176 -176
View File
@@ -1,176 +1,176 @@
use crate::ast::environment::Environment; use crate::ast::environment::Environment;
use crate::ast::types::{Purity, Signature, StaticType, Value}; use crate::ast::types::{Purity, Signature, StaticType, Value};
use std::f64::consts; use std::f64::consts;
pub fn register(env: &Environment) { pub fn register(env: &Environment) {
// Constants // Constants
env.register_constant("PI", StaticType::Float, Value::Float(consts::PI)); env.register_constant("PI", StaticType::Float, Value::Float(consts::PI));
env.register_constant("E", StaticType::Float, Value::Float(consts::E)); env.register_constant("E", StaticType::Float, Value::Float(consts::E));
// Helper to get f64 from Value (Int or Float) // Helper to get f64 from Value (Int or Float)
fn to_f64(v: &Value) -> Option<f64> { fn to_f64(v: &Value) -> Option<f64> {
match v { match v {
Value::Float(f) => Some(*f), Value::Float(f) => Some(*f),
Value::Int(i) => Some(*i as f64), Value::Int(i) => Some(*i as f64),
_ => None, _ => None,
} }
} }
// Unary functions (float -> float) // Unary functions (float -> float)
let register_unary = |name: &'static str, f: fn(f64) -> f64| { let register_unary = |name: &'static str, f: fn(f64) -> f64| {
let ty = StaticType::Function(Box::new(Signature { let ty = StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(vec![StaticType::Float]), params: StaticType::Tuple(vec![StaticType::Float]),
ret: StaticType::Float, ret: StaticType::Float,
})); }));
env.register_native_fn(name, ty, Purity::Pure, move |args| { env.register_native_fn(name, ty, Purity::Pure, move |args| {
if let Some(x) = to_f64(&args[0]) { if let Some(x) = to_f64(&args[0]) {
Value::Float(f(x)) Value::Float(f(x))
} else { } else {
Value::Void Value::Void
} }
}); });
}; };
// Unary functions (float -> int) // Unary functions (float -> int)
let register_to_int = |name: &'static str, f: fn(f64) -> f64| { let register_to_int = |name: &'static str, f: fn(f64) -> f64| {
let ty = StaticType::Function(Box::new(Signature { let ty = StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(vec![StaticType::Float]), params: StaticType::Tuple(vec![StaticType::Float]),
ret: StaticType::Int, ret: StaticType::Int,
})); }));
env.register_native_fn(name, ty, Purity::Pure, move |args| { env.register_native_fn(name, ty, Purity::Pure, move |args| {
if let Some(x) = to_f64(&args[0]) { if let Some(x) = to_f64(&args[0]) {
Value::Int(f(x) as i64) Value::Int(f(x) as i64)
} else { } else {
Value::Void Value::Void
} }
}); });
}; };
register_unary("sqrt", f64::sqrt); register_unary("sqrt", f64::sqrt);
register_unary("sin", f64::sin); register_unary("sin", f64::sin);
register_unary("cos", f64::cos); register_unary("cos", f64::cos);
register_unary("tan", f64::tan); register_unary("tan", f64::tan);
register_unary("asin", f64::asin); register_unary("asin", f64::asin);
register_unary("acos", f64::acos); register_unary("acos", f64::acos);
register_unary("atan", f64::atan); register_unary("atan", f64::atan);
register_unary("exp", f64::exp); register_unary("exp", f64::exp);
register_unary("ln", f64::ln); register_unary("ln", f64::ln);
register_unary("log10", f64::log10); register_unary("log10", f64::log10);
register_unary("fract", f64::fract); register_unary("fract", f64::fract);
// Rounding functions returning Int // Rounding functions returning Int
register_to_int("round", f64::round); register_to_int("round", f64::round);
register_to_int("floor", f64::floor); register_to_int("floor", f64::floor);
register_to_int("ceil", f64::ceil); register_to_int("ceil", f64::ceil);
register_to_int("trunc", f64::trunc); register_to_int("trunc", f64::trunc);
// Binary functions (float, float -> float) // Binary functions (float, float -> float)
let register_binary = |name: &'static str, f: fn(f64, f64) -> f64| { let register_binary = |name: &'static str, f: fn(f64, f64) -> f64| {
let ty = StaticType::Function(Box::new(Signature { let ty = StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]), params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
ret: StaticType::Float, ret: StaticType::Float,
})); }));
env.register_native_fn(name, ty, Purity::Pure, move |args| { env.register_native_fn(name, ty, Purity::Pure, move |args| {
if let (Some(a), Some(b)) = (to_f64(&args[0]), to_f64(&args[1])) { if let (Some(a), Some(b)) = (to_f64(&args[0]), to_f64(&args[1])) {
Value::Float(f(a, b)) Value::Float(f(a, b))
} else { } else {
Value::Void Value::Void
} }
}); });
}; };
register_binary("atan2", f64::atan2); register_binary("atan2", f64::atan2);
register_binary("pow", f64::powf); register_binary("pow", f64::powf);
register_binary("log", f64::log); register_binary("log", f64::log);
// Specialized abs to handle Int -> Int, Float -> Float // Specialized abs to handle Int -> Int, Float -> Float
let abs_ty = StaticType::Function(Box::new(Signature { let abs_ty = StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(vec![StaticType::Any]), params: StaticType::Tuple(vec![StaticType::Any]),
ret: StaticType::Any, ret: StaticType::Any,
})); }));
env.register_native_fn("abs", abs_ty, Purity::Pure, |args| { env.register_native_fn("abs", abs_ty, Purity::Pure, |args| {
if args.len() != 1 { if args.len() != 1 {
return Value::Void; return Value::Void;
} }
match args[0] { match args[0] {
Value::Int(i) => Value::Int(i.abs()), Value::Int(i) => Value::Int(i.abs()),
Value::Float(f) => Value::Float(f.abs()), Value::Float(f) => Value::Float(f.abs()),
_ => Value::Void, _ => Value::Void,
} }
}); });
// Variadic min / max // Variadic min / max
let variadic_ty = StaticType::Function(Box::new(Signature { let variadic_ty = StaticType::Function(Box::new(Signature {
params: StaticType::Any, params: StaticType::Any,
ret: StaticType::Any, ret: StaticType::Any,
})); }));
env.register_native_fn("min", variadic_ty.clone(), Purity::Pure, |args| { env.register_native_fn("min", variadic_ty.clone(), Purity::Pure, |args| {
if args.is_empty() { if args.is_empty() {
return Value::Void; return Value::Void;
} }
let mut best = args[0].clone(); let mut best = args[0].clone();
for arg in &args[1..] { for arg in &args[1..] {
if let Some(ord) = arg.partial_cmp(&best) if let Some(ord) = arg.partial_cmp(&best)
&& ord == std::cmp::Ordering::Less && ord == std::cmp::Ordering::Less
{ {
best = arg.clone(); best = arg.clone();
} }
} }
best best
}); });
env.register_native_fn("max", variadic_ty, Purity::Pure, |args| { env.register_native_fn("max", variadic_ty, Purity::Pure, |args| {
if args.is_empty() { if args.is_empty() {
return Value::Void; return Value::Void;
} }
let mut best = args[0].clone(); let mut best = args[0].clone();
for arg in &args[1..] { for arg in &args[1..] {
if let Some(ord) = arg.partial_cmp(&best) if let Some(ord) = arg.partial_cmp(&best)
&& ord == std::cmp::Ordering::Greater && ord == std::cmp::Ordering::Greater
{ {
best = arg.clone(); best = arg.clone();
} }
} }
best best
}); });
// Random generator factory (Closure-based) // Random generator factory (Closure-based)
let generator_sig = Signature { let generator_sig = Signature {
params: StaticType::Tuple(vec![]), params: StaticType::Tuple(vec![]),
ret: StaticType::Float, ret: StaticType::Float,
}; };
let make_random_ty = StaticType::FunctionOverloads(vec![ let make_random_ty = StaticType::FunctionOverloads(vec![
Signature { Signature {
params: StaticType::Tuple(vec![]), params: StaticType::Tuple(vec![]),
ret: StaticType::Function(Box::new(generator_sig.clone())), ret: StaticType::Function(Box::new(generator_sig.clone())),
}, },
Signature { Signature {
params: StaticType::Tuple(vec![StaticType::Int]), params: StaticType::Tuple(vec![StaticType::Int]),
ret: StaticType::Function(Box::new(generator_sig)), ret: StaticType::Function(Box::new(generator_sig)),
}, },
]); ]);
env.register_native_fn( env.register_native_fn(
"make-random", "make-random",
make_random_ty, make_random_ty,
Purity::SideEffectFree, Purity::SideEffectFree,
|args| { |args| {
let rng = if args.is_empty() { let rng = if args.is_empty() {
fastrand::Rng::new() fastrand::Rng::new()
} else if let Value::Int(s) = args[0] { } else if let Value::Int(s) = args[0] {
fastrand::Rng::with_seed(s as u64) fastrand::Rng::with_seed(s as u64)
} else { } else {
fastrand::Rng::new() fastrand::Rng::new()
}; };
let prng = std::rc::Rc::new(std::cell::RefCell::new(rng)); let prng = std::rc::Rc::new(std::cell::RefCell::new(rng));
Value::Function(std::rc::Rc::new(crate::ast::types::NativeFunction { Value::Function(std::rc::Rc::new(crate::ast::types::NativeFunction {
func: std::rc::Rc::new(move |_args| Value::Float(prng.borrow_mut().f64())), func: std::rc::Rc::new(move |_args| Value::Float(prng.borrow_mut().f64())),
purity: Purity::Impure, purity: Purity::Impure,
})) }))
}, },
); );
} }
+17 -17
View File
@@ -1,17 +1,17 @@
pub mod core; pub mod core;
pub mod datetime; pub mod datetime;
pub mod intrinsics; pub mod intrinsics;
pub mod math; pub mod math;
pub mod series; pub mod series;
pub mod streams; pub mod streams;
pub mod type_registry; pub mod type_registry;
use crate::ast::environment::Environment; use crate::ast::environment::Environment;
pub fn register(env: &Environment) { pub fn register(env: &Environment) {
core::register(env); core::register(env);
datetime::register(env); datetime::register(env);
math::register(env); math::register(env);
series::register(env); series::register(env);
streams::register(env); streams::register(env);
} }
+648 -648
View File
File diff suppressed because it is too large Load Diff
+536 -536
View File
File diff suppressed because it is too large Load Diff
+273 -273
View File
@@ -1,273 +1,273 @@
use crate::ast::types::{Keyword, Purity, Signature, StaticType, Value}; use crate::ast::types::{Keyword, Purity, Signature, StaticType, Value};
use std::any::TypeId; use std::any::TypeId;
use std::collections::HashMap; use std::collections::HashMap;
/// Represents a Rust type that can be exposed to the script environment. /// Represents a Rust type that can be exposed to the script environment.
pub trait Scriptable: 'static + Sized { pub trait Scriptable: 'static + Sized {
/// Returns the name of the type for debugging/AST. /// Returns the name of the type for debugging/AST.
fn type_name() -> &'static str; fn type_name() -> &'static str;
/// Returns the static type definition (methods, properties) for the AST. /// Returns the static type definition (methods, properties) for the AST.
fn static_type() -> StaticType; fn static_type() -> StaticType;
/// Wraps the instance into a Value (usually a Record of closures). /// Wraps the instance into a Value (usually a Record of closures).
fn wrap(self) -> Value; fn wrap(self) -> Value;
} }
/// A registry for tracking registered types and their static definitions. /// A registry for tracking registered types and their static definitions.
pub struct TypeRegistry { pub struct TypeRegistry {
known_types: HashMap<TypeId, StaticType>, known_types: HashMap<TypeId, StaticType>,
} }
impl Default for TypeRegistry { impl Default for TypeRegistry {
fn default() -> Self { fn default() -> Self {
Self::new() Self::new()
} }
} }
impl TypeRegistry { impl TypeRegistry {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
known_types: HashMap::new(), known_types: HashMap::new(),
} }
} }
/// Registers a type T. Equivalent to `RegisterType<T>` in Delphi. /// Registers a type T. Equivalent to `RegisterType<T>` in Delphi.
pub fn register<T: Scriptable>(&mut self) { pub fn register<T: Scriptable>(&mut self) {
let id = TypeId::of::<T>(); let id = TypeId::of::<T>();
self.known_types.entry(id).or_insert_with(T::static_type); self.known_types.entry(id).or_insert_with(T::static_type);
} }
/// Resolves the static type for a Rust type. /// Resolves the static type for a Rust type.
pub fn resolve_type<T: 'static>(&self) -> StaticType { pub fn resolve_type<T: 'static>(&self) -> StaticType {
self.known_types self.known_types
.get(&TypeId::of::<T>()) .get(&TypeId::of::<T>())
.cloned() .cloned()
.unwrap_or(StaticType::Any) .unwrap_or(StaticType::Any)
} }
/// Creates a factory function value that can be bound in the environment. /// Creates a factory function value that can be bound in the environment.
/// ///
/// # Arguments /// # Arguments
/// * `factory_func` - A Rust closure that takes script arguments and returns a Result<T, String>. /// * `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 pub fn create_factory<T, F>(factory_func: F) -> Value
where where
T: Scriptable, T: Scriptable,
F: Fn(&[Value]) -> Result<T, String> + 'static, F: Fn(&[Value]) -> Result<T, String> + 'static,
{ {
// The factory is a script function that calls the Rust factory, gets T, then wraps it. // The factory is a script function that calls the Rust factory, gets T, then wraps it.
let closure = move |args: &[Value]| -> Value { let closure = move |args: &[Value]| -> Value {
match factory_func(args) { match factory_func(args) {
Ok(instance) => instance.wrap(), Ok(instance) => instance.wrap(),
Err(msg) => { Err(msg) => {
// In a real system, we'd propagate this error. For now, panic or return Void. // 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. // Delphi returns Void if nil, but raises exception on error.
// Since Value doesn't have Error, we panic to stop execution. // Since Value doesn't have Error, we panic to stop execution.
panic!("Runtime Error in Factory for {}: {}", T::type_name(), msg); panic!("Runtime Error in Factory for {}: {}", T::type_name(), msg);
} }
} }
}; };
Value::make_function(Purity::Impure, closure) Value::make_function(Purity::Impure, closure)
} }
} }
/// Helper to build the shadow record for an instance. /// Helper to build the shadow record for an instance.
/// This is used inside `Scriptable::wrap`. /// This is used inside `Scriptable::wrap`.
pub struct RecordBuilder { pub struct RecordBuilder {
fields: Vec<(Keyword, Value)>, fields: Vec<(Keyword, Value)>,
} }
impl Default for RecordBuilder { impl Default for RecordBuilder {
fn default() -> Self { fn default() -> Self {
Self::new() Self::new()
} }
} }
impl RecordBuilder { impl RecordBuilder {
pub fn new() -> Self { pub fn new() -> Self {
Self { fields: Vec::new() } Self { fields: Vec::new() }
} }
pub fn method<F>(mut self, name: &str, func: F) -> Self pub fn method<F>(mut self, name: &str, func: F) -> Self
where where
F: Fn(&[Value]) -> Value + 'static, F: Fn(&[Value]) -> Value + 'static,
{ {
let key = Keyword::intern(name); let key = Keyword::intern(name);
self.fields self.fields
.push((key, Value::make_function(Purity::Impure, func))); .push((key, Value::make_function(Purity::Impure, func)));
self self
} }
// Helper for methods that return Result (propagating panics for now) // Helper for methods that return Result (propagating panics for now)
pub fn method_checked<F>(self, name: &str, func: F) -> Self pub fn method_checked<F>(self, name: &str, func: F) -> Self
where where
F: Fn(&[Value]) -> Result<Value, String> + 'static, F: Fn(&[Value]) -> Result<Value, String> + 'static,
{ {
let closure = move |args: &[Value]| match func(args) { let closure = move |args: &[Value]| match func(args) {
Ok(v) => v, Ok(v) => v,
Err(e) => panic!("Method call error: {}", e), Err(e) => panic!("Method call error: {}", e),
}; };
self.method(name, closure) self.method(name, closure)
} }
pub fn build(self) -> Value { pub fn build(self) -> Value {
let mut keys = Vec::with_capacity(self.fields.len()); let mut keys = Vec::with_capacity(self.fields.len());
let mut values = Vec::with_capacity(self.fields.len()); let mut values = Vec::with_capacity(self.fields.len());
for (k, v) in self.fields { for (k, v) in self.fields {
keys.push(k); keys.push(k);
values.push(v); values.push(v);
} }
Value::make_record(keys, values) Value::make_record(keys, values)
} }
} }
/// Helper to build the StaticType definition. /// Helper to build the StaticType definition.
pub struct TypeBuilder { pub struct TypeBuilder {
fields: Vec<(Keyword, StaticType)>, fields: Vec<(Keyword, StaticType)>,
} }
impl Default for TypeBuilder { impl Default for TypeBuilder {
fn default() -> Self { fn default() -> Self {
Self::new() Self::new()
} }
} }
impl TypeBuilder { impl TypeBuilder {
pub fn new() -> Self { pub fn new() -> Self {
Self { fields: Vec::new() } Self { fields: Vec::new() }
} }
pub fn method(mut self, name: &str, params: Vec<StaticType>, ret: StaticType) -> Self { pub fn method(mut self, name: &str, params: Vec<StaticType>, ret: StaticType) -> Self {
let key = Keyword::intern(name); let key = Keyword::intern(name);
let sig = StaticType::Function(Box::new(Signature { let sig = StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(params), params: StaticType::Tuple(params),
ret, ret,
})); }));
self.fields.push((key, sig)); self.fields.push((key, sig));
self self
} }
pub fn build(self) -> StaticType { pub fn build(self) -> StaticType {
StaticType::Record(crate::ast::types::RecordLayout::get_or_create(self.fields)) StaticType::Record(crate::ast::types::RecordLayout::get_or_create(self.fields))
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::ast::types::{StaticType, Value}; use crate::ast::types::{StaticType, Value};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct Person { struct Person {
name: String, name: String,
age: u32, age: u32,
} }
impl Scriptable for Person { impl Scriptable for Person {
fn type_name() -> &'static str { fn type_name() -> &'static str {
"Person" "Person"
} }
fn static_type() -> StaticType { fn static_type() -> StaticType {
TypeBuilder::new() TypeBuilder::new()
.method("greet", vec![], StaticType::Text) .method("greet", vec![], StaticType::Text)
.method("older", vec![], StaticType::Int) .method("older", vec![], StaticType::Int)
.build() .build()
} }
fn wrap(self) -> Value { fn wrap(self) -> Value {
let name = self.name.clone(); let name = self.name.clone();
let age = self.age; let age = self.age;
RecordBuilder::new() RecordBuilder::new()
.method("greet", move |_| { .method("greet", move |_| {
Value::Text(format!("Hello {}", name).into()) Value::Text(format!("Hello {}", name).into())
}) })
.method("older", move |_| Value::Int((age + 1) as i64)) .method("older", move |_| Value::Int((age + 1) as i64))
.build() .build()
} }
} }
#[test] #[test]
fn test_register_and_wrap() { fn test_register_and_wrap() {
let mut registry = TypeRegistry::new(); let mut registry = TypeRegistry::new();
registry.register::<Person>(); registry.register::<Person>();
let p = Person { let p = Person {
name: "Alice".to_string(), name: "Alice".to_string(),
age: 30, age: 30,
}; };
let wrapped = p.wrap(); let wrapped = p.wrap();
// Check static type // Check static type
let st = TypeRegistry::resolve_type::<Person>(&registry); let st = TypeRegistry::resolve_type::<Person>(&registry);
if let StaticType::Record(layout) = st { if let StaticType::Record(layout) = st {
assert!( assert!(
layout layout
.fields .fields
.iter() .iter()
.any(|(k, _)| *k == Keyword::intern("greet")) .any(|(k, _)| *k == Keyword::intern("greet"))
); );
} else { } else {
panic!("Expected Record type"); panic!("Expected Record type");
} }
// Check runtime behavior // Check runtime behavior
if let Value::Record(layout, values) = wrapped { if let Value::Record(layout, values) = wrapped {
let idx = layout.index_of(Keyword::intern("greet")).unwrap(); let idx = layout.index_of(Keyword::intern("greet")).unwrap();
let greet_fn = &values[idx]; let greet_fn = &values[idx];
if let Value::Function(f) = greet_fn { if let Value::Function(f) = greet_fn {
let res = (f.func)(&[]); let res = (f.func)(&[]);
if let Value::Text(s) = res { if let Value::Text(s) = res {
assert_eq!(&*s, "Hello Alice"); assert_eq!(&*s, "Hello Alice");
} else { } else {
panic!("Expected Text result"); panic!("Expected Text result");
} }
} else { } else {
panic!("Expected Function value for method"); panic!("Expected Function value for method");
} }
} else { } else {
panic!("Expected Record value"); panic!("Expected Record value");
} }
} }
#[test] #[test]
fn test_factory() { fn test_factory() {
let factory_val = TypeRegistry::create_factory(|args: &[Value]| { let factory_val = TypeRegistry::create_factory(|args: &[Value]| {
if args.len() != 2 { if args.len() != 2 {
return Err("Expected 2 args".to_string()); return Err("Expected 2 args".to_string());
} }
let name = match &args[0] { let name = match &args[0] {
Value::Text(t) => t.to_string(), Value::Text(t) => t.to_string(),
_ => return Err("Name must be text".to_string()), _ => return Err("Name must be text".to_string()),
}; };
let age = match &args[1] { let age = match &args[1] {
Value::Int(i) => *i as u32, Value::Int(i) => *i as u32,
_ => return Err("Age must be int".to_string()), _ => return Err("Age must be int".to_string()),
}; };
Ok(Person { name, age }) Ok(Person { name, age })
}); });
if let Value::Function(f) = factory_val { if let Value::Function(f) = factory_val {
let instance = (f.func)(&[Value::Text("Bob".into()), Value::Int(40)]); let instance = (f.func)(&[Value::Text("Bob".into()), Value::Int(40)]);
if let Value::Record(layout, values) = instance { if let Value::Record(layout, values) = instance {
let idx = layout.index_of(Keyword::intern("greet")).unwrap(); let idx = layout.index_of(Keyword::intern("greet")).unwrap();
let greet_fn = &values[idx]; let greet_fn = &values[idx];
if let Value::Function(gf) = greet_fn { if let Value::Function(gf) = greet_fn {
let res = (gf.func)(&[]); let res = (gf.func)(&[]);
if let Value::Text(s) = res { if let Value::Text(s) = res {
assert_eq!(&*s, "Hello Bob"); assert_eq!(&*s, "Hello Bob");
} else { } else {
panic!("Wrong return type"); panic!("Wrong return type");
} }
} }
} else { } else {
panic!("Factory should return Record"); panic!("Factory should return Record");
} }
} else { } else {
panic!("Factory is not a function"); panic!("Factory is not a function");
} }
} }
} }
+682 -682
View File
File diff suppressed because it is too large Load Diff
+171 -171
View File
@@ -1,171 +1,171 @@
use clap::Parser; use clap::Parser;
use myc::ast::environment::Environment; use myc::ast::environment::Environment;
use myc::utils::tester; use myc::utils::tester;
use std::fs; use std::fs;
use std::io::{self, IsTerminal, Read}; use std::io::{self, IsTerminal, Read};
use std::path::PathBuf; use std::path::PathBuf;
#[derive(Parser)] #[derive(Parser)]
#[command(author, version, about = "MYC AST Compiler & Benchmarker", long_about = None)] #[command(author, version, about = "MYC AST Compiler & Benchmarker", long_about = None)]
struct Cli { struct Cli {
/// The script file to run or benchmark /// The script file to run or benchmark
#[arg(value_name = "FILE")] #[arg(value_name = "FILE")]
file: Option<PathBuf>, file: Option<PathBuf>,
/// Run a script string directly /// Run a script string directly
#[arg(short, long)] #[arg(short, long)]
eval: Option<String>, eval: Option<String>,
/// Run benchmarks (Only allowed in Release mode) /// Run benchmarks (Only allowed in Release mode)
#[arg(short, long)] #[arg(short, long)]
bench: bool, bench: bool,
/// Update the benchmark baseline (Only allowed in Release mode) /// Update the benchmark baseline (Only allowed in Release mode)
#[arg(short, long)] #[arg(short, long)]
update_bench: bool, update_bench: bool,
/// Dump the compiled AST /// Dump the compiled AST
#[arg(short, long)] #[arg(short, long)]
dump: bool, dump: bool,
/// Run with TracingObserver enabled /// Run with TracingObserver enabled
#[arg(short, long)] #[arg(short, long)]
trace: bool, trace: bool,
/// Library directories to load before execution /// Library directories to load before execution
#[arg(short, long)] #[arg(short, long)]
lib: Vec<PathBuf>, lib: Vec<PathBuf>,
/// Disable optimization /// Disable optimization
#[arg(long)] #[arg(long)]
no_opt: bool, no_opt: bool,
} }
fn main() { fn main() {
let cli = Cli::parse(); let cli = Cli::parse();
let mut env = Environment::new(); let mut env = Environment::new();
env.optimization = !cli.no_opt; env.optimization = !cli.no_opt;
// Load libraries (Now just search paths) // Load libraries (Now just search paths)
for lib_path in &cli.lib { for lib_path in &cli.lib {
env.add_search_path(lib_path); env.add_search_path(lib_path);
} }
if cli.bench || cli.update_bench { if cli.bench || cli.update_bench {
if cfg!(debug_assertions) { if cfg!(debug_assertions) {
eprintln!("❌ ERROR: Benchmarks must be run in Release mode!"); eprintln!("❌ ERROR: Benchmarks must be run in Release mode!");
eprintln!(" Use: cargo run --release --bin ast -- --bench"); eprintln!(" Use: cargo run --release --bin ast -- --bench");
std::process::exit(1); std::process::exit(1);
} }
println!("🚀 Running benchmarks in RELEASE mode...\n"); println!("🚀 Running benchmarks in RELEASE mode...\n");
let filter = cli let filter = cli
.file .file
.as_ref() .as_ref()
.and_then(|p| p.file_name()) .and_then(|p| p.file_name())
.map(|n| n.to_string_lossy().to_string()); .map(|n| n.to_string_lossy().to_string());
let results = tester::run_benchmarks(cli.update_bench, filter.as_deref()); let results = tester::run_benchmarks(cli.update_bench, filter.as_deref());
for res in results { for res in results {
let diff = res let diff = res
.diff_pct .diff_pct
.map_or(String::new(), |d| format!(" ({:+.1}%)", d)); .map_or(String::new(), |d| format!(" ({:+.1}%)", d));
println!("{}: {} - {:?}{}", res.status, res.name, res.median, diff); println!("{}: {} - {:?}{}", res.status, res.name, res.median, diff);
} }
return; return;
} }
// Determine the source code to execute // Determine the source code to execute
let source = if let Some(script_str) = cli.eval { let source = if let Some(script_str) = cli.eval {
Some(script_str) Some(script_str)
} else if let Some(file_path) = cli.file { } else if let Some(file_path) = cli.file {
if file_path.to_str() == Some("-") { if file_path.to_str() == Some("-") {
read_stdin() read_stdin()
} else { } else {
match fs::read_to_string(&file_path) { match fs::read_to_string(&file_path) {
Ok(content) => Some(content), Ok(content) => Some(content),
Err(e) => { Err(e) => {
eprintln!("Error reading file {:?}: {}", file_path, e); eprintln!("Error reading file {:?}: {}", file_path, e);
std::process::exit(1); std::process::exit(1);
} }
} }
} }
} else if !io::stdin().is_terminal() { } else if !io::stdin().is_terminal() {
read_stdin() read_stdin()
} else { } else {
None None
}; };
if let Some(content) = source { if let Some(content) = source {
if cli.dump { if cli.dump {
match env.dump_ast(&content) { match env.dump_ast(&content) {
Ok(dump) => println!("{}", dump), Ok(dump) => println!("{}", dump),
Err(e) => eprintln!("Error dumping AST: {}", e), Err(e) => eprintln!("Error dumping AST: {}", e),
} }
} else if cli.trace { } else if cli.trace {
execute_trace(&env, &content); execute_trace(&env, &content);
} else { } else {
execute(&env, &content); execute(&env, &content);
} }
} else { } else {
println!("MYC AST Compiler CLI. Use --help for usage."); println!("MYC AST Compiler CLI. Use --help for usage.");
} }
} }
fn read_stdin() -> Option<String> { fn read_stdin() -> Option<String> {
let mut buffer = String::new(); let mut buffer = String::new();
match io::stdin().read_to_string(&mut buffer) { match io::stdin().read_to_string(&mut buffer) {
Ok(_) => Some(buffer), Ok(_) => Some(buffer),
Err(e) => { Err(e) => {
eprintln!("Error reading from stdin: {}", e); eprintln!("Error reading from stdin: {}", e);
std::process::exit(1); std::process::exit(1);
} }
} }
} }
fn execute(env: &Environment, source: &str) { fn execute(env: &Environment, source: &str) {
let result = env.compile(source); let result = env.compile(source);
for diag in &result.diagnostics.items { for diag in &result.diagnostics.items {
let level = format!("{:?}", diag.level).to_uppercase(); let level = format!("{:?}", diag.level).to_uppercase();
let loc = diag let loc = diag
.identity .identity
.as_ref() .as_ref()
.and_then(|id| id.location) .and_then(|id| id.location)
.map(|l| format!(" at {}:{}", l.line, l.col)) .map(|l| format!(" at {}:{}", l.line, l.col))
.unwrap_or_default(); .unwrap_or_default();
eprintln!("{}{} : {}", level, loc, diag.message); eprintln!("{}{} : {}", level, loc, diag.message);
} }
if result.diagnostics.has_errors() { if result.diagnostics.has_errors() {
std::process::exit(1); std::process::exit(1);
} }
match env.run_script_compiled(result.ast.unwrap()) { match env.run_script_compiled(result.ast.unwrap()) {
Ok(res) => println!("{}", res), Ok(res) => println!("{}", res),
Err(e) => { Err(e) => {
eprintln!("Runtime Error: {}", e); eprintln!("Runtime Error: {}", e);
std::process::exit(1); std::process::exit(1);
} }
} }
} }
fn execute_trace(env: &Environment, source: &str) { fn execute_trace(env: &Environment, source: &str) {
match env.run_debug(source) { match env.run_debug(source) {
Ok((Ok(result), logs)) => { Ok((Ok(result), logs)) => {
for line in logs { for line in logs {
println!("{}", line); println!("{}", line);
} }
println!("Result: {}", result); println!("Result: {}", result);
} }
Ok((Err(e), logs)) => { Ok((Err(e), logs)) => {
for line in logs { for line in logs {
println!("{}", line); println!("{}", line);
} }
eprintln!("Runtime Error: {}", e); eprintln!("Runtime Error: {}", e);
std::process::exit(1); std::process::exit(1);
} }
Err(e) => { Err(e) => {
eprintln!("Compilation Error: {}", e); eprintln!("Compilation Error: {}", e);
std::process::exit(1); std::process::exit(1);
} }
} }
} }
+5 -5
View File
@@ -1,7 +1,7 @@
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::ast::environment::Environment; use crate::ast::environment::Environment;
use crate::ast::nodes::UntypedKind; use crate::ast::nodes::SyntaxKind;
use crate::ast::parser::Parser; use crate::ast::parser::Parser;
use crate::ast::types::Value; use crate::ast::types::Value;
@@ -11,7 +11,7 @@ mod tests {
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let ast = parser.parse_expression(); 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); assert_eq!(val, 123);
} else { } else {
panic!("Expected Integer constant, got {:?}", ast.kind); panic!("Expected Integer constant, got {:?}", ast.kind);
@@ -24,7 +24,7 @@ mod tests {
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let ast = parser.parse_expression(); 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); assert_eq!(val, -42);
} else { } else {
panic!("Expected Integer constant, got {:?}", ast.kind); panic!("Expected Integer constant, got {:?}", ast.kind);
@@ -37,7 +37,7 @@ mod tests {
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let ast = parser.parse_expression(); 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); assert_eq!(val, 123.45);
} else { } else {
panic!("Expected Float constant, got {:?}", ast.kind); panic!("Expected Float constant, got {:?}", ast.kind);
@@ -50,7 +50,7 @@ mod tests {
let mut parser = Parser::new(source); let mut parser = Parser::new(source);
let ast = parser.parse_expression(); 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); assert_eq!(val, -10.5);
} else { } else {
panic!("Expected Float constant, got {:?}", ast.kind); panic!("Expected Float constant, got {:?}", ast.kind);
+696 -696
View File
File diff suppressed because it is too large Load Diff
+337 -337
View File
@@ -1,337 +1,337 @@
use crate::ast::environment::Environment; use crate::ast::environment::Environment;
use regex::Regex; use regex::Regex;
use std::fs; use std::fs;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
pub struct TestResult { pub struct TestResult {
pub name: String, pub name: String,
pub success: bool, pub success: bool,
pub message: String, pub message: String,
} }
pub struct BenchmarkResult { pub struct BenchmarkResult {
pub name: String, pub name: String,
pub median: Duration, pub median: Duration,
pub baseline: Option<Duration>, pub baseline: Option<Duration>,
pub diff_pct: Option<f64>, pub diff_pct: Option<f64>,
pub status: String, pub status: String,
} }
pub fn run_functional_tests() -> Vec<TestResult> { pub fn run_functional_tests() -> Vec<TestResult> {
run_functional_tests_with_optimization(false) run_functional_tests_with_optimization(false)
} }
pub fn run_functional_tests_with_optimization(enabled: bool) -> Vec<TestResult> { pub fn run_functional_tests_with_optimization(enabled: bool) -> Vec<TestResult> {
let mut results = Vec::new(); let mut results = Vec::new();
let entries = fs::read_dir("examples").unwrap(); let entries = fs::read_dir("examples").unwrap();
let output_re = Regex::new(r";; Output: (.*)").unwrap(); let output_re = Regex::new(r";; Output: (.*)").unwrap();
for entry in entries.filter_map(|e| e.ok()) { for entry in entries.filter_map(|e| e.ok()) {
let mut env = Environment::new(); // Fresh environment per test file let mut env = Environment::new(); // Fresh environment per test file
env.optimization = enabled; env.optimization = enabled;
let path = entry.path(); let path = entry.path();
if path.extension().is_some_and(|ext| ext == "myc") { if path.extension().is_some_and(|ext| ext == "myc") {
let content = fs::read_to_string(&path).unwrap(); let content = fs::read_to_string(&path).unwrap();
let name = path.file_name().unwrap().to_string_lossy().to_string(); let name = path.file_name().unwrap().to_string_lossy().to_string();
let expected_output = output_re let expected_output = output_re
.captures(&content) .captures(&content)
.map(|m| m.get(1).unwrap().as_str().trim().to_string()); .map(|m| m.get(1).unwrap().as_str().trim().to_string());
if let Some(expected) = expected_output { if let Some(expected) = expected_output {
match env.run_script(&content) { match env.run_script(&content) {
Ok(val) => { Ok(val) => {
let val_str = format!("{}", val); let val_str = format!("{}", val);
if val_str == expected { if val_str == expected {
results.push(TestResult { results.push(TestResult {
name, name,
success: true, success: true,
message: format!("OK: {}", val_str), message: format!("OK: {}", val_str),
}); });
} else { } else {
results.push(TestResult { results.push(TestResult {
name, name,
success: false, success: false,
message: format!( message: format!(
"Opt {}: Expected {}, got {}", "Opt {}: Expected {}, got {}",
if enabled { "ON" } else { "OFF" }, if enabled { "ON" } else { "OFF" },
expected, expected,
val_str val_str
), ),
}); });
} }
} }
Err(e) => results.push(TestResult { Err(e) => results.push(TestResult {
name, name,
success: false, success: false,
message: format!( message: format!(
"Opt {}: Error: {}", "Opt {}: Error: {}",
if enabled { "ON" } else { "OFF" }, if enabled { "ON" } else { "OFF" },
e e
), ),
}), }),
} }
} }
} }
} }
results results
} }
pub fn run_benchmarks(update: bool, filter: Option<&str>) -> Vec<BenchmarkResult> { pub fn run_benchmarks(update: bool, filter: Option<&str>) -> Vec<BenchmarkResult> {
let mut results = Vec::new(); let mut results = Vec::new();
let entries = fs::read_dir("examples").unwrap(); let entries = fs::read_dir("examples").unwrap();
let is_release = !cfg!(debug_assertions); let is_release = !cfg!(debug_assertions);
let baseline_re = Regex::new(r";; Benchmark: ([\d\.]+\w+)").unwrap(); let baseline_re = Regex::new(r";; Benchmark: ([\d\.]+\w+)").unwrap();
let repeat_re = Regex::new(r";; Benchmark-Repeat: (\d+)").unwrap(); let repeat_re = Regex::new(r";; Benchmark-Repeat: (\d+)").unwrap();
for entry in entries.filter_map(|e| e.ok()) { for entry in entries.filter_map(|e| e.ok()) {
let path = entry.path(); let path = entry.path();
if path.extension().is_none_or(|ext| ext != "myc") { if path.extension().is_none_or(|ext| ext != "myc") {
continue; continue;
} }
let name = path.file_name().unwrap().to_string_lossy().to_string(); let name = path.file_name().unwrap().to_string_lossy().to_string();
if let Some(f) = filter if let Some(f) = filter
&& name != f && name != f
{ {
continue; continue;
} }
let content = fs::read_to_string(&path).unwrap(); let content = fs::read_to_string(&path).unwrap();
let baseline_match = baseline_re.captures(&content); let baseline_match = baseline_re.captures(&content);
if !update && baseline_match.is_none() { if !update && baseline_match.is_none() {
continue; continue;
} }
let repeat_match = repeat_re.captures(&content); let repeat_match = repeat_re.captures(&content);
let mut repeats = repeat_match let mut repeats = repeat_match
.and_then(|m| m.get(1)) .and_then(|m| m.get(1))
.and_then(|m| m.as_str().parse::<u32>().ok()) .and_then(|m| m.as_str().parse::<u32>().ok())
.unwrap_or(1); .unwrap_or(1);
// Compile once for this file (symbols/types are compatible with all fresh environments) // Compile once for this file (symbols/types are compatible with all fresh environments)
let env = Environment::new(); let env = Environment::new();
let compiled_once = match env.compile(&content).into_result() { let compiled_once = match env.compile(&content).into_result() {
Ok(c) => c, Ok(c) => c,
Err(e) => { Err(e) => {
results.push(BenchmarkResult { results.push(BenchmarkResult {
name, name,
median: Duration::ZERO, median: Duration::ZERO,
baseline: None, baseline: None,
diff_pct: None, diff_pct: None,
status: format!("COMPILE ERROR: {}", e), status: format!("COMPILE ERROR: {}", e),
}); });
continue; continue;
} }
}; };
// Helper to measure sum of VM execution times over N executions in one environment // Helper to measure sum of VM execution times over N executions in one environment
let measure_sum = let measure_sum =
|n: u32, node: &crate::ast::compiler::TypedNode| -> Result<Duration, String> { |n: u32, node: &crate::ast::compiler::TypedNode| -> Result<Duration, String> {
let env = Environment::new(); let env = Environment::new();
// Link once per sample // Link once per sample
let linked = env.link(node.clone()); let linked = env.link(node.clone());
let func = env.instantiate(linked); let func = env.instantiate(linked);
let mut total = Duration::ZERO; let mut total = Duration::ZERO;
for _ in 0..n { for _ in 0..n {
let start = Instant::now(); let start = Instant::now();
let _ = (func.func)(&[]); let _ = (func.func)(&[]);
total += start.elapsed(); total += start.elapsed();
} }
Ok(total) Ok(total)
}; };
if update { if update {
repeats = 1; repeats = 1;
loop { loop {
match measure_sum(repeats, &compiled_once) { match measure_sum(repeats, &compiled_once) {
Ok(total) => { Ok(total) => {
if total >= Duration::from_millis(2) || repeats >= 100_000 { if total >= Duration::from_millis(2) || repeats >= 100_000 {
break; break;
} }
let nanos = total.as_nanos().max(1) as f64; let nanos = total.as_nanos().max(1) as f64;
let factor = 2_000_000.0 / nanos; let factor = 2_000_000.0 / nanos;
repeats = (repeats as f64 * factor).ceil() as u32; repeats = (repeats as f64 * factor).ceil() as u32;
repeats = repeats.max(repeats + 1); repeats = repeats.max(repeats + 1);
} }
Err(e) => { Err(e) => {
results.push(BenchmarkResult { results.push(BenchmarkResult {
name: name.clone(), name: name.clone(),
median: Duration::ZERO, median: Duration::ZERO,
baseline: None, baseline: None,
diff_pct: None, diff_pct: None,
status: format!("ERROR: {}", e), status: format!("ERROR: {}", e),
}); });
break; break;
} }
} }
} }
if results.last().is_some_and(|r| r.name == name) { if results.last().is_some_and(|r| r.name == name) {
continue; continue;
} }
} }
let mut runs = Vec::new(); let mut runs = Vec::new();
let mut error = None; let mut error = None;
// Adaptive samples: High repeats need fewer samples for stable median // Adaptive samples: High repeats need fewer samples for stable median
let num_samples = if repeats > 1000 { let num_samples = if repeats > 1000 {
10 10
} else if repeats > 100 { } else if repeats > 100 {
30 30
} else { } else {
100 100
}; };
for _ in 0..num_samples { for _ in 0..num_samples {
match measure_sum(repeats, &compiled_once) { match measure_sum(repeats, &compiled_once) {
Ok(d) => runs.push(d), Ok(d) => runs.push(d),
Err(e) => { Err(e) => {
error = Some(e); error = Some(e);
break; break;
} }
} }
} }
if let Some(e) = error { if let Some(e) = error {
results.push(BenchmarkResult { results.push(BenchmarkResult {
name, name,
median: Duration::ZERO, median: Duration::ZERO,
baseline: None, baseline: None,
diff_pct: None, diff_pct: None,
status: format!("ERROR: {}", e), status: format!("ERROR: {}", e),
}); });
continue; continue;
} }
runs.sort(); runs.sort();
let median_total = runs[runs.len() / 2]; let median_total = runs[runs.len() / 2];
let median_single = median_total / repeats; let median_single = median_total / repeats;
if update { if update {
let new_val = format_duration(median_single); let new_val = format_duration(median_single);
let mut updated_content = content.clone(); let mut updated_content = content.clone();
// 1. Update/Insert Benchmark // 1. Update/Insert Benchmark
let bench_line = format!(";; Benchmark: {}", new_val); let bench_line = format!(";; Benchmark: {}", new_val);
if let Some(m) = baseline_match { if let Some(m) = baseline_match {
updated_content = updated_content.replace(m.get(0).unwrap().as_str(), &bench_line); updated_content = updated_content.replace(m.get(0).unwrap().as_str(), &bench_line);
} else { } else {
updated_content = format!("{}\n{}", bench_line, updated_content); updated_content = format!("{}\n{}", bench_line, updated_content);
} }
// 2. Update/Insert/Remove Benchmark-Repeat // 2. Update/Insert/Remove Benchmark-Repeat
let repeat_line = if repeats > 1 { let repeat_line = if repeats > 1 {
Some(format!(";; Benchmark-Repeat: {}", repeats)) Some(format!(";; Benchmark-Repeat: {}", repeats))
} else { } else {
None None
}; };
let current_repeat_str = repeat_re let current_repeat_str = repeat_re
.captures(&updated_content) .captures(&updated_content)
.map(|m| m.get(0).unwrap().as_str().to_string()); .map(|m| m.get(0).unwrap().as_str().to_string());
if let Some(line) = repeat_line { if let Some(line) = repeat_line {
if let Some(old_line) = current_repeat_str { if let Some(old_line) = current_repeat_str {
updated_content = updated_content.replace(&old_line, &line); updated_content = updated_content.replace(&old_line, &line);
} else if let Some(m) = baseline_re.captures(&updated_content) { } else if let Some(m) = baseline_re.captures(&updated_content) {
let b_str = m.get(0).unwrap().as_str().to_string(); let b_str = m.get(0).unwrap().as_str().to_string();
if let Some(pos) = updated_content.find(&b_str) { if let Some(pos) = updated_content.find(&b_str) {
updated_content.insert_str(pos + b_str.len(), &format!("\n{}", line)); updated_content.insert_str(pos + b_str.len(), &format!("\n{}", line));
} }
} }
} else if let Some(old_line) = current_repeat_str { } else if let Some(old_line) = current_repeat_str {
updated_content = updated_content.replace(&format!("{}\n", old_line), ""); updated_content = updated_content.replace(&format!("{}\n", old_line), "");
updated_content = updated_content.replace(&old_line, ""); updated_content = updated_content.replace(&old_line, "");
} }
fs::write(&path, updated_content).unwrap(); fs::write(&path, updated_content).unwrap();
results.push(BenchmarkResult { results.push(BenchmarkResult {
name, name,
median: median_single, median: median_single,
baseline: None, baseline: None,
diff_pct: None, diff_pct: None,
status: format!("UPDATED: {}", new_val), status: format!("UPDATED: {}", new_val),
}); });
} else if let Some(m) = baseline_match { } else if let Some(m) = baseline_match {
let baseline_str = m.get(1).unwrap().as_str(); let baseline_str = m.get(1).unwrap().as_str();
let baseline = parse_duration(baseline_str).unwrap(); let baseline = parse_duration(baseline_str).unwrap();
let diff = (median_single.as_nanos() as f64 / baseline.as_nanos() as f64) - 1.0; 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 threshold = if is_release { 0.15 } else { 0.30 };
let status = if median_single > baseline && diff > threshold { let status = if median_single > baseline && diff > threshold {
"FAILED" "FAILED"
} else { } else {
"OK" "OK"
}; };
results.push(BenchmarkResult { results.push(BenchmarkResult {
name, name,
median: median_single, median: median_single,
baseline: Some(baseline), baseline: Some(baseline),
diff_pct: Some(diff * 100.0), diff_pct: Some(diff * 100.0),
status: status.to_string(), status: status.to_string(),
}); });
} }
} }
results results
} }
fn format_duration(d: Duration) -> String { fn format_duration(d: Duration) -> String {
if d.as_nanos() < 1000 { if d.as_nanos() < 1000 {
format!("{}ns", d.as_nanos()) format!("{}ns", d.as_nanos())
} else if d.as_micros() < 1000 { } else if d.as_micros() < 1000 {
format!("{:.1}us", d.as_nanos() as f64 / 1000.0) format!("{:.1}us", d.as_nanos() as f64 / 1000.0)
} else if d.as_millis() < 1000 { } else if d.as_millis() < 1000 {
format!("{:.1}ms", d.as_micros() as f64 / 1000.0) format!("{:.1}ms", d.as_micros() as f64 / 1000.0)
} else { } else {
format!("{:.1}s", d.as_millis() as f64 / 1000.0) format!("{:.1}s", d.as_millis() as f64 / 1000.0)
} }
} }
fn parse_duration(s: &str) -> Option<Duration> { fn parse_duration(s: &str) -> Option<Duration> {
use std::sync::OnceLock; use std::sync::OnceLock;
static RE: OnceLock<Regex> = OnceLock::new(); static RE: OnceLock<Regex> = OnceLock::new();
let re = RE.get_or_init(|| Regex::new(r"([\d\.]+)(\w+)").unwrap()); let re = RE.get_or_init(|| Regex::new(r"([\d\.]+)(\w+)").unwrap());
let caps = re.captures(s)?; let caps = re.captures(s)?;
let val: f64 = caps[1].parse().ok()?; let val: f64 = caps[1].parse().ok()?;
let unit = &caps[2]; let unit = &caps[2];
match unit { match unit {
"ns" => Some(Duration::from_nanos(val as u64)), "ns" => Some(Duration::from_nanos(val as u64)),
"us" => Some(Duration::from_nanos((val * 1000.0) as u64)), "us" => Some(Duration::from_nanos((val * 1000.0) as u64)),
"ms" => Some(Duration::from_nanos((val * 1_000_000.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)), "s" => Some(Duration::from_nanos((val * 1_000_000_000.0) as u64)),
_ => None, _ => None,
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
fn benchmark_regression_test() { fn benchmark_regression_test() {
use super::*; use super::*;
let results = run_benchmarks(false, None); let results = run_benchmarks(false, None);
let failures: Vec<_> = results.iter().filter(|r| r.status == "FAILED").collect(); let failures: Vec<_> = results.iter().filter(|r| r.status == "FAILED").collect();
if !failures.is_empty() { if !failures.is_empty() {
let error_msg = failures let error_msg = failures
.iter() .iter()
.map(|r| { .map(|r| {
format!( format!(
"{}: Median {:?}, Baseline {:?}, Diff {:.2}%", "{}: Median {:?}, Baseline {:?}, Diff {:.2}%",
r.name, r.name,
r.median, r.median,
r.baseline.unwrap_or_default(), r.baseline.unwrap_or_default(),
r.diff_pct.unwrap_or(0.0) r.diff_pct.unwrap_or(0.0)
) )
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join("\n"); .join("\n");
panic!("Performance regression detected:\n{}", error_msg); panic!("Performance regression detected:\n{}", error_msg);
} }
} }
} }