Formatting

This commit is contained in:
Michael Schimmel
2026-02-22 02:35:06 +01:00
parent 2123f1d279
commit 329b885c4b
25 changed files with 8053 additions and 6400 deletions
+166 -77
View File
@@ -1,9 +1,9 @@
use crate::ast::compiler::bound_nodes::{Address, BoundKind, BoundNode};
use crate::ast::nodes::{Node, Symbol, UntypedKind};
use crate::ast::types::{Identity, StaticType};
use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
use std::cell::RefCell;
use crate::ast::nodes::{Node, UntypedKind, Symbol};
use crate::ast::compiler::bound_nodes::{BoundKind, Address, BoundNode};
use crate::ast::types::{Identity, StaticType};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct LocalInfo { struct LocalInfo {
@@ -30,10 +30,19 @@ impl CompilerScope {
fn define(&mut self, sym: &Symbol) -> Result<u32, String> { fn define(&mut self, sym: &Symbol) -> Result<u32, String> {
if self.locals.contains_key(sym) { if self.locals.contains_key(sym) {
return Err(format!("Variable '{}' is already defined in this scope level.", sym.name)); return Err(format!(
"Variable '{}' is already defined in this scope level.",
sym.name
));
} }
let slot = self.slot_count; let slot = self.slot_count;
self.locals.insert(sym.clone(), LocalInfo { slot, _ty: StaticType::Any }); self.locals.insert(
sym.clone(),
LocalInfo {
slot,
_ty: StaticType::Any,
},
);
self.slot_count += 1; self.slot_count += 1;
Ok(slot) Ok(slot)
} }
@@ -79,7 +88,10 @@ impl Binder {
Self::with_boxed(globals, HashMap::new()) Self::with_boxed(globals, HashMap::new())
} }
fn with_boxed(globals: Rc<RefCell<HashMap<Symbol, u32>>>, captures: HashMap<Identity, Vec<Identity>>) -> Self { fn with_boxed(
globals: Rc<RefCell<HashMap<Symbol, u32>>>,
captures: HashMap<Identity, Vec<Identity>>,
) -> Self {
let mut binder = Self { let mut binder = Self {
functions: Vec::new(), functions: Vec::new(),
globals, globals,
@@ -89,7 +101,10 @@ impl Binder {
binder binder
} }
pub fn bind_root(globals: Rc<RefCell<HashMap<Symbol, u32>>>, node: &Node<UntypedKind>) -> Result<BoundNode, String> { pub fn bind_root(
globals: Rc<RefCell<HashMap<Symbol, u32>>>,
node: &Node<UntypedKind>,
) -> Result<BoundNode, String> {
let captures = crate::ast::compiler::upvalues::UpvalueAnalyzer::analyze(node); let captures = crate::ast::compiler::upvalues::UpvalueAnalyzer::analyze(node);
let mut binder = Self::with_boxed(globals, captures); let mut binder = Self::with_boxed(globals, captures);
binder.bind(node) binder.bind(node)
@@ -100,30 +115,43 @@ impl Binder {
UntypedKind::Nop => Ok(self.make_node(node.identity.clone(), BoundKind::Nop)), UntypedKind::Nop => Ok(self.make_node(node.identity.clone(), BoundKind::Nop)),
UntypedKind::Constant(v) => { UntypedKind::Constant(v) => {
Ok(self.make_node(node.identity.clone(), BoundKind::Constant(v.clone()))) Ok(self.make_node(node.identity.clone(), BoundKind::Constant(v.clone())))
}, }
UntypedKind::Identifier(sym) => { UntypedKind::Identifier(sym) => {
let addr = self.resolve_variable(sym)?; let addr = self.resolve_variable(sym)?;
Ok(self.make_node(node.identity.clone(), BoundKind::Get { Ok(self.make_node(
addr, node.identity.clone(),
name: sym.clone() BoundKind::Get {
})) addr,
}, name: sym.clone(),
},
))
}
UntypedKind::Parameter(sym) => { UntypedKind::Parameter(sym) => {
// Parameters are ONLY allowed if we are inside a function (Binder stack > 1) // Parameters are ONLY allowed if we are inside a function (Binder stack > 1)
if self.functions.len() <= 1 { if self.functions.len() <= 1 {
return Err(format!("Parameter '{}' is not allowed in root scope.", sym.name)); return Err(format!(
"Parameter '{}' is not allowed in root scope.",
sym.name
));
} }
let current_fn = self.functions.last_mut().unwrap(); let current_fn = self.functions.last_mut().unwrap();
let slot = current_fn.scope.define(sym)?; let slot = current_fn.scope.define(sym)?;
Ok(self.make_node(node.identity.clone(), BoundKind::Parameter { Ok(self.make_node(
name: sym.clone(), node.identity.clone(),
slot BoundKind::Parameter {
})) name: sym.clone(),
}, slot,
},
))
}
UntypedKind::If { cond, then_br, else_br } => { UntypedKind::If {
cond,
then_br,
else_br,
} => {
let cond = self.bind(cond)?; let cond = self.bind(cond)?;
let then_br = self.bind(then_br)?; let then_br = self.bind(then_br)?;
let mut else_br_bound = None; let mut else_br_bound = None;
@@ -131,19 +159,25 @@ impl Binder {
else_br_bound = Some(Box::new(self.bind(e)?)); else_br_bound = Some(Box::new(self.bind(e)?));
} }
Ok(self.make_node(node.identity.clone(), BoundKind::If { Ok(self.make_node(
cond: Box::new(cond), node.identity.clone(),
then_br: Box::new(then_br), BoundKind::If {
else_br: else_br_bound cond: Box::new(cond),
})) then_br: Box::new(then_br),
}, else_br: else_br_bound,
},
))
}
UntypedKind::Def { name, value } => { UntypedKind::Def { name, value } => {
// 1. Pre-declare name to support recursion // 1. Pre-declare name to support recursion
let slot_or_idx = if self.functions.len() == 1 { let slot_or_idx = if self.functions.len() == 1 {
let mut globals = self.globals.borrow_mut(); let mut globals = self.globals.borrow_mut();
if globals.contains_key(name) { if globals.contains_key(name) {
return Err(format!("Global variable '{}' is already defined.", name.name)); return Err(format!(
"Global variable '{}' is already defined.",
name.name
));
} }
let idx = globals.len() as u32; let idx = globals.len() as u32;
globals.insert(name.clone(), idx); globals.insert(name.clone(), idx);
@@ -158,35 +192,48 @@ impl Binder {
// 3. Return Node // 3. Return Node
if self.functions.len() == 1 { if self.functions.len() == 1 {
Ok(self.make_node(node.identity.clone(), BoundKind::DefGlobal { Ok(self.make_node(
name: name.clone(), node.identity.clone(),
global_index: slot_or_idx, BoundKind::DefGlobal {
value: Box::new(val_node) name: name.clone(),
})) global_index: slot_or_idx,
value: Box::new(val_node),
},
))
} else { } else {
let captured_by = self.capture_map.get(&node.identity).cloned().unwrap_or_default(); let captured_by = self
Ok(self.make_node(node.identity.clone(), BoundKind::DefLocal { .capture_map
name: name.clone(), .get(&node.identity)
slot: slot_or_idx, .cloned()
value: Box::new(val_node) , .unwrap_or_default();
captured_by Ok(self.make_node(
})) node.identity.clone(),
BoundKind::DefLocal {
name: name.clone(),
slot: slot_or_idx,
value: Box::new(val_node),
captured_by,
},
))
} }
}, }
UntypedKind::Assign { target, value } => { UntypedKind::Assign { target, value } => {
let val_node = self.bind(value)?; let val_node = self.bind(value)?;
if let UntypedKind::Identifier(sym) = &target.kind { if let UntypedKind::Identifier(sym) = &target.kind {
let addr = self.resolve_variable(sym)?; let addr = self.resolve_variable(sym)?;
Ok(self.make_node(node.identity.clone(), BoundKind::Set { Ok(self.make_node(
addr, node.identity.clone(),
value: Box::new(val_node) BoundKind::Set {
})) addr,
value: Box::new(val_node),
},
))
} else { } else {
Err("Assignment target must be an identifier".to_string()) Err("Assignment target must be an identifier".to_string())
} }
}, }
UntypedKind::Lambda { params, body } => { UntypedKind::Lambda { params, body } => {
let identity = node.identity.clone(); let identity = node.identity.clone();
@@ -219,60 +266,86 @@ impl Binder {
_ => None, _ => None,
}; };
Ok(self.make_node(identity, BoundKind::Lambda { Ok(self.make_node(
params: Rc::new(params_bound), identity,
upvalues: compiled_fn.upvalues, BoundKind::Lambda {
body: Rc::new(body_bound), params: Rc::new(params_bound),
positional_count, upvalues: compiled_fn.upvalues,
})) body: Rc::new(body_bound),
}, positional_count,
},
))
}
UntypedKind::Call { callee, args } => { UntypedKind::Call { callee, args } => {
let callee = self.bind(callee)?; let callee = self.bind(callee)?;
let args = self.bind(args)?; let args = self.bind(args)?;
Ok(self.make_node(node.identity.clone(), BoundKind::Call { Ok(self.make_node(
callee: Box::new(callee), node.identity.clone(),
args: Box::new(args) BoundKind::Call {
})) callee: Box::new(callee),
}, args: Box::new(args),
},
))
}
UntypedKind::Block { exprs } => { UntypedKind::Block { exprs } => {
let mut bound_exprs = Vec::new(); let mut bound_exprs = Vec::new();
for expr in exprs { for expr in exprs {
bound_exprs.push(self.bind(expr)?); bound_exprs.push(self.bind(expr)?);
} }
Ok(self.make_node(node.identity.clone(), BoundKind::Block { exprs: bound_exprs })) Ok(self.make_node(
}, node.identity.clone(),
BoundKind::Block { exprs: bound_exprs },
))
}
UntypedKind::Tuple { elements } => { UntypedKind::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(self.bind(e)?); bound_elems.push(self.bind(e)?);
} }
Ok(self.make_node(node.identity.clone(), BoundKind::Tuple { elements: bound_elems })) Ok(self.make_node(
}, node.identity.clone(),
BoundKind::Tuple {
elements: bound_elems,
},
))
}
UntypedKind::Record { fields } => { UntypedKind::Record { fields } => {
let mut bound_fields = Vec::new(); let mut bound_fields = Vec::new();
for (k, v) in fields { for (k, v) in fields {
bound_fields.push((self.bind(k)?, self.bind(v)?)); bound_fields.push((self.bind(k)?, self.bind(v)?));
} }
Ok(self.make_node(node.identity.clone(), BoundKind::Record { fields: bound_fields })) Ok(self.make_node(
}, node.identity.clone(),
BoundKind::Record {
fields: bound_fields,
},
))
}
UntypedKind::Expansion { call, expanded } => { UntypedKind::Expansion { call, expanded } => {
let bound_expanded = self.bind(expanded)?; let bound_expanded = self.bind(expanded)?;
Ok(self.make_node(node.identity.clone(), BoundKind::Expansion { Ok(self.make_node(
original_call: Rc::from(call.as_ref().clone()), node.identity.clone(),
bound_expanded: Box::new(bound_expanded), BoundKind::Expansion {
})) original_call: Rc::from(call.as_ref().clone()),
}, bound_expanded: Box::new(bound_expanded),
},
UntypedKind::Template(_) | UntypedKind::Placeholder(_) | UntypedKind::Splice(_) | UntypedKind::MacroDecl { .. } => { ))
Err(format!("Macro construct {:?} found in Binder. Macros must be expanded before binding.", node.kind))
} }
UntypedKind::Template(_)
| UntypedKind::Placeholder(_)
| UntypedKind::Splice(_)
| UntypedKind::MacroDecl { .. } => Err(format!(
"Macro construct {:?} found in Binder. Macros must be expanded before binding.",
node.kind
)),
UntypedKind::Extension(_) => { UntypedKind::Extension(_) => {
// Future: Delegate to extension binder // Future: Delegate to extension binder
Err("Custom extensions not supported in Binder yet".to_string()) Err("Custom extensions not supported in Binder yet".to_string())
@@ -308,7 +381,10 @@ impl Binder {
// 4. Global Fallback // 4. Global Fallback
if sym.context.is_some() { if sym.context.is_some() {
let fallback_sym = Symbol { name: sym.name.clone(), context: None }; let fallback_sym = Symbol {
name: sym.name.clone(),
context: None,
};
if let Some(idx) = globals.get(&fallback_sym) { if let Some(idx) = globals.get(&fallback_sym) {
return Ok(Address::Global(*idx)); return Ok(Address::Global(*idx));
} }
@@ -318,7 +394,11 @@ impl Binder {
} }
fn make_node(&self, identity: Identity, kind: BoundKind<()>) -> BoundNode { fn make_node(&self, identity: Identity, kind: BoundKind<()>) -> BoundNode {
Node { identity, kind, ty: () } Node {
identity,
kind,
ty: (),
}
} }
} }
@@ -342,9 +422,15 @@ mod tests {
if let BoundKind::Block { exprs } = &body.kind { if let BoundKind::Block { exprs } = &body.kind {
let x_decl = &exprs[0]; let x_decl = &exprs[0];
if let BoundKind::DefLocal { captured_by, .. } = &x_decl.kind { if let BoundKind::DefLocal { captured_by, .. } = &x_decl.kind {
assert!(!captured_by.is_empty(), "Variable 'x' should have capturers because it is used in lambda 'f'"); assert!(
!captured_by.is_empty(),
"Variable 'x' should have capturers because it is used in lambda 'f'"
);
} else { } else {
panic!("First expression in block should be DefLocal, got {:?}", x_decl.kind); panic!(
"First expression in block should be DefLocal, got {:?}",
x_decl.kind
);
} }
} else { } else {
panic!("Lambda body should be a Block, got {:?}", body.kind); panic!("Lambda body should be a Block, got {:?}", body.kind);
@@ -367,7 +453,10 @@ mod tests {
if let BoundKind::Block { exprs } = &body.kind { if let BoundKind::Block { exprs } = &body.kind {
let x_decl = &exprs[0]; let x_decl = &exprs[0];
if let BoundKind::DefLocal { captured_by, .. } = &x_decl.kind { if let BoundKind::DefLocal { captured_by, .. } = &x_decl.kind {
assert!(captured_by.is_empty(), "Variable 'x' should NOT have any capturers"); assert!(
captured_by.is_empty(),
"Variable 'x' should NOT have any capturers"
);
} else { } else {
panic!("First expression should be DefLocal"); panic!("First expression should be DefLocal");
} }
+104 -45
View File
@@ -1,6 +1,6 @@
use std::rc::Rc;
use crate::ast::types::{Value, StaticType, Identity};
use crate::ast::nodes::{Node, Symbol}; use crate::ast::nodes::{Node, Symbol};
use crate::ast::types::{Identity, StaticType, Value};
use std::rc::Rc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Address { pub enum Address {
@@ -110,52 +110,105 @@ pub enum BoundKind<T = ()> {
} }
impl<T> PartialEq for BoundKind<T> impl<T> PartialEq for BoundKind<T>
where T: PartialEq { where
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::Parameter { name: na, slot: sa }, BoundKind::Parameter { name: nb, slot: sb }) => { (
na == nb && sa == sb BoundKind::Parameter { name: na, slot: sa },
} BoundKind::Parameter { name: nb, slot: sb },
) => na == nb && sa == sb,
(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 { addr: aa, value: va }, BoundKind::Set { addr: ab, value: vb }) => { (
aa == ab && va == vb BoundKind::Set {
} addr: aa,
(BoundKind::DefLocal { name: na, slot: sa, value: va, captured_by: ca }, value: va,
BoundKind::DefLocal { name: nb, slot: sb, value: vb, captured_by: cb }) => { },
na == nb && sa == sb && va == vb && ca == cb BoundKind::Set {
} addr: ab,
(BoundKind::If { cond: ca, then_br: ta, else_br: ea }, value: vb,
BoundKind::If { cond: cb, then_br: tb, else_br: eb }) => { },
ca == cb && ta == tb && ea == eb ) => aa == ab && va == vb,
} (
(BoundKind::DefGlobal { name: na, global_index: ga, value: va }, BoundKind::DefLocal {
BoundKind::DefGlobal { name: nb, global_index: gb, value: vb }) => { name: na,
na == nb && ga == gb && va == vb slot: sa,
} value: va,
(BoundKind::Lambda { params: pa, upvalues: ua, body: ba, positional_count: pca }, captured_by: ca,
BoundKind::Lambda { params: pb, upvalues: ub, body: bb, positional_count: pcb }) => { },
pa == pb && ua == ub && ba == bb && pca == pcb BoundKind::DefLocal {
} name: nb,
(BoundKind::Call { callee: ca, args: aa }, BoundKind::Call { callee: cb, args: ab }) => { slot: sb,
ca == cb && aa == ab value: vb,
} captured_by: cb,
(BoundKind::Block { exprs: ea }, BoundKind::Block { exprs: eb }) => { },
ea == eb ) => na == nb && sa == sb && va == vb && ca == cb,
} (
(BoundKind::Tuple { elements: ea }, BoundKind::Tuple { elements: eb }) => { BoundKind::If {
ea == eb cond: ca,
} then_br: ta,
(BoundKind::Record { fields: fa }, BoundKind::Record { fields: fb }) => { else_br: ea,
fa == fb },
} BoundKind::If {
(BoundKind::Expansion { original_call: oa, bound_expanded: ba }, cond: cb,
BoundKind::Expansion { original_call: ob, bound_expanded: bb }) => { then_br: tb,
Rc::ptr_eq(oa, ob) && ba == bb else_br: eb,
} },
) => ca == cb && ta == tb && ea == eb,
(
BoundKind::DefGlobal {
name: na,
global_index: ga,
value: va,
},
BoundKind::DefGlobal {
name: nb,
global_index: gb,
value: vb,
},
) => na == nb && ga == gb && va == vb,
(
BoundKind::Lambda {
params: pa,
upvalues: ua,
body: ba,
positional_count: pca,
},
BoundKind::Lambda {
params: pb,
upvalues: ub,
body: bb,
positional_count: pcb,
},
) => pa == pb && ua == ub && ba == bb && pca == pcb,
(
BoundKind::Call {
callee: ca,
args: aa,
},
BoundKind::Call {
callee: cb,
args: ab,
},
) => ca == cb && aa == ab,
(BoundKind::Block { exprs: ea }, BoundKind::Block { exprs: eb }) => ea == eb,
(BoundKind::Tuple { elements: ea }, BoundKind::Tuple { elements: eb }) => ea == eb,
(BoundKind::Record { fields: fa }, BoundKind::Record { fields: fb }) => fa == fb,
(
BoundKind::Expansion {
original_call: oa,
bound_expanded: ba,
},
BoundKind::Expansion {
original_call: ob,
bound_expanded: bb,
},
) => Rc::ptr_eq(oa, ob) && ba == bb,
(BoundKind::Extension(_), BoundKind::Extension(_)) => false, // Currently no equality for extensions (BoundKind::Extension(_), BoundKind::Extension(_)) => false, // Currently no equality for extensions
_ => false, _ => false,
} }
@@ -173,16 +226,22 @@ impl<T> BoundKind<T> {
BoundKind::Parameter { name, slot } => format!("PARAM({}, Slot:{})", name.name, slot), BoundKind::Parameter { name, slot } => format!("PARAM({}, Slot:{})", name.name, slot),
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::DefLocal { name, slot, .. } => format!("DEF_LOCAL({}, Slot:{})", name.name, slot), BoundKind::DefLocal { name, slot, .. } => {
format!("DEF_LOCAL({}, Slot:{})", name.name, slot)
}
BoundKind::If { .. } => "IF".to_string(), BoundKind::If { .. } => "IF".to_string(),
BoundKind::DefGlobal { name, global_index, .. } => format!("DEF_GLOBAL({}, Idx:{})", name.name, global_index), BoundKind::DefGlobal {
BoundKind::Lambda { params, upvalues, .. } => { name, global_index, ..
} => format!("DEF_GLOBAL({}, Idx:{})", name.name, global_index),
BoundKind::Lambda {
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::Block { .. } => "BLOCK".to_string(), BoundKind::Block { .. } => "BLOCK".to_string(),
BoundKind::Tuple { elements } => format!("TUPLE({})", elements.len()), BoundKind::Tuple { elements } => format!("TUPLE({})", elements.len()),
+72 -31
View File
@@ -1,5 +1,5 @@
use crate::ast::nodes::Node;
use crate::ast::compiler::bound_nodes::BoundKind; use crate::ast::compiler::bound_nodes::BoundKind;
use crate::ast::nodes::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;
@@ -30,7 +30,8 @@ impl Dumper {
fn log<T: Debug>(&mut self, label: &str, node: &Node<BoundKind<T>, T>) { fn log<T: Debug>(&mut self, label: &str, node: &Node<BoundKind<T>, T>) {
self.write_indent(); self.write_indent();
self.output.push_str(label); self.output.push_str(label);
self.output.push_str(&format!(" <Metadata: {:?}>\n", node.ty)); self.output
.push_str(&format!(" <Metadata: {:?}>\n", node.ty));
} }
fn visit<T: Debug>(&mut self, node: &Node<BoundKind<T>, T>) { fn visit<T: Debug>(&mut self, node: &Node<BoundKind<T>, T>) {
@@ -42,26 +43,28 @@ impl Dumper {
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 } => self.log(&format!("Get: {} ({:?})", name.name, addr), node), BoundKind::Get { addr, name } => {
self.log(&format!("Get: {} ({:?})", name.name, addr), node)
}
BoundKind::Set { addr, value } => { BoundKind::Set { addr, value } => {
self.log(&format!("Set: {:?}", addr), node); self.log(&format!("Set: {:?}", addr), node);
@@ -70,34 +73,58 @@ impl Dumper {
self.indent -= 1; self.indent -= 1;
} }
BoundKind::DefLocal { name, slot, value, captured_by } => { BoundKind::DefLocal {
name,
slot,
value,
captured_by,
} => {
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(&format!("DefLocal (Name: '{}', Slot: {}, {})", name.name, slot, capture_info), node); self.log(
&format!(
"DefLocal (Name: '{}', Slot: {}, {})",
name.name, slot, capture_info
),
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();
self.output.push_str(&format!("- Capturer: Lambda at line {}, col {}\n", self.output.push_str(&format!(
capturer.location.line, capturer.location.col)); "- Capturer: Lambda at line {}, col {}\n",
capturer.location.line, capturer.location.col
));
} }
} }
self.visit(value); self.visit(value);
self.indent -= 1; self.indent -= 1;
} }
BoundKind::DefGlobal { name, global_index, value } => { BoundKind::DefGlobal {
self.log(&format!("DefGlobal (Name: '{}', Index: {})", name.name, global_index), node); name,
global_index,
value,
} => {
self.log(
&format!("DefGlobal (Name: '{}', Index: {})", name.name, global_index),
node,
);
self.indent += 1; self.indent += 1;
self.visit(value); self.visit(value);
self.indent -= 1; self.indent -= 1;
} }
BoundKind::If { cond, then_br, else_br } => { BoundKind::If {
cond,
then_br,
else_br,
} => {
self.log("If", node); self.log("If", node);
self.indent += 1; self.indent += 1;
@@ -117,7 +144,12 @@ impl Dumper {
self.indent -= 1; self.indent -= 1;
} }
BoundKind::Lambda { params, upvalues, body, .. } => { BoundKind::Lambda {
params,
upvalues,
body,
..
} => {
self.log(&format!("Lambda (Upvalues: {})", upvalues.len()), node); self.log(&format!("Lambda (Upvalues: {})", upvalues.len()), node);
self.indent += 1; self.indent += 1;
@@ -134,7 +166,10 @@ impl Dumper {
} }
BoundKind::Parameter { name, slot } => { BoundKind::Parameter { name, slot } => {
self.log(&format!("Parameter (Name: '{}', Slot: {})", name.name, slot), node); self.log(
&format!("Parameter (Name: '{}', Slot: {})", name.name, slot),
node,
);
} }
BoundKind::Call { callee, args } => { BoundKind::Call { callee, args } => {
@@ -180,8 +215,14 @@ impl Dumper {
self.indent -= 1; self.indent -= 1;
} }
BoundKind::Expansion { original_call, bound_expanded } => { BoundKind::Expansion {
self.log(&format!("Expansion (Original: {:?})", original_call.kind), node); original_call,
bound_expanded,
} => {
self.log(
&format!("Expansion (Original: {:?})", original_call.kind),
node,
);
self.indent += 1; self.indent += 1;
self.visit(bound_expanded); self.visit(bound_expanded);
self.indent -= 1; self.indent -= 1;
+15 -5
View File
@@ -1,5 +1,5 @@
use crate::ast::compiler::bound_nodes::{Address, BoundKind, BoundNode};
use std::collections::HashMap; use std::collections::HashMap;
use crate::ast::compiler::bound_nodes::{BoundKind, Address, BoundNode};
/// 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.
@@ -22,10 +22,15 @@ impl<'a, T: Clone> LambdaCollector<'a, T> {
} }
} }
BoundKind::DefGlobal { global_index, value, .. } => { BoundKind::DefGlobal {
global_index,
value,
..
} => {
// Register the full Lambda node as the template. // Register the full Lambda node as the template.
if let BoundKind::Lambda { .. } = &value.kind { if let BoundKind::Lambda { .. } = &value.kind {
self.registry.insert(*global_index, (*value).as_ref().clone()); self.registry
.insert(*global_index, (*value).as_ref().clone());
} }
self.visit(value); self.visit(value);
} }
@@ -35,12 +40,17 @@ impl<'a, T: Clone> LambdaCollector<'a, T> {
if let Address::Global(global_index) = addr if let Address::Global(global_index) = addr
&& let BoundKind::Lambda { .. } = &value.kind && let BoundKind::Lambda { .. } = &value.kind
{ {
self.registry.insert(*global_index, (*value).as_ref().clone()); self.registry
.insert(*global_index, (*value).as_ref().clone());
} }
self.visit(value); self.visit(value);
} }
BoundKind::If { cond, then_br, else_br } => { BoundKind::If {
cond,
then_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 {
+224 -68
View File
@@ -1,11 +1,15 @@
use crate::ast::nodes::{Node, Symbol, UntypedKind};
use crate::ast::types::{Identity, Value};
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
use crate::ast::nodes::{Node, UntypedKind, Symbol};
use crate::ast::types::{Value, Identity};
/// Trait for evaluating expressions during macro expansion. /// Trait for evaluating expressions during macro expansion.
pub trait MacroEvaluator { pub trait MacroEvaluator {
fn evaluate(&self, node: &Node<UntypedKind>, bindings: &HashMap<Rc<str>, Node<UntypedKind>>) -> Result<Value, String>; fn evaluate(
&self,
node: &Node<UntypedKind>,
bindings: &HashMap<Rc<str>, Node<UntypedKind>>,
) -> Result<Value, String>;
} }
/// Internal state for template expansion (Hygiene context + Parameter binding) /// Internal state for template expansion (Hygiene context + Parameter binding)
@@ -68,7 +72,10 @@ pub struct MacroExpander<E: MacroEvaluator> {
impl<E: MacroEvaluator> MacroExpander<E> { impl<E: MacroEvaluator> MacroExpander<E> {
pub fn new(registry: MacroRegistry, evaluator: E) -> Self { pub fn new(registry: MacroRegistry, evaluator: E) -> Self {
Self { registry, evaluator } Self {
registry,
evaluator,
}
} }
pub fn expand(&mut self, node: Node<UntypedKind>) -> Result<Node<UntypedKind>, String> { pub fn expand(&mut self, node: Node<UntypedKind>) -> Result<Node<UntypedKind>, String> {
@@ -89,18 +96,25 @@ impl<E: MacroEvaluator> MacroExpander<E> {
UntypedKind::Call { callee, args } => { UntypedKind::Call { callee, args } => {
if let UntypedKind::Identifier(ref sym) = callee.kind if let UntypedKind::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 UntypedKind::Tuple { elements } = expanded_args.kind {
elements elements
} else { } else {
vec![expanded_args] vec![expanded_args]
}; };
let expanded = self.expand_call(node.identity.clone(), &sym.name, params, arg_elements, body)?; let expanded = self.expand_call(
return self.expand_recursive(expanded); node.identity.clone(),
&sym.name,
params,
arg_elements,
body,
)?;
return self.expand_recursive(expanded);
} }
let expanded_callee = self.expand_recursive(*callee)?; let expanded_callee = self.expand_recursive(*callee)?;
@@ -126,7 +140,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
Ok(Node { Ok(Node {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Block { exprs: expanded_exprs }, kind: UntypedKind::Block {
exprs: expanded_exprs,
},
ty: (), ty: (),
}) })
} }
@@ -141,13 +157,17 @@ impl<E: MacroEvaluator> MacroExpander<E> {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Lambda { kind: UntypedKind::Lambda {
params: Box::new(expanded_params), params: Box::new(expanded_params),
body: Rc::new(expanded_body) body: Rc::new(expanded_body),
}, },
ty: (), ty: (),
}) })
} }
UntypedKind::If { cond, then_br, else_br } => { UntypedKind::If {
cond,
then_br,
else_br,
} => {
let cond = self.expand_recursive(*cond)?; let cond = self.expand_recursive(*cond)?;
let then_br = self.expand_recursive(*then_br)?; let then_br = self.expand_recursive(*then_br)?;
let else_br = if let Some(e) = else_br { let else_br = if let Some(e) = else_br {
@@ -157,7 +177,11 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}; };
Ok(Node { Ok(Node {
identity: node.identity, identity: node.identity,
kind: UntypedKind::If { cond: Box::new(cond), then_br: Box::new(then_br), else_br }, kind: UntypedKind::If {
cond: Box::new(cond),
then_br: Box::new(then_br),
else_br,
},
ty: (), ty: (),
}) })
} }
@@ -166,7 +190,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
let value = self.expand_recursive(*value)?; let value = self.expand_recursive(*value)?;
Ok(Node { Ok(Node {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Def { name, value: Box::new(value) }, kind: UntypedKind::Def {
name,
value: Box::new(value),
},
ty: (), ty: (),
}) })
} }
@@ -176,7 +203,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
let value = self.expand_recursive(*value)?; let value = self.expand_recursive(*value)?;
Ok(Node { Ok(Node {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Assign { target: Box::new(target), value: Box::new(value) }, kind: UntypedKind::Assign {
target: Box::new(target),
value: Box::new(value),
},
ty: (), ty: (),
}) })
} }
@@ -188,7 +218,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} }
Ok(Node { Ok(Node {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Tuple { elements: expanded_elements }, kind: UntypedKind::Tuple {
elements: expanded_elements,
},
ty: (), ty: (),
}) })
} }
@@ -200,7 +232,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} }
Ok(Node { Ok(Node {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Record { fields: expanded_fields }, kind: UntypedKind::Record {
fields: expanded_fields,
},
ty: (), ty: (),
}) })
} }
@@ -209,7 +243,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
let expanded = self.expand_recursive(*expanded)?; let expanded = self.expand_recursive(*expanded)?;
Ok(Node { Ok(Node {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Expansion { call, expanded: Box::new(expanded) }, kind: UntypedKind::Expansion {
call,
expanded: Box::new(expanded),
},
ty: (), ty: (),
}) })
} }
@@ -218,9 +255,21 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} }
} }
fn expand_call(&self, identity: Identity, name: &str, params: Vec<Rc<str>>, args: Vec<Node<UntypedKind>>, body: Node<UntypedKind>) -> Result<Node<UntypedKind>, String> { fn expand_call(
&self,
identity: Identity,
name: &str,
params: Vec<Rc<str>>,
args: Vec<Node<UntypedKind>>,
body: Node<UntypedKind>,
) -> Result<Node<UntypedKind>, String> {
if params.len() != args.len() { if params.len() != args.len() {
return Err(format!("Macro {} expects {} arguments, but got {}", name, params.len(), args.len())); return Err(format!(
"Macro {} expects {} arguments, but got {}",
name,
params.len(),
args.len()
));
} }
let mut bindings = HashMap::new(); let mut bindings = HashMap::new();
@@ -250,7 +299,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}), }),
args: Box::new(Node { args: Box::new(Node {
identity: identity.clone(), identity: identity.clone(),
kind: UntypedKind::Tuple { elements: bindings.into_values().collect() }, kind: UntypedKind::Tuple {
elements: bindings.into_values().collect(),
},
ty: (), ty: (),
}), }),
}, },
@@ -281,24 +332,37 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} }
} }
fn expand_template(&self, node: Node<UntypedKind>, state: &mut ExpansionState) -> Result<Node<UntypedKind>, String> { fn expand_template(
&self,
node: Node<UntypedKind>,
state: &mut ExpansionState,
) -> Result<Node<UntypedKind>, String> {
match node.kind { match node.kind {
UntypedKind::Identifier(mut sym) => { UntypedKind::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(Node { identity: node.identity, kind: UntypedKind::Identifier(sym), ty: () }) Ok(Node {
identity: node.identity,
kind: UntypedKind::Identifier(sym),
ty: (),
})
} }
UntypedKind::Parameter(mut sym) => { UntypedKind::Parameter(mut sym) => {
sym.context = Some(state.expansion_id.clone()); sym.context = Some(state.expansion_id.clone());
Ok(Node { identity: node.identity, kind: UntypedKind::Parameter(sym), ty: () }) Ok(Node {
identity: node.identity,
kind: UntypedKind::Parameter(sym),
ty: (),
})
} }
UntypedKind::Placeholder(inner) => { UntypedKind::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 UntypedKind::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());
} }
let val = self.evaluator.evaluate(&inner, state.bindings)?; let val = self.evaluator.evaluate(&inner, state.bindings)?;
Ok(self.value_to_node(val, node.identity)) Ok(self.value_to_node(val, node.identity))
@@ -307,8 +371,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
UntypedKind::Splice(inner) => { UntypedKind::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 UntypedKind::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());
} }
let val = self.evaluator.evaluate(&inner, state.bindings)?; let val = self.evaluator.evaluate(&inner, state.bindings)?;
Ok(self.value_to_node(val, node.identity)) Ok(self.value_to_node(val, node.identity))
@@ -319,7 +384,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
let val = self.expand_template(*value, state)?; let val = self.expand_template(*value, state)?;
Ok(Node { Ok(Node {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Def { name, value: Box::new(val) }, kind: UntypedKind::Def {
name,
value: Box::new(val),
},
ty: (), ty: (),
}) })
} }
@@ -329,7 +397,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
let value = self.expand_template(*value, state)?; let value = self.expand_template(*value, state)?;
Ok(Node { Ok(Node {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Assign { target: Box::new(target), value: Box::new(value) }, kind: UntypedKind::Assign {
target: Box::new(target),
value: Box::new(value),
},
ty: (), ty: (),
}) })
} }
@@ -346,7 +417,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} }
Ok(Node { Ok(Node {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Tuple { elements: new_elements }, kind: UntypedKind::Tuple {
elements: new_elements,
},
ty: (), ty: (),
}) })
} }
@@ -371,7 +444,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
UntypedKind::Record { fields } => { UntypedKind::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((self.expand_template(k, state)?, self.expand_template(v, state)?)); new_fields.push((
self.expand_template(k, state)?,
self.expand_template(v, state)?,
));
} }
Ok(Node { Ok(Node {
identity: node.identity, identity: node.identity,
@@ -385,12 +461,19 @@ impl<E: MacroEvaluator> MacroExpander<E> {
let expanded_args = self.expand_template(*args, state)?; let expanded_args = self.expand_template(*args, state)?;
Ok(Node { Ok(Node {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Call { callee: Box::new(expanded_callee), args: Box::new(expanded_args) }, kind: UntypedKind::Call {
callee: Box::new(expanded_callee),
args: Box::new(expanded_args),
},
ty: (), ty: (),
}) })
} }
UntypedKind::If { cond, then_br, else_br } => { UntypedKind::If {
cond,
then_br,
else_br,
} => {
let cond = self.expand_template(*cond, state)?; let cond = self.expand_template(*cond, state)?;
let then_br = self.expand_template(*then_br, state)?; let then_br = self.expand_template(*then_br, state)?;
let else_br = if let Some(e) = else_br { let else_br = if let Some(e) = else_br {
@@ -400,7 +483,11 @@ impl<E: MacroEvaluator> MacroExpander<E> {
}; };
Ok(Node { Ok(Node {
identity: node.identity, identity: node.identity,
kind: UntypedKind::If { cond: Box::new(cond), then_br: Box::new(then_br), else_br }, kind: UntypedKind::If {
cond: Box::new(cond),
then_br: Box::new(then_br),
else_br,
},
ty: (), ty: (),
}) })
} }
@@ -410,7 +497,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
let body = self.expand_template(Node::clone(&body), state)?; let body = self.expand_template(Node::clone(&body), state)?;
Ok(Node { Ok(Node {
identity: node.identity, identity: node.identity,
kind: UntypedKind::Lambda { params: Box::new(expanded_params), body: Rc::new(body) }, kind: UntypedKind::Lambda {
params: Box::new(expanded_params),
body: Rc::new(body),
},
ty: (), ty: (),
}) })
} }
@@ -419,25 +509,40 @@ impl<E: MacroEvaluator> MacroExpander<E> {
} }
} }
fn handle_splice_value(&self, val: Value, _identity: Identity, target: &mut Vec<Node<UntypedKind>>) -> Result<(), String> { fn handle_splice_value(
&self,
val: Value,
_identity: Identity,
target: &mut Vec<Node<UntypedKind>>,
) -> Result<(), String> {
match val { match val {
Value::Object(obj) => { Value::Object(obj) => {
if let Some(node) = obj.as_any().downcast_ref::<Node<UntypedKind>>() { if let Some(node) = obj.as_any().downcast_ref::<Node<UntypedKind>>() {
match &node.kind { match &node.kind {
UntypedKind::Tuple { elements } => { UntypedKind::Tuple { elements } => {
for e in elements { target.push(e.clone()); } for e in elements {
target.push(e.clone());
}
return Ok(()); return Ok(());
} }
UntypedKind::Block { exprs } => { UntypedKind::Block { exprs } => {
for e in exprs { target.push(e.clone()); } for e in exprs {
target.push(e.clone());
}
return Ok(()); return Ok(());
} }
_ => {} _ => {}
} }
} }
Err(format!("Strict Splicing: Expected Tuple or Block node, but found {}", obj.type_name())) Err(format!(
"Strict Splicing: Expected Tuple or Block node, but found {}",
obj.type_name()
))
} }
_ => Err(format!("Strict Splicing: Expected sequence (Tuple/Block Node), but found Value: {}", val)), _ => Err(format!(
"Strict Splicing: Expected sequence (Tuple/Block Node), but found Value: {}",
val
)),
} }
} }
@@ -447,9 +552,17 @@ impl<E: MacroEvaluator> MacroExpander<E> {
if let Some(node) = obj.as_any().downcast_ref::<Node<UntypedKind>>() { if let Some(node) = obj.as_any().downcast_ref::<Node<UntypedKind>>() {
return node.clone(); return node.clone();
} }
Node { identity, kind: UntypedKind::Constant(Value::Object(obj)), ty: () } Node {
identity,
kind: UntypedKind::Constant(Value::Object(obj)),
ty: (),
}
} }
_ => Node { identity, kind: UntypedKind::Constant(val), ty: () } _ => Node {
identity,
kind: UntypedKind::Constant(val),
ty: (),
},
} }
} }
} }
@@ -457,19 +570,27 @@ impl<E: MacroEvaluator> MacroExpander<E> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use std::cell::RefCell;
use crate::ast::parser::Parser;
use crate::ast::types::{Value, Object};
use crate::ast::compiler::Binder; use crate::ast::compiler::Binder;
use crate::ast::parser::Parser;
use crate::ast::types::{Object, Value};
use std::cell::RefCell;
struct SimpleEvaluator; struct SimpleEvaluator;
impl MacroEvaluator for SimpleEvaluator { impl MacroEvaluator for SimpleEvaluator {
fn evaluate(&self, node: &Node<UntypedKind>, bindings: &HashMap<Rc<str>, Node<UntypedKind>>) -> Result<Value, String> { fn evaluate(
&self,
node: &Node<UntypedKind>,
bindings: &HashMap<Rc<str>, Node<UntypedKind>>,
) -> Result<Value, String> {
if let UntypedKind::Identifier(ref sym) = node.kind if let UntypedKind::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>));
} }
Err(format!("SimpleEvaluator cannot evaluate complex expression: {:?}", node.kind)) Err(format!(
"SimpleEvaluator cannot evaluate complex expression: {:?}",
node.kind
))
} }
} }
@@ -487,11 +608,17 @@ mod tests {
let expanded = expander.expand(untyped).unwrap(); let expanded = expander.expand(untyped).unwrap();
if let UntypedKind::Block { exprs } = &expanded.kind if let UntypedKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Expansion { expanded: result, call } = &exprs[1].kind { && let UntypedKind::Expansion {
expanded: result,
call,
} = &exprs[1].kind
{
if let UntypedKind::Def { name, .. } = &result.kind { if let UntypedKind::Def { name, .. } = &result.kind {
assert_eq!(name.context, Some(call.identity.clone())); assert_eq!(name.context, Some(call.identity.clone()));
assert_eq!(name.name.as_ref(), "y"); assert_eq!(name.name.as_ref(), "y");
} else { panic!("Expected Def result, got {:?}", result.kind); } } else {
panic!("Expected Def result, got {:?}", result.kind);
}
} }
} }
@@ -509,17 +636,31 @@ mod tests {
let expanded = expander.expand(untyped).unwrap(); let expanded = expander.expand(untyped).unwrap();
if let UntypedKind::Block { exprs } = &expanded.kind if let UntypedKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Expansion { call: _, expanded: result } = &exprs[1].kind && let UntypedKind::Expansion {
&& let UntypedKind::If { cond, then_br, else_br } = &result.kind { call: _,
expanded: result,
} = &exprs[1].kind
&& let UntypedKind::If {
cond,
then_br,
else_br,
} = &result.kind
{
if let UntypedKind::Identifier(sym) = &cond.kind { if let UntypedKind::Identifier(sym) = &cond.kind {
assert_eq!(sym.name.as_ref(), "false"); assert_eq!(sym.name.as_ref(), "false");
} else { panic!("Expected identifier 'false', got {:?}", cond.kind); } } else {
panic!("Expected identifier 'false', got {:?}", cond.kind);
}
assert!(matches!(then_br.kind, UntypedKind::Nop)); assert!(matches!(then_br.kind, UntypedKind::Nop));
if let Some(eb) = else_br { if let Some(eb) = else_br {
if let UntypedKind::Constant(Value::Int(n)) = &eb.kind { if let UntypedKind::Constant(Value::Int(n)) = &eb.kind {
assert_eq!(*n, 42); assert_eq!(*n, 42);
} else { panic!("Expected 42, got {:?}", eb.kind); } } else {
} else { panic!("Else branch missing"); } panic!("Expected 42, got {:?}", eb.kind);
}
} else {
panic!("Else branch missing");
}
} }
} }
@@ -538,11 +679,18 @@ mod tests {
if let UntypedKind::Block { exprs } = &expanded.kind if let UntypedKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Def { value, .. } = &exprs[1].kind && let UntypedKind::Def { value, .. } = &exprs[1].kind
&& let UntypedKind::Expansion { expanded: result, .. } = &value.kind && let UntypedKind::Expansion {
&& let UntypedKind::Tuple { elements } = &result.kind { expanded: result, ..
} = &value.kind
&& let UntypedKind::Tuple { elements } = &result.kind
{
assert_eq!(elements.len(), 5); assert_eq!(elements.len(), 5);
if let UntypedKind::Constant(Value::Int(n)) = &elements[0].kind { assert_eq!(*n, 0); } if let UntypedKind::Constant(Value::Int(n)) = &elements[0].kind {
if let UntypedKind::Constant(Value::Int(n)) = &elements[4].kind { assert_eq!(*n, 4); } assert_eq!(*n, 0);
}
if let UntypedKind::Constant(Value::Int(n)) = &elements[4].kind {
assert_eq!(*n, 4);
}
} }
} }
@@ -564,7 +712,11 @@ mod tests {
let globals = Rc::new(RefCell::new(global_names)); let globals = Rc::new(RefCell::new(global_names));
let result = Binder::bind_root(globals, &expanded); let result = Binder::bind_root(globals, &expanded);
assert!(result.is_ok(), "Should find global '*' Error: {:?}", result.err()); assert!(
result.is_ok(),
"Should find global '*' Error: {:?}",
result.err()
);
} }
#[test] #[test]
@@ -604,6 +756,10 @@ mod tests {
let globals = Rc::new(RefCell::new(HashMap::new())); let globals = Rc::new(RefCell::new(HashMap::new()));
let bound = Binder::bind_root(globals, &expanded); let bound = Binder::bind_root(globals, &expanded);
assert!(bound.is_ok(), "Explicit Hygiene with Backticks failed: {:?}", bound.err()); assert!(
bound.is_ok(),
"Explicit Hygiene with Backticks failed: {:?}",
bound.err()
);
} }
} }
+10 -10
View File
@@ -1,20 +1,20 @@
pub mod binder; pub mod binder;
pub mod bound_nodes; pub mod bound_nodes;
pub mod tco;
pub mod upvalues;
pub mod dumper; pub mod dumper;
pub mod macros;
pub mod type_checker;
pub mod specializer;
pub mod optimizer;
pub mod lambda_collector; pub mod lambda_collector;
pub mod macros;
pub mod optimizer;
pub mod specializer;
pub mod tco;
pub mod type_checker;
pub mod upvalues;
pub use binder::*; pub use binder::*;
pub use bound_nodes::*; pub use bound_nodes::*;
pub use tco::*;
pub use upvalues::*;
pub use dumper::*; pub use dumper::*;
pub use macros::*; pub use macros::*;
pub use type_checker::*;
pub use specializer::*;
pub use optimizer::*; pub use optimizer::*;
pub use specializer::*;
pub use tco::*;
pub use type_checker::*;
pub use upvalues::*;
File diff suppressed because it is too large Load Diff
+116 -35
View File
@@ -1,9 +1,9 @@
use crate::ast::compiler::bound_nodes::{Address, BoundKind, BoundNode, TypedNode};
use crate::ast::nodes::Node;
use crate::ast::types::{Signature, StaticType, Value};
use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
use std::cell::RefCell;
use crate::ast::types::{StaticType, Value, Signature};
use crate::ast::compiler::bound_nodes::{BoundKind, Address, TypedNode, BoundNode};
use crate::ast::nodes::Node;
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MonoCacheKey { pub struct MonoCacheKey {
@@ -49,50 +49,117 @@ impl Specializer {
fn visit_node(&self, node: TypedNode) -> TypedNode { fn visit_node(&self, node: TypedNode) -> TypedNode {
let (new_kind, new_ty) = match node.kind { let (new_kind, new_ty) = match node.kind {
BoundKind::Call { callee, args } => { BoundKind::Call { callee, args } => {
let (new_callee, new_args, ret_ty) = self.specialize_call_logic(*callee, *args, node.ty.clone()); let (new_callee, new_args, ret_ty) =
(BoundKind::Call { callee: Box::new(new_callee), args: Box::new(new_args) }, ret_ty) self.specialize_call_logic(*callee, *args, node.ty.clone());
}, (
BoundKind::Call {
callee: Box::new(new_callee),
args: Box::new(new_args),
},
ret_ty,
)
}
// Recursive traversal for other nodes // Recursive traversal for other nodes
BoundKind::If { cond, then_br, else_br } => { BoundKind::If {
cond,
then_br,
else_br,
} => {
let cond = Box::new(self.visit_node(*cond)); let cond = Box::new(self.visit_node(*cond));
let then_br = Box::new(self.visit_node(*then_br)); let then_br = Box::new(self.visit_node(*then_br));
let else_br = else_br.map(|e| Box::new(self.visit_node(*e))); let else_br = else_br.map(|e| Box::new(self.visit_node(*e)));
(BoundKind::If { cond, then_br, else_br }, node.ty) (
}, BoundKind::If {
cond,
then_br,
else_br,
},
node.ty,
)
}
BoundKind::Block { exprs } => { BoundKind::Block { exprs } => {
let exprs = exprs.into_iter().map(|e| self.visit_node(e)).collect(); let exprs = exprs.into_iter().map(|e| self.visit_node(e)).collect();
(BoundKind::Block { exprs }, node.ty) (BoundKind::Block { exprs }, node.ty)
}, }
BoundKind::Lambda { params, upvalues, body, positional_count } => { BoundKind::Lambda {
params,
upvalues,
body,
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).clone())); let body = Rc::new(self.visit_node((*body).clone()));
(BoundKind::Lambda { params, upvalues, body, positional_count }, node.ty) (
}, BoundKind::Lambda {
BoundKind::DefLocal { name, slot, value, captured_by } => { params,
upvalues,
body,
positional_count,
},
node.ty,
)
}
BoundKind::DefLocal {
name,
slot,
value,
captured_by,
} => {
let value = Box::new(self.visit_node(*value)); let value = Box::new(self.visit_node(*value));
(BoundKind::DefLocal { name, slot, value, captured_by }, node.ty) (
}, BoundKind::DefLocal {
BoundKind::DefGlobal { name, global_index, value } => { name,
slot,
value,
captured_by,
},
node.ty,
)
}
BoundKind::DefGlobal {
name,
global_index,
value,
} => {
let value = Box::new(self.visit_node(*value)); let value = Box::new(self.visit_node(*value));
(BoundKind::DefGlobal { name, global_index, value }, node.ty) (
}, BoundKind::DefGlobal {
name,
global_index,
value,
},
node.ty,
)
}
BoundKind::Set { addr, value } => { BoundKind::Set { addr, value } => {
let value = Box::new(self.visit_node(*value)); let value = Box::new(self.visit_node(*value));
(BoundKind::Set { addr, value }, node.ty) (BoundKind::Set { addr, value }, node.ty)
}, }
BoundKind::Tuple { elements } => { BoundKind::Tuple { elements } => {
let elements = elements.into_iter().map(|e| self.visit_node(e)).collect(); let elements = elements.into_iter().map(|e| self.visit_node(e)).collect();
(BoundKind::Tuple { elements }, node.ty) (BoundKind::Tuple { elements }, node.ty)
}, }
BoundKind::Record { fields } => { BoundKind::Record { fields } => {
let fields = fields.into_iter().map(|(k, v)| (self.visit_node(k), self.visit_node(v))).collect(); let fields = fields
.into_iter()
.map(|(k, v)| (self.visit_node(k), self.visit_node(v)))
.collect();
(BoundKind::Record { fields }, node.ty) (BoundKind::Record { fields }, node.ty)
}, }
BoundKind::Expansion { original_call, bound_expanded } => { BoundKind::Expansion {
original_call,
bound_expanded,
} => {
let bound_expanded = Box::new(self.visit_node(*bound_expanded)); let bound_expanded = Box::new(self.visit_node(*bound_expanded));
(BoundKind::Expansion { original_call, bound_expanded }, node.ty) (
}, BoundKind::Expansion {
original_call,
bound_expanded,
},
node.ty,
)
}
// Leaf nodes or uninteresting nodes // Leaf nodes or uninteresting nodes
k => (k, node.ty), k => (k, node.ty),
@@ -105,7 +172,12 @@ impl Specializer {
} }
} }
fn specialize_call_logic(&self, callee: TypedNode, args: TypedNode, original_ty: StaticType) -> (TypedNode, TypedNode, StaticType) { fn specialize_call_logic(
&self,
callee: TypedNode,
args: TypedNode,
original_ty: StaticType,
) -> (TypedNode, TypedNode, StaticType) {
// 1. Specialize children first // 1. Specialize children first
let new_callee = self.visit_node(callee); let new_callee = self.visit_node(callee);
let new_args = self.visit_node(args); let new_args = self.visit_node(args);
@@ -131,7 +203,10 @@ impl Specializer {
} }
// --- Optimization Candidate --- // --- Optimization Candidate ---
let key = MonoCacheKey { address, arg_types: arg_types.clone() }; let key = MonoCacheKey {
address,
arg_types: arg_types.clone(),
};
// 4. Check Cache // 4. Check Cache
if let Some((val, ret_ty)) = self.cache.borrow().get(&key) { if let Some((val, ret_ty)) = self.cache.borrow().get(&key) {
@@ -152,10 +227,12 @@ impl Specializer {
&& 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)
{ {
// Cache Hit (RTL) // Cache Hit (RTL)
self.cache.borrow_mut().insert(key.clone(), (val.clone(), ret_ty.clone())); self.cache
.borrow_mut()
.insert(key.clone(), (val.clone(), ret_ty.clone()));
let specialized_callee = Node { let specialized_callee = Node {
identity: new_callee.identity.clone(), identity: new_callee.identity.clone(),
kind: BoundKind::Constant(val.clone()), kind: BoundKind::Constant(val.clone()),
ty: StaticType::Function(Box::new(Signature { ty: StaticType::Function(Box::new(Signature {
@@ -185,14 +262,18 @@ impl Specializer {
let res_ty: StaticType = ret_ty; let res_ty: StaticType = ret_ty;
// Store in cache // Store in cache
self.cache.borrow_mut().insert(key, (res_val.clone(), res_ty.clone())); self.cache
.borrow_mut()
.insert(key, (res_val.clone(), res_ty.clone()));
// PERFORMANCE: Flatten the argument tuple to match the specialized signature. // PERFORMANCE: Flatten the argument tuple to match the specialized signature.
let flat_elements = self.flatten_tuple(new_args.clone()); let flat_elements = self.flatten_tuple(new_args.clone());
let flat_types = flat_elements.iter().map(|e| e.ty.clone()).collect(); let flat_types = flat_elements.iter().map(|e| e.ty.clone()).collect();
let flattened_args = Node { let flattened_args = Node {
identity: new_args.identity.clone(), identity: new_args.identity.clone(),
kind: BoundKind::Tuple { elements: flat_elements }, kind: BoundKind::Tuple {
elements: flat_elements,
},
ty: StaticType::Tuple(flat_types), ty: StaticType::Tuple(flat_types),
}; };
@@ -205,7 +286,7 @@ impl Specializer {
})), })),
}; };
return (specialized_callee, flattened_args, res_ty); return (specialized_callee, flattened_args, res_ty);
}, }
Err(_) => { Err(_) => {
// Fallback on error // Fallback on error
} }
+92 -56
View File
@@ -1,7 +1,7 @@
use std::rc::Rc;
use crate::ast::nodes::Node;
use crate::ast::compiler::bound_nodes::{BoundKind, TypedNode}; use crate::ast::compiler::bound_nodes::{BoundKind, TypedNode};
use crate::ast::nodes::Node;
use crate::ast::types::StaticType; use crate::ast::types::StaticType;
use std::rc::Rc;
use std::fmt::Debug; use std::fmt::Debug;
@@ -36,19 +36,24 @@ impl TCO {
fn transform(node_rc: Rc<TypedNode>, is_tail_position: bool) -> ExecNode { fn transform(node_rc: Rc<TypedNode>, is_tail_position: bool) -> ExecNode {
let node = &*node_rc; let node = &*node_rc;
let new_kind = match &node.kind { let new_kind = match &node.kind {
BoundKind::Call { callee, args } => { BoundKind::Call { callee, args } => BoundKind::Call {
BoundKind::Call { callee: Box::new(Self::transform(Rc::new((**callee).clone()), false)),
callee: Box::new(Self::transform(Rc::new((**callee).clone()), false)), args: Box::new(Self::transform(Rc::new((**args).clone()), false)),
args: Box::new(Self::transform(Rc::new((**args).clone()), false)),
}
}, },
BoundKind::If { cond, then_br, else_br } => { BoundKind::If {
BoundKind::If { cond,
cond: Box::new(Self::transform(Rc::new((**cond).clone()), false)), then_br,
then_br: Box::new(Self::transform(Rc::new((**then_br).clone()), is_tail_position)), else_br,
else_br: else_br.as_ref().map(|e| Box::new(Self::transform(Rc::new((**e).clone()), is_tail_position))), } => BoundKind::If {
} cond: Box::new(Self::transform(Rc::new((**cond).clone()), false)),
then_br: Box::new(Self::transform(
Rc::new((**then_br).clone()),
is_tail_position,
)),
else_br: else_br
.as_ref()
.map(|e| Box::new(Self::transform(Rc::new((**e).clone()), is_tail_position))),
}, },
BoundKind::Block { exprs } => { BoundKind::Block { exprs } => {
@@ -60,62 +65,93 @@ impl TCO {
for (i, expr) in exprs.iter().enumerate() { for (i, expr) in exprs.iter().enumerate() {
let is_last = i == last_idx; let is_last = i == last_idx;
new_exprs.push(Self::transform(Rc::new(expr.clone()), is_tail_position && is_last)); new_exprs.push(Self::transform(
Rc::new(expr.clone()),
is_tail_position && is_last,
));
} }
BoundKind::Block { exprs: new_exprs } BoundKind::Block { exprs: new_exprs }
} }
}
BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
} => BoundKind::Lambda {
params: Rc::new(Self::transform(params.clone(), false)),
upvalues: upvalues.clone(),
body: Rc::new(Self::transform(body.clone(), true)),
positional_count: *positional_count,
}, },
BoundKind::Lambda { params, upvalues, body, positional_count } => { BoundKind::Set { addr, value } => BoundKind::Set {
BoundKind::Lambda { addr: *addr,
params: Rc::new(Self::transform(params.clone(), false)), value: Box::new(Self::transform(Rc::new((**value).clone()), false)),
upvalues: upvalues.clone(),
body: Rc::new(Self::transform(body.clone(), true)),
positional_count: *positional_count,
}
}, },
BoundKind::DefLocal {
BoundKind::Set { addr, value } => { name,
BoundKind::Set { addr: *addr, value: Box::new(Self::transform(Rc::new((**value).clone()), false)) } slot,
value,
captured_by,
} => BoundKind::DefLocal {
name: name.clone(),
slot: *slot,
value: Box::new(Self::transform(Rc::new((**value).clone()), false)),
captured_by: captured_by.clone(),
}, },
BoundKind::DefLocal { name, slot, value, captured_by } => { BoundKind::DefGlobal {
BoundKind::DefLocal { name,
name: name.clone(), global_index,
slot: *slot, value,
value: Box::new(Self::transform(Rc::new((**value).clone()), false)), } => BoundKind::DefGlobal {
captured_by: captured_by.clone() name: name.clone(),
} global_index: *global_index,
}, value: Box::new(Self::transform(Rc::new((**value).clone()), false)),
BoundKind::DefGlobal { name, global_index, value } => {
BoundKind::DefGlobal {
name: name.clone(),
global_index: *global_index,
value: Box::new(Self::transform(Rc::new((**value).clone()), false))
}
}, },
BoundKind::Record { fields } => { BoundKind::Record { fields } => {
let new_fields = fields.iter().map(|(k, v)| { let new_fields = fields
(Self::transform(Rc::new(k.clone()), false), Self::transform(Rc::new(v.clone()), false)) .iter()
}).collect(); .map(|(k, v)| {
(
Self::transform(Rc::new(k.clone()), false),
Self::transform(Rc::new(v.clone()), false),
)
})
.collect();
BoundKind::Record { fields: new_fields } BoundKind::Record { fields: new_fields }
}, }
BoundKind::Tuple { elements } => { BoundKind::Tuple { elements } => {
let new_elements = elements.iter().map(|e| Self::transform(Rc::new(e.clone()), false)).collect(); let new_elements = elements
BoundKind::Tuple { elements: new_elements } .iter()
}, .map(|e| Self::transform(Rc::new(e.clone()), false))
BoundKind::Parameter { name, slot } => BoundKind::Parameter { name: name.clone(), slot: *slot }, .collect();
BoundKind::Constant(v) => BoundKind::Constant(v.clone()), BoundKind::Tuple {
BoundKind::Get { addr, name } => BoundKind::Get { addr: *addr, name: name.clone() }, elements: new_elements,
BoundKind::Nop => BoundKind::Nop,
BoundKind::Expansion { original_call, bound_expanded } => {
BoundKind::Expansion {
original_call: original_call.clone(),
bound_expanded: Box::new(Self::transform(Rc::new((**bound_expanded).clone()), is_tail_position))
} }
} }
BoundKind::Extension(_) => { BoundKind::Parameter { name, slot } => BoundKind::Parameter {
BoundKind::Nop name: name.clone(),
} slot: *slot,
},
BoundKind::Constant(v) => BoundKind::Constant(v.clone()),
BoundKind::Get { addr, name } => BoundKind::Get {
addr: *addr,
name: name.clone(),
},
BoundKind::Nop => BoundKind::Nop,
BoundKind::Expansion {
original_call,
bound_expanded,
} => BoundKind::Expansion {
original_call: original_call.clone(),
bound_expanded: Box::new(Self::transform(
Rc::new((**bound_expanded).clone()),
is_tail_position,
)),
},
BoundKind::Extension(_) => BoundKind::Nop,
}; };
Node { Node {
+240 -81
View File
@@ -1,8 +1,8 @@
use std::collections::HashMap; use crate::ast::compiler::bound_nodes::{Address, BoundKind, BoundNode, TypedNode};
use std::rc::Rc;
use crate::ast::compiler::bound_nodes::{BoundKind, Address, BoundNode, TypedNode};
use crate::ast::nodes::Node; use crate::ast::nodes::Node;
use crate::ast::types::StaticType; use crate::ast::types::StaticType;
use std::collections::HashMap;
use std::rc::Rc;
/// Manages the types of locals and upvalues during a single type-checking pass. /// Manages the types of locals and upvalues during a single type-checking pass.
struct TypeContext<'a> { struct TypeContext<'a> {
@@ -14,7 +14,11 @@ struct TypeContext<'a> {
} }
impl<'a> TypeContext<'a> { impl<'a> TypeContext<'a> {
fn new(slot_count: u32, upvalue_types: Vec<StaticType>, parent: Option<&'a TypeContext<'a>>) -> Self { fn new(
slot_count: u32,
upvalue_types: Vec<StaticType>,
parent: Option<&'a TypeContext<'a>>,
) -> Self {
Self { Self {
_parent: parent, _parent: parent,
slots: vec![StaticType::Any; slot_count as usize], slots: vec![StaticType::Any; slot_count as usize],
@@ -24,9 +28,17 @@ impl<'a> TypeContext<'a> {
fn get_type(&self, addr: Address) -> StaticType { fn get_type(&self, addr: Address) -> StaticType {
match addr { match addr {
Address::Local(idx) => self.slots.get(idx as usize).cloned().unwrap_or(StaticType::Any), Address::Local(idx) => self
.slots
.get(idx as usize)
.cloned()
.unwrap_or(StaticType::Any),
Address::Global(_) => StaticType::Any, // Globals are dynamic for now Address::Global(_) => StaticType::Any, // Globals are dynamic for now
Address::Upvalue(idx) => self.upvalue_types.get(idx as usize).cloned().unwrap_or(StaticType::Any), Address::Upvalue(idx) => self
.upvalue_types
.get(idx as usize)
.cloned()
.unwrap_or(StaticType::Any),
} }
} }
@@ -48,7 +60,12 @@ impl TypeChecker {
pub fn check(&self, node: BoundNode, arg_types: &[StaticType]) -> Result<TypedNode, String> { pub fn check(&self, node: BoundNode, arg_types: &[StaticType]) -> Result<TypedNode, String> {
match node.kind { match node.kind {
BoundKind::Lambda { params, upvalues, body, positional_count } => { BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
} => {
// 1. Determine types of captured variables (Root lambdas have none) // 1. Determine types of captured variables (Root lambdas have none)
let mut upvalue_types = Vec::with_capacity(upvalues.len()); let mut upvalue_types = Vec::with_capacity(upvalues.len());
for _ in &upvalues { for _ in &upvalues {
@@ -66,7 +83,8 @@ impl TypeChecker {
StaticType::Tuple(arg_types.to_vec()) StaticType::Tuple(arg_types.to_vec())
}; };
let params_typed = self.check_params(params.as_ref().clone(), &arg_tuple_ty, &mut lambda_ctx)?; let params_typed =
self.check_params(params.as_ref().clone(), &arg_tuple_ty, &mut lambda_ctx)?;
// 4. Check body with the new types // 4. Check body with the new types
let body_typed = self.check_node((*body).clone(), &mut lambda_ctx)?; let body_typed = self.check_node((*body).clone(), &mut lambda_ctx)?;
@@ -112,7 +130,12 @@ impl TypeChecker {
} }
} }
fn check_params(&self, node: BoundNode, specialized_ty: &StaticType, ctx: &mut TypeContext) -> Result<TypedNode, String> { fn check_params(
&self,
node: BoundNode,
specialized_ty: &StaticType,
ctx: &mut TypeContext,
) -> Result<TypedNode, String> {
let (kind, ty) = match node.kind { let (kind, ty) = match node.kind {
BoundKind::Parameter { name, slot } => { BoundKind::Parameter { name, slot } => {
ctx.set_local_type(slot, specialized_ty.clone()); ctx.set_local_type(slot, specialized_ty.clone());
@@ -132,7 +155,12 @@ impl TypeChecker {
elem_types.push(t.ty.clone()); elem_types.push(t.ty.clone());
typed_elements.push(t); typed_elements.push(t);
} }
(BoundKind::Tuple { elements: typed_elements }, StaticType::Tuple(elem_types)) (
BoundKind::Tuple {
elements: typed_elements,
},
StaticType::Tuple(elem_types),
)
} }
_ => return Err("Invalid node in parameter list".to_string()), _ => return Err("Invalid node in parameter list".to_string()),
}; };
@@ -151,20 +179,25 @@ impl TypeChecker {
BoundKind::Constant(v) => { BoundKind::Constant(v) => {
let ty = v.static_type(); let ty = v.static_type();
(BoundKind::Constant(v), ty) (BoundKind::Constant(v), ty)
}, }
BoundKind::Parameter { name, slot } => { BoundKind::Parameter { name, slot } => (
(BoundKind::Parameter { name, slot }, ctx.get_type(Address::Local(slot))) BoundKind::Parameter { name, slot },
}, ctx.get_type(Address::Local(slot)),
),
BoundKind::Get { addr, name } => { BoundKind::Get { addr, name } => {
let ty = if let Address::Global(idx) = addr { let ty = if let Address::Global(idx) = addr {
self.global_types.borrow().get(&idx).cloned().unwrap_or(StaticType::Any) self.global_types
.borrow()
.get(&idx)
.cloned()
.unwrap_or(StaticType::Any)
} else { } else {
ctx.get_type(addr) ctx.get_type(addr)
}; };
(BoundKind::Get { addr, name }, ty) (BoundKind::Get { addr, name }, ty)
}, }
BoundKind::Set { addr, value } => { BoundKind::Set { addr, value } => {
let val_typed = self.check_node(*value, ctx)?; let val_typed = self.check_node(*value, ctx)?;
@@ -176,26 +209,62 @@ impl TypeChecker {
self.global_types.borrow_mut().insert(idx, ty.clone()); self.global_types.borrow_mut().insert(idx, ty.clone());
} }
(BoundKind::Set { addr, value: Box::new(val_typed) }, ty) (
}, BoundKind::Set {
addr,
value: Box::new(val_typed),
},
ty,
)
}
BoundKind::DefLocal { name, slot, value, captured_by } => { BoundKind::DefLocal {
name,
slot,
value,
captured_by,
} => {
let val_typed = self.check_node(*value, ctx)?; let val_typed = self.check_node(*value, ctx)?;
let ty = val_typed.ty.clone(); let ty = val_typed.ty.clone();
ctx.set_local_type(slot, ty.clone()); ctx.set_local_type(slot, ty.clone());
(BoundKind::DefLocal { name, slot, value: Box::new(val_typed), captured_by }, ty) (
}, BoundKind::DefLocal {
name,
slot,
value: Box::new(val_typed),
captured_by,
},
ty,
)
}
BoundKind::DefGlobal { name, global_index, value } => { BoundKind::DefGlobal {
name,
global_index,
value,
} => {
let val_typed = self.check_node(*value, ctx)?; let val_typed = self.check_node(*value, ctx)?;
let ty = val_typed.ty.clone(); let ty = val_typed.ty.clone();
self.global_types.borrow_mut().insert(global_index, ty.clone()); self.global_types
.borrow_mut()
.insert(global_index, ty.clone());
(BoundKind::DefGlobal { name, global_index, value: Box::new(val_typed) }, ty) (
}, BoundKind::DefGlobal {
name,
global_index,
value: Box::new(val_typed),
},
ty,
)
}
BoundKind::If { cond, then_br, else_br } => { BoundKind::If {
cond,
then_br,
else_br,
} => {
let cond_typed = self.check_node(*cond, ctx)?; let cond_typed = self.check_node(*cond, ctx)?;
let then_typed = self.check_node(*then_br, ctx)?; let then_typed = self.check_node(*then_br, ctx)?;
@@ -214,12 +283,15 @@ impl TypeChecker {
final_ty = StaticType::Any; // Delphi: MakeOptional(Then) final_ty = StaticType::Any; // Delphi: MakeOptional(Then)
} }
(BoundKind::If { (
cond: Box::new(cond_typed), BoundKind::If {
then_br: Box::new(then_typed), cond: Box::new(cond_typed),
else_br: else_typed then_br: Box::new(then_typed),
}, final_ty) else_br: else_typed,
}, },
final_ty,
)
}
BoundKind::Block { exprs } => { BoundKind::Block { exprs } => {
let mut typed_exprs = Vec::new(); let mut typed_exprs = Vec::new();
@@ -232,9 +304,14 @@ impl TypeChecker {
} }
(BoundKind::Block { exprs: typed_exprs }, last_ty) (BoundKind::Block { exprs: typed_exprs }, last_ty)
}, }
BoundKind::Lambda { params, upvalues, body, positional_count } => { BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
} => {
// 1. Determine types of captured variables // 1. Determine types of captured variables
let mut upvalue_types = Vec::with_capacity(upvalues.len()); let mut upvalue_types = Vec::with_capacity(upvalues.len());
for &addr in &upvalues { for &addr in &upvalues {
@@ -245,7 +322,8 @@ impl TypeChecker {
let mut lambda_ctx = TypeContext::new(64, upvalue_types, Some(ctx)); let mut lambda_ctx = TypeContext::new(64, upvalue_types, Some(ctx));
// 3. Check parameters and body // 3. Check parameters and body
let params_typed = self.check_params(params.as_ref().clone(), &StaticType::Any, &mut lambda_ctx)?; let params_typed =
self.check_params(params.as_ref().clone(), &StaticType::Any, &mut lambda_ctx)?;
let body_typed = self.check_node((*body).clone(), &mut lambda_ctx)?; let body_typed = self.check_node((*body).clone(), &mut lambda_ctx)?;
let ret_ty = body_typed.ty.clone(); let ret_ty = body_typed.ty.clone();
@@ -255,25 +333,34 @@ impl TypeChecker {
ret: ret_ty, ret: ret_ty,
})); }));
(BoundKind::Lambda { (
params: Rc::new(params_typed), BoundKind::Lambda {
upvalues, params: Rc::new(params_typed),
body: Rc::new(body_typed), upvalues,
positional_count, body: Rc::new(body_typed),
}, fn_ty) positional_count,
}, },
fn_ty,
)
}
BoundKind::Call { callee, args } => { BoundKind::Call { callee, args } => {
let callee_typed = self.check_node(*callee, ctx)?; let callee_typed = self.check_node(*callee, ctx)?;
let args_typed = self.check_node(*args, ctx)?; let args_typed = self.check_node(*args, ctx)?;
let ret_ty = callee_typed.ty.resolve_call(&args_typed.ty).unwrap_or(StaticType::Any); let ret_ty = callee_typed
.ty
.resolve_call(&args_typed.ty)
.unwrap_or(StaticType::Any);
(BoundKind::Call { (
callee: Box::new(callee_typed), BoundKind::Call {
args: Box::new(args_typed) callee: Box::new(callee_typed),
}, ret_ty) args: Box::new(args_typed),
}, },
ret_ty,
)
}
BoundKind::Tuple { elements } => { BoundKind::Tuple { elements } => {
let mut typed_elements = Vec::new(); let mut typed_elements = Vec::new();
@@ -288,24 +375,31 @@ impl TypeChecker {
let all_same = typed_elements.iter().all(|e| e.ty == *first_ty); let all_same = typed_elements.iter().all(|e| e.ty == *first_ty);
if all_same { if all_same {
match first_ty { match first_ty {
StaticType::Vector(inner, len) => { StaticType::Vector(inner, len) => {
StaticType::Matrix(inner.clone(), vec![typed_elements.len(), *len]) StaticType::Matrix(inner.clone(), vec![typed_elements.len(), *len])
}, }
StaticType::Matrix(inner, shape) => { StaticType::Matrix(inner, shape) => {
let mut new_shape = vec![typed_elements.len()]; let mut new_shape = vec![typed_elements.len()];
new_shape.extend(shape); new_shape.extend(shape);
StaticType::Matrix(inner.clone(), new_shape) StaticType::Matrix(inner.clone(), new_shape)
}, }
_ => StaticType::Vector(Box::new(first_ty.clone()), typed_elements.len()) _ => {
StaticType::Vector(Box::new(first_ty.clone()), typed_elements.len())
}
} }
} else { } else {
StaticType::Tuple(typed_elements.iter().map(|e| e.ty.clone()).collect()) StaticType::Tuple(typed_elements.iter().map(|e| e.ty.clone()).collect())
} }
}; };
(BoundKind::Tuple { elements: typed_elements }, ty) (
}, BoundKind::Tuple {
elements: typed_elements,
},
ty,
)
}
BoundKind::Record { fields } => { BoundKind::Record { fields } => {
let mut typed_fields = Vec::with_capacity(fields.len()); let mut typed_fields = Vec::with_capacity(fields.len());
@@ -320,21 +414,35 @@ impl TypeChecker {
typed_fields.push((kt, vt)); typed_fields.push((kt, vt));
} }
(BoundKind::Record { fields: typed_fields }, StaticType::Record(Rc::new(fields_ty))) (
}, BoundKind::Record {
fields: typed_fields,
},
StaticType::Record(Rc::new(fields_ty)),
)
}
BoundKind::Expansion { original_call, bound_expanded } => { BoundKind::Expansion {
original_call,
bound_expanded,
} => {
let expanded_typed = self.check_node(*bound_expanded, ctx)?; let expanded_typed = self.check_node(*bound_expanded, ctx)?;
let ty = expanded_typed.ty.clone(); let ty = expanded_typed.ty.clone();
(BoundKind::Expansion { (
original_call, BoundKind::Expansion {
bound_expanded: Box::new(expanded_typed) original_call,
}, ty) bound_expanded: Box::new(expanded_typed),
}, },
ty,
)
}
BoundKind::Extension(_ext) => { BoundKind::Extension(_ext) => {
return Err(format!("TypeChecking for extension '{}' not implemented", _ext.display_name())); return Err(format!(
}, "TypeChecking for extension '{}' not implemented",
_ext.display_name()
));
}
}; };
Ok(Node { Ok(Node {
@@ -379,16 +487,27 @@ mod tests {
if let BoundKind::Lambda { body, .. } = &typed.kind { if let BoundKind::Lambda { body, .. } = &typed.kind {
if let BoundKind::Block { exprs } = &body.kind { if let BoundKind::Block { exprs } = &body.kind {
let last_expr = exprs.last().unwrap(); let last_expr = exprs.last().unwrap();
assert_eq!(last_expr.ty, StaticType::Int, "Variable 'x' should be inferred as Int"); assert_eq!(
} else { panic!("Expected block in lambda body"); } last_expr.ty,
} else { panic!("Expected Lambda wrapper"); } StaticType::Int,
"Variable 'x' should be inferred as Int"
);
} else {
panic!("Expected block in lambda body");
}
} else {
panic!("Expected Lambda wrapper");
}
} }
#[test] #[test]
fn test_inference_block_type() { fn test_inference_block_type() {
// Block type = last expression type // Block type = last expression type
assert_eq!(get_ret_type(&check_source("(do 1 2.5)")), StaticType::Float); assert_eq!(get_ret_type(&check_source("(do 1 2.5)")), StaticType::Float);
assert_eq!(get_ret_type(&check_source("(do 1.5 \"test\")")), StaticType::Text); assert_eq!(
get_ret_type(&check_source("(do 1.5 \"test\")")),
StaticType::Text
);
} }
#[test] #[test]
@@ -398,13 +517,17 @@ mod tests {
let typed = check_source("(fn [a] 10)"); let typed = check_source("(fn [a] 10)");
if let StaticType::Function(sig) = &typed.ty { if let StaticType::Function(sig) = &typed.ty {
assert_eq!(sig.ret, StaticType::Int); assert_eq!(sig.ret, StaticType::Int);
} else { panic!("Expected function type, got {:?}", typed.ty); } } else {
panic!("Expected function type, got {:?}", typed.ty);
}
// Nested: (fn [] (do 1 2.5)) -> fn() -> Float // Nested: (fn [] (do 1 2.5)) -> fn() -> Float
let typed_nested = check_source("(fn [] (do 1 2.5))"); let typed_nested = check_source("(fn [] (do 1 2.5))");
if let StaticType::Function(sig) = &typed_nested.ty { if let StaticType::Function(sig) = &typed_nested.ty {
assert_eq!(sig.ret, StaticType::Float); assert_eq!(sig.ret, StaticType::Float);
} else { panic!("Expected function type"); } } else {
panic!("Expected function type");
}
} }
#[test] #[test]
@@ -414,29 +537,59 @@ mod tests {
if let BoundKind::Lambda { body, .. } = &typed.kind { if let BoundKind::Lambda { body, .. } = &typed.kind {
if let BoundKind::Block { exprs } = &body.kind { if let BoundKind::Block { exprs } = &body.kind {
let last_expr = exprs.last().unwrap(); let last_expr = exprs.last().unwrap();
assert_eq!(last_expr.ty, StaticType::Float, "Variable 'x' should be specialized to Float after assignment"); assert_eq!(
} else { panic!("Expected block"); } last_expr.ty,
} else { panic!("Expected Lambda"); } StaticType::Float,
"Variable 'x' should be specialized to Float after assignment"
);
} else {
panic!("Expected block");
}
} else {
panic!("Expected Lambda");
}
} }
#[test] #[test]
fn test_operator_overloading_inference() { fn test_operator_overloading_inference() {
assert_eq!(get_ret_type(&check_source("(+ 1 2)")), StaticType::Int); assert_eq!(get_ret_type(&check_source("(+ 1 2)")), StaticType::Int);
assert_eq!(get_ret_type(&check_source("(+ 1.0 2.0)")), StaticType::Float); assert_eq!(
assert_eq!(get_ret_type(&check_source("(+ \"a\" \"b\")")), StaticType::Text); get_ret_type(&check_source("(+ 1.0 2.0)")),
StaticType::Float
);
assert_eq!(
get_ret_type(&check_source("(+ \"a\" \"b\")")),
StaticType::Text
);
assert_eq!(get_ret_type(&check_source("(/ 1 2)")), StaticType::Float); assert_eq!(get_ret_type(&check_source("(/ 1 2)")), StaticType::Float);
} }
#[test] #[test]
fn test_datetime_inference() { fn test_datetime_inference() {
// date("2023-01-01") -> DateTime // date("2023-01-01") -> DateTime
assert_eq!(get_ret_type(&check_source("(date \"2023-01-01\")")), StaticType::DateTime); assert_eq!(
get_ret_type(&check_source("(date \"2023-01-01\")")),
StaticType::DateTime
);
// DateTime + Int -> DateTime // DateTime + Int -> DateTime
assert_eq!(get_ret_type(&check_source("(+ (date \"2023-01-01\") 86400000)")), StaticType::DateTime); assert_eq!(
get_ret_type(&check_source("(+ (date \"2023-01-01\") 86400000)")),
StaticType::DateTime
);
// DateTime - DateTime -> Int (Duration) // DateTime - DateTime -> Int (Duration)
assert_eq!(get_ret_type(&check_source("(- (date \"2023-01-02\") (date \"2023-01-01\"))")), StaticType::Int); assert_eq!(
get_ret_type(&check_source(
"(- (date \"2023-01-02\") (date \"2023-01-01\"))"
)),
StaticType::Int
);
// DateTime comparison -> Bool // DateTime comparison -> Bool
assert_eq!(get_ret_type(&check_source("(> (date \"2023-01-02\") (date \"2023-01-01\"))")), StaticType::Bool); assert_eq!(
get_ret_type(&check_source(
"(> (date \"2023-01-02\") (date \"2023-01-01\"))"
)),
StaticType::Bool
);
} }
#[test] #[test]
@@ -468,8 +621,14 @@ mod tests {
// Shape mismatch -> Tuple of Vectors // Shape mismatch -> Tuple of Vectors
let mixed = get_ret_type(&check_source("[[1 2] [3 4 5]]")); let mixed = get_ret_type(&check_source("[[1 2] [3 4 5]]"));
if let StaticType::Tuple(elements) = mixed { if let StaticType::Tuple(elements) = mixed {
assert_eq!(elements[0], StaticType::Vector(Box::new(StaticType::Int), 2)); assert_eq!(
assert_eq!(elements[1], StaticType::Vector(Box::new(StaticType::Int), 3)); elements[0],
StaticType::Vector(Box::new(StaticType::Int), 2)
);
assert_eq!(
elements[1],
StaticType::Vector(Box::new(StaticType::Int), 3)
);
} else { } else {
panic!("Expected Tuple for shape mismatch, got {:?}", mixed); panic!("Expected Tuple for shape mismatch, got {:?}", mixed);
} }
+19 -8
View File
@@ -1,6 +1,6 @@
use std::collections::{HashMap, HashSet}; use crate::ast::nodes::{Node, Symbol, UntypedKind};
use crate::ast::nodes::{Node, UntypedKind, Symbol};
use crate::ast::types::Identity; use crate::ast::types::Identity;
use std::collections::{HashMap, HashSet};
/// Analyzes the AST to find which lambdas capture which variable declarations. /// Analyzes the AST to find which lambdas capture which variable declarations.
/// Returns a map: Declaration Identity -> List of Lambda Identities that capture it. /// Returns a map: Declaration Identity -> List of Lambda Identities that capture it.
@@ -14,7 +14,8 @@ impl UpvalueAnalyzer {
Self::visit(root, &mut scopes, &mut capture_map, None); Self::visit(root, &mut scopes, &mut capture_map, None);
// Convert HashSet back to sorted Vec for the final result // Convert HashSet back to sorted Vec for the final result
capture_map.into_iter() capture_map
.into_iter()
.map(|(decl, lambdas)| (decl, lambdas.into_iter().collect())) .map(|(decl, lambdas)| (decl, lambdas.into_iter().collect()))
.collect() .collect()
} }
@@ -37,7 +38,7 @@ impl UpvalueAnalyzer {
node: &Node<UntypedKind>, node: &Node<UntypedKind>,
scopes: &mut Vec<HashMap<Symbol, Identity>>, scopes: &mut Vec<HashMap<Symbol, Identity>>,
capture_map: &mut HashMap<Identity, HashSet<Identity>>, capture_map: &mut HashMap<Identity, HashSet<Identity>>,
current_lambda: Option<Identity> current_lambda: Option<Identity>,
) { ) {
match &node.kind { match &node.kind {
UntypedKind::Identifier(sym) => { UntypedKind::Identifier(sym) => {
@@ -47,7 +48,8 @@ impl UpvalueAnalyzer {
if depth > 0 { if depth > 0 {
// Captured from an outer scope! // Captured from an outer scope!
if let Some(lambda_id) = &current_lambda { if let Some(lambda_id) = &current_lambda {
capture_map.entry(decl_id.clone()) capture_map
.entry(decl_id.clone())
.or_default() .or_default()
.insert(lambda_id.clone()); .insert(lambda_id.clone());
} }
@@ -78,7 +80,11 @@ impl UpvalueAnalyzer {
Self::visit(body, scopes, capture_map, current_lambda); Self::visit(body, scopes, capture_map, current_lambda);
scopes.pop(); scopes.pop();
} }
UntypedKind::If { cond, then_br, else_br } => { UntypedKind::If {
cond,
then_br,
else_br,
} => {
Self::visit(cond, scopes, capture_map, current_lambda.clone()); Self::visit(cond, scopes, capture_map, current_lambda.clone());
Self::visit(then_br, scopes, capture_map, current_lambda.clone()); Self::visit(then_br, scopes, capture_map, current_lambda.clone());
if let Some(e) = else_br { if let Some(e) = else_br {
@@ -112,10 +118,15 @@ impl UpvalueAnalyzer {
UntypedKind::Expansion { call: _, expanded } => { UntypedKind::Expansion { call: _, expanded } => {
Self::visit(expanded, scopes, capture_map, current_lambda); Self::visit(expanded, scopes, capture_map, current_lambda);
} }
UntypedKind::Template(body) | UntypedKind::Placeholder(body) | UntypedKind::Splice(body) => { UntypedKind::Template(body)
| UntypedKind::Placeholder(body)
| UntypedKind::Splice(body) => {
Self::visit(body, scopes, capture_map, current_lambda); Self::visit(body, scopes, capture_map, current_lambda);
} }
UntypedKind::Nop | UntypedKind::Constant(_) | UntypedKind::Extension(_) | UntypedKind::Parameter(_) => {} UntypedKind::Nop
| UntypedKind::Constant(_)
| UntypedKind::Extension(_)
| UntypedKind::Parameter(_) => {}
} }
} }
} }
+1 -1
View File
@@ -14,7 +14,7 @@ use crate::ast::compiler::lambda_collector::LambdaCollector;
use crate::ast::compiler::macros::{MacroEvaluator, MacroExpander, MacroRegistry}; use crate::ast::compiler::macros::{MacroEvaluator, MacroExpander, MacroRegistry};
use crate::ast::compiler::optimizer::Optimizer; use crate::ast::compiler::optimizer::Optimizer;
use crate::ast::compiler::specializer::{FunctionRegistry, MonoCache, Specializer}; use crate::ast::compiler::specializer::{FunctionRegistry, MonoCache, Specializer};
use crate::ast::compiler::tco::{TCO, ExecNode}; use crate::ast::compiler::tco::{ExecNode, TCO};
use crate::ast::rtl; use crate::ast::rtl;
use crate::ast::rtl::intrinsics; use crate::ast::rtl::intrinsics;
+44 -16
View File
@@ -1,14 +1,20 @@
use std::iter::Peekable;
use std::str::Chars;
use std::rc::Rc;
use crate::ast::types::SourceLocation; use crate::ast::types::SourceLocation;
use std::iter::Peekable;
use std::rc::Rc;
use std::str::Chars;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum TokenKind { pub enum TokenKind {
LeftParen, RightParen, LeftParen,
LeftBracket, RightBracket, RightParen,
LeftBrace, RightBrace, LeftBracket,
Quote, Backtick, Tilde, At, RightBracket,
LeftBrace,
RightBrace,
Quote,
Backtick,
Tilde,
At,
Identifier(Rc<str>), Identifier(Rc<str>),
Keyword(Rc<str>), Keyword(Rc<str>),
Integer(i64), Integer(i64),
@@ -41,15 +47,23 @@ impl<'a> Lexer<'a> {
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 { line: self.line, col: self.col }; let start_location = SourceLocation {
line: self.line,
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 => return Ok(Token { kind: TokenKind::EOF, location: start_location }), None => {
return Ok(Token {
kind: TokenKind::EOF,
location: start_location,
});
}
}; };
let kind = match char { let kind = match char {
@@ -68,7 +82,9 @@ impl<'a> Lexer<'a> {
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 == '-' && self.input.peek().is_some_and(|p| p.is_ascii_digit())) => { c if c.is_ascii_digit()
|| (c == '-' && self.input.peek().is_some_and(|p| p.is_ascii_digit())) =>
{
let mut num_str = String::from(c); 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 == '.' {
@@ -80,9 +96,9 @@ impl<'a> Lexer<'a> {
} }
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) => {
@@ -90,10 +106,20 @@ impl<'a> Lexer<'a> {
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!("Unexpected character: {} at {}:{}", char, self.line, self.col - 1)), _ => {
return Err(format!(
"Unexpected character: {} at {}:{}",
char,
self.line,
self.col - 1
));
}
}; };
Ok(Token { kind, location: start_location }) Ok(Token {
kind,
location: start_location,
})
} }
fn peek(&mut self) -> Option<&char> { fn peek(&mut self) -> Option<&char> {
@@ -125,7 +151,9 @@ impl<'a> Lexer<'a> {
} }
fn read_while<F>(&mut self, mut predicate: F) -> String fn read_while<F>(&mut self, mut predicate: F) -> String
where F: FnMut(char) -> bool { where
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) {
+4 -4
View File
@@ -1,8 +1,8 @@
pub mod types; pub mod compiler;
pub mod environment;
pub mod lexer; pub mod lexer;
pub mod nodes; pub mod nodes;
pub mod parser; pub mod parser;
pub mod compiler;
pub mod environment;
pub mod vm;
pub mod rtl; pub mod rtl;
pub mod types;
pub mod vm;
+11 -5
View File
@@ -1,7 +1,7 @@
use std::rc::Rc; use crate::ast::types::{Identity, Object, Value};
use std::fmt::Debug;
use std::any::Any; use std::any::Any;
use crate::ast::types::{Identity, Value, Object}; use std::fmt::Debug;
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)]
@@ -14,13 +14,19 @@ pub struct Symbol {
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 { name, context: None } Self {
name,
context: None,
}
} }
} }
impl From<&str> for Symbol { impl From<&str> for Symbol {
fn from(name: &str) -> Self { fn from(name: &str) -> Self {
Self { name: Rc::from(name), context: None } Self {
name: Rc::from(name),
context: None,
}
} }
} }
+85 -30
View File
@@ -1,7 +1,7 @@
use std::rc::Rc;
use crate::ast::lexer::{Lexer, Token, TokenKind}; use crate::ast::lexer::{Lexer, Token, TokenKind};
use crate::ast::types::{Identity, NodeIdentity, Value, Keyword}; use crate::ast::nodes::{Node, Symbol, UntypedKind};
use crate::ast::nodes::{Node, UntypedKind, Symbol}; use crate::ast::types::{Identity, Keyword, NodeIdentity, Value};
use std::rc::Rc;
pub struct Parser<'a> { pub struct Parser<'a> {
lexer: Lexer<'a>, lexer: Lexer<'a>,
@@ -12,7 +12,10 @@ impl<'a> Parser<'a> {
pub fn new(input: &'a str) -> Result<Self, String> { pub fn new(input: &'a str) -> Result<Self, String> {
let mut lexer = Lexer::new(input); let mut lexer = Lexer::new(input);
let current_token = lexer.next_token()?; let current_token = lexer.next_token()?;
Ok(Self { lexer, current_token }) Ok(Self {
lexer,
current_token,
})
} }
fn advance(&mut self) -> Result<Token, String> { fn advance(&mut self) -> Result<Token, String> {
@@ -26,7 +29,9 @@ impl<'a> Parser<'a> {
pub fn parse_expression(&mut self) -> Result<Node<UntypedKind>, String> { pub fn parse_expression(&mut self) -> Result<Node<UntypedKind>, String> {
let token_loc = self.current_token.location; let token_loc = self.current_token.location;
let identity = Rc::new(NodeIdentity { location: token_loc }); let identity = Rc::new(NodeIdentity {
location: token_loc,
});
match self.peek() { match self.peek() {
TokenKind::LeftParen => self.parse_list(), TokenKind::LeftParen => self.parse_list(),
@@ -41,7 +46,9 @@ impl<'a> Parser<'a> {
callee: Box::new(self.make_id_node("quote", identity.clone())), callee: Box::new(self.make_id_node("quote", identity.clone())),
args: Box::new(Node { args: Box::new(Node {
identity, identity,
kind: UntypedKind::Tuple { elements: vec![expr] }, kind: UntypedKind::Tuple {
elements: vec![expr],
},
ty: (), ty: (),
}), }),
}, },
@@ -86,31 +93,45 @@ impl<'a> Parser<'a> {
fn parse_atom(&mut self) -> Result<Node<UntypedKind>, String> { fn parse_atom(&mut self) -> Result<Node<UntypedKind>, String> {
let token = self.advance()?; let token = self.advance()?;
let identity = Rc::new(NodeIdentity { location: token.location }); let identity = Rc::new(NodeIdentity {
location: token.location,
});
let kind = match token.kind { let kind = match token.kind {
TokenKind::Integer(n) => UntypedKind::Constant(Value::Int(n)), TokenKind::Integer(n) => UntypedKind::Constant(Value::Int(n)),
TokenKind::Float(n) => UntypedKind::Constant(Value::Float(n)), TokenKind::Float(n) => UntypedKind::Constant(Value::Float(n)),
TokenKind::String(s) => UntypedKind::Constant(Value::Text(s)), TokenKind::String(s) => UntypedKind::Constant(Value::Text(s)),
TokenKind::Keyword(k) => UntypedKind::Constant(Value::Keyword(Keyword::intern(&k))), TokenKind::Keyword(k) => UntypedKind::Constant(Value::Keyword(Keyword::intern(&k))),
TokenKind::Identifier(id) => { TokenKind::Identifier(id) => match id.as_ref() {
match id.as_ref() { "..." => UntypedKind::Nop,
"..." => UntypedKind::Nop, _ => UntypedKind::Identifier(id.into()),
_ => UntypedKind::Identifier(id.into()), },
} _ => {
return Err(format!(
"Unexpected token in atom: {:?} at {:?}",
token.kind, token.location
));
} }
_ => return Err(format!("Unexpected token in atom: {:?} at {:?}", token.kind, token.location)),
}; };
Ok(Node { identity, kind, ty: () }) Ok(Node {
identity,
kind,
ty: (),
})
} }
fn parse_list(&mut self) -> Result<Node<UntypedKind>, String> { fn parse_list(&mut self) -> Result<Node<UntypedKind>, String> {
let start_loc = self.advance()?.location; // consume '(' let start_loc = self.advance()?.location; // consume '('
let identity = Rc::new(NodeIdentity { location: start_loc }); let identity = Rc::new(NodeIdentity {
location: start_loc,
});
if *self.peek() == TokenKind::RightParen { if *self.peek() == TokenKind::RightParen {
return Err(format!("Empty list () is not a valid expression at {:?}", start_loc)); return Err(format!(
"Empty list () is not a valid expression at {:?}",
start_loc
));
} }
let head = self.parse_expression()?; let head = self.parse_expression()?;
@@ -144,7 +165,11 @@ impl<'a> Parser<'a> {
Ok(Node { Ok(Node {
identity, identity,
kind: UntypedKind::If { cond, then_br, else_br }, kind: UntypedKind::If {
cond,
then_br,
else_br,
},
ty: (), ty: (),
}) })
} }
@@ -213,17 +238,26 @@ impl<'a> Parser<'a> {
let body = self.parse_expression()?; let body = self.parse_expression()?;
Ok(Node { Ok(Node {
identity, identity,
kind: UntypedKind::MacroDecl { name, params, body: Box::new(body) }, kind: UntypedKind::MacroDecl {
name,
params,
body: Box::new(body),
},
ty: (), ty: (),
}) })
} }
fn parse_param_vector(&mut self) -> Result<Node<UntypedKind>, String> { fn parse_param_vector(&mut self) -> Result<Node<UntypedKind>, String> {
if *self.peek() != TokenKind::LeftBracket { if *self.peek() != TokenKind::LeftBracket {
return Err(format!("Expected parameter vector [...] for fn, found {:?}", self.peek())); return Err(format!(
"Expected parameter vector [...] for fn, found {:?}",
self.peek()
));
} }
let token = self.advance()?; let token = self.advance()?;
let identity = Rc::new(NodeIdentity { location: token.location }); let identity = Rc::new(NodeIdentity {
location: token.location,
});
let mut elements = Vec::new(); let mut elements = Vec::new();
while *self.peek() != TokenKind::RightBracket { while *self.peek() != TokenKind::RightBracket {
@@ -232,17 +266,24 @@ impl<'a> Parser<'a> {
TokenKind::Identifier(name) => { TokenKind::Identifier(name) => {
let name = name.clone(); let name = name.clone();
let token = self.advance()?; let token = self.advance()?;
let p_identity = Rc::new(NodeIdentity { location: token.location }); let p_identity = Rc::new(NodeIdentity {
location: token.location,
});
elements.push(Node { elements.push(Node {
identity: p_identity, identity: p_identity,
kind: UntypedKind::Parameter(name.into()), kind: UntypedKind::Parameter(name.into()),
ty: (), ty: (),
}); });
}, }
TokenKind::LeftBracket => { TokenKind::LeftBracket => {
elements.push(self.parse_param_vector()?); elements.push(self.parse_param_vector()?);
}, }
_ => return Err(format!("Expected identifier or nested parameter vector, found {:?}", next_peek)), _ => {
return Err(format!(
"Expected identifier or nested parameter vector, found {:?}",
next_peek
));
}
} }
} }
self.expect(TokenKind::RightBracket)?; self.expect(TokenKind::RightBracket)?;
@@ -253,7 +294,11 @@ impl<'a> Parser<'a> {
}) })
} }
fn parse_call(&mut self, callee: Node<UntypedKind>, identity: Identity) -> Result<Node<UntypedKind>, String> { fn parse_call(
&mut self,
callee: Node<UntypedKind>,
identity: Identity,
) -> Result<Node<UntypedKind>, String> {
let mut elements = Vec::new(); let mut elements = Vec::new();
while *self.peek() != TokenKind::RightParen && *self.peek() != TokenKind::EOF { while *self.peek() != TokenKind::RightParen && *self.peek() != TokenKind::EOF {
@@ -269,7 +314,10 @@ impl<'a> Parser<'a> {
Ok(Node { Ok(Node {
identity, identity,
kind: UntypedKind::Call { callee: Box::new(callee), args: Box::new(args_node) }, kind: UntypedKind::Call {
callee: Box::new(callee),
args: Box::new(args_node),
},
ty: (), ty: (),
}) })
} }
@@ -286,7 +334,9 @@ impl<'a> Parser<'a> {
self.expect(TokenKind::RightBracket)?; self.expect(TokenKind::RightBracket)?;
Ok(Node { Ok(Node {
identity: Rc::new(NodeIdentity { location: token.location }), identity: Rc::new(NodeIdentity {
location: token.location,
}),
kind: UntypedKind::Tuple { elements }, kind: UntypedKind::Tuple { elements },
ty: (), ty: (),
}) })
@@ -306,7 +356,7 @@ impl<'a> Parser<'a> {
// strictly we could allow any expression and check at runtime. // strictly we could allow any expression and check at runtime.
// Delphi enforces keywords. We can do minimal check here. // Delphi enforces keywords. We can do minimal check here.
match &key_node.kind { match &key_node.kind {
UntypedKind::Constant(Value::Keyword(_)) => {}, UntypedKind::Constant(Value::Keyword(_)) => {}
_ => return Err("Record keys must be keywords (syntactically)".to_string()), _ => return Err("Record keys must be keywords (syntactically)".to_string()),
} }
@@ -320,7 +370,9 @@ impl<'a> Parser<'a> {
self.expect(TokenKind::RightBrace)?; self.expect(TokenKind::RightBrace)?;
Ok(Node { Ok(Node {
identity: Rc::new(NodeIdentity { location: token.location }), identity: Rc::new(NodeIdentity {
location: token.location,
}),
kind: UntypedKind::Record { fields }, kind: UntypedKind::Record { fields },
ty: (), ty: (),
}) })
@@ -331,7 +383,10 @@ impl<'a> Parser<'a> {
if token.kind == kind { if token.kind == kind {
Ok(()) Ok(())
} else { } else {
Err(format!("Expected {:?}, but found {:?} at {:?}", kind, token.kind, token.location)) Err(format!(
"Expected {:?}, but found {:?} at {:?}",
kind, token.kind, token.location
))
} }
} }
+208 -69
View File
@@ -1,7 +1,6 @@
use std::rc::Rc;
use crate::ast::types::{Value, StaticType, Signature};
use crate::ast::environment::Environment; use crate::ast::environment::Environment;
use crate::ast::types::{Signature, StaticType, Value};
use std::rc::Rc;
pub fn register(env: &Environment) { pub fn register(env: &Environment) {
register_constants(env); register_constants(env);
@@ -23,10 +22,22 @@ fn register_constants(env: &Environment) {
fn register_arithmetic(env: &Environment) { fn register_arithmetic(env: &Environment) {
// --- Add (+) --- // --- Add (+) ---
let add_ty = StaticType::FunctionOverloads(vec![ let add_ty = StaticType::FunctionOverloads(vec![
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Int }, Signature {
Signature { params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]), ret: StaticType::Float }, params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
Signature { params: StaticType::Tuple(vec![StaticType::Text, StaticType::Text]), ret: StaticType::Text }, ret: StaticType::Int,
Signature { params: StaticType::Tuple(vec![StaticType::DateTime, StaticType::Int]), ret: StaticType::DateTime }, },
Signature {
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
ret: StaticType::Float,
},
Signature {
params: StaticType::Tuple(vec![StaticType::Text, StaticType::Text]),
ret: StaticType::Text,
},
Signature {
params: StaticType::Tuple(vec![StaticType::DateTime, StaticType::Int]),
ret: StaticType::DateTime,
},
]); ]);
env.register_native("+", add_ty, true, |args| { env.register_native("+", add_ty, true, |args| {
if args.len() == 2 { if args.len() == 2 {
@@ -39,7 +50,7 @@ fn register_arithmetic(env: &Environment) {
let mut res = a.to_string(); let mut res = a.to_string();
res.push_str(b); res.push_str(b);
Value::Text(Rc::from(res)) Value::Text(Rc::from(res))
}, }
(Value::DateTime(ts), Value::Int(ms)) => Value::DateTime(ts + ms), (Value::DateTime(ts), Value::Int(ms)) => Value::DateTime(ts + ms),
_ => Value::Void, _ => Value::Void,
} }
@@ -47,24 +58,51 @@ fn register_arithmetic(env: &Environment) {
// Variadic sum // Variadic sum
let mut acc = 0.0; let mut acc = 0.0;
for arg in args { for arg in args {
if let Value::Int(i) = arg { acc += i as f64; } if let Value::Int(i) = arg {
else if let Value::Float(f) = arg { acc += f; } acc += i as f64;
} else if let Value::Float(f) = arg {
acc += f;
}
}
if acc.fract() == 0.0 {
Value::Int(acc as i64)
} else {
Value::Float(acc)
} }
if acc.fract() == 0.0 { Value::Int(acc as i64) } else { Value::Float(acc) }
} }
}); });
// --- Subtract (-) --- // --- Subtract (-) ---
let sub_ty = StaticType::FunctionOverloads(vec![ let sub_ty = StaticType::FunctionOverloads(vec![
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Int }, Signature {
Signature { params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]), ret: StaticType::Float }, params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
Signature { params: StaticType::Tuple(vec![StaticType::Int]), ret: StaticType::Int }, // Negation ret: StaticType::Int,
Signature { params: StaticType::Tuple(vec![StaticType::Float]), ret: StaticType::Float }, },
Signature { params: StaticType::Tuple(vec![StaticType::DateTime, StaticType::DateTime]), ret: StaticType::Int }, Signature {
Signature { params: StaticType::Tuple(vec![StaticType::DateTime, StaticType::Int]), ret: StaticType::DateTime }, params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
ret: StaticType::Float,
},
Signature {
params: StaticType::Tuple(vec![StaticType::Int]),
ret: StaticType::Int,
}, // Negation
Signature {
params: StaticType::Tuple(vec![StaticType::Float]),
ret: StaticType::Float,
},
Signature {
params: StaticType::Tuple(vec![StaticType::DateTime, StaticType::DateTime]),
ret: StaticType::Int,
},
Signature {
params: StaticType::Tuple(vec![StaticType::DateTime, StaticType::Int]),
ret: StaticType::DateTime,
},
]); ]);
env.register_native("-", sub_ty, true, |args| { env.register_native("-", sub_ty, true, |args| {
if args.is_empty() { return Value::Void; } if args.is_empty() {
return Value::Void;
}
if args.len() == 1 { if args.len() == 1 {
return match args[0] { return match args[0] {
Value::Int(i) => Value::Int(-i), Value::Int(i) => Value::Int(-i),
@@ -90,16 +128,29 @@ fn register_arithmetic(env: &Environment) {
_ => return Value::Void, _ => return Value::Void,
}; };
for arg in &args[1..] { for arg in &args[1..] {
if let Value::Int(i) = arg { acc -= *i as f64; } if let Value::Int(i) = arg {
else if let Value::Float(f) = arg { acc -= f; } acc -= *i as f64;
} else if let Value::Float(f) = arg {
acc -= f;
}
}
if acc.fract() == 0.0 {
Value::Int(acc as i64)
} else {
Value::Float(acc)
} }
if acc.fract() == 0.0 { Value::Int(acc as i64) } else { Value::Float(acc) }
}); });
// --- Multiply (*) --- // --- Multiply (*) ---
let mul_ty = StaticType::FunctionOverloads(vec![ let mul_ty = StaticType::FunctionOverloads(vec![
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Int }, Signature {
Signature { params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]), ret: StaticType::Float }, params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
ret: StaticType::Int,
},
Signature {
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
ret: StaticType::Float,
},
]); ]);
env.register_native("*", mul_ty, true, |args| { env.register_native("*", mul_ty, true, |args| {
if args.len() == 2 { if args.len() == 2 {
@@ -113,35 +164,69 @@ fn register_arithmetic(env: &Environment) {
} else { } else {
let mut acc = 1.0; let mut acc = 1.0;
for arg in args { for arg in args {
if let Value::Int(i) = arg { acc *= i as f64; } if let Value::Int(i) = arg {
else if let Value::Float(f) = arg { acc *= f; } acc *= i as f64;
} else if let Value::Float(f) = arg {
acc *= f;
}
}
if acc.fract() == 0.0 {
Value::Int(acc as i64)
} else {
Value::Float(acc)
} }
if acc.fract() == 0.0 { Value::Int(acc as i64) } else { Value::Float(acc) }
} }
}); });
// --- Divide (/) --- // --- Divide (/) ---
let div_ty = StaticType::FunctionOverloads(vec![ let div_ty = StaticType::FunctionOverloads(vec![
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Float }, Signature {
Signature { params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]), ret: StaticType::Float }, params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
ret: StaticType::Float,
},
Signature {
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
ret: StaticType::Float,
},
]); ]);
env.register_native("/", div_ty, true, |args| { env.register_native("/", div_ty, true, |args| {
if args.len() != 2 { return Value::Void; } if args.len() != 2 {
let a = match args[0] { Value::Int(i) => i as f64, Value::Float(f) => f, _ => return Value::Void }; return Value::Void;
let b = match args[1] { Value::Int(i) => i as f64, Value::Float(f) => f, _ => return Value::Void }; }
if b == 0.0 { Value::Float(f64::NAN) } else { Value::Float(a / b) } let a = match args[0] {
Value::Int(i) => i as f64,
Value::Float(f) => f,
_ => return Value::Void,
};
let b = match args[1] {
Value::Int(i) => i as f64,
Value::Float(f) => f,
_ => return Value::Void,
};
if b == 0.0 {
Value::Float(f64::NAN)
} else {
Value::Float(a / b)
}
}); });
// --- Integer Divide (//) --- // --- Integer Divide (//) ---
let int_div_ty = StaticType::Function(Box::new( let int_div_ty = StaticType::Function(Box::new(Signature {
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Int } params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
)); ret: StaticType::Int,
}));
env.register_native("//", int_div_ty, true, |args| { env.register_native("//", int_div_ty, true, |args| {
if args.len() != 2 { return Value::Void; } if args.len() != 2 {
return Value::Void;
}
match (&args[0], &args[1]) { match (&args[0], &args[1]) {
(Value::Int(a), Value::Int(b)) => { (Value::Int(a), Value::Int(b)) => {
if *b == 0 { Value::Void } else { Value::Int(a / b) } if *b == 0 {
}, Value::Void
} else {
Value::Int(a / b)
}
}
// Also allow float truncation? Delphi RTL says IntDivide takes DataValue and returns DataValue. // Also allow float truncation? Delphi RTL says IntDivide takes DataValue and returns DataValue.
// Usually div is for integers. Let's stick to Int. // Usually div is for integers. Let's stick to Int.
_ => Value::Void, _ => Value::Void,
@@ -149,15 +234,22 @@ fn register_arithmetic(env: &Environment) {
}); });
// --- Modulus (%) --- // --- Modulus (%) ---
let mod_ty = StaticType::Function(Box::new( let mod_ty = StaticType::Function(Box::new(Signature {
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Int } params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
)); ret: StaticType::Int,
}));
env.register_native("%", mod_ty, true, |args| { env.register_native("%", mod_ty, true, |args| {
if args.len() != 2 { return Value::Void; } if args.len() != 2 {
return Value::Void;
}
match (&args[0], &args[1]) { match (&args[0], &args[1]) {
(Value::Int(a), Value::Int(b)) => { (Value::Int(a), Value::Int(b)) => {
if *b == 0 { Value::Void } else { Value::Int(a % b) } if *b == 0 {
}, Value::Void
} else {
Value::Int(a % b)
}
}
_ => Value::Void, _ => Value::Void,
} }
}); });
@@ -165,14 +257,25 @@ fn register_arithmetic(env: &Environment) {
fn register_comparison(env: &Environment) { fn register_comparison(env: &Environment) {
let cmp_ty = StaticType::FunctionOverloads(vec![ let cmp_ty = StaticType::FunctionOverloads(vec![
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Bool }, Signature {
Signature { params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]), ret: StaticType::Bool }, params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
Signature { params: StaticType::Tuple(vec![StaticType::DateTime, StaticType::DateTime]), ret: StaticType::Bool }, ret: StaticType::Bool,
},
Signature {
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
ret: StaticType::Bool,
},
Signature {
params: StaticType::Tuple(vec![StaticType::DateTime, StaticType::DateTime]),
ret: StaticType::Bool,
},
]); ]);
// --- Greater Than (>) --- // --- Greater Than (>) ---
env.register_native(">", cmp_ty.clone(), true, |args| { env.register_native(">", cmp_ty.clone(), true, |args| {
if args.len() != 2 { return Value::Void; } if args.len() != 2 {
return Value::Void;
}
match (&args[0], &args[1]) { match (&args[0], &args[1]) {
(Value::Int(a), Value::Int(b)) => Value::Bool(a > b), (Value::Int(a), Value::Int(b)) => Value::Bool(a > b),
(Value::Int(a), Value::Float(b)) => Value::Bool((*a as f64) > *b), (Value::Int(a), Value::Float(b)) => Value::Bool((*a as f64) > *b),
@@ -185,7 +288,9 @@ fn register_comparison(env: &Environment) {
// --- Less Than (<) --- // --- Less Than (<) ---
env.register_native("<", cmp_ty.clone(), true, |args| { env.register_native("<", cmp_ty.clone(), true, |args| {
if args.len() != 2 { return Value::Void; } if args.len() != 2 {
return Value::Void;
}
match (&args[0], &args[1]) { match (&args[0], &args[1]) {
(Value::Int(a), Value::Int(b)) => Value::Bool(a < b), (Value::Int(a), Value::Int(b)) => Value::Bool(a < b),
(Value::Int(a), Value::Float(b)) => Value::Bool((*a as f64) < *b), (Value::Int(a), Value::Float(b)) => Value::Bool((*a as f64) < *b),
@@ -198,7 +303,9 @@ fn register_comparison(env: &Environment) {
// --- Greater Or Equal (>=) --- // --- Greater Or Equal (>=) ---
env.register_native(">=", cmp_ty.clone(), true, |args| { env.register_native(">=", cmp_ty.clone(), true, |args| {
if args.len() != 2 { return Value::Void; } if args.len() != 2 {
return Value::Void;
}
match (&args[0], &args[1]) { match (&args[0], &args[1]) {
(Value::Int(a), Value::Int(b)) => Value::Bool(a >= b), (Value::Int(a), Value::Int(b)) => Value::Bool(a >= b),
(Value::Int(a), Value::Float(b)) => Value::Bool((*a as f64) >= *b), (Value::Int(a), Value::Float(b)) => Value::Bool((*a as f64) >= *b),
@@ -211,7 +318,9 @@ fn register_comparison(env: &Environment) {
// --- Less Or Equal (<=) --- // --- Less Or Equal (<=) ---
env.register_native("<=", cmp_ty.clone(), true, |args| { env.register_native("<=", cmp_ty.clone(), true, |args| {
if args.len() != 2 { return Value::Void; } if args.len() != 2 {
return Value::Void;
}
match (&args[0], &args[1]) { match (&args[0], &args[1]) {
(Value::Int(a), Value::Int(b)) => Value::Bool(a <= b), (Value::Int(a), Value::Int(b)) => Value::Bool(a <= b),
(Value::Int(a), Value::Float(b)) => Value::Bool((*a as f64) <= *b), (Value::Int(a), Value::Float(b)) => Value::Bool((*a as f64) <= *b),
@@ -223,11 +332,14 @@ fn register_comparison(env: &Environment) {
}); });
// --- Equal (=) --- // --- Equal (=) ---
let eq_ty = StaticType::Function(Box::new( let eq_ty = StaticType::Function(Box::new(Signature {
Signature { params: StaticType::Tuple(vec![StaticType::Any, StaticType::Any]), ret: StaticType::Bool } params: StaticType::Tuple(vec![StaticType::Any, StaticType::Any]),
)); ret: StaticType::Bool,
}));
env.register_native("=", eq_ty.clone(), true, |args| { env.register_native("=", eq_ty.clone(), true, |args| {
if args.len() != 2 { return Value::Void; } if args.len() != 2 {
return Value::Void;
}
// Simple equality check. // Simple equality check.
// Note: Floating point equality is tricky, but we follow standard behavior for now. // Note: Floating point equality is tricky, but we follow standard behavior for now.
match (&args[0], &args[1]) { match (&args[0], &args[1]) {
@@ -246,7 +358,9 @@ fn register_comparison(env: &Environment) {
// --- Not Equal (<>) --- // --- Not Equal (<>) ---
env.register_native("<>", eq_ty, true, |args| { env.register_native("<>", eq_ty, true, |args| {
if args.len() != 2 { return Value::Void; } if args.len() != 2 {
return Value::Void;
}
match (&args[0], &args[1]) { match (&args[0], &args[1]) {
(Value::Int(a), Value::Int(b)) => Value::Bool(a != b), (Value::Int(a), Value::Int(b)) => Value::Bool(a != b),
(Value::Float(a), Value::Float(b)) => Value::Bool((a - b).abs() >= f64::EPSILON), (Value::Float(a), Value::Float(b)) => Value::Bool((a - b).abs() >= f64::EPSILON),
@@ -265,11 +379,19 @@ fn register_comparison(env: &Environment) {
fn register_logic(env: &Environment) { fn register_logic(env: &Environment) {
// --- Not (not) --- // --- Not (not) ---
let not_ty = StaticType::FunctionOverloads(vec![ let not_ty = StaticType::FunctionOverloads(vec![
Signature { params: StaticType::Tuple(vec![StaticType::Bool]), ret: StaticType::Bool }, Signature {
Signature { params: StaticType::Tuple(vec![StaticType::Int]), ret: StaticType::Int }, params: StaticType::Tuple(vec![StaticType::Bool]),
ret: StaticType::Bool,
},
Signature {
params: StaticType::Tuple(vec![StaticType::Int]),
ret: StaticType::Int,
},
]); ]);
env.register_native("not", not_ty, true, |args| { env.register_native("not", not_ty, true, |args| {
if args.len() != 1 { return Value::Void; } if args.len() != 1 {
return Value::Void;
}
match &args[0] { match &args[0] {
Value::Bool(b) => Value::Bool(!b), Value::Bool(b) => Value::Bool(!b),
Value::Int(i) => Value::Int(!i), // Bitwise NOT Value::Int(i) => Value::Int(!i), // Bitwise NOT
@@ -279,11 +401,19 @@ fn register_logic(env: &Environment) {
// --- And (and) --- // --- And (and) ---
let logic_op_ty = StaticType::FunctionOverloads(vec![ let logic_op_ty = StaticType::FunctionOverloads(vec![
Signature { params: StaticType::Tuple(vec![StaticType::Bool, StaticType::Bool]), ret: StaticType::Bool }, Signature {
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Int }, params: StaticType::Tuple(vec![StaticType::Bool, StaticType::Bool]),
ret: StaticType::Bool,
},
Signature {
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
ret: StaticType::Int,
},
]); ]);
env.register_native("and", logic_op_ty.clone(), true, |args| { env.register_native("and", logic_op_ty.clone(), true, |args| {
if args.len() != 2 { return Value::Void; } if args.len() != 2 {
return Value::Void;
}
match (&args[0], &args[1]) { match (&args[0], &args[1]) {
(Value::Bool(a), Value::Bool(b)) => Value::Bool(*a && *b), (Value::Bool(a), Value::Bool(b)) => Value::Bool(*a && *b),
(Value::Int(a), Value::Int(b)) => Value::Int(a & b), (Value::Int(a), Value::Int(b)) => Value::Int(a & b),
@@ -293,7 +423,9 @@ fn register_logic(env: &Environment) {
// --- Or (or) --- // --- Or (or) ---
env.register_native("or", logic_op_ty.clone(), true, |args| { env.register_native("or", logic_op_ty.clone(), true, |args| {
if args.len() != 2 { return Value::Void; } if args.len() != 2 {
return Value::Void;
}
match (&args[0], &args[1]) { match (&args[0], &args[1]) {
(Value::Bool(a), Value::Bool(b)) => Value::Bool(*a || *b), (Value::Bool(a), Value::Bool(b)) => Value::Bool(*a || *b),
(Value::Int(a), Value::Int(b)) => Value::Int(a | b), (Value::Int(a), Value::Int(b)) => Value::Int(a | b),
@@ -303,7 +435,9 @@ fn register_logic(env: &Environment) {
// --- Xor (xor) --- // --- Xor (xor) ---
env.register_native("xor", logic_op_ty.clone(), true, |args| { env.register_native("xor", logic_op_ty.clone(), true, |args| {
if args.len() != 2 { return Value::Void; } if args.len() != 2 {
return Value::Void;
}
match (&args[0], &args[1]) { match (&args[0], &args[1]) {
(Value::Bool(a), Value::Bool(b)) => Value::Bool(*a ^ *b), (Value::Bool(a), Value::Bool(b)) => Value::Bool(*a ^ *b),
(Value::Int(a), Value::Int(b)) => Value::Int(a ^ b), (Value::Int(a), Value::Int(b)) => Value::Int(a ^ b),
@@ -312,11 +446,14 @@ fn register_logic(env: &Environment) {
}); });
// --- Shift Left (<<) --- // --- Shift Left (<<) ---
let shift_ty = StaticType::Function(Box::new( let shift_ty = StaticType::Function(Box::new(Signature {
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Int } params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
)); ret: StaticType::Int,
}));
env.register_native("<<", shift_ty.clone(), true, |args| { env.register_native("<<", shift_ty.clone(), true, |args| {
if args.len() != 2 { return Value::Void; } if args.len() != 2 {
return Value::Void;
}
match (&args[0], &args[1]) { match (&args[0], &args[1]) {
(Value::Int(a), Value::Int(b)) => Value::Int(a << b), (Value::Int(a), Value::Int(b)) => Value::Int(a << b),
_ => Value::Void, _ => Value::Void,
@@ -325,7 +462,9 @@ fn register_logic(env: &Environment) {
// --- Shift Right (>>) --- // --- Shift Right (>>) ---
env.register_native(">>", shift_ty, true, |args| { env.register_native(">>", shift_ty, true, |args| {
if args.len() != 2 { return Value::Void; } if args.len() != 2 {
return Value::Void;
}
match (&args[0], &args[1]) { match (&args[0], &args[1]) {
(Value::Int(a), Value::Int(b)) => Value::Int(a >> b), (Value::Int(a), Value::Int(b)) => Value::Int(a >> b),
_ => Value::Void, _ => Value::Void,
+7 -3
View File
@@ -1,18 +1,22 @@
use crate::ast::types::{Value, StaticType, Signature};
use crate::ast::environment::Environment; use crate::ast::environment::Environment;
use crate::ast::types::{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("date", date_ty, true, |args| { env.register_native("date", date_ty, true, |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.and_hms_opt(0, 0, 0).unwrap().and_utc().timestamp_millis(); let ts = dt
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.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
+31 -23
View File
@@ -1,5 +1,5 @@
use crate::ast::types::{StaticType, Value};
use std::rc::Rc; use std::rc::Rc;
use crate::ast::types::{Value, StaticType};
/// 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.
@@ -14,7 +14,7 @@ pub fn lookup(name: &str, args: &[StaticType]) -> Option<(Value, StaticType)> {
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::Function(Rc::new(|args| { Value::Function(Rc::new(|args| {
@@ -24,18 +24,27 @@ pub fn lookup(name: &str, args: &[StaticType]) -> Option<(Value, StaticType)> {
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::Function(Rc::new(|args| { Value::Function(Rc::new(|args| {
let a = match args[0] { Value::Int(i) => i, _ => 0 }; let a = match args[0] {
let b = match args[1] { Value::Int(i) => i, _ => 0 }; Value::Int(i) => i,
let c = match args[2] { Value::Int(i) => i, _ => 0 }; _ => 0,
};
let b = match args[1] {
Value::Int(i) => i,
_ => 0,
};
let c = match args[2] {
Value::Int(i) => i,
_ => 0,
};
Value::Int(a - b - c) Value::Int(a - b - c)
})), })),
StaticType::Int StaticType::Int,
)), )),
("*", [StaticType::Int, StaticType::Int]) => Some(( ("*", [StaticType::Int, StaticType::Int]) => Some((
Value::Function(Rc::new(|args| { Value::Function(Rc::new(|args| {
@@ -45,64 +54,63 @@ pub fn lookup(name: &str, args: &[StaticType]) -> Option<(Value, StaticType)> {
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::Function(Rc::new(|args| { Value::Function(Rc::new(|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::Function(Rc::new(|args| { Value::Function(Rc::new(|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::Function(Rc::new(|args| { Value::Function(Rc::new(|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::Function(Rc::new(|args| { Value::Function(Rc::new(|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::Function(Rc::new(|args| { Value::Function(Rc::new(|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
} }
} }
+1 -1
View File
@@ -1,7 +1,7 @@
pub mod core; pub mod core;
pub mod datetime; pub mod datetime;
pub mod type_registry;
pub mod intrinsics; pub mod intrinsics;
pub mod type_registry;
use crate::ast::environment::Environment; use crate::ast::environment::Environment;
+65 -38
View File
@@ -1,7 +1,7 @@
use crate::ast::types::{Keyword, Signature, StaticType, Value};
use std::any::TypeId;
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
use std::any::{TypeId};
use crate::ast::types::{Value, StaticType, Keyword, Signature};
/// 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 {
@@ -41,19 +41,20 @@ impl TypeRegistry {
/// 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.get(&TypeId::of::<T>()).cloned().unwrap_or(StaticType::Any) self.known_types
.get(&TypeId::of::<T>())
.cloned()
.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>( pub fn create_factory<T, F>(factory_func: F) -> Value
factory_func: F
) -> Value
where where
T: Scriptable, T: Scriptable,
F: Fn(Vec<Value>) -> Result<T, String> + 'static F: Fn(Vec<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: Vec<Value>| -> Value { let closure = move |args: Vec<Value>| -> Value {
@@ -85,13 +86,12 @@ impl Default for RecordBuilder {
impl RecordBuilder { impl RecordBuilder {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self { fields: Vec::new() }
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 F: Fn(Vec<Value>) -> Value + 'static where
F: Fn(Vec<Value>) -> Value + 'static,
{ {
let key = Keyword::intern(name); let key = Keyword::intern(name);
self.fields.push((key, Value::Function(Rc::new(func)))); self.fields.push((key, Value::Function(Rc::new(func))));
@@ -100,13 +100,12 @@ impl RecordBuilder {
// 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 F: Fn(Vec<Value>) -> Result<Value, String> + 'static where
F: Fn(Vec<Value>) -> Result<Value, String> + 'static,
{ {
let closure = move |args: Vec<Value>| { let closure = move |args: Vec<Value>| match func(args) {
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)
} }
@@ -135,14 +134,15 @@ impl Default for TypeBuilder {
impl TypeBuilder { impl TypeBuilder {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self { fields: Vec::new() }
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 { params: StaticType::Tuple(params), ret })); let sig = StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(params),
ret,
}));
self.fields.push((key, sig)); self.fields.push((key, sig));
self self
} }
@@ -155,7 +155,7 @@ impl TypeBuilder {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::ast::types::{Value, StaticType}; use crate::ast::types::{StaticType, Value};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct Person { struct Person {
@@ -164,7 +164,9 @@ mod tests {
} }
impl Scriptable for Person { impl Scriptable for Person {
fn type_name() -> &'static str { "Person" } fn type_name() -> &'static str {
"Person"
}
fn static_type() -> StaticType { fn static_type() -> StaticType {
TypeBuilder::new() TypeBuilder::new()
@@ -178,7 +180,9 @@ mod tests {
let age = self.age; let age = self.age;
RecordBuilder::new() RecordBuilder::new()
.method("greet", move |_| Value::Text(format!("Hello {}", name).into())) .method("greet", move |_| {
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()
} }
@@ -189,7 +193,10 @@ mod tests {
let mut registry = TypeRegistry::new(); let mut registry = TypeRegistry::new();
registry.register::<Person>(); registry.register::<Person>();
let p = Person { name: "Alice".to_string(), age: 30 }; let p = Person {
name: "Alice".to_string(),
age: 30,
};
let wrapped = p.wrap(); let wrapped = p.wrap();
// Check static type // Check static type
@@ -202,7 +209,11 @@ mod tests {
// Check runtime behavior // Check runtime behavior
if let Value::Record(r) = wrapped { if let Value::Record(r) = wrapped {
let idx = r.keys.iter().position(|k| *k == Keyword::intern("greet")).unwrap(); let idx = r
.keys
.iter()
.position(|k| *k == Keyword::intern("greet"))
.unwrap();
let greet_fn = &r.values[idx]; let greet_fn = &r.values[idx];
if let Value::Function(f) = greet_fn { if let Value::Function(f) = greet_fn {
@@ -226,24 +237,40 @@ mod tests {
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] { Value::Text(t) => t.to_string(), _ => return Err("Name must be text".to_string()) }; let name = match &args[0] {
let age = match &args[1] { Value::Int(i) => *i as u32, _ => return Err("Age must be int".to_string()) }; Value::Text(t) => t.to_string(),
_ => return Err("Name must be text".to_string()),
};
let age = match &args[1] {
Value::Int(i) => *i as u32,
_ => return Err("Age must be int".to_string()),
};
Ok(Person { name, age }) Ok(Person { name, age })
}); });
if let Value::Function(f) = factory_val { if let Value::Function(f) = factory_val {
let instance = f(vec![Value::Text("Bob".into()), Value::Int(40)]); let instance = f(vec![Value::Text("Bob".into()), Value::Int(40)]);
if let Value::Record(r) = instance { if let Value::Record(r) = instance {
let idx = r.keys.iter().position(|k| *k == Keyword::intern("greet")).unwrap(); let idx = r
let greet_fn = &r.values[idx]; .keys
.iter()
.position(|k| *k == Keyword::intern("greet"))
.unwrap();
let greet_fn = &r.values[idx];
if let Value::Function(gf) = greet_fn { if let Value::Function(gf) = greet_fn {
let res = gf(vec![]); let res = gf(vec![]);
if let Value::Text(s) = res { if let Value::Text(s) = res {
assert_eq!(&*s, "Hello Bob"); assert_eq!(&*s, "Hello Bob");
} else { panic!("Wrong return type"); } } else {
} panic!("Wrong return type");
} else { panic!("Factory should return Record"); } }
} else { panic!("Factory is not a function"); } }
} else {
panic!("Factory should return Record");
}
} else {
panic!("Factory is not a function");
}
} }
} }
+67 -45
View File
@@ -1,11 +1,11 @@
use std::collections::HashMap;
use std::rc::Rc;
use std::cell::RefCell;
use std::fmt;
use std::sync::OnceLock;
use std::sync::Mutex; // Still needed for global keyword registry
use std::any::Any;
use chrono::{TimeZone, Utc}; use chrono::{TimeZone, Utc};
use std::any::Any;
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt;
use std::rc::Rc;
use std::sync::Mutex; // Still needed for global keyword registry
use std::sync::OnceLock;
/// Simple source location /// Simple source location
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -31,11 +31,17 @@ static KEYWORD_REVERSE: OnceLock<Mutex<Vec<String>>> = OnceLock::new();
impl Keyword { impl Keyword {
pub fn intern(name: &str) -> Self { pub fn intern(name: &str) -> Self {
let mut reg = KEYWORD_REGISTRY.get_or_init(|| Mutex::new(HashMap::new())).lock().unwrap(); let mut reg = KEYWORD_REGISTRY
.get_or_init(|| Mutex::new(HashMap::new()))
.lock()
.unwrap();
if let Some(&id) = reg.get(name) { if let Some(&id) = reg.get(name) {
Keyword(id) Keyword(id)
} else { } else {
let mut rev = KEYWORD_REVERSE.get_or_init(|| Mutex::new(Vec::new())).lock().unwrap(); let mut rev = KEYWORD_REVERSE
.get_or_init(|| Mutex::new(Vec::new()))
.lock()
.unwrap();
let id = rev.len() as u32; let id = rev.len() as u32;
reg.insert(name.to_string(), id); reg.insert(name.to_string(), id);
rev.push(name.to_string()); rev.push(name.to_string());
@@ -44,7 +50,10 @@ impl Keyword {
} }
pub fn name(&self) -> String { pub fn name(&self) -> String {
let rev = KEYWORD_REVERSE.get_or_init(|| Mutex::new(Vec::new())).lock().unwrap(); let rev = KEYWORD_REVERSE
.get_or_init(|| Mutex::new(Vec::new()))
.lock()
.unwrap();
rev[self.0 as usize].clone() rev[self.0 as usize].clone()
} }
} }
@@ -80,7 +89,7 @@ pub enum Value {
Tuple(ValueList), Tuple(ValueList),
Record(Rc<RecordData>), Record(Rc<RecordData>),
Function(Rc<dyn Fn(Vec<Value>) -> Value>), Function(Rc<dyn Fn(Vec<Value>) -> Value>),
Object(Rc<dyn Object>), // For compiled Closures and other opaque types Object(Rc<dyn Object>), // For compiled Closures and other opaque types
Cell(Rc<RefCell<Value>>), // Boxed value for captures Cell(Rc<RefCell<Value>>), // Boxed value for captures
TailCallRequest(Box<(Rc<dyn Object>, Vec<Value>)>), // Internal: For TCO (Boxed to keep Value small) TailCallRequest(Box<(Rc<dyn Object>, Vec<Value>)>), // Internal: For TCO (Boxed to keep Value small)
} }
@@ -124,9 +133,9 @@ pub enum StaticType {
DateTime, DateTime,
Text, Text,
Keyword, Keyword,
List(Box<StaticType>), // Legacy / Dynamic list List(Box<StaticType>), // Legacy / Dynamic list
Tuple(Vec<StaticType>), // Heterogeneous fixed-size Tuple(Vec<StaticType>), // Heterogeneous fixed-size
Vector(Box<StaticType>, usize), // Homogeneous fixed-size Vector(Box<StaticType>, usize), // Homogeneous fixed-size
Matrix(Box<StaticType>, Vec<usize>), // Multi-dimensional homogeneous Matrix(Box<StaticType>, Vec<usize>), // Multi-dimensional homogeneous
Record(Rc<Vec<(Keyword, StaticType)>>), Record(Rc<Vec<(Keyword, StaticType)>>),
Function(Box<Signature>), Function(Box<Signature>),
@@ -149,31 +158,37 @@ impl fmt::Display for StaticType {
StaticType::Tuple(elements) => { StaticType::Tuple(elements) => {
write!(f, "[")?; write!(f, "[")?;
for (i, el) in elements.iter().enumerate() { for (i, el) in elements.iter().enumerate() {
if i > 0 { write!(f, " ")?; } if i > 0 {
write!(f, " ")?;
}
write!(f, "{}", el)?; write!(f, "{}", el)?;
} }
write!(f, "]") write!(f, "]")
}, }
StaticType::Vector(inner, len) => write!(f, "vector<{}, {}>", inner, len), StaticType::Vector(inner, len) => write!(f, "vector<{}, {}>", inner, len),
StaticType::Matrix(inner, shape) => { StaticType::Matrix(inner, shape) => {
write!(f, "matrix<{}, [", inner)?; write!(f, "matrix<{}, [", inner)?;
for (i, s) in shape.iter().enumerate() { for (i, s) in shape.iter().enumerate() {
if i > 0 { write!(f, " ")?; } if i > 0 {
write!(f, " ")?;
}
write!(f, "{}", s)?; write!(f, "{}", s)?;
} }
write!(f, "]>") write!(f, "]>")
}, }
StaticType::Record(fields) => { StaticType::Record(fields) => {
write!(f, "{{")?; write!(f, "{{")?;
for (i, (k, v)) in fields.iter().enumerate() { for (i, (k, v)) in fields.iter().enumerate() {
if i > 0 { write!(f, ", ")?; } if i > 0 {
write!(f, ", ")?;
}
write!(f, ":{} {}", k.name(), v)?; write!(f, ":{} {}", k.name(), v)?;
} }
write!(f, "}}") write!(f, "}}")
}, }
StaticType::Function(sig) => { StaticType::Function(sig) => {
write!(f, "fn({}) -> {}", sig.params, sig.ret) write!(f, "fn({}) -> {}", sig.params, sig.ret)
}, }
StaticType::FunctionOverloads(sigs) => { StaticType::FunctionOverloads(sigs) => {
write!(f, "overloads({} variants)", sigs.len()) write!(f, "overloads({} variants)", sigs.len())
} }
@@ -192,12 +207,16 @@ impl StaticType {
match (self, other) { match (self, other) {
// A Vector is a Tuple // A Vector is a Tuple
(StaticType::Tuple(elements), StaticType::Vector(inner, len)) => { (StaticType::Tuple(elements), StaticType::Vector(inner, len)) => {
if elements.len() != *len { return false; } if elements.len() != *len {
return false;
}
elements.iter().all(|e| e.is_assignable_from(inner)) elements.iter().all(|e| e.is_assignable_from(inner))
}, }
// A Matrix is a Vector (of Vectors/Matrices) // A Matrix is a Vector (of Vectors/Matrices)
(StaticType::Vector(inner, len), StaticType::Matrix(m_inner, shape)) => { (StaticType::Vector(inner, len), StaticType::Matrix(m_inner, shape)) => {
if shape.is_empty() || shape[0] != *len { return false; } if shape.is_empty() || shape[0] != *len {
return false;
}
if shape.len() == 1 { if shape.len() == 1 {
inner.is_assignable_from(m_inner) inner.is_assignable_from(m_inner)
} else { } else {
@@ -205,8 +224,8 @@ impl StaticType {
let sub_shape = shape[1..].to_vec(); let sub_shape = shape[1..].to_vec();
inner.is_assignable_from(&StaticType::Matrix(m_inner.clone(), sub_shape)) inner.is_assignable_from(&StaticType::Matrix(m_inner.clone(), sub_shape))
} }
}, }
_ => false _ => false,
} }
} }
@@ -221,11 +240,10 @@ impl StaticType {
None None
} }
} }
StaticType::FunctionOverloads(sigs) => { StaticType::FunctionOverloads(sigs) => sigs
sigs.iter() .iter()
.find(|sig| sig.params.is_assignable_from(args_ty)) .find(|sig| sig.params.is_assignable_from(args_ty))
.map(|sig| sig.ret.clone()) .map(|sig| sig.ret.clone()),
}
_ => None, _ => None,
} }
} }
@@ -268,7 +286,7 @@ impl Value {
pub fn make_record(keys: Vec<Keyword>, values: Vec<Value>) -> Self { pub fn make_record(keys: Vec<Keyword>, values: Vec<Value>) -> Self {
Value::Record(Rc::new(RecordData { Value::Record(Rc::new(RecordData {
keys: Rc::new(keys), keys: Rc::new(keys),
values: Rc::new(values) values: Rc::new(values),
})) }))
} }
@@ -297,25 +315,25 @@ impl Value {
StaticType::Vector(inner, len) => { StaticType::Vector(inner, len) => {
// Possible Matrix // Possible Matrix
StaticType::Matrix(inner.clone(), vec![values.len(), *len]) StaticType::Matrix(inner.clone(), vec![values.len(), *len])
}, }
StaticType::Matrix(inner, shape) => { StaticType::Matrix(inner, shape) => {
let mut new_shape = vec![values.len()]; let mut new_shape = vec![values.len()];
new_shape.extend(shape); new_shape.extend(shape);
StaticType::Matrix(inner.clone(), new_shape) StaticType::Matrix(inner.clone(), new_shape)
}, }
_ => StaticType::Vector(Box::new(first_ty.clone()), values.len()) _ => StaticType::Vector(Box::new(first_ty.clone()), values.len()),
} }
} else { } else {
StaticType::Tuple(element_types) StaticType::Tuple(element_types)
} }
}, }
Value::Record(r) => { Value::Record(r) => {
let mut fields = Vec::with_capacity(r.values.len()); let mut fields = Vec::with_capacity(r.values.len());
for (i, v) in r.values.iter().enumerate() { for (i, v) in r.values.iter().enumerate() {
fields.push((r.keys[i], v.static_type())); fields.push((r.keys[i], v.static_type()));
} }
StaticType::Record(Rc::new(fields)) StaticType::Record(Rc::new(fields))
}, }
Value::Function(_) => StaticType::Any, // Dynamic function Value::Function(_) => StaticType::Any, // Dynamic function
Value::Object(o) => StaticType::Object(o.type_name()), Value::Object(o) => StaticType::Object(o.type_name()),
Value::Cell(c) => c.borrow().static_type(), Value::Cell(c) => c.borrow().static_type(),
@@ -331,30 +349,34 @@ impl fmt::Display for Value {
Value::Bool(b) => write!(f, "{}", b), Value::Bool(b) => write!(f, "{}", b),
Value::Int(i) => write!(f, "{}", i), Value::Int(i) => write!(f, "{}", i),
Value::Float(fl) => write!(f, "{}", fl), Value::Float(fl) => write!(f, "{}", fl),
Value::DateTime(ts) => { Value::DateTime(ts) => match Utc.timestamp_millis_opt(*ts) {
match Utc.timestamp_millis_opt(*ts) { chrono::LocalResult::Single(dt) => {
chrono::LocalResult::Single(dt) => write!(f, "#{}#", dt.format("%Y-%m-%d %H:%M:%S")), write!(f, "#{}#", dt.format("%Y-%m-%d %H:%M:%S"))
_ => write!(f, "#timestamp({})#", ts),
} }
_ => write!(f, "#timestamp({})#", ts),
}, },
Value::Text(t) => write!(f, "\"{}\"", t), Value::Text(t) => write!(f, "\"{}\"", t),
Value::Keyword(k) => write!(f, ":{}", k.name()), Value::Keyword(k) => write!(f, ":{}", k.name()),
Value::Tuple(values) => { Value::Tuple(values) => {
write!(f, "[")?; write!(f, "[")?;
for (i, val) in values.iter().enumerate() { for (i, val) in values.iter().enumerate() {
if i > 0 { write!(f, " ")?; } if i > 0 {
write!(f, " ")?;
}
write!(f, "{}", val)?; write!(f, "{}", val)?;
} }
write!(f, "]") write!(f, "]")
}, }
Value::Record(r) => { Value::Record(r) => {
write!(f, "{{")?; write!(f, "{{")?;
for i in 0..r.values.len() { for i in 0..r.values.len() {
if i > 0 { write!(f, ", ")?; } if i > 0 {
write!(f, ", ")?;
}
write!(f, ":{} {}", r.keys[i].name(), r.values[i])?; write!(f, ":{} {}", r.keys[i].name(), r.values[i])?;
} }
write!(f, "}}") write!(f, "}}")
}, }
Value::Function(_) => write!(f, "<native fn>"), Value::Function(_) => write!(f, "<native fn>"),
Value::Object(o) => write!(f, "<{}>", o.type_name()), Value::Object(o) => write!(f, "<{}>", o.type_name()),
Value::Cell(c) => write!(f, "{}", c.borrow()), Value::Cell(c) => write!(f, "{}", c.borrow()),
+339 -92
View File
@@ -1,10 +1,10 @@
use std::rc::Rc;
use std::cell::RefCell;
use std::any::Any;
use crate::ast::compiler::bound_nodes::{Address, BoundKind, TypedNode}; use crate::ast::compiler::bound_nodes::{Address, BoundKind, TypedNode};
use crate::ast::compiler::tco::ExecNode; use crate::ast::compiler::tco::ExecNode;
use crate::ast::types::{Value, Object};
use crate::ast::nodes::Node; use crate::ast::nodes::Node;
use crate::ast::types::{Object, Value};
use std::any::Any;
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Closure { pub struct Closure {
@@ -17,14 +17,30 @@ pub struct Closure {
impl Closure { impl Closure {
#[inline] #[inline]
pub fn new(params: Rc<TypedNode>, body: Rc<TypedNode>, exec: Rc<ExecNode>, upvalues: Vec<Rc<RefCell<Value>>>, positional_count: Option<u32>) -> Self { pub fn new(
Self { parameter_node: params, function_node: body, exec_node: exec, upvalues, positional_count } params: Rc<TypedNode>,
body: Rc<TypedNode>,
exec: Rc<ExecNode>,
upvalues: Vec<Rc<RefCell<Value>>>,
positional_count: Option<u32>,
) -> Self {
Self {
parameter_node: params,
function_node: body,
exec_node: exec,
upvalues,
positional_count,
}
} }
} }
impl Object for Closure { impl Object for Closure {
fn type_name(&self) -> &'static str { "closure" } fn type_name(&self) -> &'static str {
fn as_any(&self) -> &dyn Any { self } "closure"
}
fn as_any(&self) -> &dyn Any {
self
}
} }
#[derive(Debug)] #[derive(Debug)]
@@ -48,17 +64,33 @@ pub struct TracingObserver {
} }
impl TracingObserver { impl TracingObserver {
pub fn new() -> Self { Self { logs: Vec::new(), indent: 0 } } pub fn new() -> Self {
fn pad(&self) -> String { "| ".repeat(self.indent) } Self {
logs: Vec::new(),
indent: 0,
}
}
fn pad(&self) -> String {
"| ".repeat(self.indent)
}
} }
impl Default for TracingObserver { fn default() -> Self { Self::new() } } impl Default for TracingObserver {
fn default() -> Self {
Self::new()
}
}
impl VMObserver for TracingObserver { impl VMObserver for TracingObserver {
const ACTIVE: bool = true; const ACTIVE: bool = true;
fn before_eval(&mut self, _vm: &VM, node: &ExecNode) { fn before_eval(&mut self, _vm: &VM, node: &ExecNode) {
let pad = self.pad(); let pad = self.pad();
self.logs.push(format!("{}{} [{}]: {{", pad, node.kind.display_name(), node.ty.ty)); self.logs.push(format!(
"{}{} [{}]: {{",
pad,
node.kind.display_name(),
node.ty.ty
));
self.indent += 1; self.indent += 1;
} }
fn after_eval(&mut self, vm: &VM, node: &ExecNode, res: &Result<Value, String>) { fn after_eval(&mut self, vm: &VM, node: &ExecNode, res: &Result<Value, String>) {
@@ -68,11 +100,18 @@ impl VMObserver for TracingObserver {
BoundKind::DefLocal { .. } | BoundKind::Set { .. } | BoundKind::DefGlobal { .. } => { BoundKind::DefLocal { .. } | BoundKind::Set { .. } | BoundKind::DefGlobal { .. } => {
let s_pad = format!("{}| ", pad); let s_pad = format!("{}| ", pad);
self.logs.push(format!("{}--- Scope Status ---", s_pad)); self.logs.push(format!("{}--- Scope Status ---", s_pad));
self.logs.push(format!("{}Stack (top 5): {:?}", s_pad, vm.stack.iter().rev().take(5).collect::<Vec<_>>())); self.logs.push(format!(
}, "{}Stack (top 5): {:?}",
s_pad,
vm.stack.iter().rev().take(5).collect::<Vec<_>>()
));
}
_ => {} _ => {}
} }
let res_str = match res { Ok(v) => format!("{}", v), Err(e) => format!("ERROR: {}", e) }; let res_str = match res {
Ok(v) => format!("{}", v),
Err(e) => format!("ERROR: {}", e),
};
self.logs.push(format!("{}}} -> {}", pad, res_str)); self.logs.push(format!("{}}} -> {}", pad, res_str));
} }
} }
@@ -214,11 +253,21 @@ macro_rules! dispatch_eval {
} }
impl VM { impl VM {
pub fn new(globals: Rc<RefCell<Vec<Value>>>) -> Self { Self { stack: Vec::new(), globals, frames: Vec::new() } } pub fn new(globals: Rc<RefCell<Vec<Value>>>) -> Self {
Self {
stack: Vec::new(),
globals,
frames: Vec::new(),
}
}
pub fn run(&mut self, root: &ExecNode) -> Result<Value, String> { pub fn run(&mut self, root: &ExecNode) -> Result<Value, String> {
self.stack.clear(); self.frames.clear(); self.stack.clear();
self.frames.push(CallFrame { stack_base: 0, closure: None }); self.frames.clear();
self.frames.push(CallFrame {
stack_base: 0,
closure: None,
});
let mut result = self.eval(root); let mut result = self.eval(root);
self.frames.pop(); self.frames.pop();
loop { loop {
@@ -227,15 +276,25 @@ impl VM {
let (next_obj, next_args) = *payload; let (next_obj, next_args) = *payload;
if let Some(closure) = next_obj.as_any().downcast_ref::<Closure>() { if let Some(closure) = next_obj.as_any().downcast_ref::<Closure>() {
self.stack.clear(); self.stack.clear();
self.frames.push(CallFrame { stack_base: 0, closure: Some(Rc::new(closure.clone())) }); self.frames.push(CallFrame {
if let Some(count) = closure.positional_count && next_args.len() == count as usize { stack_base: 0,
closure: Some(Rc::new(closure.clone())),
});
if let Some(count) = closure.positional_count
&& next_args.len() == count as usize
{
self.stack.extend(next_args); self.stack.extend(next_args);
} else { } else {
self.unpack(&closure.parameter_node, &next_args, &mut 0)?; self.unpack(&closure.parameter_node, &next_args, &mut 0)?;
} }
result = self.eval(&closure.exec_node); result = self.eval(&closure.exec_node);
self.frames.pop(); self.frames.pop();
} else { return Err(format!("Tail call target is not a closure: {}", next_obj.type_name())); } } else {
return Err(format!(
"Tail call target is not a closure: {}",
next_obj.type_name()
));
}
} }
_ => return result, _ => return result,
} }
@@ -243,34 +302,74 @@ impl VM {
} }
pub fn run_with_args(&mut self, closure: &Closure, args: Vec<Value>) -> Result<Value, String> { pub fn run_with_args(&mut self, closure: &Closure, args: Vec<Value>) -> Result<Value, String> {
self.stack.clear(); self.frames.clear(); self.stack.clear();
self.frames.push(CallFrame { stack_base: 0, closure: Some(Rc::new(closure.clone())) }); self.frames.clear();
if let Some(count) = closure.positional_count && args.len() == count as usize { self.stack.extend(args); } self.frames.push(CallFrame {
else { self.unpack(&closure.parameter_node, &args, &mut 0)?; } stack_base: 0,
closure: Some(Rc::new(closure.clone())),
});
if let Some(count) = closure.positional_count
&& args.len() == count as usize
{
self.stack.extend(args);
} else {
self.unpack(&closure.parameter_node, &args, &mut 0)?;
}
self.eval(&closure.exec_node) self.eval(&closure.exec_node)
} }
pub fn run_with_args_observed<O: VMObserver>(&mut self, observer: &mut O, closure: &Closure, args: Vec<Value>) -> Result<Value, String> { pub fn run_with_args_observed<O: VMObserver>(
self.stack.clear(); self.frames.clear(); &mut self,
self.frames.push(CallFrame { stack_base: 0, closure: Some(Rc::new(closure.clone())) }); observer: &mut O,
if let Some(count) = closure.positional_count && args.len() == count as usize { self.stack.extend(args); } closure: &Closure,
else { self.unpack(&closure.parameter_node, &args, &mut 0)?; } args: Vec<Value>,
) -> Result<Value, String> {
self.stack.clear();
self.frames.clear();
self.frames.push(CallFrame {
stack_base: 0,
closure: Some(Rc::new(closure.clone())),
});
if let Some(count) = closure.positional_count
&& args.len() == count as usize
{
self.stack.extend(args);
} else {
self.unpack(&closure.parameter_node, &args, &mut 0)?;
}
self.eval_observed(observer, &closure.exec_node) self.eval_observed(observer, &closure.exec_node)
} }
pub fn run_with_observer<O: VMObserver>(&mut self, observer: &mut O, root: &ExecNode) -> Result<Value, String> { pub fn run_with_observer<O: VMObserver>(
self.stack.clear(); self.frames.clear(); &mut self,
self.frames.push(CallFrame { stack_base: 0, closure: None }); observer: &mut O,
root: &ExecNode,
) -> Result<Value, String> {
self.stack.clear();
self.frames.clear();
self.frames.push(CallFrame {
stack_base: 0,
closure: None,
});
let result = self.eval_observed(observer, root); let result = self.eval_observed(observer, root);
self.frames.pop(); self.frames.pop();
result result
} }
#[inline(always)] fn eval(&mut self, node: &ExecNode) -> Result<Value, String> { dispatch_eval!(self, node, eval) } #[inline(always)]
fn eval(&mut self, node: &ExecNode) -> Result<Value, String> {
dispatch_eval!(self, node, eval)
}
fn eval_observed<O: VMObserver>(&mut self, observer: &mut O, node: &ExecNode) -> Result<Value, String> { fn eval_observed<O: VMObserver>(
&mut self,
observer: &mut O,
node: &ExecNode,
) -> Result<Value, String> {
observer.before_eval(self, node); observer.before_eval(self, node);
let wrapper = |vm: &mut Self, obs: &mut O| -> Result<Value, String> { dispatch_eval!(vm, node, eval_observed, obs) }; let wrapper = |vm: &mut Self, obs: &mut O| -> Result<Value, String> {
dispatch_eval!(vm, node, eval_observed, obs)
};
let result = wrapper(self, observer); let result = wrapper(self, observer);
observer.after_eval(self, node, &result); observer.after_eval(self, node, &result);
result result
@@ -282,24 +381,32 @@ impl VM {
let frame = self.frames.last().ok_or("No call frame")?; let frame = self.frames.last().ok_or("No call frame")?;
let abs_index = frame.stack_base + (idx as usize); let abs_index = frame.stack_base + (idx as usize);
if abs_index < self.stack.len() { if abs_index < self.stack.len() {
if let Value::Cell(cell) = &self.stack[abs_index] { Ok(cell.clone()) } if let Value::Cell(cell) = &self.stack[abs_index] {
else { Ok(cell.clone())
} else {
let val = self.stack[abs_index].clone(); let val = self.stack[abs_index].clone();
let cell = Rc::new(RefCell::new(val)); let cell = Rc::new(RefCell::new(val));
self.stack[abs_index] = Value::Cell(cell.clone()); self.stack[abs_index] = Value::Cell(cell.clone());
Ok(cell) Ok(cell)
} }
} else { Err(format!("Stack underflow capture local {}", idx)) } } else {
}, Err(format!("Stack underflow capture local {}", idx))
}
}
Address::Upvalue(idx) => { Address::Upvalue(idx) => {
let frame = self.frames.last().ok_or("No call frame")?; let frame = self.frames.last().ok_or("No call frame")?;
if let Some(closure) = &frame.closure { if let Some(closure) = &frame.closure {
let idx = idx as usize; let idx = idx as usize;
if idx < closure.upvalues.len() { Ok(closure.upvalues[idx].clone()) } if idx < closure.upvalues.len() {
else { Err(format!("Upvalue access out of bounds capture {}", idx)) } Ok(closure.upvalues[idx].clone())
} else { Err("Current frame has no closure".to_string()) } } else {
}, Err(format!("Upvalue access out of bounds capture {}", idx))
Address::Global(_) => Err("Cannot capture global directly".to_string()), }
} else {
Err("Current frame has no closure".to_string())
}
}
Address::Global(_) => Err("Cannot capture global directly".to_string()),
} }
} }
@@ -309,22 +416,35 @@ impl VM {
let frame = self.frames.last().ok_or("No call frame")?; let frame = self.frames.last().ok_or("No call frame")?;
let abs_index = frame.stack_base + (idx as usize); let abs_index = frame.stack_base + (idx as usize);
if abs_index < self.stack.len() { if abs_index < self.stack.len() {
match &self.stack[abs_index] { Value::Cell(cell) => Ok(cell.borrow().clone()), val => Ok(val.clone()) } match &self.stack[abs_index] {
} else { Err(format!("Stack underflow access local {}", idx)) } Value::Cell(cell) => Ok(cell.borrow().clone()),
}, val => Ok(val.clone()),
}
} else {
Err(format!("Stack underflow access local {}", idx))
}
}
Address::Global(idx) => { Address::Global(idx) => {
let idx = idx as usize; let idx = idx as usize;
let globals = self.globals.borrow(); let globals = self.globals.borrow();
if idx < globals.len() { Ok(globals[idx].clone()) } if idx < globals.len() {
else { Err(format!("Global access out of bounds {}", idx)) } Ok(globals[idx].clone())
}, } else {
Err(format!("Global access out of bounds {}", idx))
}
}
Address::Upvalue(idx) => { Address::Upvalue(idx) => {
let frame = self.frames.last().ok_or("No call frame")?; let frame = self.frames.last().ok_or("No call frame")?;
if let Some(closure) = &frame.closure { if let Some(closure) = &frame.closure {
let idx = idx as usize; let idx = idx as usize;
if idx < closure.upvalues.len() { Ok(closure.upvalues[idx].borrow().clone()) } if idx < closure.upvalues.len() {
else { Err(format!("Upvalue access out of bounds {}", idx)) } Ok(closure.upvalues[idx].borrow().clone())
} else { Err("Current frame has no closure (cannot access upvalues)".to_string()) } } else {
Err(format!("Upvalue access out of bounds {}", idx))
}
} else {
Err("Current frame has no closure (cannot access upvalues)".to_string())
}
} }
} }
} }
@@ -335,54 +455,89 @@ impl VM {
let frame = self.frames.last().ok_or("No call frame")?; let frame = self.frames.last().ok_or("No call frame")?;
let abs_index = frame.stack_base + (idx as usize); let abs_index = frame.stack_base + (idx as usize);
if abs_index < self.stack.len() { if abs_index < self.stack.len() {
if let Value::Cell(cell) = &self.stack[abs_index] { *cell.borrow_mut() = value; } if let Value::Cell(cell) = &self.stack[abs_index] {
else { self.stack[abs_index] = value; } *cell.borrow_mut() = value;
} else if abs_index == self.stack.len() { self.stack.push(value); } } else {
else { return Err(format!("Stack gap write local {}", idx)); } self.stack[abs_index] = value;
}
} else if abs_index == self.stack.len() {
self.stack.push(value);
} else {
return Err(format!("Stack gap write local {}", idx));
}
Ok(()) Ok(())
}, }
Address::Global(idx) => { Address::Global(idx) => {
let idx = idx as usize; let idx = idx as usize;
let mut globals = self.globals.borrow_mut(); let mut globals = self.globals.borrow_mut();
if idx >= globals.len() { globals.resize(idx + 1, Value::Void); } if idx >= globals.len() {
globals.resize(idx + 1, Value::Void);
}
globals[idx] = value; globals[idx] = value;
Ok(()) Ok(())
}, }
Address::Upvalue(idx) => { Address::Upvalue(idx) => {
let frame = self.frames.last().ok_or("No call frame")?; let frame = self.frames.last().ok_or("No call frame")?;
if let Some(closure) = &frame.closure { if let Some(closure) = &frame.closure {
let idx = idx as usize; let idx = idx as usize;
if idx < closure.upvalues.len() { *closure.upvalues[idx].borrow_mut() = value; Ok(()) } if idx < closure.upvalues.len() {
else { Err(format!("Upvalue assignment out of bounds {}", idx)) } *closure.upvalues[idx].borrow_mut() = value;
} else { Err("Current frame has no closure".to_string()) } Ok(())
}, } else {
Err(format!("Upvalue assignment out of bounds {}", idx))
}
} else {
Err("Current frame has no closure".to_string())
}
}
} }
} }
fn flatten_value(val: Value, into: &mut Vec<Value>) { fn flatten_value(val: Value, into: &mut Vec<Value>) {
if let Some(values) = val.as_slice() { for item in values.iter() { Self::flatten_value(item.clone(), into); } } if let Some(values) = val.as_slice() {
else { into.push(val); } for item in values.iter() {
Self::flatten_value(item.clone(), into);
}
} else {
into.push(val);
}
} }
fn prepare_args(&mut self, args: &ExecNode) -> Result<Vec<Value>, String> { fn prepare_args(&mut self, args: &ExecNode) -> Result<Vec<Value>, String> {
let mut arg_vals = Vec::new(); let mut arg_vals = Vec::new();
match &args.kind { match &args.kind {
BoundKind::Tuple { elements } => { self.eval_and_flatten(elements, &mut arg_vals)?; } BoundKind::Tuple { elements } => {
_ => { VM::flatten_value(self.eval(args)?, &mut arg_vals); } self.eval_and_flatten(elements, &mut arg_vals)?;
}
_ => {
VM::flatten_value(self.eval(args)?, &mut arg_vals);
}
} }
Ok(arg_vals) Ok(arg_vals)
} }
fn prepare_args_observed<O: VMObserver>(&mut self, observer: &mut O, args: &ExecNode) -> Result<Vec<Value>, String> { fn prepare_args_observed<O: VMObserver>(
&mut self,
observer: &mut O,
args: &ExecNode,
) -> Result<Vec<Value>, String> {
let mut arg_vals = Vec::new(); let mut arg_vals = Vec::new();
match &args.kind { match &args.kind {
BoundKind::Tuple { elements } => { self.eval_observed_and_flatten(observer, elements, &mut arg_vals)?; } BoundKind::Tuple { elements } => {
_ => { VM::flatten_value(self.eval_observed(observer, args)?, &mut arg_vals); } self.eval_observed_and_flatten(observer, elements, &mut arg_vals)?;
}
_ => {
VM::flatten_value(self.eval_observed(observer, args)?, &mut arg_vals);
}
} }
Ok(arg_vals) Ok(arg_vals)
} }
fn eval_and_flatten(&mut self, elements: &[ExecNode], into: &mut Vec<Value>) -> Result<(), String> { fn eval_and_flatten(
&mut self,
elements: &[ExecNode],
into: &mut Vec<Value>,
) -> Result<(), String> {
for e in elements { for e in elements {
match &e.kind { match &e.kind {
BoundKind::Tuple { elements: sub } => self.eval_and_flatten(sub, into)?, BoundKind::Tuple { elements: sub } => self.eval_and_flatten(sub, into)?,
@@ -392,36 +547,56 @@ impl VM {
Ok(()) Ok(())
} }
fn eval_observed_and_flatten<O: VMObserver>(&mut self, observer: &mut O, elements: &[ExecNode], into: &mut Vec<Value>) -> Result<(), String> { fn eval_observed_and_flatten<O: VMObserver>(
&mut self,
observer: &mut O,
elements: &[ExecNode],
into: &mut Vec<Value>,
) -> Result<(), String> {
for e in elements { for e in elements {
match &e.kind { match &e.kind {
BoundKind::Tuple { elements: sub } => self.eval_observed_and_flatten(observer, sub, into)?, BoundKind::Tuple { elements: sub } => {
self.eval_observed_and_flatten(observer, sub, into)?
}
_ => into.push(self.eval_observed(observer, e)?), _ => into.push(self.eval_observed(observer, e)?),
} }
} }
Ok(()) Ok(())
} }
fn unpack<T>(&mut self, pattern: &Node<BoundKind<T>, T>, values: &[Value], offset: &mut usize) -> Result<(), String> { fn unpack<T>(
&mut self,
pattern: &Node<BoundKind<T>, T>,
values: &[Value],
offset: &mut usize,
) -> Result<(), String> {
match &pattern.kind { match &pattern.kind {
BoundKind::Parameter { slot, .. } => { BoundKind::Parameter { slot, .. } => {
let val = values.get(*offset).cloned().unwrap_or(Value::Void); let val = values.get(*offset).cloned().unwrap_or(Value::Void);
*offset += 1; *offset += 1;
let frame = self.frames.last().ok_or("No call frame")?; let frame = self.frames.last().ok_or("No call frame")?;
let abs_index = frame.stack_base + (*slot as usize); let abs_index = frame.stack_base + (*slot as usize);
if abs_index == self.stack.len() { self.stack.push(val); } if abs_index == self.stack.len() {
else if abs_index < self.stack.len() { self.stack[abs_index] = val; } self.stack.push(val);
else { return Err(format!("Stack gap during unpack at slot {}", slot)); } } else if abs_index < self.stack.len() {
self.stack[abs_index] = val;
} else {
return Err(format!("Stack gap during unpack at slot {}", slot));
}
Ok(()) Ok(())
} }
BoundKind::Tuple { elements } => { BoundKind::Tuple { elements } => {
if let Some(sub_values) = values.get(*offset).and_then(|v| v.as_slice()) { if let Some(sub_values) = values.get(*offset).and_then(|v| v.as_slice()) {
*offset += 1; *offset += 1;
let mut sub_offset = 0; let mut sub_offset = 0;
for el in elements { self.unpack(el, sub_values, &mut sub_offset)?; } for el in elements {
self.unpack(el, sub_values, &mut sub_offset)?;
}
return Ok(()); return Ok(());
} }
for el in elements { self.unpack(el, values, offset)?; } for el in elements {
self.unpack(el, values, offset)?;
}
Ok(()) Ok(())
} }
_ => Err("Invalid node in parameter pattern".to_string()), _ => Err("Invalid node in parameter pattern".to_string()),
@@ -432,27 +607,96 @@ impl VM {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::ast::nodes::{Node, Symbol};
use crate::ast::compiler::tco::TCO; use crate::ast::compiler::tco::TCO;
use crate::ast::types::{SourceLocation, NodeIdentity, StaticType}; use crate::ast::nodes::{Node, Symbol};
use crate::ast::types::{NodeIdentity, SourceLocation, StaticType};
fn make_dummy_identity() -> Rc<NodeIdentity> { Rc::new(NodeIdentity { location: SourceLocation { line: 0, col: 0 } }) } fn make_dummy_identity() -> Rc<NodeIdentity> {
Rc::new(NodeIdentity {
location: SourceLocation { line: 0, col: 0 },
})
}
#[test] #[test]
fn test_capture_boxing_modification() { fn test_capture_boxing_modification() {
let id = make_dummy_identity(); let id = make_dummy_identity();
let lambda_body = Node { let lambda_body = Node {
identity: id.clone(), ty: StaticType::Void, identity: id.clone(),
kind: BoundKind::Set { addr: Address::Upvalue(0), value: Box::new(Node { identity: id.clone(), ty: StaticType::Int, kind: BoundKind::Constant(Value::Int(20)) }) }, ty: StaticType::Void,
kind: BoundKind::Set {
addr: Address::Upvalue(0),
value: Box::new(Node {
identity: id.clone(),
ty: StaticType::Int,
kind: BoundKind::Constant(Value::Int(20)),
}),
},
}; };
let root = Node { let root = Node {
identity: id.clone(), ty: StaticType::Int, identity: id.clone(),
ty: StaticType::Int,
kind: BoundKind::Block { kind: BoundKind::Block {
exprs: vec![ exprs: vec![
Node { identity: id.clone(), ty: StaticType::Int, kind: BoundKind::Set { addr: Address::Local(0), value: Box::new(Node { identity: id.clone(), ty: StaticType::Int, kind: BoundKind::Constant(Value::Int(10)) }) } }, Node {
Node { identity: id.clone(), ty: StaticType::Any, kind: BoundKind::Set { addr: Address::Local(1), value: Box::new(Node { identity: id.clone(), ty: StaticType::Any, kind: BoundKind::Lambda { params: Rc::new(Node { identity: id.clone(), ty: StaticType::Tuple(vec![]), kind: BoundKind::Tuple { elements: vec![] } }), upvalues: vec![Address::Local(0)], body: Rc::new(lambda_body), positional_count: Some(0) } }) } }, identity: id.clone(),
Node { identity: id.clone(), ty: StaticType::Void, kind: BoundKind::Call { callee: Box::new(Node { identity: id.clone(), ty: StaticType::Any, kind: BoundKind::Get { addr: Address::Local(1), name: Symbol::from("f") } }), args: Box::new(Node { identity: id.clone(), ty: StaticType::Tuple(vec![]), kind: BoundKind::Tuple { elements: vec![] } }) } }, ty: StaticType::Int,
Node { identity: id.clone(), ty: StaticType::Int, kind: BoundKind::Get { addr: Address::Local(0), name: Symbol::from("x") } }, kind: BoundKind::Set {
addr: Address::Local(0),
value: Box::new(Node {
identity: id.clone(),
ty: StaticType::Int,
kind: BoundKind::Constant(Value::Int(10)),
}),
},
},
Node {
identity: id.clone(),
ty: StaticType::Any,
kind: BoundKind::Set {
addr: Address::Local(1),
value: Box::new(Node {
identity: id.clone(),
ty: StaticType::Any,
kind: BoundKind::Lambda {
params: Rc::new(Node {
identity: id.clone(),
ty: StaticType::Tuple(vec![]),
kind: BoundKind::Tuple { elements: vec![] },
}),
upvalues: vec![Address::Local(0)],
body: Rc::new(lambda_body),
positional_count: Some(0),
},
}),
},
},
Node {
identity: id.clone(),
ty: StaticType::Void,
kind: BoundKind::Call {
callee: Box::new(Node {
identity: id.clone(),
ty: StaticType::Any,
kind: BoundKind::Get {
addr: Address::Local(1),
name: Symbol::from("f"),
},
}),
args: Box::new(Node {
identity: id.clone(),
ty: StaticType::Tuple(vec![]),
kind: BoundKind::Tuple { elements: vec![] },
}),
},
},
Node {
identity: id.clone(),
ty: StaticType::Int,
kind: BoundKind::Get {
addr: Address::Local(0),
name: Symbol::from("x"),
},
},
], ],
}, },
}; };
@@ -460,6 +704,9 @@ mod tests {
let mut vm = VM::new(globals); let mut vm = VM::new(globals);
let exec_root = TCO::optimize(root); let exec_root = TCO::optimize(root);
let result = vm.run(&exec_root); let result = vm.run(&exec_root);
match result { Ok(Value::Int(val)) => assert_eq!(val, 20), _ => panic!("Expected Int(20)") } match result {
Ok(Value::Int(val)) => assert_eq!(val, 20),
_ => panic!("Expected Int(20)"),
}
} }
} }
+5 -3
View File
@@ -1,6 +1,6 @@
use clap::Parser;
use myc::ast::environment::Environment; use myc::ast::environment::Environment;
use myc::utils::tester; use myc::utils::tester;
use clap::Parser;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
@@ -51,7 +51,9 @@ fn main() {
println!("🚀 Running benchmarks in RELEASE mode...\n"); println!("🚀 Running benchmarks in RELEASE mode...\n");
let results = tester::run_benchmarks(cli.update_bench); let results = tester::run_benchmarks(cli.update_bench);
for res in results { for res in results {
let diff = res.diff_pct.map_or(String::new(), |d| format!(" ({:+.1}%)", d)); let diff = res
.diff_pct
.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;
@@ -81,7 +83,7 @@ fn main() {
} else { } else {
execute(&env, &content); execute(&env, &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);
+8 -2
View File
@@ -54,7 +54,9 @@ pub fn run_functional_tests_with_optimization(enabled: bool) -> Vec<TestResult>
success: false, success: false,
message: format!( message: format!(
"Opt {}: Expected {}, got {}", "Opt {}: Expected {}, got {}",
if enabled { "ON" } else { "OFF" }, expected, val_str if enabled { "ON" } else { "OFF" },
expected,
val_str
), ),
}); });
} }
@@ -62,7 +64,11 @@ pub fn run_functional_tests_with_optimization(enabled: bool) -> Vec<TestResult>
Err(e) => results.push(TestResult { Err(e) => results.push(TestResult {
name, name,
success: false, success: false,
message: format!("Opt {}: Error: {}", if enabled { "ON" } else { "OFF" }, e), message: format!(
"Opt {}: Error: {}",
if enabled { "ON" } else { "OFF" },
e
),
}), }),
} }
} }