Formatting
This commit is contained in:
+142
-53
@@ -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::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)]
|
||||
struct LocalInfo {
|
||||
@@ -30,10 +30,19 @@ impl CompilerScope {
|
||||
|
||||
fn define(&mut self, sym: &Symbol) -> Result<u32, String> {
|
||||
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;
|
||||
self.locals.insert(sym.clone(), LocalInfo { slot, _ty: StaticType::Any });
|
||||
self.locals.insert(
|
||||
sym.clone(),
|
||||
LocalInfo {
|
||||
slot,
|
||||
_ty: StaticType::Any,
|
||||
},
|
||||
);
|
||||
self.slot_count += 1;
|
||||
Ok(slot)
|
||||
}
|
||||
@@ -79,7 +88,10 @@ impl Binder {
|
||||
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 {
|
||||
functions: Vec::new(),
|
||||
globals,
|
||||
@@ -89,7 +101,10 @@ impl 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 mut binder = Self::with_boxed(globals, captures);
|
||||
binder.bind(node)
|
||||
@@ -100,30 +115,43 @@ impl Binder {
|
||||
UntypedKind::Nop => Ok(self.make_node(node.identity.clone(), BoundKind::Nop)),
|
||||
UntypedKind::Constant(v) => {
|
||||
Ok(self.make_node(node.identity.clone(), BoundKind::Constant(v.clone())))
|
||||
},
|
||||
}
|
||||
|
||||
UntypedKind::Identifier(sym) => {
|
||||
let addr = self.resolve_variable(sym)?;
|
||||
Ok(self.make_node(node.identity.clone(), BoundKind::Get {
|
||||
Ok(self.make_node(
|
||||
node.identity.clone(),
|
||||
BoundKind::Get {
|
||||
addr,
|
||||
name: sym.clone()
|
||||
}))
|
||||
name: sym.clone(),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
UntypedKind::Parameter(sym) => {
|
||||
// Parameters are ONLY allowed if we are inside a function (Binder stack > 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 slot = current_fn.scope.define(sym)?;
|
||||
Ok(self.make_node(node.identity.clone(), BoundKind::Parameter {
|
||||
Ok(self.make_node(
|
||||
node.identity.clone(),
|
||||
BoundKind::Parameter {
|
||||
name: sym.clone(),
|
||||
slot
|
||||
}))
|
||||
slot,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
UntypedKind::If { cond, then_br, else_br } => {
|
||||
UntypedKind::If {
|
||||
cond,
|
||||
then_br,
|
||||
else_br,
|
||||
} => {
|
||||
let cond = self.bind(cond)?;
|
||||
let then_br = self.bind(then_br)?;
|
||||
let mut else_br_bound = None;
|
||||
@@ -131,19 +159,25 @@ impl Binder {
|
||||
else_br_bound = Some(Box::new(self.bind(e)?));
|
||||
}
|
||||
|
||||
Ok(self.make_node(node.identity.clone(), BoundKind::If {
|
||||
Ok(self.make_node(
|
||||
node.identity.clone(),
|
||||
BoundKind::If {
|
||||
cond: Box::new(cond),
|
||||
then_br: Box::new(then_br),
|
||||
else_br: else_br_bound
|
||||
}))
|
||||
else_br: else_br_bound,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
UntypedKind::Def { name, value } => {
|
||||
// 1. Pre-declare name to support recursion
|
||||
let slot_or_idx = if self.functions.len() == 1 {
|
||||
let mut globals = self.globals.borrow_mut();
|
||||
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;
|
||||
globals.insert(name.clone(), idx);
|
||||
@@ -158,35 +192,48 @@ impl Binder {
|
||||
|
||||
// 3. Return Node
|
||||
if self.functions.len() == 1 {
|
||||
Ok(self.make_node(node.identity.clone(), BoundKind::DefGlobal {
|
||||
Ok(self.make_node(
|
||||
node.identity.clone(),
|
||||
BoundKind::DefGlobal {
|
||||
name: name.clone(),
|
||||
global_index: slot_or_idx,
|
||||
value: Box::new(val_node)
|
||||
}))
|
||||
value: Box::new(val_node),
|
||||
},
|
||||
))
|
||||
} else {
|
||||
let captured_by = self.capture_map.get(&node.identity).cloned().unwrap_or_default();
|
||||
Ok(self.make_node(node.identity.clone(), BoundKind::DefLocal {
|
||||
let captured_by = self
|
||||
.capture_map
|
||||
.get(&node.identity)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
Ok(self.make_node(
|
||||
node.identity.clone(),
|
||||
BoundKind::DefLocal {
|
||||
name: name.clone(),
|
||||
slot: slot_or_idx,
|
||||
value: Box::new(val_node) ,
|
||||
captured_by
|
||||
}))
|
||||
}
|
||||
value: Box::new(val_node),
|
||||
captured_by,
|
||||
},
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
UntypedKind::Assign { target, value } => {
|
||||
let val_node = self.bind(value)?;
|
||||
|
||||
if let UntypedKind::Identifier(sym) = &target.kind {
|
||||
let addr = self.resolve_variable(sym)?;
|
||||
Ok(self.make_node(node.identity.clone(), BoundKind::Set {
|
||||
Ok(self.make_node(
|
||||
node.identity.clone(),
|
||||
BoundKind::Set {
|
||||
addr,
|
||||
value: Box::new(val_node)
|
||||
}))
|
||||
value: Box::new(val_node),
|
||||
},
|
||||
))
|
||||
} else {
|
||||
Err("Assignment target must be an identifier".to_string())
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
UntypedKind::Lambda { params, body } => {
|
||||
let identity = node.identity.clone();
|
||||
@@ -219,60 +266,86 @@ impl Binder {
|
||||
_ => None,
|
||||
};
|
||||
|
||||
Ok(self.make_node(identity, BoundKind::Lambda {
|
||||
Ok(self.make_node(
|
||||
identity,
|
||||
BoundKind::Lambda {
|
||||
params: Rc::new(params_bound),
|
||||
upvalues: compiled_fn.upvalues,
|
||||
body: Rc::new(body_bound),
|
||||
positional_count,
|
||||
}))
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
UntypedKind::Call { callee, args } => {
|
||||
let callee = self.bind(callee)?;
|
||||
let args = self.bind(args)?;
|
||||
|
||||
Ok(self.make_node(node.identity.clone(), BoundKind::Call {
|
||||
Ok(self.make_node(
|
||||
node.identity.clone(),
|
||||
BoundKind::Call {
|
||||
callee: Box::new(callee),
|
||||
args: Box::new(args)
|
||||
}))
|
||||
args: Box::new(args),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
UntypedKind::Block { exprs } => {
|
||||
let mut bound_exprs = Vec::new();
|
||||
for expr in exprs {
|
||||
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 } => {
|
||||
let mut bound_elems = Vec::new();
|
||||
for e in elements {
|
||||
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 } => {
|
||||
let mut bound_fields = Vec::new();
|
||||
for (k, v) in fields {
|
||||
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 } => {
|
||||
let bound_expanded = self.bind(expanded)?;
|
||||
Ok(self.make_node(node.identity.clone(), BoundKind::Expansion {
|
||||
Ok(self.make_node(
|
||||
node.identity.clone(),
|
||||
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(_) => {
|
||||
// Future: Delegate to extension binder
|
||||
Err("Custom extensions not supported in Binder yet".to_string())
|
||||
@@ -308,7 +381,10 @@ impl Binder {
|
||||
|
||||
// 4. Global Fallback
|
||||
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) {
|
||||
return Ok(Address::Global(*idx));
|
||||
}
|
||||
@@ -318,7 +394,11 @@ impl Binder {
|
||||
}
|
||||
|
||||
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 {
|
||||
let x_decl = &exprs[0];
|
||||
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 {
|
||||
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 {
|
||||
panic!("Lambda body should be a Block, got {:?}", body.kind);
|
||||
@@ -367,7 +453,10 @@ mod tests {
|
||||
if let BoundKind::Block { exprs } = &body.kind {
|
||||
let x_decl = &exprs[0];
|
||||
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 {
|
||||
panic!("First expression should be DefLocal");
|
||||
}
|
||||
|
||||
+104
-45
@@ -1,6 +1,6 @@
|
||||
use std::rc::Rc;
|
||||
use crate::ast::types::{Value, StaticType, Identity};
|
||||
use crate::ast::nodes::{Node, Symbol};
|
||||
use crate::ast::types::{Identity, StaticType, Value};
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum Address {
|
||||
@@ -110,52 +110,105 @@ pub enum BoundKind<T = ()> {
|
||||
}
|
||||
|
||||
impl<T> PartialEq for BoundKind<T>
|
||||
where T: PartialEq {
|
||||
where
|
||||
T: PartialEq,
|
||||
{
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self, other) {
|
||||
(BoundKind::Nop, BoundKind::Nop) => true,
|
||||
(BoundKind::Constant(a), BoundKind::Constant(b)) => a == b,
|
||||
(BoundKind::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 }) => {
|
||||
aa == ab && na == nb
|
||||
}
|
||||
(BoundKind::Set { addr: aa, value: va }, BoundKind::Set { addr: ab, value: vb }) => {
|
||||
aa == ab && va == vb
|
||||
}
|
||||
(BoundKind::DefLocal { name: na, slot: sa, value: va, captured_by: ca },
|
||||
BoundKind::DefLocal { name: nb, slot: sb, value: vb, captured_by: cb }) => {
|
||||
na == nb && sa == sb && va == vb && ca == cb
|
||||
}
|
||||
(BoundKind::If { cond: ca, then_br: ta, else_br: ea },
|
||||
BoundKind::If { cond: cb, then_br: tb, 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::Set {
|
||||
addr: aa,
|
||||
value: va,
|
||||
},
|
||||
BoundKind::Set {
|
||||
addr: ab,
|
||||
value: vb,
|
||||
},
|
||||
) => aa == ab && va == vb,
|
||||
(
|
||||
BoundKind::DefLocal {
|
||||
name: na,
|
||||
slot: sa,
|
||||
value: va,
|
||||
captured_by: ca,
|
||||
},
|
||||
BoundKind::DefLocal {
|
||||
name: nb,
|
||||
slot: sb,
|
||||
value: vb,
|
||||
captured_by: cb,
|
||||
},
|
||||
) => na == nb && sa == sb && va == vb && ca == cb,
|
||||
(
|
||||
BoundKind::If {
|
||||
cond: ca,
|
||||
then_br: ta,
|
||||
else_br: ea,
|
||||
},
|
||||
BoundKind::If {
|
||||
cond: cb,
|
||||
then_br: tb,
|
||||
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
|
||||
_ => false,
|
||||
}
|
||||
@@ -173,16 +226,22 @@ impl<T> BoundKind<T> {
|
||||
BoundKind::Parameter { name, slot } => format!("PARAM({}, Slot:{})", name.name, slot),
|
||||
BoundKind::Get { addr, name } => format!("GET({}, {:?})", name.name, 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::DefGlobal { name, global_index, .. } => format!("DEF_GLOBAL({}, Idx:{})", name.name, global_index),
|
||||
BoundKind::Lambda { params, upvalues, .. } => {
|
||||
BoundKind::DefGlobal {
|
||||
name, global_index, ..
|
||||
} => format!("DEF_GLOBAL({}, Idx:{})", name.name, global_index),
|
||||
BoundKind::Lambda {
|
||||
params, upvalues, ..
|
||||
} => {
|
||||
let p_str = match ¶ms.kind {
|
||||
BoundKind::Tuple { elements } => format!("p:{}", elements.len()),
|
||||
_ => "p:1".to_string(),
|
||||
};
|
||||
format!("LAMBDA({}, Captures:{})", p_str, upvalues.len())
|
||||
},
|
||||
}
|
||||
BoundKind::Call { .. } => "CALL".to_string(),
|
||||
BoundKind::Block { .. } => "BLOCK".to_string(),
|
||||
BoundKind::Tuple { elements } => format!("TUPLE({})", elements.len()),
|
||||
|
||||
+56
-15
@@ -1,5 +1,5 @@
|
||||
use crate::ast::nodes::Node;
|
||||
use crate::ast::compiler::bound_nodes::BoundKind;
|
||||
use crate::ast::nodes::Node;
|
||||
use crate::ast::types::Value;
|
||||
use crate::ast::vm::Closure;
|
||||
use std::fmt::Debug;
|
||||
@@ -30,7 +30,8 @@ impl Dumper {
|
||||
fn log<T: Debug>(&mut self, label: &str, node: &Node<BoundKind<T>, T>) {
|
||||
self.write_indent();
|
||||
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>) {
|
||||
@@ -60,8 +61,10 @@ impl Dumper {
|
||||
}
|
||||
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 } => {
|
||||
self.log(&format!("Set: {:?}", addr), node);
|
||||
@@ -70,34 +73,58 @@ impl Dumper {
|
||||
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() {
|
||||
String::from("not captured")
|
||||
} else {
|
||||
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;
|
||||
if !captured_by.is_empty() {
|
||||
for capturer in captured_by {
|
||||
self.write_indent();
|
||||
self.output.push_str(&format!("- Capturer: Lambda at line {}, col {}\n",
|
||||
capturer.location.line, capturer.location.col));
|
||||
self.output.push_str(&format!(
|
||||
"- Capturer: Lambda at line {}, col {}\n",
|
||||
capturer.location.line, capturer.location.col
|
||||
));
|
||||
}
|
||||
}
|
||||
self.visit(value);
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::DefGlobal { name, global_index, value } => {
|
||||
self.log(&format!("DefGlobal (Name: '{}', Index: {})", name.name, global_index), node);
|
||||
BoundKind::DefGlobal {
|
||||
name,
|
||||
global_index,
|
||||
value,
|
||||
} => {
|
||||
self.log(
|
||||
&format!("DefGlobal (Name: '{}', Index: {})", name.name, global_index),
|
||||
node,
|
||||
);
|
||||
self.indent += 1;
|
||||
self.visit(value);
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::If { cond, then_br, else_br } => {
|
||||
BoundKind::If {
|
||||
cond,
|
||||
then_br,
|
||||
else_br,
|
||||
} => {
|
||||
self.log("If", node);
|
||||
self.indent += 1;
|
||||
|
||||
@@ -117,7 +144,12 @@ impl Dumper {
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Lambda { params, upvalues, body, .. } => {
|
||||
BoundKind::Lambda {
|
||||
params,
|
||||
upvalues,
|
||||
body,
|
||||
..
|
||||
} => {
|
||||
self.log(&format!("Lambda (Upvalues: {})", upvalues.len()), node);
|
||||
self.indent += 1;
|
||||
|
||||
@@ -134,7 +166,10 @@ impl Dumper {
|
||||
}
|
||||
|
||||
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 } => {
|
||||
@@ -180,8 +215,14 @@ impl Dumper {
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Expansion { original_call, bound_expanded } => {
|
||||
self.log(&format!("Expansion (Original: {:?})", original_call.kind), node);
|
||||
BoundKind::Expansion {
|
||||
original_call,
|
||||
bound_expanded,
|
||||
} => {
|
||||
self.log(
|
||||
&format!("Expansion (Original: {:?})", original_call.kind),
|
||||
node,
|
||||
);
|
||||
self.indent += 1;
|
||||
self.visit(bound_expanded);
|
||||
self.indent -= 1;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::ast::compiler::bound_nodes::{Address, BoundKind, BoundNode};
|
||||
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.
|
||||
/// 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.
|
||||
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);
|
||||
}
|
||||
@@ -35,12 +40,17 @@ impl<'a, T: Clone> LambdaCollector<'a, T> {
|
||||
if let Address::Global(global_index) = addr
|
||||
&& 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);
|
||||
}
|
||||
|
||||
BoundKind::If { cond, then_br, else_br } => {
|
||||
BoundKind::If {
|
||||
cond,
|
||||
then_br,
|
||||
else_br,
|
||||
} => {
|
||||
self.visit(cond);
|
||||
self.visit(then_br);
|
||||
if let Some(e) = else_br {
|
||||
|
||||
+213
-57
@@ -1,11 +1,15 @@
|
||||
use crate::ast::nodes::{Node, Symbol, UntypedKind};
|
||||
use crate::ast::types::{Identity, Value};
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use crate::ast::nodes::{Node, UntypedKind, Symbol};
|
||||
use crate::ast::types::{Value, Identity};
|
||||
|
||||
/// Trait for evaluating expressions during macro expansion.
|
||||
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)
|
||||
@@ -68,7 +72,10 @@ pub struct MacroExpander<E: MacroEvaluator> {
|
||||
|
||||
impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
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> {
|
||||
@@ -89,7 +96,8 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
|
||||
UntypedKind::Call { callee, args } => {
|
||||
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)?;
|
||||
|
||||
// Extract argument nodes from the expanded tuple
|
||||
@@ -99,7 +107,13 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
vec![expanded_args]
|
||||
};
|
||||
|
||||
let expanded = self.expand_call(node.identity.clone(), &sym.name, params, arg_elements, body)?;
|
||||
let expanded = self.expand_call(
|
||||
node.identity.clone(),
|
||||
&sym.name,
|
||||
params,
|
||||
arg_elements,
|
||||
body,
|
||||
)?;
|
||||
return self.expand_recursive(expanded);
|
||||
}
|
||||
|
||||
@@ -126,7 +140,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
|
||||
Ok(Node {
|
||||
identity: node.identity,
|
||||
kind: UntypedKind::Block { exprs: expanded_exprs },
|
||||
kind: UntypedKind::Block {
|
||||
exprs: expanded_exprs,
|
||||
},
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
@@ -141,13 +157,17 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
identity: node.identity,
|
||||
kind: UntypedKind::Lambda {
|
||||
params: Box::new(expanded_params),
|
||||
body: Rc::new(expanded_body)
|
||||
body: Rc::new(expanded_body),
|
||||
},
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
UntypedKind::If { cond, then_br, else_br } => {
|
||||
UntypedKind::If {
|
||||
cond,
|
||||
then_br,
|
||||
else_br,
|
||||
} => {
|
||||
let cond = self.expand_recursive(*cond)?;
|
||||
let then_br = self.expand_recursive(*then_br)?;
|
||||
let else_br = if let Some(e) = else_br {
|
||||
@@ -157,7 +177,11 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
};
|
||||
Ok(Node {
|
||||
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: (),
|
||||
})
|
||||
}
|
||||
@@ -166,7 +190,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
let value = self.expand_recursive(*value)?;
|
||||
Ok(Node {
|
||||
identity: node.identity,
|
||||
kind: UntypedKind::Def { name, value: Box::new(value) },
|
||||
kind: UntypedKind::Def {
|
||||
name,
|
||||
value: Box::new(value),
|
||||
},
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
@@ -176,7 +203,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
let value = self.expand_recursive(*value)?;
|
||||
Ok(Node {
|
||||
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: (),
|
||||
})
|
||||
}
|
||||
@@ -188,7 +218,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
}
|
||||
Ok(Node {
|
||||
identity: node.identity,
|
||||
kind: UntypedKind::Tuple { elements: expanded_elements },
|
||||
kind: UntypedKind::Tuple {
|
||||
elements: expanded_elements,
|
||||
},
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
@@ -200,7 +232,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
}
|
||||
Ok(Node {
|
||||
identity: node.identity,
|
||||
kind: UntypedKind::Record { fields: expanded_fields },
|
||||
kind: UntypedKind::Record {
|
||||
fields: expanded_fields,
|
||||
},
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
@@ -209,7 +243,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
let expanded = self.expand_recursive(*expanded)?;
|
||||
Ok(Node {
|
||||
identity: node.identity,
|
||||
kind: UntypedKind::Expansion { call, expanded: Box::new(expanded) },
|
||||
kind: UntypedKind::Expansion {
|
||||
call,
|
||||
expanded: Box::new(expanded),
|
||||
},
|
||||
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() {
|
||||
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();
|
||||
@@ -250,7 +299,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
}),
|
||||
args: Box::new(Node {
|
||||
identity: identity.clone(),
|
||||
kind: UntypedKind::Tuple { elements: bindings.into_values().collect() },
|
||||
kind: UntypedKind::Tuple {
|
||||
elements: bindings.into_values().collect(),
|
||||
},
|
||||
ty: (),
|
||||
}),
|
||||
},
|
||||
@@ -281,23 +332,36 @@ 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 {
|
||||
UntypedKind::Identifier(mut sym) => {
|
||||
// Inside a template, all internal identifiers are colored.
|
||||
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) => {
|
||||
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) => {
|
||||
// Break out of template for substitution/evaluation
|
||||
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());
|
||||
}
|
||||
let val = self.evaluator.evaluate(&inner, state.bindings)?;
|
||||
@@ -307,7 +371,8 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
UntypedKind::Splice(inner) => {
|
||||
// Splicing is also a form of substitution
|
||||
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());
|
||||
}
|
||||
let val = self.evaluator.evaluate(&inner, state.bindings)?;
|
||||
@@ -319,7 +384,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
let val = self.expand_template(*value, state)?;
|
||||
Ok(Node {
|
||||
identity: node.identity,
|
||||
kind: UntypedKind::Def { name, value: Box::new(val) },
|
||||
kind: UntypedKind::Def {
|
||||
name,
|
||||
value: Box::new(val),
|
||||
},
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
@@ -329,7 +397,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
let value = self.expand_template(*value, state)?;
|
||||
Ok(Node {
|
||||
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: (),
|
||||
})
|
||||
}
|
||||
@@ -346,7 +417,9 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
}
|
||||
Ok(Node {
|
||||
identity: node.identity,
|
||||
kind: UntypedKind::Tuple { elements: new_elements },
|
||||
kind: UntypedKind::Tuple {
|
||||
elements: new_elements,
|
||||
},
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
@@ -371,7 +444,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
UntypedKind::Record { fields } => {
|
||||
let mut new_fields = Vec::new();
|
||||
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 {
|
||||
identity: node.identity,
|
||||
@@ -385,12 +461,19 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
let expanded_args = self.expand_template(*args, state)?;
|
||||
Ok(Node {
|
||||
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: (),
|
||||
})
|
||||
}
|
||||
|
||||
UntypedKind::If { cond, then_br, else_br } => {
|
||||
UntypedKind::If {
|
||||
cond,
|
||||
then_br,
|
||||
else_br,
|
||||
} => {
|
||||
let cond = self.expand_template(*cond, state)?;
|
||||
let then_br = self.expand_template(*then_br, state)?;
|
||||
let else_br = if let Some(e) = else_br {
|
||||
@@ -400,7 +483,11 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
};
|
||||
Ok(Node {
|
||||
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: (),
|
||||
})
|
||||
}
|
||||
@@ -410,7 +497,10 @@ impl<E: MacroEvaluator> MacroExpander<E> {
|
||||
let body = self.expand_template(Node::clone(&body), state)?;
|
||||
Ok(Node {
|
||||
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: (),
|
||||
})
|
||||
}
|
||||
@@ -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 {
|
||||
Value::Object(obj) => {
|
||||
if let Some(node) = obj.as_any().downcast_ref::<Node<UntypedKind>>() {
|
||||
match &node.kind {
|
||||
UntypedKind::Tuple { elements } => {
|
||||
for e in elements { target.push(e.clone()); }
|
||||
for e in elements {
|
||||
target.push(e.clone());
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
UntypedKind::Block { exprs } => {
|
||||
for e in exprs { target.push(e.clone()); }
|
||||
for e in exprs {
|
||||
target.push(e.clone());
|
||||
}
|
||||
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>>() {
|
||||
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)]
|
||||
mod tests {
|
||||
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::parser::Parser;
|
||||
use crate::ast::types::{Object, Value};
|
||||
use std::cell::RefCell;
|
||||
|
||||
struct 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
|
||||
&& 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>));
|
||||
}
|
||||
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();
|
||||
|
||||
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 {
|
||||
assert_eq!(name.context, Some(call.identity.clone()));
|
||||
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();
|
||||
|
||||
if let UntypedKind::Block { exprs } = &expanded.kind
|
||||
&& let UntypedKind::Expansion { call: _, expanded: result } = &exprs[1].kind
|
||||
&& let UntypedKind::If { cond, then_br, else_br } = &result.kind {
|
||||
&& let UntypedKind::Expansion {
|
||||
call: _,
|
||||
expanded: result,
|
||||
} = &exprs[1].kind
|
||||
&& let UntypedKind::If {
|
||||
cond,
|
||||
then_br,
|
||||
else_br,
|
||||
} = &result.kind
|
||||
{
|
||||
if let UntypedKind::Identifier(sym) = &cond.kind {
|
||||
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));
|
||||
if let Some(eb) = else_br {
|
||||
if let UntypedKind::Constant(Value::Int(n)) = &eb.kind {
|
||||
assert_eq!(*n, 42);
|
||||
} else { panic!("Expected 42, got {:?}", eb.kind); }
|
||||
} else { panic!("Else branch missing"); }
|
||||
} else {
|
||||
panic!("Expected 42, got {:?}", eb.kind);
|
||||
}
|
||||
} else {
|
||||
panic!("Else branch missing");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -538,11 +679,18 @@ mod tests {
|
||||
|
||||
if let UntypedKind::Block { exprs } = &expanded.kind
|
||||
&& let UntypedKind::Def { value, .. } = &exprs[1].kind
|
||||
&& let UntypedKind::Expansion { expanded: result, .. } = &value.kind
|
||||
&& let UntypedKind::Tuple { elements } = &result.kind {
|
||||
&& let UntypedKind::Expansion {
|
||||
expanded: result, ..
|
||||
} = &value.kind
|
||||
&& let UntypedKind::Tuple { elements } = &result.kind
|
||||
{
|
||||
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[4].kind { assert_eq!(*n, 4); }
|
||||
if let UntypedKind::Constant(Value::Int(n)) = &elements[0].kind {
|
||||
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 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]
|
||||
@@ -604,6 +756,10 @@ mod tests {
|
||||
|
||||
let globals = Rc::new(RefCell::new(HashMap::new()));
|
||||
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
@@ -1,20 +1,20 @@
|
||||
pub mod binder;
|
||||
pub mod bound_nodes;
|
||||
pub mod tco;
|
||||
pub mod upvalues;
|
||||
pub mod dumper;
|
||||
pub mod macros;
|
||||
pub mod type_checker;
|
||||
pub mod specializer;
|
||||
pub mod optimizer;
|
||||
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 bound_nodes::*;
|
||||
pub use tco::*;
|
||||
pub use upvalues::*;
|
||||
pub use dumper::*;
|
||||
pub use macros::*;
|
||||
pub use type_checker::*;
|
||||
pub use specializer::*;
|
||||
pub use optimizer::*;
|
||||
pub use specializer::*;
|
||||
pub use tco::*;
|
||||
pub use type_checker::*;
|
||||
pub use upvalues::*;
|
||||
|
||||
+607
-140
File diff suppressed because it is too large
Load Diff
+108
-27
@@ -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::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)]
|
||||
pub struct MonoCacheKey {
|
||||
@@ -49,50 +49,117 @@ impl Specializer {
|
||||
fn visit_node(&self, node: TypedNode) -> TypedNode {
|
||||
let (new_kind, new_ty) = match node.kind {
|
||||
BoundKind::Call { callee, args } => {
|
||||
let (new_callee, 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)
|
||||
let (new_callee, 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
|
||||
BoundKind::If { cond, then_br, else_br } => {
|
||||
BoundKind::If {
|
||||
cond,
|
||||
then_br,
|
||||
else_br,
|
||||
} => {
|
||||
let cond = Box::new(self.visit_node(*cond));
|
||||
let then_br = Box::new(self.visit_node(*then_br));
|
||||
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 } => {
|
||||
let exprs = exprs.into_iter().map(|e| self.visit_node(e)).collect();
|
||||
(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 body = Rc::new(self.visit_node((*body).clone()));
|
||||
(BoundKind::Lambda { params, upvalues, body, positional_count }, node.ty)
|
||||
(
|
||||
BoundKind::Lambda {
|
||||
params,
|
||||
upvalues,
|
||||
body,
|
||||
positional_count,
|
||||
},
|
||||
BoundKind::DefLocal { name, slot, value, captured_by } => {
|
||||
node.ty,
|
||||
)
|
||||
}
|
||||
BoundKind::DefLocal {
|
||||
name,
|
||||
slot,
|
||||
value,
|
||||
captured_by,
|
||||
} => {
|
||||
let value = Box::new(self.visit_node(*value));
|
||||
(BoundKind::DefLocal { name, slot, value, captured_by }, node.ty)
|
||||
(
|
||||
BoundKind::DefLocal {
|
||||
name,
|
||||
slot,
|
||||
value,
|
||||
captured_by,
|
||||
},
|
||||
BoundKind::DefGlobal { name, global_index, value } => {
|
||||
node.ty,
|
||||
)
|
||||
}
|
||||
BoundKind::DefGlobal {
|
||||
name,
|
||||
global_index,
|
||||
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 } => {
|
||||
let value = Box::new(self.visit_node(*value));
|
||||
(BoundKind::Set { addr, value }, node.ty)
|
||||
},
|
||||
}
|
||||
BoundKind::Tuple { elements } => {
|
||||
let elements = elements.into_iter().map(|e| self.visit_node(e)).collect();
|
||||
(BoundKind::Tuple { elements }, node.ty)
|
||||
},
|
||||
}
|
||||
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::Expansion { original_call, bound_expanded } => {
|
||||
}
|
||||
BoundKind::Expansion {
|
||||
original_call,
|
||||
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
|
||||
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
|
||||
let new_callee = self.visit_node(callee);
|
||||
let new_args = self.visit_node(args);
|
||||
@@ -131,7 +203,10 @@ impl Specializer {
|
||||
}
|
||||
|
||||
// --- Optimization Candidate ---
|
||||
let key = MonoCacheKey { address, arg_types: arg_types.clone() };
|
||||
let key = MonoCacheKey {
|
||||
address,
|
||||
arg_types: arg_types.clone(),
|
||||
};
|
||||
|
||||
// 4. Check Cache
|
||||
if let Some((val, ret_ty)) = self.cache.borrow().get(&key) {
|
||||
@@ -153,7 +228,9 @@ impl Specializer {
|
||||
&& let Some((val, ret_ty)) = rtl_lookup(&name.name, &arg_types)
|
||||
{
|
||||
// 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 {
|
||||
identity: new_callee.identity.clone(),
|
||||
@@ -185,14 +262,18 @@ impl Specializer {
|
||||
let res_ty: StaticType = ret_ty;
|
||||
|
||||
// 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.
|
||||
let flat_elements = self.flatten_tuple(new_args.clone());
|
||||
let flat_types = flat_elements.iter().map(|e| e.ty.clone()).collect();
|
||||
let flattened_args = Node {
|
||||
identity: new_args.identity.clone(),
|
||||
kind: BoundKind::Tuple { elements: flat_elements },
|
||||
kind: BoundKind::Tuple {
|
||||
elements: flat_elements,
|
||||
},
|
||||
ty: StaticType::Tuple(flat_types),
|
||||
};
|
||||
|
||||
@@ -205,7 +286,7 @@ impl Specializer {
|
||||
})),
|
||||
};
|
||||
return (specialized_callee, flattened_args, res_ty);
|
||||
},
|
||||
}
|
||||
Err(_) => {
|
||||
// Fallback on error
|
||||
}
|
||||
|
||||
+72
-36
@@ -1,7 +1,7 @@
|
||||
use std::rc::Rc;
|
||||
use crate::ast::nodes::Node;
|
||||
use crate::ast::compiler::bound_nodes::{BoundKind, TypedNode};
|
||||
use crate::ast::nodes::Node;
|
||||
use crate::ast::types::StaticType;
|
||||
use std::rc::Rc;
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
@@ -36,19 +36,24 @@ impl TCO {
|
||||
fn transform(node_rc: Rc<TypedNode>, is_tail_position: bool) -> ExecNode {
|
||||
let node = &*node_rc;
|
||||
let new_kind = match &node.kind {
|
||||
BoundKind::Call { callee, args } => {
|
||||
BoundKind::Call {
|
||||
BoundKind::Call { callee, args } => BoundKind::Call {
|
||||
callee: Box::new(Self::transform(Rc::new((**callee).clone()), false)),
|
||||
args: Box::new(Self::transform(Rc::new((**args).clone()), false)),
|
||||
}
|
||||
},
|
||||
|
||||
BoundKind::If { cond, then_br, else_br } => {
|
||||
BoundKind::If {
|
||||
cond,
|
||||
then_br,
|
||||
else_br,
|
||||
} => 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))),
|
||||
}
|
||||
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 } => {
|
||||
@@ -60,62 +65,93 @@ impl TCO {
|
||||
|
||||
for (i, expr) in exprs.iter().enumerate() {
|
||||
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::Lambda { params, upvalues, body, positional_count } => {
|
||||
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::Set { addr, value } => {
|
||||
BoundKind::Set { addr: *addr, value: Box::new(Self::transform(Rc::new((**value).clone()), false)) }
|
||||
BoundKind::Set { addr, value } => BoundKind::Set {
|
||||
addr: *addr,
|
||||
value: Box::new(Self::transform(Rc::new((**value).clone()), false)),
|
||||
},
|
||||
BoundKind::DefLocal { name, slot, value, captured_by } => {
|
||||
BoundKind::DefLocal {
|
||||
name,
|
||||
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()
|
||||
}
|
||||
captured_by: captured_by.clone(),
|
||||
},
|
||||
BoundKind::DefGlobal { name, global_index, value } => {
|
||||
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))
|
||||
}
|
||||
value: Box::new(Self::transform(Rc::new((**value).clone()), false)),
|
||||
},
|
||||
BoundKind::Record { fields } => {
|
||||
let new_fields = fields.iter().map(|(k, v)| {
|
||||
(Self::transform(Rc::new(k.clone()), false), Self::transform(Rc::new(v.clone()), false))
|
||||
}).collect();
|
||||
let new_fields = fields
|
||||
.iter()
|
||||
.map(|(k, v)| {
|
||||
(
|
||||
Self::transform(Rc::new(k.clone()), false),
|
||||
Self::transform(Rc::new(v.clone()), false),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
BoundKind::Record { fields: new_fields }
|
||||
},
|
||||
}
|
||||
BoundKind::Tuple { elements } => {
|
||||
let new_elements = elements.iter().map(|e| Self::transform(Rc::new(e.clone()), false)).collect();
|
||||
BoundKind::Tuple { elements: new_elements }
|
||||
let new_elements = elements
|
||||
.iter()
|
||||
.map(|e| Self::transform(Rc::new(e.clone()), false))
|
||||
.collect();
|
||||
BoundKind::Tuple {
|
||||
elements: new_elements,
|
||||
}
|
||||
}
|
||||
BoundKind::Parameter { name, slot } => BoundKind::Parameter {
|
||||
name: name.clone(),
|
||||
slot: *slot,
|
||||
},
|
||||
BoundKind::Parameter { name, slot } => BoundKind::Parameter { name: name.clone(), slot: *slot },
|
||||
BoundKind::Constant(v) => BoundKind::Constant(v.clone()),
|
||||
BoundKind::Get { addr, name } => BoundKind::Get { addr: *addr, name: name.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,
|
||||
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
|
||||
}
|
||||
bound_expanded: Box::new(Self::transform(
|
||||
Rc::new((**bound_expanded).clone()),
|
||||
is_tail_position,
|
||||
)),
|
||||
},
|
||||
BoundKind::Extension(_) => BoundKind::Nop,
|
||||
};
|
||||
|
||||
Node {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use crate::ast::compiler::bound_nodes::{BoundKind, Address, BoundNode, TypedNode};
|
||||
use crate::ast::compiler::bound_nodes::{Address, BoundKind, BoundNode, TypedNode};
|
||||
use crate::ast::nodes::Node;
|
||||
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.
|
||||
struct TypeContext<'a> {
|
||||
@@ -14,7 +14,11 @@ struct 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 {
|
||||
_parent: parent,
|
||||
slots: vec![StaticType::Any; slot_count as usize],
|
||||
@@ -24,9 +28,17 @@ impl<'a> TypeContext<'a> {
|
||||
|
||||
fn get_type(&self, addr: Address) -> StaticType {
|
||||
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::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> {
|
||||
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)
|
||||
let mut upvalue_types = Vec::with_capacity(upvalues.len());
|
||||
for _ in &upvalues {
|
||||
@@ -66,7 +83,8 @@ impl TypeChecker {
|
||||
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
|
||||
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 {
|
||||
BoundKind::Parameter { name, slot } => {
|
||||
ctx.set_local_type(slot, specialized_ty.clone());
|
||||
@@ -132,7 +155,12 @@ impl TypeChecker {
|
||||
elem_types.push(t.ty.clone());
|
||||
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()),
|
||||
};
|
||||
@@ -151,20 +179,25 @@ impl TypeChecker {
|
||||
BoundKind::Constant(v) => {
|
||||
let ty = v.static_type();
|
||||
(BoundKind::Constant(v), ty)
|
||||
},
|
||||
}
|
||||
|
||||
BoundKind::Parameter { name, slot } => {
|
||||
(BoundKind::Parameter { name, slot }, ctx.get_type(Address::Local(slot)))
|
||||
},
|
||||
BoundKind::Parameter { name, slot } => (
|
||||
BoundKind::Parameter { name, slot },
|
||||
ctx.get_type(Address::Local(slot)),
|
||||
),
|
||||
|
||||
BoundKind::Get { addr, name } => {
|
||||
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 {
|
||||
ctx.get_type(addr)
|
||||
};
|
||||
(BoundKind::Get { addr, name }, ty)
|
||||
},
|
||||
}
|
||||
|
||||
BoundKind::Set { addr, value } => {
|
||||
let val_typed = self.check_node(*value, ctx)?;
|
||||
@@ -176,26 +209,62 @@ impl TypeChecker {
|
||||
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 ty = val_typed.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 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 then_typed = self.check_node(*then_br, ctx)?;
|
||||
|
||||
@@ -214,12 +283,15 @@ impl TypeChecker {
|
||||
final_ty = StaticType::Any; // Delphi: MakeOptional(Then)
|
||||
}
|
||||
|
||||
(BoundKind::If {
|
||||
(
|
||||
BoundKind::If {
|
||||
cond: Box::new(cond_typed),
|
||||
then_br: Box::new(then_typed),
|
||||
else_br: else_typed
|
||||
}, final_ty)
|
||||
else_br: else_typed,
|
||||
},
|
||||
final_ty,
|
||||
)
|
||||
}
|
||||
|
||||
BoundKind::Block { exprs } => {
|
||||
let mut typed_exprs = Vec::new();
|
||||
@@ -232,9 +304,14 @@ impl TypeChecker {
|
||||
}
|
||||
|
||||
(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
|
||||
let mut upvalue_types = Vec::with_capacity(upvalues.len());
|
||||
for &addr in &upvalues {
|
||||
@@ -245,7 +322,8 @@ impl TypeChecker {
|
||||
let mut lambda_ctx = TypeContext::new(64, upvalue_types, Some(ctx));
|
||||
|
||||
// 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 ret_ty = body_typed.ty.clone();
|
||||
|
||||
@@ -255,25 +333,34 @@ impl TypeChecker {
|
||||
ret: ret_ty,
|
||||
}));
|
||||
|
||||
(BoundKind::Lambda {
|
||||
(
|
||||
BoundKind::Lambda {
|
||||
params: Rc::new(params_typed),
|
||||
upvalues,
|
||||
body: Rc::new(body_typed),
|
||||
positional_count,
|
||||
}, fn_ty)
|
||||
},
|
||||
fn_ty,
|
||||
)
|
||||
}
|
||||
|
||||
BoundKind::Call { callee, args } => {
|
||||
let callee_typed = self.check_node(*callee, 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 {
|
||||
(
|
||||
BoundKind::Call {
|
||||
callee: Box::new(callee_typed),
|
||||
args: Box::new(args_typed)
|
||||
}, ret_ty)
|
||||
args: Box::new(args_typed),
|
||||
},
|
||||
ret_ty,
|
||||
)
|
||||
}
|
||||
|
||||
BoundKind::Tuple { elements } => {
|
||||
let mut typed_elements = Vec::new();
|
||||
@@ -291,21 +378,28 @@ impl TypeChecker {
|
||||
match first_ty {
|
||||
StaticType::Vector(inner, len) => {
|
||||
StaticType::Matrix(inner.clone(), vec![typed_elements.len(), *len])
|
||||
},
|
||||
}
|
||||
StaticType::Matrix(inner, shape) => {
|
||||
let mut new_shape = vec![typed_elements.len()];
|
||||
new_shape.extend(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 {
|
||||
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 } => {
|
||||
let mut typed_fields = Vec::with_capacity(fields.len());
|
||||
@@ -320,21 +414,35 @@ impl TypeChecker {
|
||||
|
||||
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 ty = expanded_typed.ty.clone();
|
||||
(BoundKind::Expansion {
|
||||
(
|
||||
BoundKind::Expansion {
|
||||
original_call,
|
||||
bound_expanded: Box::new(expanded_typed)
|
||||
}, ty)
|
||||
bound_expanded: Box::new(expanded_typed),
|
||||
},
|
||||
ty,
|
||||
)
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -379,16 +487,27 @@ mod tests {
|
||||
if let BoundKind::Lambda { body, .. } = &typed.kind {
|
||||
if let BoundKind::Block { exprs } = &body.kind {
|
||||
let last_expr = exprs.last().unwrap();
|
||||
assert_eq!(last_expr.ty, StaticType::Int, "Variable 'x' should be inferred as Int");
|
||||
} else { panic!("Expected block in lambda body"); }
|
||||
} else { panic!("Expected Lambda wrapper"); }
|
||||
assert_eq!(
|
||||
last_expr.ty,
|
||||
StaticType::Int,
|
||||
"Variable 'x' should be inferred as Int"
|
||||
);
|
||||
} else {
|
||||
panic!("Expected block in lambda body");
|
||||
}
|
||||
} else {
|
||||
panic!("Expected Lambda wrapper");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_inference_block_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.5 \"test\")")), StaticType::Text);
|
||||
assert_eq!(
|
||||
get_ret_type(&check_source("(do 1.5 \"test\")")),
|
||||
StaticType::Text
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -398,13 +517,17 @@ mod tests {
|
||||
let typed = check_source("(fn [a] 10)");
|
||||
if let StaticType::Function(sig) = &typed.ty {
|
||||
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
|
||||
let typed_nested = check_source("(fn [] (do 1 2.5))");
|
||||
if let StaticType::Function(sig) = &typed_nested.ty {
|
||||
assert_eq!(sig.ret, StaticType::Float);
|
||||
} else { panic!("Expected function type"); }
|
||||
} else {
|
||||
panic!("Expected function type");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -414,29 +537,59 @@ mod tests {
|
||||
if let BoundKind::Lambda { body, .. } = &typed.kind {
|
||||
if let BoundKind::Block { exprs } = &body.kind {
|
||||
let last_expr = exprs.last().unwrap();
|
||||
assert_eq!(last_expr.ty, StaticType::Float, "Variable 'x' should be specialized to Float after assignment");
|
||||
} else { panic!("Expected block"); }
|
||||
} else { panic!("Expected Lambda"); }
|
||||
assert_eq!(
|
||||
last_expr.ty,
|
||||
StaticType::Float,
|
||||
"Variable 'x' should be specialized to Float after assignment"
|
||||
);
|
||||
} else {
|
||||
panic!("Expected block");
|
||||
}
|
||||
} else {
|
||||
panic!("Expected Lambda");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_operator_overloading_inference() {
|
||||
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!(get_ret_type(&check_source("(+ \"a\" \"b\")")), StaticType::Text);
|
||||
assert_eq!(
|
||||
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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_datetime_inference() {
|
||||
// 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
|
||||
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)
|
||||
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
|
||||
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]
|
||||
@@ -468,8 +621,14 @@ mod tests {
|
||||
// Shape mismatch -> Tuple of Vectors
|
||||
let mixed = get_ret_type(&check_source("[[1 2] [3 4 5]]"));
|
||||
if let StaticType::Tuple(elements) = mixed {
|
||||
assert_eq!(elements[0], StaticType::Vector(Box::new(StaticType::Int), 2));
|
||||
assert_eq!(elements[1], StaticType::Vector(Box::new(StaticType::Int), 3));
|
||||
assert_eq!(
|
||||
elements[0],
|
||||
StaticType::Vector(Box::new(StaticType::Int), 2)
|
||||
);
|
||||
assert_eq!(
|
||||
elements[1],
|
||||
StaticType::Vector(Box::new(StaticType::Int), 3)
|
||||
);
|
||||
} else {
|
||||
panic!("Expected Tuple for shape mismatch, got {:?}", mixed);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use crate::ast::nodes::{Node, UntypedKind, Symbol};
|
||||
use crate::ast::nodes::{Node, Symbol, UntypedKind};
|
||||
use crate::ast::types::Identity;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
/// Analyzes the AST to find which lambdas capture which variable declarations.
|
||||
/// 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);
|
||||
|
||||
// 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()))
|
||||
.collect()
|
||||
}
|
||||
@@ -37,7 +38,7 @@ impl UpvalueAnalyzer {
|
||||
node: &Node<UntypedKind>,
|
||||
scopes: &mut Vec<HashMap<Symbol, Identity>>,
|
||||
capture_map: &mut HashMap<Identity, HashSet<Identity>>,
|
||||
current_lambda: Option<Identity>
|
||||
current_lambda: Option<Identity>,
|
||||
) {
|
||||
match &node.kind {
|
||||
UntypedKind::Identifier(sym) => {
|
||||
@@ -47,7 +48,8 @@ impl UpvalueAnalyzer {
|
||||
if depth > 0 {
|
||||
// Captured from an outer scope!
|
||||
if let Some(lambda_id) = ¤t_lambda {
|
||||
capture_map.entry(decl_id.clone())
|
||||
capture_map
|
||||
.entry(decl_id.clone())
|
||||
.or_default()
|
||||
.insert(lambda_id.clone());
|
||||
}
|
||||
@@ -78,7 +80,11 @@ impl UpvalueAnalyzer {
|
||||
Self::visit(body, scopes, capture_map, current_lambda);
|
||||
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(then_br, scopes, capture_map, current_lambda.clone());
|
||||
if let Some(e) = else_br {
|
||||
@@ -112,10 +118,15 @@ impl UpvalueAnalyzer {
|
||||
UntypedKind::Expansion { call: _, expanded } => {
|
||||
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);
|
||||
}
|
||||
UntypedKind::Nop | UntypedKind::Constant(_) | UntypedKind::Extension(_) | UntypedKind::Parameter(_) => {}
|
||||
UntypedKind::Nop
|
||||
| UntypedKind::Constant(_)
|
||||
| UntypedKind::Extension(_)
|
||||
| UntypedKind::Parameter(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ use crate::ast::compiler::lambda_collector::LambdaCollector;
|
||||
use crate::ast::compiler::macros::{MacroEvaluator, MacroExpander, MacroRegistry};
|
||||
use crate::ast::compiler::optimizer::Optimizer;
|
||||
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::intrinsics;
|
||||
|
||||
|
||||
+42
-14
@@ -1,14 +1,20 @@
|
||||
use std::iter::Peekable;
|
||||
use std::str::Chars;
|
||||
use std::rc::Rc;
|
||||
use crate::ast::types::SourceLocation;
|
||||
use std::iter::Peekable;
|
||||
use std::rc::Rc;
|
||||
use std::str::Chars;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum TokenKind {
|
||||
LeftParen, RightParen,
|
||||
LeftBracket, RightBracket,
|
||||
LeftBrace, RightBrace,
|
||||
Quote, Backtick, Tilde, At,
|
||||
LeftParen,
|
||||
RightParen,
|
||||
LeftBracket,
|
||||
RightBracket,
|
||||
LeftBrace,
|
||||
RightBrace,
|
||||
Quote,
|
||||
Backtick,
|
||||
Tilde,
|
||||
At,
|
||||
Identifier(Rc<str>),
|
||||
Keyword(Rc<str>),
|
||||
Integer(i64),
|
||||
@@ -41,15 +47,23 @@ impl<'a> Lexer<'a> {
|
||||
pub fn next_token(&mut self) -> Result<Token, String> {
|
||||
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() {
|
||||
Some(c) => {
|
||||
let current_char = c;
|
||||
self.col += 1;
|
||||
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 {
|
||||
@@ -68,7 +82,9 @@ impl<'a> Lexer<'a> {
|
||||
TokenKind::Keyword(Rc::from(id))
|
||||
}
|
||||
'"' => TokenKind::String(Rc::from(self.read_string()?)),
|
||||
c if c.is_ascii_digit() || (c == '-' && self.input.peek().is_some_and(|p| p.is_ascii_digit())) => {
|
||||
c if c.is_ascii_digit()
|
||||
|| (c == '-' && self.input.peek().is_some_and(|p| p.is_ascii_digit())) =>
|
||||
{
|
||||
let mut num_str = String::from(c);
|
||||
while let Some(&next) = self.peek() {
|
||||
if next.is_ascii_digit() || next == '.' {
|
||||
@@ -90,10 +106,20 @@ impl<'a> Lexer<'a> {
|
||||
id.push_str(&self.read_while(is_ident_char));
|
||||
TokenKind::Identifier(Rc::from(id))
|
||||
}
|
||||
_ => return Err(format!("Unexpected character: {} at {}:{}", char, self.line, self.col - 1)),
|
||||
_ => {
|
||||
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> {
|
||||
@@ -125,7 +151,9 @@ impl<'a> Lexer<'a> {
|
||||
}
|
||||
|
||||
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();
|
||||
while let Some(&c) = self.peek() {
|
||||
if predicate(c) {
|
||||
|
||||
+4
-4
@@ -1,8 +1,8 @@
|
||||
pub mod types;
|
||||
pub mod compiler;
|
||||
pub mod environment;
|
||||
pub mod lexer;
|
||||
pub mod nodes;
|
||||
pub mod parser;
|
||||
pub mod compiler;
|
||||
pub mod environment;
|
||||
pub mod vm;
|
||||
pub mod rtl;
|
||||
pub mod types;
|
||||
pub mod vm;
|
||||
|
||||
+11
-5
@@ -1,7 +1,7 @@
|
||||
use std::rc::Rc;
|
||||
use std::fmt::Debug;
|
||||
use crate::ast::types::{Identity, Object, Value};
|
||||
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.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
@@ -14,13 +14,19 @@ pub struct Symbol {
|
||||
|
||||
impl From<Rc<str>> for Symbol {
|
||||
fn from(name: Rc<str>) -> Self {
|
||||
Self { name, context: None }
|
||||
Self {
|
||||
name,
|
||||
context: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Symbol {
|
||||
fn from(name: &str) -> Self {
|
||||
Self { name: Rc::from(name), context: None }
|
||||
Self {
|
||||
name: Rc::from(name),
|
||||
context: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+83
-28
@@ -1,7 +1,7 @@
|
||||
use std::rc::Rc;
|
||||
use crate::ast::lexer::{Lexer, Token, TokenKind};
|
||||
use crate::ast::types::{Identity, NodeIdentity, Value, Keyword};
|
||||
use crate::ast::nodes::{Node, UntypedKind, Symbol};
|
||||
use crate::ast::nodes::{Node, Symbol, UntypedKind};
|
||||
use crate::ast::types::{Identity, Keyword, NodeIdentity, Value};
|
||||
use std::rc::Rc;
|
||||
|
||||
pub struct Parser<'a> {
|
||||
lexer: Lexer<'a>,
|
||||
@@ -12,7 +12,10 @@ impl<'a> Parser<'a> {
|
||||
pub fn new(input: &'a str) -> Result<Self, String> {
|
||||
let mut lexer = Lexer::new(input);
|
||||
let current_token = lexer.next_token()?;
|
||||
Ok(Self { lexer, current_token })
|
||||
Ok(Self {
|
||||
lexer,
|
||||
current_token,
|
||||
})
|
||||
}
|
||||
|
||||
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> {
|
||||
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() {
|
||||
TokenKind::LeftParen => self.parse_list(),
|
||||
@@ -41,7 +46,9 @@ impl<'a> Parser<'a> {
|
||||
callee: Box::new(self.make_id_node("quote", identity.clone())),
|
||||
args: Box::new(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Tuple { elements: vec![expr] },
|
||||
kind: UntypedKind::Tuple {
|
||||
elements: vec![expr],
|
||||
},
|
||||
ty: (),
|
||||
}),
|
||||
},
|
||||
@@ -86,31 +93,45 @@ impl<'a> Parser<'a> {
|
||||
|
||||
fn parse_atom(&mut self) -> Result<Node<UntypedKind>, String> {
|
||||
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 {
|
||||
TokenKind::Integer(n) => UntypedKind::Constant(Value::Int(n)),
|
||||
TokenKind::Float(n) => UntypedKind::Constant(Value::Float(n)),
|
||||
TokenKind::String(s) => UntypedKind::Constant(Value::Text(s)),
|
||||
TokenKind::Keyword(k) => UntypedKind::Constant(Value::Keyword(Keyword::intern(&k))),
|
||||
TokenKind::Identifier(id) => {
|
||||
match id.as_ref() {
|
||||
TokenKind::Identifier(id) => match id.as_ref() {
|
||||
"..." => UntypedKind::Nop,
|
||||
_ => 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> {
|
||||
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 {
|
||||
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()?;
|
||||
@@ -144,7 +165,11 @@ impl<'a> Parser<'a> {
|
||||
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::If { cond, then_br, else_br },
|
||||
kind: UntypedKind::If {
|
||||
cond,
|
||||
then_br,
|
||||
else_br,
|
||||
},
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
@@ -213,17 +238,26 @@ impl<'a> Parser<'a> {
|
||||
let body = self.parse_expression()?;
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::MacroDecl { name, params, body: Box::new(body) },
|
||||
kind: UntypedKind::MacroDecl {
|
||||
name,
|
||||
params,
|
||||
body: Box::new(body),
|
||||
},
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_param_vector(&mut self) -> Result<Node<UntypedKind>, String> {
|
||||
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 identity = Rc::new(NodeIdentity { location: token.location });
|
||||
let identity = Rc::new(NodeIdentity {
|
||||
location: token.location,
|
||||
});
|
||||
|
||||
let mut elements = Vec::new();
|
||||
while *self.peek() != TokenKind::RightBracket {
|
||||
@@ -232,17 +266,24 @@ impl<'a> Parser<'a> {
|
||||
TokenKind::Identifier(name) => {
|
||||
let name = name.clone();
|
||||
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 {
|
||||
identity: p_identity,
|
||||
kind: UntypedKind::Parameter(name.into()),
|
||||
ty: (),
|
||||
});
|
||||
},
|
||||
}
|
||||
TokenKind::LeftBracket => {
|
||||
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)?;
|
||||
@@ -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();
|
||||
|
||||
while *self.peek() != TokenKind::RightParen && *self.peek() != TokenKind::EOF {
|
||||
@@ -269,7 +314,10 @@ impl<'a> Parser<'a> {
|
||||
|
||||
Ok(Node {
|
||||
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: (),
|
||||
})
|
||||
}
|
||||
@@ -286,7 +334,9 @@ impl<'a> Parser<'a> {
|
||||
self.expect(TokenKind::RightBracket)?;
|
||||
|
||||
Ok(Node {
|
||||
identity: Rc::new(NodeIdentity { location: token.location }),
|
||||
identity: Rc::new(NodeIdentity {
|
||||
location: token.location,
|
||||
}),
|
||||
kind: UntypedKind::Tuple { elements },
|
||||
ty: (),
|
||||
})
|
||||
@@ -306,7 +356,7 @@ impl<'a> Parser<'a> {
|
||||
// strictly we could allow any expression and check at runtime.
|
||||
// Delphi enforces keywords. We can do minimal check here.
|
||||
match &key_node.kind {
|
||||
UntypedKind::Constant(Value::Keyword(_)) => {},
|
||||
UntypedKind::Constant(Value::Keyword(_)) => {}
|
||||
_ => return Err("Record keys must be keywords (syntactically)".to_string()),
|
||||
}
|
||||
|
||||
@@ -320,7 +370,9 @@ impl<'a> Parser<'a> {
|
||||
self.expect(TokenKind::RightBrace)?;
|
||||
|
||||
Ok(Node {
|
||||
identity: Rc::new(NodeIdentity { location: token.location }),
|
||||
identity: Rc::new(NodeIdentity {
|
||||
location: token.location,
|
||||
}),
|
||||
kind: UntypedKind::Record { fields },
|
||||
ty: (),
|
||||
})
|
||||
@@ -331,7 +383,10 @@ impl<'a> Parser<'a> {
|
||||
if token.kind == kind {
|
||||
Ok(())
|
||||
} 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
@@ -1,7 +1,6 @@
|
||||
use std::rc::Rc;
|
||||
use crate::ast::types::{Value, StaticType, Signature};
|
||||
use crate::ast::environment::Environment;
|
||||
|
||||
use crate::ast::types::{Signature, StaticType, Value};
|
||||
use std::rc::Rc;
|
||||
|
||||
pub fn register(env: &Environment) {
|
||||
register_constants(env);
|
||||
@@ -23,10 +22,22 @@ fn register_constants(env: &Environment) {
|
||||
fn register_arithmetic(env: &Environment) {
|
||||
// --- Add (+) ---
|
||||
let add_ty = StaticType::FunctionOverloads(vec![
|
||||
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Int },
|
||||
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 },
|
||||
Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
|
||||
ret: StaticType::Int,
|
||||
},
|
||||
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| {
|
||||
if args.len() == 2 {
|
||||
@@ -39,7 +50,7 @@ fn register_arithmetic(env: &Environment) {
|
||||
let mut res = a.to_string();
|
||||
res.push_str(b);
|
||||
Value::Text(Rc::from(res))
|
||||
},
|
||||
}
|
||||
(Value::DateTime(ts), Value::Int(ms)) => Value::DateTime(ts + ms),
|
||||
_ => Value::Void,
|
||||
}
|
||||
@@ -47,24 +58,51 @@ fn register_arithmetic(env: &Environment) {
|
||||
// Variadic sum
|
||||
let mut acc = 0.0;
|
||||
for arg in args {
|
||||
if let Value::Int(i) = arg { acc += i as f64; }
|
||||
else if let Value::Float(f) = arg { acc += f; }
|
||||
if let Value::Int(i) = arg {
|
||||
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 (-) ---
|
||||
let sub_ty = StaticType::FunctionOverloads(vec![
|
||||
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Int },
|
||||
Signature { 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 },
|
||||
Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
|
||||
ret: StaticType::Int,
|
||||
},
|
||||
Signature {
|
||||
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| {
|
||||
if args.is_empty() { return Value::Void; }
|
||||
if args.is_empty() {
|
||||
return Value::Void;
|
||||
}
|
||||
if args.len() == 1 {
|
||||
return match args[0] {
|
||||
Value::Int(i) => Value::Int(-i),
|
||||
@@ -90,16 +128,29 @@ fn register_arithmetic(env: &Environment) {
|
||||
_ => return Value::Void,
|
||||
};
|
||||
for arg in &args[1..] {
|
||||
if let Value::Int(i) = arg { acc -= *i as f64; }
|
||||
else if let Value::Float(f) = arg { acc -= f; }
|
||||
if let Value::Int(i) = arg {
|
||||
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 (*) ---
|
||||
let mul_ty = StaticType::FunctionOverloads(vec![
|
||||
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Int },
|
||||
Signature { params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]), ret: StaticType::Float },
|
||||
Signature {
|
||||
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| {
|
||||
if args.len() == 2 {
|
||||
@@ -113,35 +164,69 @@ fn register_arithmetic(env: &Environment) {
|
||||
} else {
|
||||
let mut acc = 1.0;
|
||||
for arg in args {
|
||||
if let Value::Int(i) = arg { acc *= i as f64; }
|
||||
else if let Value::Float(f) = arg { acc *= f; }
|
||||
if let Value::Int(i) = arg {
|
||||
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 (/) ---
|
||||
let div_ty = StaticType::FunctionOverloads(vec![
|
||||
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Float },
|
||||
Signature { params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]), ret: StaticType::Float },
|
||||
Signature {
|
||||
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| {
|
||||
if args.len() != 2 { return Value::Void; }
|
||||
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) }
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
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 (//) ---
|
||||
let int_div_ty = StaticType::Function(Box::new(
|
||||
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Int }
|
||||
));
|
||||
let int_div_ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
|
||||
ret: StaticType::Int,
|
||||
}));
|
||||
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]) {
|
||||
(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.
|
||||
// Usually div is for integers. Let's stick to Int.
|
||||
_ => Value::Void,
|
||||
@@ -149,15 +234,22 @@ fn register_arithmetic(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Modulus (%) ---
|
||||
let mod_ty = StaticType::Function(Box::new(
|
||||
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Int }
|
||||
));
|
||||
let mod_ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
|
||||
ret: StaticType::Int,
|
||||
}));
|
||||
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]) {
|
||||
(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,
|
||||
}
|
||||
});
|
||||
@@ -165,14 +257,25 @@ fn register_arithmetic(env: &Environment) {
|
||||
|
||||
fn register_comparison(env: &Environment) {
|
||||
let cmp_ty = StaticType::FunctionOverloads(vec![
|
||||
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), 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 },
|
||||
Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
|
||||
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 (>) ---
|
||||
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]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Bool(a > b),
|
||||
(Value::Int(a), Value::Float(b)) => Value::Bool((*a as f64) > *b),
|
||||
@@ -185,7 +288,9 @@ fn register_comparison(env: &Environment) {
|
||||
|
||||
// --- Less Than (<) ---
|
||||
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]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Bool(a < 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 (>=) ---
|
||||
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]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Bool(a >= 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 (<=) ---
|
||||
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]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Bool(a <= b),
|
||||
(Value::Int(a), Value::Float(b)) => Value::Bool((*a as f64) <= *b),
|
||||
@@ -223,11 +332,14 @@ fn register_comparison(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Equal (=) ---
|
||||
let eq_ty = StaticType::Function(Box::new(
|
||||
Signature { params: StaticType::Tuple(vec![StaticType::Any, StaticType::Any]), ret: StaticType::Bool }
|
||||
));
|
||||
let eq_ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Any, StaticType::Any]),
|
||||
ret: StaticType::Bool,
|
||||
}));
|
||||
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.
|
||||
// Note: Floating point equality is tricky, but we follow standard behavior for now.
|
||||
match (&args[0], &args[1]) {
|
||||
@@ -246,7 +358,9 @@ fn register_comparison(env: &Environment) {
|
||||
|
||||
// --- Not Equal (<>) ---
|
||||
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]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Bool(a != b),
|
||||
(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) {
|
||||
// --- Not (not) ---
|
||||
let not_ty = StaticType::FunctionOverloads(vec![
|
||||
Signature { params: StaticType::Tuple(vec![StaticType::Bool]), ret: StaticType::Bool },
|
||||
Signature { params: StaticType::Tuple(vec![StaticType::Int]), ret: StaticType::Int },
|
||||
Signature {
|
||||
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| {
|
||||
if args.len() != 1 { return Value::Void; }
|
||||
if args.len() != 1 {
|
||||
return Value::Void;
|
||||
}
|
||||
match &args[0] {
|
||||
Value::Bool(b) => Value::Bool(!b),
|
||||
Value::Int(i) => Value::Int(!i), // Bitwise NOT
|
||||
@@ -279,11 +401,19 @@ fn register_logic(env: &Environment) {
|
||||
|
||||
// --- And (and) ---
|
||||
let logic_op_ty = StaticType::FunctionOverloads(vec![
|
||||
Signature { params: StaticType::Tuple(vec![StaticType::Bool, StaticType::Bool]), ret: StaticType::Bool },
|
||||
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Int },
|
||||
Signature {
|
||||
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| {
|
||||
if args.len() != 2 { return Value::Void; }
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Bool(a), Value::Bool(b)) => Value::Bool(*a && *b),
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a & b),
|
||||
@@ -293,7 +423,9 @@ fn register_logic(env: &Environment) {
|
||||
|
||||
// --- Or (or) ---
|
||||
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]) {
|
||||
(Value::Bool(a), Value::Bool(b)) => Value::Bool(*a || *b),
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a | b),
|
||||
@@ -303,7 +435,9 @@ fn register_logic(env: &Environment) {
|
||||
|
||||
// --- Xor (xor) ---
|
||||
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]) {
|
||||
(Value::Bool(a), Value::Bool(b)) => Value::Bool(*a ^ *b),
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a ^ b),
|
||||
@@ -312,11 +446,14 @@ fn register_logic(env: &Environment) {
|
||||
});
|
||||
|
||||
// --- Shift Left (<<) ---
|
||||
let shift_ty = StaticType::Function(Box::new(
|
||||
Signature { params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]), ret: StaticType::Int }
|
||||
));
|
||||
let shift_ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Int, StaticType::Int]),
|
||||
ret: StaticType::Int,
|
||||
}));
|
||||
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]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a << b),
|
||||
_ => Value::Void,
|
||||
@@ -325,7 +462,9 @@ fn register_logic(env: &Environment) {
|
||||
|
||||
// --- Shift Right (>>) ---
|
||||
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]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a >> b),
|
||||
_ => Value::Void,
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
use crate::ast::types::{Value, StaticType, Signature};
|
||||
use crate::ast::environment::Environment;
|
||||
use crate::ast::types::{Signature, StaticType, Value};
|
||||
use chrono::{NaiveDate, NaiveDateTime};
|
||||
|
||||
pub fn register(env: &Environment) {
|
||||
let date_ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Text]),
|
||||
ret: StaticType::DateTime
|
||||
ret: StaticType::DateTime,
|
||||
}));
|
||||
|
||||
env.register_native("date", date_ty, true, |args| {
|
||||
if let Value::Text(s) = &args[0] {
|
||||
// Try parse YYYY-MM-DD
|
||||
if let Ok(dt) = NaiveDate::parse_from_str(s, "%Y-%m-%d") {
|
||||
let ts = dt.and_hms_opt(0, 0, 0).unwrap().and_utc().timestamp_millis();
|
||||
let ts = dt
|
||||
.and_hms_opt(0, 0, 0)
|
||||
.unwrap()
|
||||
.and_utc()
|
||||
.timestamp_millis();
|
||||
return Value::DateTime(ts);
|
||||
}
|
||||
// Try parse YYYY-MM-DD HH:MM:SS
|
||||
|
||||
+23
-15
@@ -1,5 +1,5 @@
|
||||
use crate::ast::types::{StaticType, Value};
|
||||
use std::rc::Rc;
|
||||
use crate::ast::types::{Value, StaticType};
|
||||
|
||||
/// Looks up a specialized intrinsic function for the given operator and argument types.
|
||||
/// 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
|
||||
}
|
||||
})),
|
||||
StaticType::Int
|
||||
StaticType::Int,
|
||||
)),
|
||||
("-", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
@@ -24,18 +24,27 @@ pub fn lookup(name: &str, args: &[StaticType]) -> Option<(Value, StaticType)> {
|
||||
Value::Int(0)
|
||||
}
|
||||
})),
|
||||
StaticType::Int
|
||||
StaticType::Int,
|
||||
)),
|
||||
("-", [StaticType::Int, StaticType::Int, StaticType::Int]) => Some((
|
||||
// Variadic optimization for 3 args (common in some Lisp dialects, though - usually is binary/unary)
|
||||
// MyC's core.rs supports variadic subtraction.
|
||||
Value::Function(Rc::new(|args| {
|
||||
let a = match args[0] { Value::Int(i) => i, _ => 0 };
|
||||
let b = match args[1] { Value::Int(i) => i, _ => 0 };
|
||||
let c = match args[2] { Value::Int(i) => i, _ => 0 };
|
||||
let a = match args[0] {
|
||||
Value::Int(i) => i,
|
||||
_ => 0,
|
||||
};
|
||||
let b = match args[1] {
|
||||
Value::Int(i) => i,
|
||||
_ => 0,
|
||||
};
|
||||
let c = match args[2] {
|
||||
Value::Int(i) => i,
|
||||
_ => 0,
|
||||
};
|
||||
Value::Int(a - b - c)
|
||||
})),
|
||||
StaticType::Int
|
||||
StaticType::Int,
|
||||
)),
|
||||
("*", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
@@ -45,7 +54,7 @@ pub fn lookup(name: &str, args: &[StaticType]) -> Option<(Value, StaticType)> {
|
||||
Value::Int(0)
|
||||
}
|
||||
})),
|
||||
StaticType::Int
|
||||
StaticType::Int,
|
||||
)),
|
||||
|
||||
// --- Integer Comparison ---
|
||||
@@ -57,7 +66,7 @@ pub fn lookup(name: &str, args: &[StaticType]) -> Option<(Value, StaticType)> {
|
||||
Value::Bool(false)
|
||||
}
|
||||
})),
|
||||
StaticType::Bool
|
||||
StaticType::Bool,
|
||||
)),
|
||||
("<", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
@@ -67,7 +76,7 @@ pub fn lookup(name: &str, args: &[StaticType]) -> Option<(Value, StaticType)> {
|
||||
Value::Bool(false)
|
||||
}
|
||||
})),
|
||||
StaticType::Bool
|
||||
StaticType::Bool,
|
||||
)),
|
||||
(">", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
@@ -77,7 +86,7 @@ pub fn lookup(name: &str, args: &[StaticType]) -> Option<(Value, StaticType)> {
|
||||
Value::Bool(false)
|
||||
}
|
||||
})),
|
||||
StaticType::Bool
|
||||
StaticType::Bool,
|
||||
)),
|
||||
(">=", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
@@ -87,7 +96,7 @@ pub fn lookup(name: &str, args: &[StaticType]) -> Option<(Value, StaticType)> {
|
||||
Value::Bool(false)
|
||||
}
|
||||
})),
|
||||
StaticType::Bool
|
||||
StaticType::Bool,
|
||||
)),
|
||||
("=", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
@@ -97,12 +106,11 @@ pub fn lookup(name: &str, args: &[StaticType]) -> Option<(Value, StaticType)> {
|
||||
Value::Bool(false)
|
||||
}
|
||||
})),
|
||||
StaticType::Bool
|
||||
StaticType::Bool,
|
||||
)),
|
||||
|
||||
// --- Constant Unary for -1 (decrement optimization) ---
|
||||
// Special case: tak uses (- x 1). The specializer sees Call("-", [Int, Int]).
|
||||
|
||||
_ => None
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
pub mod core;
|
||||
pub mod datetime;
|
||||
pub mod type_registry;
|
||||
pub mod intrinsics;
|
||||
pub mod type_registry;
|
||||
|
||||
use crate::ast::environment::Environment;
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::ast::types::{Keyword, Signature, StaticType, Value};
|
||||
use std::any::TypeId;
|
||||
use std::collections::HashMap;
|
||||
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.
|
||||
pub trait Scriptable: 'static + Sized {
|
||||
@@ -41,19 +41,20 @@ impl TypeRegistry {
|
||||
|
||||
/// Resolves the static type for a Rust type.
|
||||
pub fn resolve_type<T: 'static>(&self) -> StaticType {
|
||||
self.known_types.get(&TypeId::of::<T>()).cloned().unwrap_or(StaticType::Any)
|
||||
self.known_types
|
||||
.get(&TypeId::of::<T>())
|
||||
.cloned()
|
||||
.unwrap_or(StaticType::Any)
|
||||
}
|
||||
|
||||
/// Creates a factory function value that can be bound in the environment.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `factory_func` - A Rust closure that takes script arguments and returns a Result<T, String>.
|
||||
pub fn create_factory<T, F>(
|
||||
factory_func: F
|
||||
) -> Value
|
||||
pub fn create_factory<T, F>(factory_func: F) -> Value
|
||||
where
|
||||
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.
|
||||
let closure = move |args: Vec<Value>| -> Value {
|
||||
@@ -85,13 +86,12 @@ impl Default for RecordBuilder {
|
||||
|
||||
impl RecordBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
fields: Vec::new(),
|
||||
}
|
||||
Self { fields: Vec::new() }
|
||||
}
|
||||
|
||||
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);
|
||||
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)
|
||||
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>| {
|
||||
match func(args) {
|
||||
let closure = move |args: Vec<Value>| match func(args) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("Method call error: {}", e),
|
||||
}
|
||||
};
|
||||
self.method(name, closure)
|
||||
}
|
||||
@@ -135,14 +134,15 @@ impl Default for TypeBuilder {
|
||||
|
||||
impl TypeBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
fields: Vec::new(),
|
||||
}
|
||||
Self { fields: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn method(mut self, name: &str, params: Vec<StaticType>, ret: StaticType) -> Self {
|
||||
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
|
||||
}
|
||||
@@ -155,7 +155,7 @@ impl TypeBuilder {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ast::types::{Value, StaticType};
|
||||
use crate::ast::types::{StaticType, Value};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Person {
|
||||
@@ -164,7 +164,9 @@ mod tests {
|
||||
}
|
||||
|
||||
impl Scriptable for Person {
|
||||
fn type_name() -> &'static str { "Person" }
|
||||
fn type_name() -> &'static str {
|
||||
"Person"
|
||||
}
|
||||
|
||||
fn static_type() -> StaticType {
|
||||
TypeBuilder::new()
|
||||
@@ -178,7 +180,9 @@ mod tests {
|
||||
let age = self.age;
|
||||
|
||||
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))
|
||||
.build()
|
||||
}
|
||||
@@ -189,7 +193,10 @@ mod tests {
|
||||
let mut registry = TypeRegistry::new();
|
||||
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();
|
||||
|
||||
// Check static type
|
||||
@@ -202,7 +209,11 @@ mod tests {
|
||||
|
||||
// Check runtime behavior
|
||||
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];
|
||||
|
||||
if let Value::Function(f) = greet_fn {
|
||||
@@ -226,24 +237,40 @@ mod tests {
|
||||
if args.len() != 2 {
|
||||
return Err("Expected 2 args".to_string());
|
||||
}
|
||||
let name = match &args[0] { Value::Text(t) => t.to_string(), _ => return Err("Name must be text".to_string()) };
|
||||
let age = match &args[1] { Value::Int(i) => *i as u32, _ => return Err("Age must be int".to_string()) };
|
||||
let name = match &args[0] {
|
||||
Value::Text(t) => t.to_string(),
|
||||
_ => return Err("Name must be text".to_string()),
|
||||
};
|
||||
let age = match &args[1] {
|
||||
Value::Int(i) => *i as u32,
|
||||
_ => return Err("Age must be int".to_string()),
|
||||
};
|
||||
Ok(Person { name, age })
|
||||
});
|
||||
|
||||
if let Value::Function(f) = factory_val {
|
||||
let instance = f(vec![Value::Text("Bob".into()), Value::Int(40)]);
|
||||
if let Value::Record(r) = instance {
|
||||
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];
|
||||
|
||||
if let Value::Function(gf) = greet_fn {
|
||||
let res = gf(vec![]);
|
||||
if let Value::Text(s) = res {
|
||||
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"); }
|
||||
}
|
||||
}
|
||||
|
||||
+62
-40
@@ -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 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
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
@@ -31,11 +31,17 @@ static KEYWORD_REVERSE: OnceLock<Mutex<Vec<String>>> = OnceLock::new();
|
||||
|
||||
impl Keyword {
|
||||
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) {
|
||||
Keyword(id)
|
||||
} 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;
|
||||
reg.insert(name.to_string(), id);
|
||||
rev.push(name.to_string());
|
||||
@@ -44,7 +50,10 @@ impl Keyword {
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -149,31 +158,37 @@ impl fmt::Display for StaticType {
|
||||
StaticType::Tuple(elements) => {
|
||||
write!(f, "[")?;
|
||||
for (i, el) in elements.iter().enumerate() {
|
||||
if i > 0 { write!(f, " ")?; }
|
||||
if i > 0 {
|
||||
write!(f, " ")?;
|
||||
}
|
||||
write!(f, "{}", el)?;
|
||||
}
|
||||
write!(f, "]")
|
||||
},
|
||||
}
|
||||
StaticType::Vector(inner, len) => write!(f, "vector<{}, {}>", inner, len),
|
||||
StaticType::Matrix(inner, shape) => {
|
||||
write!(f, "matrix<{}, [", inner)?;
|
||||
for (i, s) in shape.iter().enumerate() {
|
||||
if i > 0 { write!(f, " ")?; }
|
||||
if i > 0 {
|
||||
write!(f, " ")?;
|
||||
}
|
||||
write!(f, "{}", s)?;
|
||||
}
|
||||
write!(f, "]>")
|
||||
},
|
||||
}
|
||||
StaticType::Record(fields) => {
|
||||
write!(f, "{{")?;
|
||||
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, "}}")
|
||||
},
|
||||
}
|
||||
StaticType::Function(sig) => {
|
||||
write!(f, "fn({}) -> {}", sig.params, sig.ret)
|
||||
},
|
||||
}
|
||||
StaticType::FunctionOverloads(sigs) => {
|
||||
write!(f, "overloads({} variants)", sigs.len())
|
||||
}
|
||||
@@ -192,12 +207,16 @@ impl StaticType {
|
||||
match (self, other) {
|
||||
// A Vector is a Tuple
|
||||
(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))
|
||||
},
|
||||
}
|
||||
// A Matrix is a Vector (of Vectors/Matrices)
|
||||
(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 {
|
||||
inner.is_assignable_from(m_inner)
|
||||
} else {
|
||||
@@ -205,8 +224,8 @@ impl StaticType {
|
||||
let sub_shape = shape[1..].to_vec();
|
||||
inner.is_assignable_from(&StaticType::Matrix(m_inner.clone(), sub_shape))
|
||||
}
|
||||
},
|
||||
_ => false
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,11 +240,10 @@ impl StaticType {
|
||||
None
|
||||
}
|
||||
}
|
||||
StaticType::FunctionOverloads(sigs) => {
|
||||
sigs.iter()
|
||||
StaticType::FunctionOverloads(sigs) => sigs
|
||||
.iter()
|
||||
.find(|sig| sig.params.is_assignable_from(args_ty))
|
||||
.map(|sig| sig.ret.clone())
|
||||
}
|
||||
.map(|sig| sig.ret.clone()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -268,7 +286,7 @@ impl Value {
|
||||
pub fn make_record(keys: Vec<Keyword>, values: Vec<Value>) -> Self {
|
||||
Value::Record(Rc::new(RecordData {
|
||||
keys: Rc::new(keys),
|
||||
values: Rc::new(values)
|
||||
values: Rc::new(values),
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -297,25 +315,25 @@ impl Value {
|
||||
StaticType::Vector(inner, len) => {
|
||||
// Possible Matrix
|
||||
StaticType::Matrix(inner.clone(), vec![values.len(), *len])
|
||||
},
|
||||
}
|
||||
StaticType::Matrix(inner, shape) => {
|
||||
let mut new_shape = vec![values.len()];
|
||||
new_shape.extend(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 {
|
||||
StaticType::Tuple(element_types)
|
||||
}
|
||||
},
|
||||
}
|
||||
Value::Record(r) => {
|
||||
let mut fields = Vec::with_capacity(r.values.len());
|
||||
for (i, v) in r.values.iter().enumerate() {
|
||||
fields.push((r.keys[i], v.static_type()));
|
||||
}
|
||||
StaticType::Record(Rc::new(fields))
|
||||
},
|
||||
}
|
||||
Value::Function(_) => StaticType::Any, // Dynamic function
|
||||
Value::Object(o) => StaticType::Object(o.type_name()),
|
||||
Value::Cell(c) => c.borrow().static_type(),
|
||||
@@ -331,30 +349,34 @@ impl fmt::Display for Value {
|
||||
Value::Bool(b) => write!(f, "{}", b),
|
||||
Value::Int(i) => write!(f, "{}", i),
|
||||
Value::Float(fl) => write!(f, "{}", fl),
|
||||
Value::DateTime(ts) => {
|
||||
match Utc.timestamp_millis_opt(*ts) {
|
||||
chrono::LocalResult::Single(dt) => write!(f, "#{}#", dt.format("%Y-%m-%d %H:%M:%S")),
|
||||
_ => write!(f, "#timestamp({})#", ts),
|
||||
Value::DateTime(ts) => match Utc.timestamp_millis_opt(*ts) {
|
||||
chrono::LocalResult::Single(dt) => {
|
||||
write!(f, "#{}#", dt.format("%Y-%m-%d %H:%M:%S"))
|
||||
}
|
||||
_ => write!(f, "#timestamp({})#", ts),
|
||||
},
|
||||
Value::Text(t) => write!(f, "\"{}\"", t),
|
||||
Value::Keyword(k) => write!(f, ":{}", k.name()),
|
||||
Value::Tuple(values) => {
|
||||
write!(f, "[")?;
|
||||
for (i, val) in values.iter().enumerate() {
|
||||
if i > 0 { write!(f, " ")?; }
|
||||
if i > 0 {
|
||||
write!(f, " ")?;
|
||||
}
|
||||
write!(f, "{}", val)?;
|
||||
}
|
||||
write!(f, "]")
|
||||
},
|
||||
}
|
||||
Value::Record(r) => {
|
||||
write!(f, "{{")?;
|
||||
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, "}}")
|
||||
},
|
||||
}
|
||||
Value::Function(_) => write!(f, "<native fn>"),
|
||||
Value::Object(o) => write!(f, "<{}>", o.type_name()),
|
||||
Value::Cell(c) => write!(f, "{}", c.borrow()),
|
||||
|
||||
+338
-91
@@ -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::tco::ExecNode;
|
||||
use crate::ast::types::{Value, Object};
|
||||
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)]
|
||||
pub struct Closure {
|
||||
@@ -17,14 +17,30 @@ pub struct Closure {
|
||||
|
||||
impl Closure {
|
||||
#[inline]
|
||||
pub fn new(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 }
|
||||
pub fn new(
|
||||
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 {
|
||||
fn type_name(&self) -> &'static str { "closure" }
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn type_name(&self) -> &'static str {
|
||||
"closure"
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -48,17 +64,33 @@ pub struct TracingObserver {
|
||||
}
|
||||
|
||||
impl TracingObserver {
|
||||
pub fn new() -> Self { Self { logs: Vec::new(), indent: 0 } }
|
||||
fn pad(&self) -> String { "| ".repeat(self.indent) }
|
||||
pub fn new() -> Self {
|
||||
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 {
|
||||
const ACTIVE: bool = true;
|
||||
fn before_eval(&mut self, _vm: &VM, node: &ExecNode) {
|
||||
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;
|
||||
}
|
||||
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 { .. } => {
|
||||
let s_pad = format!("{}| ", 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));
|
||||
}
|
||||
}
|
||||
@@ -214,11 +253,21 @@ macro_rules! dispatch_eval {
|
||||
}
|
||||
|
||||
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> {
|
||||
self.stack.clear(); self.frames.clear();
|
||||
self.frames.push(CallFrame { stack_base: 0, closure: None });
|
||||
self.stack.clear();
|
||||
self.frames.clear();
|
||||
self.frames.push(CallFrame {
|
||||
stack_base: 0,
|
||||
closure: None,
|
||||
});
|
||||
let mut result = self.eval(root);
|
||||
self.frames.pop();
|
||||
loop {
|
||||
@@ -227,15 +276,25 @@ impl VM {
|
||||
let (next_obj, next_args) = *payload;
|
||||
if let Some(closure) = next_obj.as_any().downcast_ref::<Closure>() {
|
||||
self.stack.clear();
|
||||
self.frames.push(CallFrame { stack_base: 0, closure: Some(Rc::new(closure.clone())) });
|
||||
if let Some(count) = closure.positional_count && next_args.len() == count as usize {
|
||||
self.frames.push(CallFrame {
|
||||
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);
|
||||
} else {
|
||||
self.unpack(&closure.parameter_node, &next_args, &mut 0)?;
|
||||
}
|
||||
result = self.eval(&closure.exec_node);
|
||||
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,
|
||||
}
|
||||
@@ -243,34 +302,74 @@ impl VM {
|
||||
}
|
||||
|
||||
pub fn run_with_args(&mut self, closure: &Closure, 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.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(&closure.exec_node)
|
||||
}
|
||||
|
||||
pub fn run_with_args_observed<O: VMObserver>(&mut self, observer: &mut O, closure: &Closure, 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)?; }
|
||||
pub fn run_with_args_observed<O: VMObserver>(
|
||||
&mut self,
|
||||
observer: &mut O,
|
||||
closure: &Closure,
|
||||
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)
|
||||
}
|
||||
|
||||
pub fn run_with_observer<O: VMObserver>(&mut self, observer: &mut O, root: &ExecNode) -> Result<Value, String> {
|
||||
self.stack.clear(); self.frames.clear();
|
||||
self.frames.push(CallFrame { stack_base: 0, closure: None });
|
||||
pub fn run_with_observer<O: VMObserver>(
|
||||
&mut self,
|
||||
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);
|
||||
self.frames.pop();
|
||||
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);
|
||||
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);
|
||||
observer.after_eval(self, node, &result);
|
||||
result
|
||||
@@ -282,23 +381,31 @@ impl VM {
|
||||
let frame = self.frames.last().ok_or("No call frame")?;
|
||||
let abs_index = frame.stack_base + (idx as usize);
|
||||
if abs_index < self.stack.len() {
|
||||
if let Value::Cell(cell) = &self.stack[abs_index] { Ok(cell.clone()) }
|
||||
else {
|
||||
if let Value::Cell(cell) = &self.stack[abs_index] {
|
||||
Ok(cell.clone())
|
||||
} else {
|
||||
let val = self.stack[abs_index].clone();
|
||||
let cell = Rc::new(RefCell::new(val));
|
||||
self.stack[abs_index] = Value::Cell(cell.clone());
|
||||
Ok(cell)
|
||||
}
|
||||
} else { Err(format!("Stack underflow capture local {}", idx)) }
|
||||
},
|
||||
} else {
|
||||
Err(format!("Stack underflow capture local {}", idx))
|
||||
}
|
||||
}
|
||||
Address::Upvalue(idx) => {
|
||||
let frame = self.frames.last().ok_or("No call frame")?;
|
||||
if let Some(closure) = &frame.closure {
|
||||
let idx = idx as usize;
|
||||
if idx < closure.upvalues.len() { Ok(closure.upvalues[idx].clone()) }
|
||||
else { Err(format!("Upvalue access out of bounds capture {}", idx)) }
|
||||
} else { Err("Current frame has no closure".to_string()) }
|
||||
},
|
||||
if idx < closure.upvalues.len() {
|
||||
Ok(closure.upvalues[idx].clone())
|
||||
} else {
|
||||
Err(format!("Upvalue access out of bounds capture {}", idx))
|
||||
}
|
||||
} 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 abs_index = frame.stack_base + (idx as usize);
|
||||
if abs_index < self.stack.len() {
|
||||
match &self.stack[abs_index] { Value::Cell(cell) => Ok(cell.borrow().clone()), val => Ok(val.clone()) }
|
||||
} else { Err(format!("Stack underflow access local {}", idx)) }
|
||||
},
|
||||
match &self.stack[abs_index] {
|
||||
Value::Cell(cell) => Ok(cell.borrow().clone()),
|
||||
val => Ok(val.clone()),
|
||||
}
|
||||
} else {
|
||||
Err(format!("Stack underflow access local {}", idx))
|
||||
}
|
||||
}
|
||||
Address::Global(idx) => {
|
||||
let idx = idx as usize;
|
||||
let globals = self.globals.borrow();
|
||||
if idx < globals.len() { Ok(globals[idx].clone()) }
|
||||
else { Err(format!("Global access out of bounds {}", idx)) }
|
||||
},
|
||||
if idx < globals.len() {
|
||||
Ok(globals[idx].clone())
|
||||
} else {
|
||||
Err(format!("Global access out of bounds {}", idx))
|
||||
}
|
||||
}
|
||||
Address::Upvalue(idx) => {
|
||||
let frame = self.frames.last().ok_or("No call frame")?;
|
||||
if let Some(closure) = &frame.closure {
|
||||
let idx = idx as usize;
|
||||
if idx < closure.upvalues.len() { Ok(closure.upvalues[idx].borrow().clone()) }
|
||||
else { Err(format!("Upvalue access out of bounds {}", idx)) }
|
||||
} else { Err("Current frame has no closure (cannot access upvalues)".to_string()) }
|
||||
if idx < closure.upvalues.len() {
|
||||
Ok(closure.upvalues[idx].borrow().clone())
|
||||
} 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 abs_index = frame.stack_base + (idx as usize);
|
||||
if abs_index < self.stack.len() {
|
||||
if let Value::Cell(cell) = &self.stack[abs_index] { *cell.borrow_mut() = value; }
|
||||
else { 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)); }
|
||||
if let Value::Cell(cell) = &self.stack[abs_index] {
|
||||
*cell.borrow_mut() = value;
|
||||
} else {
|
||||
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(())
|
||||
},
|
||||
}
|
||||
Address::Global(idx) => {
|
||||
let idx = idx as usize;
|
||||
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;
|
||||
Ok(())
|
||||
},
|
||||
}
|
||||
Address::Upvalue(idx) => {
|
||||
let frame = self.frames.last().ok_or("No call frame")?;
|
||||
if let Some(closure) = &frame.closure {
|
||||
let idx = idx as usize;
|
||||
if idx < closure.upvalues.len() { *closure.upvalues[idx].borrow_mut() = value; Ok(()) }
|
||||
else { Err(format!("Upvalue assignment out of bounds {}", idx)) }
|
||||
} else { Err("Current frame has no closure".to_string()) }
|
||||
},
|
||||
if idx < closure.upvalues.len() {
|
||||
*closure.upvalues[idx].borrow_mut() = value;
|
||||
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>) {
|
||||
if let Some(values) = val.as_slice() { for item in values.iter() { Self::flatten_value(item.clone(), into); } }
|
||||
else { into.push(val); }
|
||||
if let Some(values) = val.as_slice() {
|
||||
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> {
|
||||
let mut arg_vals = Vec::new();
|
||||
match &args.kind {
|
||||
BoundKind::Tuple { elements } => { self.eval_and_flatten(elements, &mut arg_vals)?; }
|
||||
_ => { VM::flatten_value(self.eval(args)?, &mut arg_vals); }
|
||||
BoundKind::Tuple { elements } => {
|
||||
self.eval_and_flatten(elements, &mut arg_vals)?;
|
||||
}
|
||||
_ => {
|
||||
VM::flatten_value(self.eval(args)?, &mut 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();
|
||||
match &args.kind {
|
||||
BoundKind::Tuple { elements } => { self.eval_observed_and_flatten(observer, elements, &mut arg_vals)?; }
|
||||
_ => { VM::flatten_value(self.eval_observed(observer, args)?, &mut arg_vals); }
|
||||
BoundKind::Tuple { elements } => {
|
||||
self.eval_observed_and_flatten(observer, elements, &mut arg_vals)?;
|
||||
}
|
||||
_ => {
|
||||
VM::flatten_value(self.eval_observed(observer, args)?, &mut 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 {
|
||||
match &e.kind {
|
||||
BoundKind::Tuple { elements: sub } => self.eval_and_flatten(sub, into)?,
|
||||
@@ -392,36 +547,56 @@ impl VM {
|
||||
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 {
|
||||
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)?),
|
||||
}
|
||||
}
|
||||
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 {
|
||||
BoundKind::Parameter { slot, .. } => {
|
||||
let val = values.get(*offset).cloned().unwrap_or(Value::Void);
|
||||
*offset += 1;
|
||||
let frame = self.frames.last().ok_or("No call frame")?;
|
||||
let abs_index = frame.stack_base + (*slot as usize);
|
||||
if abs_index == self.stack.len() { self.stack.push(val); }
|
||||
else if abs_index < self.stack.len() { self.stack[abs_index] = val; }
|
||||
else { return Err(format!("Stack gap during unpack at slot {}", slot)); }
|
||||
if abs_index == self.stack.len() {
|
||||
self.stack.push(val);
|
||||
} else if abs_index < self.stack.len() {
|
||||
self.stack[abs_index] = val;
|
||||
} else {
|
||||
return Err(format!("Stack gap during unpack at slot {}", slot));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
BoundKind::Tuple { elements } => {
|
||||
if let Some(sub_values) = values.get(*offset).and_then(|v| v.as_slice()) {
|
||||
*offset += 1;
|
||||
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(());
|
||||
}
|
||||
for el in elements { self.unpack(el, values, offset)?; }
|
||||
for el in elements {
|
||||
self.unpack(el, values, offset)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
_ => Err("Invalid node in parameter pattern".to_string()),
|
||||
@@ -432,27 +607,96 @@ impl VM {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ast::nodes::{Node, Symbol};
|
||||
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]
|
||||
fn test_capture_boxing_modification() {
|
||||
let id = make_dummy_identity();
|
||||
let lambda_body = Node {
|
||||
identity: id.clone(), 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)) }) },
|
||||
identity: id.clone(),
|
||||
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 {
|
||||
identity: id.clone(), ty: StaticType::Int,
|
||||
identity: id.clone(),
|
||||
ty: StaticType::Int,
|
||||
kind: BoundKind::Block {
|
||||
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 { 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") } },
|
||||
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 {
|
||||
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 exec_root = TCO::optimize(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
@@ -1,6 +1,6 @@
|
||||
use clap::Parser;
|
||||
use myc::ast::environment::Environment;
|
||||
use myc::utils::tester;
|
||||
use clap::Parser;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
@@ -51,7 +51,9 @@ fn main() {
|
||||
println!("🚀 Running benchmarks in RELEASE mode...\n");
|
||||
let results = tester::run_benchmarks(cli.update_bench);
|
||||
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);
|
||||
}
|
||||
return;
|
||||
@@ -81,7 +83,7 @@ fn main() {
|
||||
} else {
|
||||
execute(&env, &content);
|
||||
}
|
||||
},
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error reading file {:?}: {}", file_path, e);
|
||||
std::process::exit(1);
|
||||
|
||||
+8
-2
@@ -54,7 +54,9 @@ pub fn run_functional_tests_with_optimization(enabled: bool) -> Vec<TestResult>
|
||||
success: false,
|
||||
message: format!(
|
||||
"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 {
|
||||
name,
|
||||
success: false,
|
||||
message: format!("Opt {}: Error: {}", if enabled { "ON" } else { "OFF" }, e),
|
||||
message: format!(
|
||||
"Opt {}: Error: {}",
|
||||
if enabled { "ON" } else { "OFF" },
|
||||
e
|
||||
),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user