Formatting
This commit is contained in:
+501
-412
@@ -1,412 +1,501 @@
|
||||
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 {
|
||||
slot: u32,
|
||||
// Note: Binder doesn't strictly need the type anymore,
|
||||
// but it might be useful for built-ins during resolution.
|
||||
// For now we keep it as Any or Unknown.
|
||||
_ty: StaticType,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct CompilerScope {
|
||||
locals: HashMap<Symbol, LocalInfo>,
|
||||
slot_count: u32,
|
||||
}
|
||||
|
||||
impl CompilerScope {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
locals: HashMap::new(),
|
||||
slot_count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
let slot = self.slot_count;
|
||||
self.locals.insert(sym.clone(), LocalInfo { slot, _ty: StaticType::Any });
|
||||
self.slot_count += 1;
|
||||
Ok(slot)
|
||||
}
|
||||
|
||||
fn resolve(&self, sym: &Symbol) -> Option<LocalInfo> {
|
||||
self.locals.get(sym).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
struct FunctionCompiler {
|
||||
scope: CompilerScope,
|
||||
upvalues: Vec<Address>,
|
||||
}
|
||||
|
||||
impl FunctionCompiler {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
scope: CompilerScope::new(),
|
||||
upvalues: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_upvalue(&mut self, addr: Address) -> u32 {
|
||||
if let Some(idx) = self.upvalues.iter().position(|&a| a == addr) {
|
||||
return idx as u32;
|
||||
}
|
||||
let idx = self.upvalues.len() as u32;
|
||||
self.upvalues.push(addr);
|
||||
idx
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Binder {
|
||||
functions: Vec<FunctionCompiler>,
|
||||
// Globals mapping: Symbol -> Index
|
||||
globals: Rc<RefCell<HashMap<Symbol, u32>>>,
|
||||
// Map of Declaration Identity -> List of Lambda Identities that capture it
|
||||
capture_map: HashMap<Identity, Vec<Identity>>,
|
||||
}
|
||||
|
||||
impl Binder {
|
||||
pub fn new(globals: Rc<RefCell<HashMap<Symbol, u32>>>) -> Self {
|
||||
Self::with_boxed(globals, HashMap::new())
|
||||
}
|
||||
|
||||
fn with_boxed(globals: Rc<RefCell<HashMap<Symbol, u32>>>, captures: HashMap<Identity, Vec<Identity>>) -> Self {
|
||||
let mut binder = Self {
|
||||
functions: Vec::new(),
|
||||
globals,
|
||||
capture_map: captures,
|
||||
};
|
||||
binder.functions.push(FunctionCompiler::new());
|
||||
binder
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
pub fn bind(&mut self, node: &Node<UntypedKind>) -> Result<BoundNode, String> {
|
||||
match &node.kind {
|
||||
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 {
|
||||
addr,
|
||||
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));
|
||||
}
|
||||
let current_fn = self.functions.last_mut().unwrap();
|
||||
let slot = current_fn.scope.define(sym)?;
|
||||
Ok(self.make_node(node.identity.clone(), BoundKind::Parameter {
|
||||
name: sym.clone(),
|
||||
slot
|
||||
}))
|
||||
},
|
||||
|
||||
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;
|
||||
if let Some(e) = else_br {
|
||||
else_br_bound = Some(Box::new(self.bind(e)?));
|
||||
}
|
||||
|
||||
Ok(self.make_node(node.identity.clone(), BoundKind::If {
|
||||
cond: Box::new(cond),
|
||||
then_br: Box::new(then_br),
|
||||
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));
|
||||
}
|
||||
let idx = globals.len() as u32;
|
||||
globals.insert(name.clone(), idx);
|
||||
idx
|
||||
} else {
|
||||
let current_fn = self.functions.last_mut().unwrap();
|
||||
current_fn.scope.define(name)?
|
||||
};
|
||||
|
||||
// 2. Bind Value (now 'name' is visible)
|
||||
let val_node = self.bind(value)?;
|
||||
|
||||
// 3. Return Node
|
||||
if self.functions.len() == 1 {
|
||||
Ok(self.make_node(node.identity.clone(), BoundKind::DefGlobal {
|
||||
name: name.clone(),
|
||||
global_index: slot_or_idx,
|
||||
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 {
|
||||
name: name.clone(),
|
||||
slot: slot_or_idx,
|
||||
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 {
|
||||
addr,
|
||||
value: Box::new(val_node)
|
||||
}))
|
||||
} else {
|
||||
Err("Assignment target must be an identifier".to_string())
|
||||
}
|
||||
},
|
||||
|
||||
UntypedKind::Lambda { params, body } => {
|
||||
let identity = node.identity.clone();
|
||||
self.functions.push(FunctionCompiler::new());
|
||||
|
||||
// 1. Bind the parameter pattern/tuple
|
||||
let params_bound = self.bind(params)?;
|
||||
|
||||
// 2. Bind the body
|
||||
let body_bound = self.bind(body)?;
|
||||
|
||||
let compiled_fn = self.functions.pop().unwrap();
|
||||
|
||||
// 3. Static optimization: check if parameters are purely positional
|
||||
let positional_count = match ¶ms_bound.kind {
|
||||
BoundKind::Tuple { elements } => {
|
||||
let mut count = 0;
|
||||
let mut all_params = true;
|
||||
for e in elements {
|
||||
if matches!(e.kind, BoundKind::Parameter { .. }) {
|
||||
count += 1;
|
||||
} else {
|
||||
all_params = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if all_params { Some(count) } else { None }
|
||||
}
|
||||
BoundKind::Parameter { .. } => Some(1),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
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 {
|
||||
callee: Box::new(callee),
|
||||
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 }))
|
||||
},
|
||||
|
||||
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 }))
|
||||
},
|
||||
|
||||
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 }))
|
||||
},
|
||||
|
||||
UntypedKind::Expansion { call, expanded } => {
|
||||
let bound_expanded = self.bind(expanded)?;
|
||||
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::Extension(_) => {
|
||||
// Future: Delegate to extension binder
|
||||
Err("Custom extensions not supported in Binder yet".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_variable(&mut self, sym: &Symbol) -> Result<Address, String> {
|
||||
let current_fn_idx = self.functions.len() - 1;
|
||||
|
||||
// 1. Try local in current function
|
||||
if let Some(info) = self.functions[current_fn_idx].scope.resolve(sym) {
|
||||
return Ok(Address::Local(info.slot));
|
||||
}
|
||||
|
||||
// 2. Try enclosing scopes (capture chain)
|
||||
for i in (0..current_fn_idx).rev() {
|
||||
if let Some(info) = self.functions[i].scope.resolve(sym) {
|
||||
let mut addr = Address::Local(info.slot);
|
||||
|
||||
for k in (i + 1)..=current_fn_idx {
|
||||
addr = Address::Upvalue(self.functions[k].add_upvalue(addr));
|
||||
}
|
||||
return Ok(addr);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Try Global
|
||||
let globals = self.globals.borrow();
|
||||
if let Some(idx) = globals.get(sym) {
|
||||
return Ok(Address::Global(*idx));
|
||||
}
|
||||
|
||||
// 4. Global Fallback
|
||||
if sym.context.is_some() {
|
||||
let fallback_sym = Symbol { name: sym.name.clone(), context: None };
|
||||
if let Some(idx) = globals.get(&fallback_sym) {
|
||||
return Ok(Address::Global(*idx));
|
||||
}
|
||||
}
|
||||
|
||||
Err(format!("Undefined variable '{}'", sym.name))
|
||||
}
|
||||
|
||||
fn make_node(&self, identity: Identity, kind: BoundKind<()>) -> BoundNode {
|
||||
Node { identity, kind, ty: () }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ast::parser::Parser;
|
||||
|
||||
#[test]
|
||||
fn test_upvalue_capture_sets_is_boxed() {
|
||||
// Wrap in a lambda to ensure 'x' is a local variable, not a global
|
||||
let source = "(fn [] (do (def x 10) (def f (fn [] x)) x))";
|
||||
let mut parser = Parser::new(source).unwrap();
|
||||
let untyped = parser.parse_expression().unwrap();
|
||||
|
||||
let globals = Rc::new(RefCell::new(HashMap::new()));
|
||||
let bound = Binder::bind_root(globals, &untyped).unwrap();
|
||||
|
||||
// Structure: Lambda -> Block -> [ DefLocal(x), DefLocal(f), Get(x) ]
|
||||
if let BoundKind::Lambda { body, .. } = &bound.kind {
|
||||
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'");
|
||||
} else {
|
||||
panic!("First expression in block should be DefLocal, got {:?}", x_decl.kind);
|
||||
}
|
||||
} else {
|
||||
panic!("Lambda body should be a Block, got {:?}", body.kind);
|
||||
}
|
||||
} else {
|
||||
panic!("Root should be a Lambda, got {:?}", bound.kind);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_no_capture_not_boxed() {
|
||||
let source = "(fn [] (do (def x 10) x))";
|
||||
let mut parser = Parser::new(source).unwrap();
|
||||
let untyped = parser.parse_expression().unwrap();
|
||||
|
||||
let globals = Rc::new(RefCell::new(HashMap::new()));
|
||||
let bound = Binder::bind_root(globals, &untyped).unwrap();
|
||||
|
||||
if let BoundKind::Lambda { body, .. } = &bound.kind {
|
||||
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");
|
||||
} else {
|
||||
panic!("First expression should be DefLocal");
|
||||
}
|
||||
} else {
|
||||
panic!("Lambda body should be a Block");
|
||||
}
|
||||
} else {
|
||||
panic!("Root should be a Lambda");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_redefinition_error() {
|
||||
let source = "(do (def x 1) (def x 2))";
|
||||
let mut parser = Parser::new(source).unwrap();
|
||||
let untyped = parser.parse_expression().unwrap();
|
||||
|
||||
let globals = Rc::new(RefCell::new(HashMap::new()));
|
||||
let result = Binder::bind_root(globals, &untyped);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("already defined"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_repro_global_redefinition() {
|
||||
let globals = Rc::new(RefCell::new(HashMap::new()));
|
||||
|
||||
// First run: defines 'x'
|
||||
let source1 = "(def x 1)";
|
||||
let untyped1 = Parser::new(source1).unwrap().parse_expression().unwrap();
|
||||
assert!(Binder::bind_root(globals.clone(), &untyped1).is_ok());
|
||||
|
||||
// Second run: attempts to redefine 'x' in the same global environment
|
||||
let source2 = "(def x 2)";
|
||||
let untyped2 = Parser::new(source2).unwrap().parse_expression().unwrap();
|
||||
let result = Binder::bind_root(globals.clone(), &untyped2);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("already defined"));
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct LocalInfo {
|
||||
slot: u32,
|
||||
// Note: Binder doesn't strictly need the type anymore,
|
||||
// but it might be useful for built-ins during resolution.
|
||||
// For now we keep it as Any or Unknown.
|
||||
_ty: StaticType,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct CompilerScope {
|
||||
locals: HashMap<Symbol, LocalInfo>,
|
||||
slot_count: u32,
|
||||
}
|
||||
|
||||
impl CompilerScope {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
locals: HashMap::new(),
|
||||
slot_count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
));
|
||||
}
|
||||
let slot = self.slot_count;
|
||||
self.locals.insert(
|
||||
sym.clone(),
|
||||
LocalInfo {
|
||||
slot,
|
||||
_ty: StaticType::Any,
|
||||
},
|
||||
);
|
||||
self.slot_count += 1;
|
||||
Ok(slot)
|
||||
}
|
||||
|
||||
fn resolve(&self, sym: &Symbol) -> Option<LocalInfo> {
|
||||
self.locals.get(sym).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
struct FunctionCompiler {
|
||||
scope: CompilerScope,
|
||||
upvalues: Vec<Address>,
|
||||
}
|
||||
|
||||
impl FunctionCompiler {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
scope: CompilerScope::new(),
|
||||
upvalues: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_upvalue(&mut self, addr: Address) -> u32 {
|
||||
if let Some(idx) = self.upvalues.iter().position(|&a| a == addr) {
|
||||
return idx as u32;
|
||||
}
|
||||
let idx = self.upvalues.len() as u32;
|
||||
self.upvalues.push(addr);
|
||||
idx
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Binder {
|
||||
functions: Vec<FunctionCompiler>,
|
||||
// Globals mapping: Symbol -> Index
|
||||
globals: Rc<RefCell<HashMap<Symbol, u32>>>,
|
||||
// Map of Declaration Identity -> List of Lambda Identities that capture it
|
||||
capture_map: HashMap<Identity, Vec<Identity>>,
|
||||
}
|
||||
|
||||
impl Binder {
|
||||
pub fn new(globals: Rc<RefCell<HashMap<Symbol, u32>>>) -> Self {
|
||||
Self::with_boxed(globals, HashMap::new())
|
||||
}
|
||||
|
||||
fn with_boxed(
|
||||
globals: Rc<RefCell<HashMap<Symbol, u32>>>,
|
||||
captures: HashMap<Identity, Vec<Identity>>,
|
||||
) -> Self {
|
||||
let mut binder = Self {
|
||||
functions: Vec::new(),
|
||||
globals,
|
||||
capture_map: captures,
|
||||
};
|
||||
binder.functions.push(FunctionCompiler::new());
|
||||
binder
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
pub fn bind(&mut self, node: &Node<UntypedKind>) -> Result<BoundNode, String> {
|
||||
match &node.kind {
|
||||
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 {
|
||||
addr,
|
||||
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
|
||||
));
|
||||
}
|
||||
let current_fn = self.functions.last_mut().unwrap();
|
||||
let slot = current_fn.scope.define(sym)?;
|
||||
Ok(self.make_node(
|
||||
node.identity.clone(),
|
||||
BoundKind::Parameter {
|
||||
name: sym.clone(),
|
||||
slot,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
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;
|
||||
if let Some(e) = else_br {
|
||||
else_br_bound = Some(Box::new(self.bind(e)?));
|
||||
}
|
||||
|
||||
Ok(self.make_node(
|
||||
node.identity.clone(),
|
||||
BoundKind::If {
|
||||
cond: Box::new(cond),
|
||||
then_br: Box::new(then_br),
|
||||
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
|
||||
));
|
||||
}
|
||||
let idx = globals.len() as u32;
|
||||
globals.insert(name.clone(), idx);
|
||||
idx
|
||||
} else {
|
||||
let current_fn = self.functions.last_mut().unwrap();
|
||||
current_fn.scope.define(name)?
|
||||
};
|
||||
|
||||
// 2. Bind Value (now 'name' is visible)
|
||||
let val_node = self.bind(value)?;
|
||||
|
||||
// 3. Return Node
|
||||
if self.functions.len() == 1 {
|
||||
Ok(self.make_node(
|
||||
node.identity.clone(),
|
||||
BoundKind::DefGlobal {
|
||||
name: name.clone(),
|
||||
global_index: slot_or_idx,
|
||||
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 {
|
||||
name: name.clone(),
|
||||
slot: slot_or_idx,
|
||||
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 {
|
||||
addr,
|
||||
value: Box::new(val_node),
|
||||
},
|
||||
))
|
||||
} else {
|
||||
Err("Assignment target must be an identifier".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
UntypedKind::Lambda { params, body } => {
|
||||
let identity = node.identity.clone();
|
||||
self.functions.push(FunctionCompiler::new());
|
||||
|
||||
// 1. Bind the parameter pattern/tuple
|
||||
let params_bound = self.bind(params)?;
|
||||
|
||||
// 2. Bind the body
|
||||
let body_bound = self.bind(body)?;
|
||||
|
||||
let compiled_fn = self.functions.pop().unwrap();
|
||||
|
||||
// 3. Static optimization: check if parameters are purely positional
|
||||
let positional_count = match ¶ms_bound.kind {
|
||||
BoundKind::Tuple { elements } => {
|
||||
let mut count = 0;
|
||||
let mut all_params = true;
|
||||
for e in elements {
|
||||
if matches!(e.kind, BoundKind::Parameter { .. }) {
|
||||
count += 1;
|
||||
} else {
|
||||
all_params = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if all_params { Some(count) } else { None }
|
||||
}
|
||||
BoundKind::Parameter { .. } => Some(1),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
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 {
|
||||
callee: Box::new(callee),
|
||||
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 },
|
||||
))
|
||||
}
|
||||
|
||||
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,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
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,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
UntypedKind::Expansion { call, expanded } => {
|
||||
let bound_expanded = self.bind(expanded)?;
|
||||
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::Extension(_) => {
|
||||
// Future: Delegate to extension binder
|
||||
Err("Custom extensions not supported in Binder yet".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_variable(&mut self, sym: &Symbol) -> Result<Address, String> {
|
||||
let current_fn_idx = self.functions.len() - 1;
|
||||
|
||||
// 1. Try local in current function
|
||||
if let Some(info) = self.functions[current_fn_idx].scope.resolve(sym) {
|
||||
return Ok(Address::Local(info.slot));
|
||||
}
|
||||
|
||||
// 2. Try enclosing scopes (capture chain)
|
||||
for i in (0..current_fn_idx).rev() {
|
||||
if let Some(info) = self.functions[i].scope.resolve(sym) {
|
||||
let mut addr = Address::Local(info.slot);
|
||||
|
||||
for k in (i + 1)..=current_fn_idx {
|
||||
addr = Address::Upvalue(self.functions[k].add_upvalue(addr));
|
||||
}
|
||||
return Ok(addr);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Try Global
|
||||
let globals = self.globals.borrow();
|
||||
if let Some(idx) = globals.get(sym) {
|
||||
return Ok(Address::Global(*idx));
|
||||
}
|
||||
|
||||
// 4. Global Fallback
|
||||
if sym.context.is_some() {
|
||||
let fallback_sym = Symbol {
|
||||
name: sym.name.clone(),
|
||||
context: None,
|
||||
};
|
||||
if let Some(idx) = globals.get(&fallback_sym) {
|
||||
return Ok(Address::Global(*idx));
|
||||
}
|
||||
}
|
||||
|
||||
Err(format!("Undefined variable '{}'", sym.name))
|
||||
}
|
||||
|
||||
fn make_node(&self, identity: Identity, kind: BoundKind<()>) -> BoundNode {
|
||||
Node {
|
||||
identity,
|
||||
kind,
|
||||
ty: (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ast::parser::Parser;
|
||||
|
||||
#[test]
|
||||
fn test_upvalue_capture_sets_is_boxed() {
|
||||
// Wrap in a lambda to ensure 'x' is a local variable, not a global
|
||||
let source = "(fn [] (do (def x 10) (def f (fn [] x)) x))";
|
||||
let mut parser = Parser::new(source).unwrap();
|
||||
let untyped = parser.parse_expression().unwrap();
|
||||
|
||||
let globals = Rc::new(RefCell::new(HashMap::new()));
|
||||
let bound = Binder::bind_root(globals, &untyped).unwrap();
|
||||
|
||||
// Structure: Lambda -> Block -> [ DefLocal(x), DefLocal(f), Get(x) ]
|
||||
if let BoundKind::Lambda { body, .. } = &bound.kind {
|
||||
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'"
|
||||
);
|
||||
} else {
|
||||
panic!(
|
||||
"First expression in block should be DefLocal, got {:?}",
|
||||
x_decl.kind
|
||||
);
|
||||
}
|
||||
} else {
|
||||
panic!("Lambda body should be a Block, got {:?}", body.kind);
|
||||
}
|
||||
} else {
|
||||
panic!("Root should be a Lambda, got {:?}", bound.kind);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_no_capture_not_boxed() {
|
||||
let source = "(fn [] (do (def x 10) x))";
|
||||
let mut parser = Parser::new(source).unwrap();
|
||||
let untyped = parser.parse_expression().unwrap();
|
||||
|
||||
let globals = Rc::new(RefCell::new(HashMap::new()));
|
||||
let bound = Binder::bind_root(globals, &untyped).unwrap();
|
||||
|
||||
if let BoundKind::Lambda { body, .. } = &bound.kind {
|
||||
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"
|
||||
);
|
||||
} else {
|
||||
panic!("First expression should be DefLocal");
|
||||
}
|
||||
} else {
|
||||
panic!("Lambda body should be a Block");
|
||||
}
|
||||
} else {
|
||||
panic!("Root should be a Lambda");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_redefinition_error() {
|
||||
let source = "(do (def x 1) (def x 2))";
|
||||
let mut parser = Parser::new(source).unwrap();
|
||||
let untyped = parser.parse_expression().unwrap();
|
||||
|
||||
let globals = Rc::new(RefCell::new(HashMap::new()));
|
||||
let result = Binder::bind_root(globals, &untyped);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("already defined"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_repro_global_redefinition() {
|
||||
let globals = Rc::new(RefCell::new(HashMap::new()));
|
||||
|
||||
// First run: defines 'x'
|
||||
let source1 = "(def x 1)";
|
||||
let untyped1 = Parser::new(source1).unwrap().parse_expression().unwrap();
|
||||
assert!(Binder::bind_root(globals.clone(), &untyped1).is_ok());
|
||||
|
||||
// Second run: attempts to redefine 'x' in the same global environment
|
||||
let source2 = "(def x 2)";
|
||||
let untyped2 = Parser::new(source2).unwrap().parse_expression().unwrap();
|
||||
let result = Binder::bind_root(globals.clone(), &untyped2);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("already defined"));
|
||||
}
|
||||
}
|
||||
|
||||
+253
-194
@@ -1,194 +1,253 @@
|
||||
use std::rc::Rc;
|
||||
use crate::ast::types::{Value, StaticType, Identity};
|
||||
use crate::ast::nodes::{Node, Symbol};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum Address {
|
||||
Local(u32), // Stack-Slot index (relative to frame base)
|
||||
Upvalue(u32), // Index in the closure's upvalue array
|
||||
Global(u32), // Index in the global environment vector
|
||||
}
|
||||
|
||||
/// Trait for DSL-specific AST extensions (like Pipe, Series-Access, etc.)
|
||||
pub trait BoundExtension<T>: std::fmt::Debug {
|
||||
fn clone_box(&self) -> Box<dyn BoundExtension<T>>;
|
||||
fn display_name(&self) -> String;
|
||||
}
|
||||
|
||||
impl<T> Clone for Box<dyn BoundExtension<T>> {
|
||||
fn clone(&self) -> Self {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
/// Type alias for a node that has been bound. Defaults to untyped (T = ()).
|
||||
pub type BoundNode<T = ()> = Node<BoundKind<T>, T>;
|
||||
|
||||
/// Type alias for a node that has been fully type-checked.
|
||||
pub type TypedNode = BoundNode<StaticType>;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum BoundKind<T = ()> {
|
||||
Nop,
|
||||
Constant(Value),
|
||||
|
||||
/// A positional parameter in a Lambda's parameter list.
|
||||
Parameter {
|
||||
name: Symbol,
|
||||
slot: u32,
|
||||
},
|
||||
|
||||
// Variable Access (Resolved)
|
||||
Get {
|
||||
addr: Address,
|
||||
name: Symbol,
|
||||
},
|
||||
|
||||
// Variable Update (Assignment)
|
||||
Set {
|
||||
addr: Address,
|
||||
value: Box<BoundNode<T>>,
|
||||
},
|
||||
|
||||
// Variable Declaration (Local)
|
||||
DefLocal {
|
||||
name: Symbol,
|
||||
slot: u32,
|
||||
value: Box<BoundNode<T>>,
|
||||
captured_by: Vec<Identity>,
|
||||
},
|
||||
|
||||
If {
|
||||
cond: Box<BoundNode<T>>,
|
||||
then_br: Box<BoundNode<T>>,
|
||||
else_br: Option<Box<BoundNode<T>>>,
|
||||
},
|
||||
|
||||
// Global Definition (Locals are implicit by stack position)
|
||||
DefGlobal {
|
||||
name: Symbol,
|
||||
global_index: u32,
|
||||
value: Box<BoundNode<T>>,
|
||||
},
|
||||
|
||||
Lambda {
|
||||
params: Rc<BoundNode<T>>,
|
||||
// The list of variables captured from enclosing scopes
|
||||
upvalues: Vec<Address>,
|
||||
body: Rc<BoundNode<T>>,
|
||||
/// Static optimization: number of positional parameters if the pattern is flat.
|
||||
positional_count: Option<u32>,
|
||||
},
|
||||
|
||||
Call {
|
||||
callee: Box<BoundNode<T>>,
|
||||
args: Box<BoundNode<T>>,
|
||||
},
|
||||
|
||||
Block {
|
||||
exprs: Vec<BoundNode<T>>,
|
||||
},
|
||||
|
||||
Tuple {
|
||||
elements: Vec<BoundNode<T>>,
|
||||
},
|
||||
|
||||
Record {
|
||||
fields: Vec<RecordField<T>>,
|
||||
},
|
||||
|
||||
/// An expanded macro call, preserving the original call for debugging and UI.
|
||||
Expansion {
|
||||
/// The original call from the untyped AST.
|
||||
original_call: Rc<Node<crate::ast::nodes::UntypedKind>>,
|
||||
/// The result of binding the expanded AST.
|
||||
bound_expanded: Box<BoundNode<T>>,
|
||||
},
|
||||
|
||||
/// DSL-specific extension slot
|
||||
Extension(Box<dyn BoundExtension<T>>),
|
||||
}
|
||||
|
||||
impl<T> PartialEq for BoundKind<T>
|
||||
where T: PartialEq {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self, other) {
|
||||
(BoundKind::Nop, BoundKind::Nop) => true,
|
||||
(BoundKind::Constant(a), BoundKind::Constant(b)) => a == b,
|
||||
(BoundKind::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::Extension(_), BoundKind::Extension(_)) => false, // Currently no equality for extensions
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A single field in a Record literal (Key-Value pair)
|
||||
pub type RecordField<T> = (BoundNode<T>, BoundNode<T>);
|
||||
|
||||
impl<T> BoundKind<T> {
|
||||
pub fn display_name(&self) -> String {
|
||||
match self {
|
||||
BoundKind::Nop => "NOP".to_string(),
|
||||
BoundKind::Constant(v) => format!("CONST({})", v),
|
||||
BoundKind::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::If { .. } => "IF".to_string(),
|
||||
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()),
|
||||
BoundKind::Record { fields } => format!("RECORD({})", fields.len()),
|
||||
BoundKind::Expansion { .. } => "EXPANSION".to_string(),
|
||||
BoundKind::Extension(ext) => ext.display_name(),
|
||||
}
|
||||
}
|
||||
}
|
||||
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 {
|
||||
Local(u32), // Stack-Slot index (relative to frame base)
|
||||
Upvalue(u32), // Index in the closure's upvalue array
|
||||
Global(u32), // Index in the global environment vector
|
||||
}
|
||||
|
||||
/// Trait for DSL-specific AST extensions (like Pipe, Series-Access, etc.)
|
||||
pub trait BoundExtension<T>: std::fmt::Debug {
|
||||
fn clone_box(&self) -> Box<dyn BoundExtension<T>>;
|
||||
fn display_name(&self) -> String;
|
||||
}
|
||||
|
||||
impl<T> Clone for Box<dyn BoundExtension<T>> {
|
||||
fn clone(&self) -> Self {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
/// Type alias for a node that has been bound. Defaults to untyped (T = ()).
|
||||
pub type BoundNode<T = ()> = Node<BoundKind<T>, T>;
|
||||
|
||||
/// Type alias for a node that has been fully type-checked.
|
||||
pub type TypedNode = BoundNode<StaticType>;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum BoundKind<T = ()> {
|
||||
Nop,
|
||||
Constant(Value),
|
||||
|
||||
/// A positional parameter in a Lambda's parameter list.
|
||||
Parameter {
|
||||
name: Symbol,
|
||||
slot: u32,
|
||||
},
|
||||
|
||||
// Variable Access (Resolved)
|
||||
Get {
|
||||
addr: Address,
|
||||
name: Symbol,
|
||||
},
|
||||
|
||||
// Variable Update (Assignment)
|
||||
Set {
|
||||
addr: Address,
|
||||
value: Box<BoundNode<T>>,
|
||||
},
|
||||
|
||||
// Variable Declaration (Local)
|
||||
DefLocal {
|
||||
name: Symbol,
|
||||
slot: u32,
|
||||
value: Box<BoundNode<T>>,
|
||||
captured_by: Vec<Identity>,
|
||||
},
|
||||
|
||||
If {
|
||||
cond: Box<BoundNode<T>>,
|
||||
then_br: Box<BoundNode<T>>,
|
||||
else_br: Option<Box<BoundNode<T>>>,
|
||||
},
|
||||
|
||||
// Global Definition (Locals are implicit by stack position)
|
||||
DefGlobal {
|
||||
name: Symbol,
|
||||
global_index: u32,
|
||||
value: Box<BoundNode<T>>,
|
||||
},
|
||||
|
||||
Lambda {
|
||||
params: Rc<BoundNode<T>>,
|
||||
// The list of variables captured from enclosing scopes
|
||||
upvalues: Vec<Address>,
|
||||
body: Rc<BoundNode<T>>,
|
||||
/// Static optimization: number of positional parameters if the pattern is flat.
|
||||
positional_count: Option<u32>,
|
||||
},
|
||||
|
||||
Call {
|
||||
callee: Box<BoundNode<T>>,
|
||||
args: Box<BoundNode<T>>,
|
||||
},
|
||||
|
||||
Block {
|
||||
exprs: Vec<BoundNode<T>>,
|
||||
},
|
||||
|
||||
Tuple {
|
||||
elements: Vec<BoundNode<T>>,
|
||||
},
|
||||
|
||||
Record {
|
||||
fields: Vec<RecordField<T>>,
|
||||
},
|
||||
|
||||
/// An expanded macro call, preserving the original call for debugging and UI.
|
||||
Expansion {
|
||||
/// The original call from the untyped AST.
|
||||
original_call: Rc<Node<crate::ast::nodes::UntypedKind>>,
|
||||
/// The result of binding the expanded AST.
|
||||
bound_expanded: Box<BoundNode<T>>,
|
||||
},
|
||||
|
||||
/// DSL-specific extension slot
|
||||
Extension(Box<dyn BoundExtension<T>>),
|
||||
}
|
||||
|
||||
impl<T> PartialEq for BoundKind<T>
|
||||
where
|
||||
T: PartialEq,
|
||||
{
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self, other) {
|
||||
(BoundKind::Nop, BoundKind::Nop) => true,
|
||||
(BoundKind::Constant(a), BoundKind::Constant(b)) => a == b,
|
||||
(
|
||||
BoundKind::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::Extension(_), BoundKind::Extension(_)) => false, // Currently no equality for extensions
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A single field in a Record literal (Key-Value pair)
|
||||
pub type RecordField<T> = (BoundNode<T>, BoundNode<T>);
|
||||
|
||||
impl<T> BoundKind<T> {
|
||||
pub fn display_name(&self) -> String {
|
||||
match self {
|
||||
BoundKind::Nop => "NOP".to_string(),
|
||||
BoundKind::Constant(v) => format!("CONST({})", v),
|
||||
BoundKind::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::If { .. } => "IF".to_string(),
|
||||
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()),
|
||||
BoundKind::Record { fields } => format!("RECORD({})", fields.len()),
|
||||
BoundKind::Expansion { .. } => "EXPANSION".to_string(),
|
||||
BoundKind::Extension(ext) => ext.display_name(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+236
-195
@@ -1,195 +1,236 @@
|
||||
use crate::ast::nodes::Node;
|
||||
use crate::ast::compiler::bound_nodes::BoundKind;
|
||||
use crate::ast::types::Value;
|
||||
use crate::ast::vm::Closure;
|
||||
use std::fmt::Debug;
|
||||
|
||||
/// Human-readable AST dumper for the bound AST.
|
||||
pub struct Dumper {
|
||||
output: String,
|
||||
indent: usize,
|
||||
}
|
||||
|
||||
impl Dumper {
|
||||
/// Produces a formatted string representation of the given bound AST node and its children.
|
||||
pub fn dump<T: Debug>(node: &Node<BoundKind<T>, T>) -> String {
|
||||
let mut dumper = Self {
|
||||
output: String::new(),
|
||||
indent: 0,
|
||||
};
|
||||
dumper.visit(node);
|
||||
dumper.output
|
||||
}
|
||||
|
||||
fn write_indent(&mut self) {
|
||||
for _ in 0..self.indent {
|
||||
self.output.push_str(" ");
|
||||
}
|
||||
}
|
||||
|
||||
fn log<T: Debug>(&mut self, label: &str, node: &Node<BoundKind<T>, T>) {
|
||||
self.write_indent();
|
||||
self.output.push_str(label);
|
||||
self.output.push_str(&format!(" <Metadata: {:?}>\n", node.ty));
|
||||
}
|
||||
|
||||
fn visit<T: Debug>(&mut self, node: &Node<BoundKind<T>, T>) {
|
||||
match &node.kind {
|
||||
BoundKind::Nop => self.log("Nop", node),
|
||||
BoundKind::Constant(v) => {
|
||||
self.log(&format!("Constant: {}", v), node);
|
||||
// Introspect Closure AST if possible
|
||||
if let Value::Object(obj) = v
|
||||
&& let Some(closure) = obj.as_any().downcast_ref::<Closure>()
|
||||
{
|
||||
self.indent += 1;
|
||||
self.write_indent();
|
||||
self.output.push_str("--- Specialized Body ---\n");
|
||||
// We need to cast the inner TypedNode to the generic T required by visit.
|
||||
// Since Dumper is generic over T, but Closure stores TypedNode (where T = StaticType),
|
||||
// we can only fully dump if T is StaticType.
|
||||
// However, we can hack it by creating a new Dumper for the inner AST string.
|
||||
|
||||
// We can't call self.visit because types mismatch if T != StaticType.
|
||||
// So we just recursively dump to string and append.
|
||||
let inner_dump = Dumper::dump(&closure.function_node);
|
||||
for line in inner_dump.lines() {
|
||||
self.write_indent();
|
||||
self.output.push_str(line);
|
||||
self.output.push('\n');
|
||||
}
|
||||
self.indent -= 1;
|
||||
}
|
||||
},
|
||||
BoundKind::Get { addr, name } => self.log(&format!("Get: {} ({:?})", name.name, addr), node),
|
||||
|
||||
BoundKind::Set { addr, value } => {
|
||||
self.log(&format!("Set: {:?}", addr), node);
|
||||
self.indent += 1;
|
||||
self.visit(value);
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
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.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.visit(value);
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
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 } => {
|
||||
self.log("If", node);
|
||||
self.indent += 1;
|
||||
|
||||
self.write_indent();
|
||||
self.output.push_str("Condition:\n");
|
||||
self.visit(cond);
|
||||
|
||||
self.write_indent();
|
||||
self.output.push_str("Then:\n");
|
||||
self.visit(then_br);
|
||||
|
||||
if let Some(e) = else_br {
|
||||
self.write_indent();
|
||||
self.output.push_str("Else:\n");
|
||||
self.visit(e);
|
||||
}
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Lambda { params, upvalues, body, .. } => {
|
||||
self.log(&format!("Lambda (Upvalues: {})", upvalues.len()), node);
|
||||
self.indent += 1;
|
||||
|
||||
self.write_indent();
|
||||
self.output.push_str("Parameters:\n");
|
||||
self.visit(params);
|
||||
|
||||
if !upvalues.is_empty() {
|
||||
self.write_indent();
|
||||
self.output.push_str(&format!("Upvalues: {:?}\n", upvalues));
|
||||
}
|
||||
self.visit(body);
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Parameter { name, slot } => {
|
||||
self.log(&format!("Parameter (Name: '{}', Slot: {})", name.name, slot), node);
|
||||
}
|
||||
|
||||
BoundKind::Call { callee, args } => {
|
||||
self.log("Call", node);
|
||||
self.indent += 1;
|
||||
|
||||
self.write_indent();
|
||||
self.output.push_str("Callee:\n");
|
||||
self.visit(callee);
|
||||
|
||||
self.write_indent();
|
||||
self.output.push_str("Arguments:\n");
|
||||
self.visit(args);
|
||||
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Block { exprs } => {
|
||||
self.log("Block", node);
|
||||
self.indent += 1;
|
||||
for expr in exprs {
|
||||
self.visit(expr);
|
||||
}
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Tuple { elements } => {
|
||||
self.log("Tuple", node);
|
||||
self.indent += 1;
|
||||
for el in elements {
|
||||
self.visit(el);
|
||||
}
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Record { fields } => {
|
||||
self.log("Record", node);
|
||||
self.indent += 1;
|
||||
for (k, v) in fields {
|
||||
self.visit(k);
|
||||
self.visit(v);
|
||||
}
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Expansion { original_call, bound_expanded } => {
|
||||
self.log(&format!("Expansion (Original: {:?})", original_call.kind), node);
|
||||
self.indent += 1;
|
||||
self.visit(bound_expanded);
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Extension(ext) => {
|
||||
self.log(&ext.display_name(), node);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
/// Human-readable AST dumper for the bound AST.
|
||||
pub struct Dumper {
|
||||
output: String,
|
||||
indent: usize,
|
||||
}
|
||||
|
||||
impl Dumper {
|
||||
/// Produces a formatted string representation of the given bound AST node and its children.
|
||||
pub fn dump<T: Debug>(node: &Node<BoundKind<T>, T>) -> String {
|
||||
let mut dumper = Self {
|
||||
output: String::new(),
|
||||
indent: 0,
|
||||
};
|
||||
dumper.visit(node);
|
||||
dumper.output
|
||||
}
|
||||
|
||||
fn write_indent(&mut self) {
|
||||
for _ in 0..self.indent {
|
||||
self.output.push_str(" ");
|
||||
}
|
||||
}
|
||||
|
||||
fn log<T: Debug>(&mut self, label: &str, node: &Node<BoundKind<T>, T>) {
|
||||
self.write_indent();
|
||||
self.output.push_str(label);
|
||||
self.output
|
||||
.push_str(&format!(" <Metadata: {:?}>\n", node.ty));
|
||||
}
|
||||
|
||||
fn visit<T: Debug>(&mut self, node: &Node<BoundKind<T>, T>) {
|
||||
match &node.kind {
|
||||
BoundKind::Nop => self.log("Nop", node),
|
||||
BoundKind::Constant(v) => {
|
||||
self.log(&format!("Constant: {}", v), node);
|
||||
// Introspect Closure AST if possible
|
||||
if let Value::Object(obj) = v
|
||||
&& let Some(closure) = obj.as_any().downcast_ref::<Closure>()
|
||||
{
|
||||
self.indent += 1;
|
||||
self.write_indent();
|
||||
self.output.push_str("--- Specialized Body ---\n");
|
||||
// We need to cast the inner TypedNode to the generic T required by visit.
|
||||
// Since Dumper is generic over T, but Closure stores TypedNode (where T = StaticType),
|
||||
// we can only fully dump if T is StaticType.
|
||||
// However, we can hack it by creating a new Dumper for the inner AST string.
|
||||
|
||||
// We can't call self.visit because types mismatch if T != StaticType.
|
||||
// So we just recursively dump to string and append.
|
||||
let inner_dump = Dumper::dump(&closure.function_node);
|
||||
for line in inner_dump.lines() {
|
||||
self.write_indent();
|
||||
self.output.push_str(line);
|
||||
self.output.push('\n');
|
||||
}
|
||||
self.indent -= 1;
|
||||
}
|
||||
}
|
||||
BoundKind::Get { addr, name } => {
|
||||
self.log(&format!("Get: {} ({:?})", name.name, addr), node)
|
||||
}
|
||||
|
||||
BoundKind::Set { addr, value } => {
|
||||
self.log(&format!("Set: {:?}", addr), node);
|
||||
self.indent += 1;
|
||||
self.visit(value);
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
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.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.visit(value);
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
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,
|
||||
} => {
|
||||
self.log("If", node);
|
||||
self.indent += 1;
|
||||
|
||||
self.write_indent();
|
||||
self.output.push_str("Condition:\n");
|
||||
self.visit(cond);
|
||||
|
||||
self.write_indent();
|
||||
self.output.push_str("Then:\n");
|
||||
self.visit(then_br);
|
||||
|
||||
if let Some(e) = else_br {
|
||||
self.write_indent();
|
||||
self.output.push_str("Else:\n");
|
||||
self.visit(e);
|
||||
}
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Lambda {
|
||||
params,
|
||||
upvalues,
|
||||
body,
|
||||
..
|
||||
} => {
|
||||
self.log(&format!("Lambda (Upvalues: {})", upvalues.len()), node);
|
||||
self.indent += 1;
|
||||
|
||||
self.write_indent();
|
||||
self.output.push_str("Parameters:\n");
|
||||
self.visit(params);
|
||||
|
||||
if !upvalues.is_empty() {
|
||||
self.write_indent();
|
||||
self.output.push_str(&format!("Upvalues: {:?}\n", upvalues));
|
||||
}
|
||||
self.visit(body);
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Parameter { name, slot } => {
|
||||
self.log(
|
||||
&format!("Parameter (Name: '{}', Slot: {})", name.name, slot),
|
||||
node,
|
||||
);
|
||||
}
|
||||
|
||||
BoundKind::Call { callee, args } => {
|
||||
self.log("Call", node);
|
||||
self.indent += 1;
|
||||
|
||||
self.write_indent();
|
||||
self.output.push_str("Callee:\n");
|
||||
self.visit(callee);
|
||||
|
||||
self.write_indent();
|
||||
self.output.push_str("Arguments:\n");
|
||||
self.visit(args);
|
||||
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Block { exprs } => {
|
||||
self.log("Block", node);
|
||||
self.indent += 1;
|
||||
for expr in exprs {
|
||||
self.visit(expr);
|
||||
}
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Tuple { elements } => {
|
||||
self.log("Tuple", node);
|
||||
self.indent += 1;
|
||||
for el in elements {
|
||||
self.visit(el);
|
||||
}
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Record { fields } => {
|
||||
self.log("Record", node);
|
||||
self.indent += 1;
|
||||
for (k, v) in fields {
|
||||
self.visit(k);
|
||||
self.visit(v);
|
||||
}
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Expansion {
|
||||
original_call,
|
||||
bound_expanded,
|
||||
} => {
|
||||
self.log(
|
||||
&format!("Expansion (Original: {:?})", original_call.kind),
|
||||
node,
|
||||
);
|
||||
self.indent += 1;
|
||||
self.visit(bound_expanded);
|
||||
self.indent -= 1;
|
||||
}
|
||||
|
||||
BoundKind::Extension(ext) => {
|
||||
self.log(&ext.display_name(), node);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,85 +1,95 @@
|
||||
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.
|
||||
pub struct LambdaCollector<'a, T> {
|
||||
registry: &'a mut HashMap<u32, BoundNode<T>>,
|
||||
}
|
||||
|
||||
impl<'a, T: Clone> LambdaCollector<'a, T> {
|
||||
/// Performs a full traversal of the AST and populates the provided registry.
|
||||
pub fn collect(node: &BoundNode<T>, registry: &'a mut HashMap<u32, BoundNode<T>>) {
|
||||
let mut collector = Self { registry };
|
||||
collector.visit(node);
|
||||
}
|
||||
|
||||
fn visit(&mut self, node: &BoundNode<T>) {
|
||||
match &node.kind {
|
||||
BoundKind::Block { exprs } => {
|
||||
for expr in exprs {
|
||||
self.visit(expr);
|
||||
}
|
||||
}
|
||||
|
||||
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.visit(value);
|
||||
}
|
||||
|
||||
BoundKind::Set { addr, value } => {
|
||||
// Also track assignments to globals if they hold lambdas.
|
||||
if let Address::Global(global_index) = addr
|
||||
&& let BoundKind::Lambda { .. } = &value.kind
|
||||
{
|
||||
self.registry.insert(*global_index, (*value).as_ref().clone());
|
||||
}
|
||||
self.visit(value);
|
||||
}
|
||||
|
||||
BoundKind::If { cond, then_br, else_br } => {
|
||||
self.visit(cond);
|
||||
self.visit(then_br);
|
||||
if let Some(e) = else_br {
|
||||
self.visit(e);
|
||||
}
|
||||
}
|
||||
|
||||
BoundKind::Lambda { params, body, .. } => {
|
||||
self.visit(params);
|
||||
self.visit(body);
|
||||
}
|
||||
|
||||
BoundKind::DefLocal { value, .. } => {
|
||||
self.visit(value);
|
||||
}
|
||||
|
||||
BoundKind::Call { callee, args } => {
|
||||
self.visit(callee);
|
||||
self.visit(args);
|
||||
}
|
||||
|
||||
BoundKind::Tuple { elements } => {
|
||||
for el in elements {
|
||||
self.visit(el);
|
||||
}
|
||||
}
|
||||
|
||||
BoundKind::Record { fields } => {
|
||||
for (k, v) in fields {
|
||||
self.visit(k);
|
||||
self.visit(v);
|
||||
}
|
||||
}
|
||||
|
||||
BoundKind::Expansion { bound_expanded, .. } => {
|
||||
self.visit(bound_expanded);
|
||||
}
|
||||
|
||||
_ => {} // Leaf nodes
|
||||
}
|
||||
}
|
||||
}
|
||||
use crate::ast::compiler::bound_nodes::{Address, BoundKind, BoundNode};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// A pass that collects all global function definitions (lambdas) into a registry.
|
||||
/// This allows the Specializer to retrieve the original AST of a function for monomorphization.
|
||||
pub struct LambdaCollector<'a, T> {
|
||||
registry: &'a mut HashMap<u32, BoundNode<T>>,
|
||||
}
|
||||
|
||||
impl<'a, T: Clone> LambdaCollector<'a, T> {
|
||||
/// Performs a full traversal of the AST and populates the provided registry.
|
||||
pub fn collect(node: &BoundNode<T>, registry: &'a mut HashMap<u32, BoundNode<T>>) {
|
||||
let mut collector = Self { registry };
|
||||
collector.visit(node);
|
||||
}
|
||||
|
||||
fn visit(&mut self, node: &BoundNode<T>) {
|
||||
match &node.kind {
|
||||
BoundKind::Block { exprs } => {
|
||||
for expr in exprs {
|
||||
self.visit(expr);
|
||||
}
|
||||
}
|
||||
|
||||
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.visit(value);
|
||||
}
|
||||
|
||||
BoundKind::Set { addr, value } => {
|
||||
// Also track assignments to globals if they hold lambdas.
|
||||
if let Address::Global(global_index) = addr
|
||||
&& let BoundKind::Lambda { .. } = &value.kind
|
||||
{
|
||||
self.registry
|
||||
.insert(*global_index, (*value).as_ref().clone());
|
||||
}
|
||||
self.visit(value);
|
||||
}
|
||||
|
||||
BoundKind::If {
|
||||
cond,
|
||||
then_br,
|
||||
else_br,
|
||||
} => {
|
||||
self.visit(cond);
|
||||
self.visit(then_br);
|
||||
if let Some(e) = else_br {
|
||||
self.visit(e);
|
||||
}
|
||||
}
|
||||
|
||||
BoundKind::Lambda { params, body, .. } => {
|
||||
self.visit(params);
|
||||
self.visit(body);
|
||||
}
|
||||
|
||||
BoundKind::DefLocal { value, .. } => {
|
||||
self.visit(value);
|
||||
}
|
||||
|
||||
BoundKind::Call { callee, args } => {
|
||||
self.visit(callee);
|
||||
self.visit(args);
|
||||
}
|
||||
|
||||
BoundKind::Tuple { elements } => {
|
||||
for el in elements {
|
||||
self.visit(el);
|
||||
}
|
||||
}
|
||||
|
||||
BoundKind::Record { fields } => {
|
||||
for (k, v) in fields {
|
||||
self.visit(k);
|
||||
self.visit(v);
|
||||
}
|
||||
}
|
||||
|
||||
BoundKind::Expansion { bound_expanded, .. } => {
|
||||
self.visit(bound_expanded);
|
||||
}
|
||||
|
||||
_ => {} // Leaf nodes
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+765
-609
File diff suppressed because it is too large
Load Diff
+20
-20
@@ -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 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 mod binder;
|
||||
pub mod bound_nodes;
|
||||
pub mod dumper;
|
||||
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 dumper::*;
|
||||
pub use macros::*;
|
||||
pub use optimizer::*;
|
||||
pub use specializer::*;
|
||||
pub use tco::*;
|
||||
pub use type_checker::*;
|
||||
pub use upvalues::*;
|
||||
|
||||
+1344
-877
File diff suppressed because it is too large
Load Diff
+313
-232
@@ -1,232 +1,313 @@
|
||||
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 {
|
||||
pub address: Address,
|
||||
pub arg_types: Vec<StaticType>,
|
||||
}
|
||||
|
||||
pub type CompileFunc = Rc<dyn Fn(BoundNode, &[StaticType]) -> Result<(Value, StaticType), String>>;
|
||||
pub type RtlLookupFunc = Rc<dyn Fn(&str, &[StaticType]) -> Option<(Value, StaticType)>>;
|
||||
|
||||
pub trait FunctionRegistry {
|
||||
fn resolve(&self, addr: Address) -> Option<BoundNode>;
|
||||
}
|
||||
|
||||
pub type MonoCache = HashMap<MonoCacheKey, (Value, StaticType)>;
|
||||
|
||||
pub struct Specializer {
|
||||
pub cache: Rc<RefCell<MonoCache>>,
|
||||
registry: Option<Rc<dyn FunctionRegistry>>,
|
||||
compiler: Option<CompileFunc>,
|
||||
rtl_lookup: Option<RtlLookupFunc>,
|
||||
}
|
||||
|
||||
impl Specializer {
|
||||
pub fn new(
|
||||
registry: Option<Rc<dyn FunctionRegistry>>,
|
||||
compiler: Option<CompileFunc>,
|
||||
rtl_lookup: Option<RtlLookupFunc>,
|
||||
cache: Option<Rc<RefCell<MonoCache>>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
cache: cache.unwrap_or_else(|| Rc::new(RefCell::new(HashMap::new()))),
|
||||
registry,
|
||||
compiler,
|
||||
rtl_lookup,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn specialize(&self, node: TypedNode) -> TypedNode {
|
||||
self.visit_node(node)
|
||||
}
|
||||
|
||||
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)
|
||||
},
|
||||
|
||||
// Recursive traversal for other nodes
|
||||
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::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 } => {
|
||||
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::DefLocal { name, slot, value, captured_by } => {
|
||||
let value = Box::new(self.visit_node(*value));
|
||||
(BoundKind::DefLocal { name, slot, value, captured_by }, 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::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();
|
||||
(BoundKind::Record { fields }, node.ty)
|
||||
},
|
||||
BoundKind::Expansion { original_call, bound_expanded } => {
|
||||
let bound_expanded = Box::new(self.visit_node(*bound_expanded));
|
||||
(BoundKind::Expansion { original_call, bound_expanded }, node.ty)
|
||||
},
|
||||
|
||||
// Leaf nodes or uninteresting nodes
|
||||
k => (k, node.ty),
|
||||
};
|
||||
|
||||
Node {
|
||||
identity: node.identity,
|
||||
kind: new_kind,
|
||||
ty: new_ty,
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// 2. Check if this call is a candidate (Callee is Get(Address))
|
||||
let address = if let BoundKind::Get { addr, .. } = &new_callee.kind {
|
||||
*addr
|
||||
} else {
|
||||
// Not a direct call to a named function/variable
|
||||
return (new_callee, new_args, original_ty);
|
||||
};
|
||||
|
||||
// 3. Check if all argument types are statically known
|
||||
let arg_types: Vec<StaticType> = if let StaticType::Tuple(elements) = &new_args.ty {
|
||||
elements.clone()
|
||||
} else {
|
||||
vec![new_args.ty.clone()]
|
||||
};
|
||||
|
||||
if arg_types.iter().any(|t| matches!(t, StaticType::Any)) {
|
||||
// Cannot specialize with unknown types
|
||||
return (new_callee, new_args, original_ty);
|
||||
}
|
||||
|
||||
// --- Optimization Candidate ---
|
||||
let key = MonoCacheKey { address, arg_types: arg_types.clone() };
|
||||
|
||||
// 4. Check Cache
|
||||
if let Some((val, ret_ty)) = self.cache.borrow().get(&key) {
|
||||
// Cache Hit! Replace Callee with Constant(Function)
|
||||
let specialized_callee = Node {
|
||||
identity: new_callee.identity.clone(),
|
||||
kind: BoundKind::Constant(val.clone()),
|
||||
ty: StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(arg_types),
|
||||
ret: ret_ty.clone(),
|
||||
})),
|
||||
};
|
||||
return (specialized_callee, new_args, ret_ty.clone());
|
||||
}
|
||||
|
||||
// 5. Check RTL (Host Functions)
|
||||
if let Some(rtl_lookup) = &self.rtl_lookup
|
||||
&& let BoundKind::Get { name, .. } = &new_callee.kind
|
||||
&& let Some((val, ret_ty)) = rtl_lookup(&name.name, &arg_types)
|
||||
{
|
||||
// Cache Hit (RTL)
|
||||
self.cache.borrow_mut().insert(key.clone(), (val.clone(), ret_ty.clone()));
|
||||
|
||||
let specialized_callee = Node {
|
||||
identity: new_callee.identity.clone(),
|
||||
kind: BoundKind::Constant(val.clone()),
|
||||
ty: StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(arg_types),
|
||||
ret: ret_ty.clone(),
|
||||
})),
|
||||
};
|
||||
return (specialized_callee, new_args, ret_ty);
|
||||
}
|
||||
|
||||
// 6. Resolve Function Definition
|
||||
if let Some(func_node) = self.registry.as_ref().and_then(|r| r.resolve(address)) {
|
||||
// Check constraints (no closures with state)
|
||||
if let BoundKind::Lambda { upvalues, .. } = &func_node.kind {
|
||||
if !upvalues.is_empty() {
|
||||
return (new_callee, new_args, original_ty);
|
||||
}
|
||||
} else {
|
||||
return (new_callee, new_args, original_ty);
|
||||
}
|
||||
|
||||
// 7. Compile Specialization (User Code)
|
||||
if let Some(compiler) = &self.compiler {
|
||||
match compiler(func_node, &arg_types) {
|
||||
Ok((compiled_val, ret_ty)) => {
|
||||
let res_val: Value = compiled_val;
|
||||
let res_ty: StaticType = ret_ty;
|
||||
|
||||
// Store in cache
|
||||
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 },
|
||||
ty: StaticType::Tuple(flat_types),
|
||||
};
|
||||
|
||||
let specialized_callee = Node {
|
||||
identity: new_callee.identity.clone(),
|
||||
kind: BoundKind::Constant(res_val),
|
||||
ty: StaticType::Function(Box::new(Signature {
|
||||
params: flattened_args.ty.clone(),
|
||||
ret: res_ty.clone(),
|
||||
})),
|
||||
};
|
||||
return (specialized_callee, flattened_args, res_ty);
|
||||
},
|
||||
Err(_) => {
|
||||
// Fallback on error
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: Dynamic Call
|
||||
(new_callee, new_args, original_ty)
|
||||
}
|
||||
|
||||
fn flatten_tuple(&self, node: TypedNode) -> Vec<TypedNode> {
|
||||
match node.kind {
|
||||
BoundKind::Tuple { elements } => {
|
||||
let mut flat = Vec::new();
|
||||
for el in elements {
|
||||
flat.extend(self.flatten_tuple(el));
|
||||
}
|
||||
flat
|
||||
}
|
||||
_ => vec![node],
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct MonoCacheKey {
|
||||
pub address: Address,
|
||||
pub arg_types: Vec<StaticType>,
|
||||
}
|
||||
|
||||
pub type CompileFunc = Rc<dyn Fn(BoundNode, &[StaticType]) -> Result<(Value, StaticType), String>>;
|
||||
pub type RtlLookupFunc = Rc<dyn Fn(&str, &[StaticType]) -> Option<(Value, StaticType)>>;
|
||||
|
||||
pub trait FunctionRegistry {
|
||||
fn resolve(&self, addr: Address) -> Option<BoundNode>;
|
||||
}
|
||||
|
||||
pub type MonoCache = HashMap<MonoCacheKey, (Value, StaticType)>;
|
||||
|
||||
pub struct Specializer {
|
||||
pub cache: Rc<RefCell<MonoCache>>,
|
||||
registry: Option<Rc<dyn FunctionRegistry>>,
|
||||
compiler: Option<CompileFunc>,
|
||||
rtl_lookup: Option<RtlLookupFunc>,
|
||||
}
|
||||
|
||||
impl Specializer {
|
||||
pub fn new(
|
||||
registry: Option<Rc<dyn FunctionRegistry>>,
|
||||
compiler: Option<CompileFunc>,
|
||||
rtl_lookup: Option<RtlLookupFunc>,
|
||||
cache: Option<Rc<RefCell<MonoCache>>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
cache: cache.unwrap_or_else(|| Rc::new(RefCell::new(HashMap::new()))),
|
||||
registry,
|
||||
compiler,
|
||||
rtl_lookup,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn specialize(&self, node: TypedNode) -> TypedNode {
|
||||
self.visit_node(node)
|
||||
}
|
||||
|
||||
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,
|
||||
)
|
||||
}
|
||||
|
||||
// Recursive traversal for other nodes
|
||||
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::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,
|
||||
} => {
|
||||
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::DefLocal {
|
||||
name,
|
||||
slot,
|
||||
value,
|
||||
captured_by,
|
||||
} => {
|
||||
let value = Box::new(self.visit_node(*value));
|
||||
(
|
||||
BoundKind::DefLocal {
|
||||
name,
|
||||
slot,
|
||||
value,
|
||||
captured_by,
|
||||
},
|
||||
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::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();
|
||||
(BoundKind::Record { fields }, node.ty)
|
||||
}
|
||||
BoundKind::Expansion {
|
||||
original_call,
|
||||
bound_expanded,
|
||||
} => {
|
||||
let bound_expanded = Box::new(self.visit_node(*bound_expanded));
|
||||
(
|
||||
BoundKind::Expansion {
|
||||
original_call,
|
||||
bound_expanded,
|
||||
},
|
||||
node.ty,
|
||||
)
|
||||
}
|
||||
|
||||
// Leaf nodes or uninteresting nodes
|
||||
k => (k, node.ty),
|
||||
};
|
||||
|
||||
Node {
|
||||
identity: node.identity,
|
||||
kind: new_kind,
|
||||
ty: new_ty,
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// 2. Check if this call is a candidate (Callee is Get(Address))
|
||||
let address = if let BoundKind::Get { addr, .. } = &new_callee.kind {
|
||||
*addr
|
||||
} else {
|
||||
// Not a direct call to a named function/variable
|
||||
return (new_callee, new_args, original_ty);
|
||||
};
|
||||
|
||||
// 3. Check if all argument types are statically known
|
||||
let arg_types: Vec<StaticType> = if let StaticType::Tuple(elements) = &new_args.ty {
|
||||
elements.clone()
|
||||
} else {
|
||||
vec![new_args.ty.clone()]
|
||||
};
|
||||
|
||||
if arg_types.iter().any(|t| matches!(t, StaticType::Any)) {
|
||||
// Cannot specialize with unknown types
|
||||
return (new_callee, new_args, original_ty);
|
||||
}
|
||||
|
||||
// --- Optimization Candidate ---
|
||||
let key = MonoCacheKey {
|
||||
address,
|
||||
arg_types: arg_types.clone(),
|
||||
};
|
||||
|
||||
// 4. Check Cache
|
||||
if let Some((val, ret_ty)) = self.cache.borrow().get(&key) {
|
||||
// Cache Hit! Replace Callee with Constant(Function)
|
||||
let specialized_callee = Node {
|
||||
identity: new_callee.identity.clone(),
|
||||
kind: BoundKind::Constant(val.clone()),
|
||||
ty: StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(arg_types),
|
||||
ret: ret_ty.clone(),
|
||||
})),
|
||||
};
|
||||
return (specialized_callee, new_args, ret_ty.clone());
|
||||
}
|
||||
|
||||
// 5. Check RTL (Host Functions)
|
||||
if let Some(rtl_lookup) = &self.rtl_lookup
|
||||
&& let BoundKind::Get { name, .. } = &new_callee.kind
|
||||
&& let Some((val, ret_ty)) = rtl_lookup(&name.name, &arg_types)
|
||||
{
|
||||
// Cache Hit (RTL)
|
||||
self.cache
|
||||
.borrow_mut()
|
||||
.insert(key.clone(), (val.clone(), ret_ty.clone()));
|
||||
|
||||
let specialized_callee = Node {
|
||||
identity: new_callee.identity.clone(),
|
||||
kind: BoundKind::Constant(val.clone()),
|
||||
ty: StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(arg_types),
|
||||
ret: ret_ty.clone(),
|
||||
})),
|
||||
};
|
||||
return (specialized_callee, new_args, ret_ty);
|
||||
}
|
||||
|
||||
// 6. Resolve Function Definition
|
||||
if let Some(func_node) = self.registry.as_ref().and_then(|r| r.resolve(address)) {
|
||||
// Check constraints (no closures with state)
|
||||
if let BoundKind::Lambda { upvalues, .. } = &func_node.kind {
|
||||
if !upvalues.is_empty() {
|
||||
return (new_callee, new_args, original_ty);
|
||||
}
|
||||
} else {
|
||||
return (new_callee, new_args, original_ty);
|
||||
}
|
||||
|
||||
// 7. Compile Specialization (User Code)
|
||||
if let Some(compiler) = &self.compiler {
|
||||
match compiler(func_node, &arg_types) {
|
||||
Ok((compiled_val, ret_ty)) => {
|
||||
let res_val: Value = compiled_val;
|
||||
let res_ty: StaticType = ret_ty;
|
||||
|
||||
// Store in cache
|
||||
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,
|
||||
},
|
||||
ty: StaticType::Tuple(flat_types),
|
||||
};
|
||||
|
||||
let specialized_callee = Node {
|
||||
identity: new_callee.identity.clone(),
|
||||
kind: BoundKind::Constant(res_val),
|
||||
ty: StaticType::Function(Box::new(Signature {
|
||||
params: flattened_args.ty.clone(),
|
||||
ret: res_ty.clone(),
|
||||
})),
|
||||
};
|
||||
return (specialized_callee, flattened_args, res_ty);
|
||||
}
|
||||
Err(_) => {
|
||||
// Fallback on error
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: Dynamic Call
|
||||
(new_callee, new_args, original_ty)
|
||||
}
|
||||
|
||||
fn flatten_tuple(&self, node: TypedNode) -> Vec<TypedNode> {
|
||||
match node.kind {
|
||||
BoundKind::Tuple { elements } => {
|
||||
let mut flat = Vec::new();
|
||||
for el in elements {
|
||||
flat.extend(self.flatten_tuple(el));
|
||||
}
|
||||
flat
|
||||
}
|
||||
_ => vec![node],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+167
-131
@@ -1,131 +1,167 @@
|
||||
use std::rc::Rc;
|
||||
use crate::ast::nodes::Node;
|
||||
use crate::ast::compiler::bound_nodes::{BoundKind, TypedNode};
|
||||
use crate::ast::types::StaticType;
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RuntimeMetadata {
|
||||
pub ty: StaticType,
|
||||
pub is_tail: bool,
|
||||
/// The original, high-level typed node. Perfect for Debuggers and Optimizers.
|
||||
pub original: Rc<TypedNode>,
|
||||
}
|
||||
|
||||
impl Debug for RuntimeMetadata {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Metadata")
|
||||
.field("ty", &self.ty)
|
||||
.field("is_tail", &self.is_tail)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
/// The ExecNode is the AST used by the VM. It carries TCO flags and links to source.
|
||||
pub type ExecNode = Node<BoundKind<RuntimeMetadata>, RuntimeMetadata>;
|
||||
|
||||
pub struct TCO;
|
||||
|
||||
impl TCO {
|
||||
/// Lowers a TypedNode (Compiler-AST) to an ExecNode (VM-AST) and marks tail positions.
|
||||
pub fn optimize(node: TypedNode) -> ExecNode {
|
||||
Self::transform(Rc::new(node), true)
|
||||
}
|
||||
|
||||
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 {
|
||||
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: Box::new(Self::transform(Rc::new((**cond).clone()), false)),
|
||||
then_br: Box::new(Self::transform(Rc::new((**then_br).clone()), is_tail_position)),
|
||||
else_br: else_br.as_ref().map(|e| Box::new(Self::transform(Rc::new((**e).clone()), is_tail_position))),
|
||||
}
|
||||
},
|
||||
|
||||
BoundKind::Block { exprs } => {
|
||||
if exprs.is_empty() {
|
||||
BoundKind::Block { exprs: vec![] }
|
||||
} else {
|
||||
let last_idx = exprs.len() - 1;
|
||||
let mut new_exprs = Vec::with_capacity(exprs.len());
|
||||
|
||||
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));
|
||||
}
|
||||
BoundKind::Block { exprs: new_exprs }
|
||||
}
|
||||
},
|
||||
|
||||
BoundKind::Lambda { params, upvalues, body, positional_count } => {
|
||||
BoundKind::Lambda {
|
||||
params: Rc::new(Self::transform(params.clone(), false)),
|
||||
upvalues: upvalues.clone(),
|
||||
body: Rc::new(Self::transform(body.clone(), true)),
|
||||
positional_count: *positional_count,
|
||||
}
|
||||
},
|
||||
|
||||
BoundKind::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: name.clone(),
|
||||
slot: *slot,
|
||||
value: Box::new(Self::transform(Rc::new((**value).clone()), false)),
|
||||
captured_by: captured_by.clone()
|
||||
}
|
||||
},
|
||||
BoundKind::DefGlobal { name, global_index, value } => {
|
||||
BoundKind::DefGlobal {
|
||||
name: name.clone(),
|
||||
global_index: *global_index,
|
||||
value: Box::new(Self::transform(Rc::new((**value).clone()), false))
|
||||
}
|
||||
},
|
||||
BoundKind::Record { fields } => {
|
||||
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 }
|
||||
},
|
||||
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::Nop => BoundKind::Nop,
|
||||
BoundKind::Expansion { original_call, bound_expanded } => {
|
||||
BoundKind::Expansion {
|
||||
original_call: original_call.clone(),
|
||||
bound_expanded: Box::new(Self::transform(Rc::new((**bound_expanded).clone()), is_tail_position))
|
||||
}
|
||||
}
|
||||
BoundKind::Extension(_) => {
|
||||
BoundKind::Nop
|
||||
}
|
||||
};
|
||||
|
||||
Node {
|
||||
identity: node.identity.clone(),
|
||||
kind: new_kind,
|
||||
ty: RuntimeMetadata {
|
||||
ty: node.ty.clone(),
|
||||
is_tail: is_tail_position,
|
||||
original: node_rc,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RuntimeMetadata {
|
||||
pub ty: StaticType,
|
||||
pub is_tail: bool,
|
||||
/// The original, high-level typed node. Perfect for Debuggers and Optimizers.
|
||||
pub original: Rc<TypedNode>,
|
||||
}
|
||||
|
||||
impl Debug for RuntimeMetadata {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Metadata")
|
||||
.field("ty", &self.ty)
|
||||
.field("is_tail", &self.is_tail)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
/// The ExecNode is the AST used by the VM. It carries TCO flags and links to source.
|
||||
pub type ExecNode = Node<BoundKind<RuntimeMetadata>, RuntimeMetadata>;
|
||||
|
||||
pub struct TCO;
|
||||
|
||||
impl TCO {
|
||||
/// Lowers a TypedNode (Compiler-AST) to an ExecNode (VM-AST) and marks tail positions.
|
||||
pub fn optimize(node: TypedNode) -> ExecNode {
|
||||
Self::transform(Rc::new(node), true)
|
||||
}
|
||||
|
||||
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 {
|
||||
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: Box::new(Self::transform(Rc::new((**cond).clone()), false)),
|
||||
then_br: Box::new(Self::transform(
|
||||
Rc::new((**then_br).clone()),
|
||||
is_tail_position,
|
||||
)),
|
||||
else_br: else_br
|
||||
.as_ref()
|
||||
.map(|e| Box::new(Self::transform(Rc::new((**e).clone()), is_tail_position))),
|
||||
},
|
||||
|
||||
BoundKind::Block { exprs } => {
|
||||
if exprs.is_empty() {
|
||||
BoundKind::Block { exprs: vec![] }
|
||||
} else {
|
||||
let last_idx = exprs.len() - 1;
|
||||
let mut new_exprs = Vec::with_capacity(exprs.len());
|
||||
|
||||
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,
|
||||
));
|
||||
}
|
||||
BoundKind::Block { exprs: new_exprs }
|
||||
}
|
||||
}
|
||||
|
||||
BoundKind::Lambda {
|
||||
params,
|
||||
upvalues,
|
||||
body,
|
||||
positional_count,
|
||||
} => BoundKind::Lambda {
|
||||
params: Rc::new(Self::transform(params.clone(), false)),
|
||||
upvalues: upvalues.clone(),
|
||||
body: Rc::new(Self::transform(body.clone(), true)),
|
||||
positional_count: *positional_count,
|
||||
},
|
||||
|
||||
BoundKind::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: name.clone(),
|
||||
slot: *slot,
|
||||
value: Box::new(Self::transform(Rc::new((**value).clone()), false)),
|
||||
captured_by: captured_by.clone(),
|
||||
},
|
||||
BoundKind::DefGlobal {
|
||||
name,
|
||||
global_index,
|
||||
value,
|
||||
} => BoundKind::DefGlobal {
|
||||
name: name.clone(),
|
||||
global_index: *global_index,
|
||||
value: Box::new(Self::transform(Rc::new((**value).clone()), false)),
|
||||
},
|
||||
BoundKind::Record { fields } => {
|
||||
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,
|
||||
}
|
||||
}
|
||||
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::Nop => BoundKind::Nop,
|
||||
BoundKind::Expansion {
|
||||
original_call,
|
||||
bound_expanded,
|
||||
} => BoundKind::Expansion {
|
||||
original_call: original_call.clone(),
|
||||
bound_expanded: Box::new(Self::transform(
|
||||
Rc::new((**bound_expanded).clone()),
|
||||
is_tail_position,
|
||||
)),
|
||||
},
|
||||
BoundKind::Extension(_) => BoundKind::Nop,
|
||||
};
|
||||
|
||||
Node {
|
||||
identity: node.identity.clone(),
|
||||
kind: new_kind,
|
||||
ty: RuntimeMetadata {
|
||||
ty: node.ty.clone(),
|
||||
is_tail: is_tail_position,
|
||||
original: node_rc,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+650
-491
File diff suppressed because it is too large
Load Diff
+132
-121
@@ -1,121 +1,132 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use crate::ast::nodes::{Node, UntypedKind, Symbol};
|
||||
use crate::ast::types::Identity;
|
||||
|
||||
/// Analyzes the AST to find which lambdas capture which variable declarations.
|
||||
/// Returns a map: Declaration Identity -> List of Lambda Identities that capture it.
|
||||
pub struct UpvalueAnalyzer;
|
||||
|
||||
impl UpvalueAnalyzer {
|
||||
pub fn analyze(root: &Node<UntypedKind>) -> HashMap<Identity, Vec<Identity>> {
|
||||
let mut capture_map: HashMap<Identity, HashSet<Identity>> = HashMap::new();
|
||||
let mut scopes = vec![HashMap::new()]; // Root scope
|
||||
|
||||
Self::visit(root, &mut scopes, &mut capture_map, None);
|
||||
|
||||
// Convert HashSet back to sorted Vec for the final result
|
||||
capture_map.into_iter()
|
||||
.map(|(decl, lambdas)| (decl, lambdas.into_iter().collect()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn visit_params(node: &Node<UntypedKind>, scope: &mut HashMap<Symbol, Identity>) {
|
||||
match &node.kind {
|
||||
UntypedKind::Parameter(sym) => {
|
||||
scope.insert(sym.clone(), node.identity.clone());
|
||||
}
|
||||
UntypedKind::Tuple { elements } => {
|
||||
for el in elements {
|
||||
Self::visit_params(el, scope);
|
||||
}
|
||||
}
|
||||
_ => {} // Ignore other nodes in parameter patterns for now
|
||||
}
|
||||
}
|
||||
|
||||
fn visit(
|
||||
node: &Node<UntypedKind>,
|
||||
scopes: &mut Vec<HashMap<Symbol, Identity>>,
|
||||
capture_map: &mut HashMap<Identity, HashSet<Identity>>,
|
||||
current_lambda: Option<Identity>
|
||||
) {
|
||||
match &node.kind {
|
||||
UntypedKind::Identifier(sym) => {
|
||||
// Resolve name in scope stack (from inner to outer)
|
||||
for (depth, scope) in scopes.iter().rev().enumerate() {
|
||||
if let Some(decl_id) = scope.get(sym) {
|
||||
if depth > 0 {
|
||||
// Captured from an outer scope!
|
||||
if let Some(lambda_id) = ¤t_lambda {
|
||||
capture_map.entry(decl_id.clone())
|
||||
.or_default()
|
||||
.insert(lambda_id.clone());
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
UntypedKind::Def { name, value } => {
|
||||
Self::visit(value, scopes, capture_map, current_lambda.clone());
|
||||
if let Some(current) = scopes.last_mut() {
|
||||
current.insert(name.clone(), node.identity.clone());
|
||||
}
|
||||
}
|
||||
UntypedKind::Lambda { params, body } => {
|
||||
let mut new_scope = HashMap::new();
|
||||
Self::visit_params(params, &mut new_scope);
|
||||
|
||||
scopes.push(new_scope);
|
||||
// The current node is the lambda causing captures in its body
|
||||
Self::visit(body, scopes, capture_map, Some(node.identity.clone()));
|
||||
scopes.pop();
|
||||
}
|
||||
UntypedKind::MacroDecl { params, body, .. } => {
|
||||
let mut new_scope = HashMap::new();
|
||||
Self::visit_params(params, &mut new_scope);
|
||||
scopes.push(new_scope);
|
||||
Self::visit(body, scopes, capture_map, current_lambda);
|
||||
scopes.pop();
|
||||
}
|
||||
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 {
|
||||
Self::visit(e, scopes, capture_map, current_lambda.clone());
|
||||
}
|
||||
}
|
||||
UntypedKind::Assign { target, value } => {
|
||||
Self::visit(target, scopes, capture_map, current_lambda.clone());
|
||||
Self::visit(value, scopes, capture_map, current_lambda.clone());
|
||||
}
|
||||
UntypedKind::Call { callee, args } => {
|
||||
Self::visit(callee, scopes, capture_map, current_lambda.clone());
|
||||
Self::visit(args, scopes, capture_map, current_lambda.clone());
|
||||
}
|
||||
UntypedKind::Block { exprs } => {
|
||||
for expr in exprs {
|
||||
Self::visit(expr, scopes, capture_map, current_lambda.clone());
|
||||
}
|
||||
}
|
||||
UntypedKind::Tuple { elements } => {
|
||||
for el in elements {
|
||||
Self::visit(el, scopes, capture_map, current_lambda.clone());
|
||||
}
|
||||
}
|
||||
UntypedKind::Record { fields } => {
|
||||
for (k, v) in fields {
|
||||
Self::visit(k, scopes, capture_map, current_lambda.clone());
|
||||
Self::visit(v, scopes, capture_map, current_lambda.clone());
|
||||
}
|
||||
}
|
||||
UntypedKind::Expansion { call: _, expanded } => {
|
||||
Self::visit(expanded, scopes, capture_map, current_lambda);
|
||||
}
|
||||
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(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
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.
|
||||
pub struct UpvalueAnalyzer;
|
||||
|
||||
impl UpvalueAnalyzer {
|
||||
pub fn analyze(root: &Node<UntypedKind>) -> HashMap<Identity, Vec<Identity>> {
|
||||
let mut capture_map: HashMap<Identity, HashSet<Identity>> = HashMap::new();
|
||||
let mut scopes = vec![HashMap::new()]; // Root scope
|
||||
|
||||
Self::visit(root, &mut scopes, &mut capture_map, None);
|
||||
|
||||
// Convert HashSet back to sorted Vec for the final result
|
||||
capture_map
|
||||
.into_iter()
|
||||
.map(|(decl, lambdas)| (decl, lambdas.into_iter().collect()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn visit_params(node: &Node<UntypedKind>, scope: &mut HashMap<Symbol, Identity>) {
|
||||
match &node.kind {
|
||||
UntypedKind::Parameter(sym) => {
|
||||
scope.insert(sym.clone(), node.identity.clone());
|
||||
}
|
||||
UntypedKind::Tuple { elements } => {
|
||||
for el in elements {
|
||||
Self::visit_params(el, scope);
|
||||
}
|
||||
}
|
||||
_ => {} // Ignore other nodes in parameter patterns for now
|
||||
}
|
||||
}
|
||||
|
||||
fn visit(
|
||||
node: &Node<UntypedKind>,
|
||||
scopes: &mut Vec<HashMap<Symbol, Identity>>,
|
||||
capture_map: &mut HashMap<Identity, HashSet<Identity>>,
|
||||
current_lambda: Option<Identity>,
|
||||
) {
|
||||
match &node.kind {
|
||||
UntypedKind::Identifier(sym) => {
|
||||
// Resolve name in scope stack (from inner to outer)
|
||||
for (depth, scope) in scopes.iter().rev().enumerate() {
|
||||
if let Some(decl_id) = scope.get(sym) {
|
||||
if depth > 0 {
|
||||
// Captured from an outer scope!
|
||||
if let Some(lambda_id) = ¤t_lambda {
|
||||
capture_map
|
||||
.entry(decl_id.clone())
|
||||
.or_default()
|
||||
.insert(lambda_id.clone());
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
UntypedKind::Def { name, value } => {
|
||||
Self::visit(value, scopes, capture_map, current_lambda.clone());
|
||||
if let Some(current) = scopes.last_mut() {
|
||||
current.insert(name.clone(), node.identity.clone());
|
||||
}
|
||||
}
|
||||
UntypedKind::Lambda { params, body } => {
|
||||
let mut new_scope = HashMap::new();
|
||||
Self::visit_params(params, &mut new_scope);
|
||||
|
||||
scopes.push(new_scope);
|
||||
// The current node is the lambda causing captures in its body
|
||||
Self::visit(body, scopes, capture_map, Some(node.identity.clone()));
|
||||
scopes.pop();
|
||||
}
|
||||
UntypedKind::MacroDecl { params, body, .. } => {
|
||||
let mut new_scope = HashMap::new();
|
||||
Self::visit_params(params, &mut new_scope);
|
||||
scopes.push(new_scope);
|
||||
Self::visit(body, scopes, capture_map, current_lambda);
|
||||
scopes.pop();
|
||||
}
|
||||
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 {
|
||||
Self::visit(e, scopes, capture_map, current_lambda.clone());
|
||||
}
|
||||
}
|
||||
UntypedKind::Assign { target, value } => {
|
||||
Self::visit(target, scopes, capture_map, current_lambda.clone());
|
||||
Self::visit(value, scopes, capture_map, current_lambda.clone());
|
||||
}
|
||||
UntypedKind::Call { callee, args } => {
|
||||
Self::visit(callee, scopes, capture_map, current_lambda.clone());
|
||||
Self::visit(args, scopes, capture_map, current_lambda.clone());
|
||||
}
|
||||
UntypedKind::Block { exprs } => {
|
||||
for expr in exprs {
|
||||
Self::visit(expr, scopes, capture_map, current_lambda.clone());
|
||||
}
|
||||
}
|
||||
UntypedKind::Tuple { elements } => {
|
||||
for el in elements {
|
||||
Self::visit(el, scopes, capture_map, current_lambda.clone());
|
||||
}
|
||||
}
|
||||
UntypedKind::Record { fields } => {
|
||||
for (k, v) in fields {
|
||||
Self::visit(k, scopes, capture_map, current_lambda.clone());
|
||||
Self::visit(v, scopes, capture_map, current_lambda.clone());
|
||||
}
|
||||
}
|
||||
UntypedKind::Expansion { call: _, expanded } => {
|
||||
Self::visit(expanded, scopes, capture_map, current_lambda);
|
||||
}
|
||||
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(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+355
-355
@@ -1,355 +1,355 @@
|
||||
use crate::ast::compiler::binder::Binder;
|
||||
use crate::ast::compiler::{TypeChecker, TypedNode};
|
||||
use crate::ast::nodes::{Node, Symbol, UntypedKind};
|
||||
use crate::ast::parser::Parser;
|
||||
use crate::ast::types::{Object, StaticType, Value};
|
||||
use crate::ast::vm::{TracingObserver, VM};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::ast::compiler::bound_nodes::{Address, BoundNode};
|
||||
use crate::ast::compiler::dumper::Dumper;
|
||||
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::rtl;
|
||||
use crate::ast::rtl::intrinsics;
|
||||
|
||||
pub struct Environment {
|
||||
pub global_names: Rc<RefCell<HashMap<Symbol, u32>>>,
|
||||
pub global_types: Rc<RefCell<HashMap<u32, StaticType>>>,
|
||||
pub global_purity: Rc<RefCell<HashMap<u32, bool>>>,
|
||||
pub global_values: Rc<RefCell<Vec<Value>>>,
|
||||
pub function_registry: Rc<RefCell<HashMap<u32, BoundNode>>>,
|
||||
pub typed_function_registry: Rc<RefCell<HashMap<u32, TypedNode>>>,
|
||||
pub monomorph_cache: Rc<RefCell<MonoCache>>,
|
||||
pub debug_mode: bool,
|
||||
pub optimization: bool,
|
||||
}
|
||||
|
||||
struct EnvFunctionRegistry {
|
||||
registry: Rc<RefCell<HashMap<u32, BoundNode>>>,
|
||||
}
|
||||
|
||||
impl FunctionRegistry for EnvFunctionRegistry {
|
||||
fn resolve(&self, addr: Address) -> Option<BoundNode> {
|
||||
if let Address::Global(idx) = addr {
|
||||
self.registry.borrow().get(&idx).cloned()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Evaluator used during macro expansion to allow compile-time logic.
|
||||
struct RuntimeMacroEvaluator {
|
||||
global_names: Rc<RefCell<HashMap<Symbol, u32>>>,
|
||||
global_types: Rc<RefCell<HashMap<u32, StaticType>>>,
|
||||
global_values: Rc<RefCell<Vec<Value>>>,
|
||||
}
|
||||
|
||||
impl MacroEvaluator for RuntimeMacroEvaluator {
|
||||
fn evaluate(
|
||||
&self,
|
||||
node: &Node<UntypedKind>,
|
||||
bindings: &HashMap<Rc<str>, Node<UntypedKind>>,
|
||||
) -> Result<Value, String> {
|
||||
// 1. Check if it's a simple parameter substitution
|
||||
if let UntypedKind::Identifier(sym) = &node.kind
|
||||
&& let Some(arg_node) = bindings.get(&sym.name)
|
||||
{
|
||||
return Ok(Value::Object(Rc::new(arg_node.clone()) as Rc<dyn Object>));
|
||||
}
|
||||
|
||||
// 2. Full evaluation for complex compile-time expressions
|
||||
let bound_ast = Binder::bind_root(self.global_names.clone(), node)?;
|
||||
|
||||
let checker = TypeChecker::new(self.global_types.clone());
|
||||
let typed_ast = checker.check(bound_ast, &[])?;
|
||||
let exec_ast = TCO::optimize(typed_ast);
|
||||
|
||||
let mut vm = VM::new(self.global_values.clone());
|
||||
vm.run(&exec_ast)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Environment {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Environment {
|
||||
pub fn new() -> Self {
|
||||
let env = Self {
|
||||
global_names: Rc::new(RefCell::new(HashMap::new())),
|
||||
global_types: Rc::new(RefCell::new(HashMap::new())),
|
||||
global_purity: Rc::new(RefCell::new(HashMap::new())),
|
||||
global_values: Rc::new(RefCell::new(Vec::new())),
|
||||
function_registry: Rc::new(RefCell::new(HashMap::new())),
|
||||
typed_function_registry: Rc::new(RefCell::new(HashMap::new())),
|
||||
monomorph_cache: Rc::new(RefCell::new(HashMap::new())),
|
||||
debug_mode: false,
|
||||
optimization: true,
|
||||
};
|
||||
env.register_stdlib();
|
||||
env
|
||||
}
|
||||
|
||||
pub fn set_debug_mode(&mut self, enabled: bool) {
|
||||
self.debug_mode = enabled;
|
||||
}
|
||||
|
||||
fn get_expander(&self) -> MacroExpander<RuntimeMacroEvaluator> {
|
||||
let evaluator = RuntimeMacroEvaluator {
|
||||
global_names: self.global_names.clone(),
|
||||
global_types: self.global_types.clone(),
|
||||
global_values: self.global_values.clone(),
|
||||
};
|
||||
MacroExpander::new(MacroRegistry::new(), evaluator)
|
||||
}
|
||||
|
||||
pub fn register_native(
|
||||
&self,
|
||||
name: &str,
|
||||
ty: StaticType,
|
||||
is_pure: bool,
|
||||
func: impl Fn(Vec<Value>) -> Value + 'static,
|
||||
) {
|
||||
let mut names = self.global_names.borrow_mut();
|
||||
let mut types = self.global_types.borrow_mut();
|
||||
let mut values = self.global_values.borrow_mut();
|
||||
let mut purity = self.global_purity.borrow_mut();
|
||||
|
||||
let idx = values.len() as u32;
|
||||
names.insert(Symbol::from(name), idx);
|
||||
types.insert(idx, ty);
|
||||
purity.insert(idx, is_pure);
|
||||
values.push(Value::Function(Rc::new(func)));
|
||||
}
|
||||
|
||||
pub fn register_constant(&self, name: &str, ty: StaticType, val: Value) {
|
||||
let mut names = self.global_names.borrow_mut();
|
||||
let mut types = self.global_types.borrow_mut();
|
||||
let mut values = self.global_values.borrow_mut();
|
||||
let mut purity = self.global_purity.borrow_mut();
|
||||
|
||||
let idx = values.len() as u32;
|
||||
names.insert(Symbol::from(name), idx);
|
||||
types.insert(idx, ty);
|
||||
purity.insert(idx, true); // Constants are always pure
|
||||
values.push(val);
|
||||
}
|
||||
|
||||
fn register_stdlib(&self) {
|
||||
// Register all standard library functions via RTL module
|
||||
rtl::register(self);
|
||||
}
|
||||
|
||||
pub fn dump_ast(&self, source: &str) -> Result<String, String> {
|
||||
let compiled = self.compile(source)?;
|
||||
let linked = self.link(compiled);
|
||||
Ok(Dumper::dump(&linked))
|
||||
}
|
||||
|
||||
/// Frontend: Parse -> Expand Macros -> Bind -> Type Check
|
||||
pub fn compile(&self, source: &str) -> Result<TypedNode, String> {
|
||||
// 1. Parse
|
||||
let mut parser = Parser::new(source)?;
|
||||
let untyped_ast = parser.parse_expression()?;
|
||||
|
||||
// 2. Check for trailing tokens
|
||||
if !parser.at_eof() {
|
||||
return Err(
|
||||
"Unexpected trailing expressions in script. Use (do ...) for sequences."
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
// 3. Expand Macros
|
||||
let expanded_ast = self.get_expander().expand(untyped_ast)?;
|
||||
|
||||
// 4. Bind
|
||||
let bound_ast = Binder::bind_root(self.global_names.clone(), &expanded_ast)?;
|
||||
|
||||
// 5. Collect Lambdas (Populate the registry with untyped templates)
|
||||
LambdaCollector::collect(&bound_ast, &mut self.function_registry.borrow_mut());
|
||||
|
||||
// 6. Type Check
|
||||
let checker = TypeChecker::new(self.global_types.clone());
|
||||
let typed_ast = checker.check(bound_ast, &[])?;
|
||||
|
||||
// 7. Collect Typed Lambdas
|
||||
LambdaCollector::collect(&typed_ast, &mut self.typed_function_registry.borrow_mut());
|
||||
|
||||
Ok(typed_ast)
|
||||
}
|
||||
|
||||
/// Backend: Optimization (TCO, etc.)
|
||||
pub fn link(&self, node: TypedNode) -> ExecNode {
|
||||
// 1. Specialize (Always performed for correctness)
|
||||
let specialized = self.specialize_node(node);
|
||||
|
||||
// 2. Optimize (Level 1: Cracking, Level 2: Collapsing)
|
||||
let optimizer = Optimizer::new(self.optimization)
|
||||
.with_globals(self.global_values.clone())
|
||||
.with_purity(self.global_purity.clone())
|
||||
.with_registry(self.typed_function_registry.clone());
|
||||
let optimized = optimizer.optimize(specialized);
|
||||
|
||||
// 3. TCO (Always performed, converts to ExecNode)
|
||||
TCO::optimize(optimized)
|
||||
}
|
||||
|
||||
fn specialize_node(&self, node: TypedNode) -> TypedNode {
|
||||
let registry = Rc::new(EnvFunctionRegistry {
|
||||
registry: self.function_registry.clone(),
|
||||
});
|
||||
|
||||
let rtl_lookup = Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args));
|
||||
|
||||
let func_reg = self.function_registry.clone();
|
||||
let mono_cache = self.monomorph_cache.clone();
|
||||
let global_values = self.global_values.clone();
|
||||
let global_types = self.global_types.clone();
|
||||
let global_purity = self.global_purity.clone();
|
||||
let optimization = self.optimization;
|
||||
|
||||
let compiler = Rc::new(
|
||||
move |func_template: BoundNode,
|
||||
arg_types: &[StaticType]|
|
||||
-> Result<(Value, StaticType), String> {
|
||||
// 1. Re-TypeCheck the template with concrete argument types
|
||||
let checker = TypeChecker::new(global_types.clone());
|
||||
let retyped_ast = checker.check(func_template, arg_types)?;
|
||||
|
||||
// 2. Specialize (Recursive)
|
||||
let sub_registry = Rc::new(EnvFunctionRegistry {
|
||||
registry: func_reg.clone(),
|
||||
});
|
||||
let sub_rtl_lookup =
|
||||
Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args));
|
||||
|
||||
let sub_specializer = Specializer::new(
|
||||
Some(sub_registry),
|
||||
None,
|
||||
Some(sub_rtl_lookup),
|
||||
Some(mono_cache.clone()),
|
||||
);
|
||||
|
||||
let specialized_ast = sub_specializer.specialize(retyped_ast);
|
||||
|
||||
// 3. Optimize (Phase 2: Cracking & Folding)
|
||||
let optimizer = Optimizer::new(optimization)
|
||||
.with_globals(global_values.clone())
|
||||
.with_purity(global_purity.clone());
|
||||
let optimized_ast = optimizer.optimize(specialized_ast);
|
||||
|
||||
// 4. TCO (converts to ExecNode)
|
||||
let tco_ast = TCO::optimize(optimized_ast);
|
||||
|
||||
// 5. Compile to Value (VM)
|
||||
let mut vm = VM::new(global_values.clone());
|
||||
let compiled_val = match vm.run(&tco_ast) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Err(format!("VM Error during specialization: {}", e)),
|
||||
};
|
||||
|
||||
// 6. Determine correct return type from the newly inferred function signature
|
||||
let ret_type = if let StaticType::Function(sig) = &tco_ast.ty.ty {
|
||||
sig.ret.clone()
|
||||
} else {
|
||||
StaticType::Any
|
||||
};
|
||||
|
||||
Ok((compiled_val, ret_type))
|
||||
},
|
||||
);
|
||||
|
||||
let specializer = Specializer::new(
|
||||
Some(registry),
|
||||
Some(compiler),
|
||||
Some(rtl_lookup),
|
||||
Some(self.monomorph_cache.clone()),
|
||||
);
|
||||
|
||||
specializer.specialize(node)
|
||||
}
|
||||
|
||||
/// Runtime: Execute the linked AST in the VM
|
||||
pub fn run(&self, node: &ExecNode) -> Result<Value, String> {
|
||||
let mut vm = VM::new(self.global_values.clone());
|
||||
let mut result = vm.run(node)?;
|
||||
|
||||
// Handle potential script body closure
|
||||
if let Value::Object(obj) = &result
|
||||
&& let Some(closure) = obj.as_any().downcast_ref::<crate::ast::vm::Closure>()
|
||||
{
|
||||
result = vm.run(&closure.exec_node)?;
|
||||
}
|
||||
|
||||
// IMPORTANT: Resolve any pending tail call requests from the top-level execution
|
||||
while let Value::TailCallRequest(payload) = result {
|
||||
let (next_obj, next_args) = *payload;
|
||||
if let Some(closure) = next_obj.as_any().downcast_ref::<crate::ast::vm::Closure>() {
|
||||
result = vm.run_with_args(closure, next_args)?;
|
||||
} else {
|
||||
return Err(format!(
|
||||
"Tail call target is not a closure: {}",
|
||||
next_obj.type_name()
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn run_script(&self, source: &str) -> Result<Value, String> {
|
||||
if self.debug_mode {
|
||||
let (res, logs) = self.run_debug(source)?;
|
||||
for line in logs {
|
||||
println!("{}", line);
|
||||
}
|
||||
res
|
||||
} else {
|
||||
let compiled = self.compile(source)?;
|
||||
let linked = self.link(compiled);
|
||||
self.run(&linked)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_debug(&self, source: &str) -> Result<(Result<Value, String>, Vec<String>), String> {
|
||||
let compiled = self.compile(source)?;
|
||||
let linked = self.link(compiled);
|
||||
|
||||
// Execute with TracingObserver
|
||||
let mut vm = VM::new(self.global_values.clone());
|
||||
let mut observer = TracingObserver::new();
|
||||
let mut result = vm.run_with_observer(&mut observer, &linked);
|
||||
|
||||
// If result is a closure (script entry), execute the body too
|
||||
if let Ok(Value::Object(obj)) = &result
|
||||
&& let Some(closure) = obj.as_any().downcast_ref::<crate::ast::vm::Closure>()
|
||||
{
|
||||
result = vm.run_with_observer(&mut observer, &closure.exec_node);
|
||||
}
|
||||
|
||||
// Resolve top-level tail calls
|
||||
while let Ok(Value::TailCallRequest(payload)) = result {
|
||||
let (next_obj, next_args) = *payload;
|
||||
if let Some(closure) = next_obj.as_any().downcast_ref::<crate::ast::vm::Closure>() {
|
||||
result = vm.run_with_args_observed(&mut observer, closure, next_args);
|
||||
} else {
|
||||
result = Err(format!(
|
||||
"Tail call target is not a closure: {}",
|
||||
next_obj.type_name()
|
||||
));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok((result, observer.logs))
|
||||
}
|
||||
}
|
||||
use crate::ast::compiler::binder::Binder;
|
||||
use crate::ast::compiler::{TypeChecker, TypedNode};
|
||||
use crate::ast::nodes::{Node, Symbol, UntypedKind};
|
||||
use crate::ast::parser::Parser;
|
||||
use crate::ast::types::{Object, StaticType, Value};
|
||||
use crate::ast::vm::{TracingObserver, VM};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::ast::compiler::bound_nodes::{Address, BoundNode};
|
||||
use crate::ast::compiler::dumper::Dumper;
|
||||
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::{ExecNode, TCO};
|
||||
use crate::ast::rtl;
|
||||
use crate::ast::rtl::intrinsics;
|
||||
|
||||
pub struct Environment {
|
||||
pub global_names: Rc<RefCell<HashMap<Symbol, u32>>>,
|
||||
pub global_types: Rc<RefCell<HashMap<u32, StaticType>>>,
|
||||
pub global_purity: Rc<RefCell<HashMap<u32, bool>>>,
|
||||
pub global_values: Rc<RefCell<Vec<Value>>>,
|
||||
pub function_registry: Rc<RefCell<HashMap<u32, BoundNode>>>,
|
||||
pub typed_function_registry: Rc<RefCell<HashMap<u32, TypedNode>>>,
|
||||
pub monomorph_cache: Rc<RefCell<MonoCache>>,
|
||||
pub debug_mode: bool,
|
||||
pub optimization: bool,
|
||||
}
|
||||
|
||||
struct EnvFunctionRegistry {
|
||||
registry: Rc<RefCell<HashMap<u32, BoundNode>>>,
|
||||
}
|
||||
|
||||
impl FunctionRegistry for EnvFunctionRegistry {
|
||||
fn resolve(&self, addr: Address) -> Option<BoundNode> {
|
||||
if let Address::Global(idx) = addr {
|
||||
self.registry.borrow().get(&idx).cloned()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Evaluator used during macro expansion to allow compile-time logic.
|
||||
struct RuntimeMacroEvaluator {
|
||||
global_names: Rc<RefCell<HashMap<Symbol, u32>>>,
|
||||
global_types: Rc<RefCell<HashMap<u32, StaticType>>>,
|
||||
global_values: Rc<RefCell<Vec<Value>>>,
|
||||
}
|
||||
|
||||
impl MacroEvaluator for RuntimeMacroEvaluator {
|
||||
fn evaluate(
|
||||
&self,
|
||||
node: &Node<UntypedKind>,
|
||||
bindings: &HashMap<Rc<str>, Node<UntypedKind>>,
|
||||
) -> Result<Value, String> {
|
||||
// 1. Check if it's a simple parameter substitution
|
||||
if let UntypedKind::Identifier(sym) = &node.kind
|
||||
&& let Some(arg_node) = bindings.get(&sym.name)
|
||||
{
|
||||
return Ok(Value::Object(Rc::new(arg_node.clone()) as Rc<dyn Object>));
|
||||
}
|
||||
|
||||
// 2. Full evaluation for complex compile-time expressions
|
||||
let bound_ast = Binder::bind_root(self.global_names.clone(), node)?;
|
||||
|
||||
let checker = TypeChecker::new(self.global_types.clone());
|
||||
let typed_ast = checker.check(bound_ast, &[])?;
|
||||
let exec_ast = TCO::optimize(typed_ast);
|
||||
|
||||
let mut vm = VM::new(self.global_values.clone());
|
||||
vm.run(&exec_ast)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Environment {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Environment {
|
||||
pub fn new() -> Self {
|
||||
let env = Self {
|
||||
global_names: Rc::new(RefCell::new(HashMap::new())),
|
||||
global_types: Rc::new(RefCell::new(HashMap::new())),
|
||||
global_purity: Rc::new(RefCell::new(HashMap::new())),
|
||||
global_values: Rc::new(RefCell::new(Vec::new())),
|
||||
function_registry: Rc::new(RefCell::new(HashMap::new())),
|
||||
typed_function_registry: Rc::new(RefCell::new(HashMap::new())),
|
||||
monomorph_cache: Rc::new(RefCell::new(HashMap::new())),
|
||||
debug_mode: false,
|
||||
optimization: true,
|
||||
};
|
||||
env.register_stdlib();
|
||||
env
|
||||
}
|
||||
|
||||
pub fn set_debug_mode(&mut self, enabled: bool) {
|
||||
self.debug_mode = enabled;
|
||||
}
|
||||
|
||||
fn get_expander(&self) -> MacroExpander<RuntimeMacroEvaluator> {
|
||||
let evaluator = RuntimeMacroEvaluator {
|
||||
global_names: self.global_names.clone(),
|
||||
global_types: self.global_types.clone(),
|
||||
global_values: self.global_values.clone(),
|
||||
};
|
||||
MacroExpander::new(MacroRegistry::new(), evaluator)
|
||||
}
|
||||
|
||||
pub fn register_native(
|
||||
&self,
|
||||
name: &str,
|
||||
ty: StaticType,
|
||||
is_pure: bool,
|
||||
func: impl Fn(Vec<Value>) -> Value + 'static,
|
||||
) {
|
||||
let mut names = self.global_names.borrow_mut();
|
||||
let mut types = self.global_types.borrow_mut();
|
||||
let mut values = self.global_values.borrow_mut();
|
||||
let mut purity = self.global_purity.borrow_mut();
|
||||
|
||||
let idx = values.len() as u32;
|
||||
names.insert(Symbol::from(name), idx);
|
||||
types.insert(idx, ty);
|
||||
purity.insert(idx, is_pure);
|
||||
values.push(Value::Function(Rc::new(func)));
|
||||
}
|
||||
|
||||
pub fn register_constant(&self, name: &str, ty: StaticType, val: Value) {
|
||||
let mut names = self.global_names.borrow_mut();
|
||||
let mut types = self.global_types.borrow_mut();
|
||||
let mut values = self.global_values.borrow_mut();
|
||||
let mut purity = self.global_purity.borrow_mut();
|
||||
|
||||
let idx = values.len() as u32;
|
||||
names.insert(Symbol::from(name), idx);
|
||||
types.insert(idx, ty);
|
||||
purity.insert(idx, true); // Constants are always pure
|
||||
values.push(val);
|
||||
}
|
||||
|
||||
fn register_stdlib(&self) {
|
||||
// Register all standard library functions via RTL module
|
||||
rtl::register(self);
|
||||
}
|
||||
|
||||
pub fn dump_ast(&self, source: &str) -> Result<String, String> {
|
||||
let compiled = self.compile(source)?;
|
||||
let linked = self.link(compiled);
|
||||
Ok(Dumper::dump(&linked))
|
||||
}
|
||||
|
||||
/// Frontend: Parse -> Expand Macros -> Bind -> Type Check
|
||||
pub fn compile(&self, source: &str) -> Result<TypedNode, String> {
|
||||
// 1. Parse
|
||||
let mut parser = Parser::new(source)?;
|
||||
let untyped_ast = parser.parse_expression()?;
|
||||
|
||||
// 2. Check for trailing tokens
|
||||
if !parser.at_eof() {
|
||||
return Err(
|
||||
"Unexpected trailing expressions in script. Use (do ...) for sequences."
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
// 3. Expand Macros
|
||||
let expanded_ast = self.get_expander().expand(untyped_ast)?;
|
||||
|
||||
// 4. Bind
|
||||
let bound_ast = Binder::bind_root(self.global_names.clone(), &expanded_ast)?;
|
||||
|
||||
// 5. Collect Lambdas (Populate the registry with untyped templates)
|
||||
LambdaCollector::collect(&bound_ast, &mut self.function_registry.borrow_mut());
|
||||
|
||||
// 6. Type Check
|
||||
let checker = TypeChecker::new(self.global_types.clone());
|
||||
let typed_ast = checker.check(bound_ast, &[])?;
|
||||
|
||||
// 7. Collect Typed Lambdas
|
||||
LambdaCollector::collect(&typed_ast, &mut self.typed_function_registry.borrow_mut());
|
||||
|
||||
Ok(typed_ast)
|
||||
}
|
||||
|
||||
/// Backend: Optimization (TCO, etc.)
|
||||
pub fn link(&self, node: TypedNode) -> ExecNode {
|
||||
// 1. Specialize (Always performed for correctness)
|
||||
let specialized = self.specialize_node(node);
|
||||
|
||||
// 2. Optimize (Level 1: Cracking, Level 2: Collapsing)
|
||||
let optimizer = Optimizer::new(self.optimization)
|
||||
.with_globals(self.global_values.clone())
|
||||
.with_purity(self.global_purity.clone())
|
||||
.with_registry(self.typed_function_registry.clone());
|
||||
let optimized = optimizer.optimize(specialized);
|
||||
|
||||
// 3. TCO (Always performed, converts to ExecNode)
|
||||
TCO::optimize(optimized)
|
||||
}
|
||||
|
||||
fn specialize_node(&self, node: TypedNode) -> TypedNode {
|
||||
let registry = Rc::new(EnvFunctionRegistry {
|
||||
registry: self.function_registry.clone(),
|
||||
});
|
||||
|
||||
let rtl_lookup = Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args));
|
||||
|
||||
let func_reg = self.function_registry.clone();
|
||||
let mono_cache = self.monomorph_cache.clone();
|
||||
let global_values = self.global_values.clone();
|
||||
let global_types = self.global_types.clone();
|
||||
let global_purity = self.global_purity.clone();
|
||||
let optimization = self.optimization;
|
||||
|
||||
let compiler = Rc::new(
|
||||
move |func_template: BoundNode,
|
||||
arg_types: &[StaticType]|
|
||||
-> Result<(Value, StaticType), String> {
|
||||
// 1. Re-TypeCheck the template with concrete argument types
|
||||
let checker = TypeChecker::new(global_types.clone());
|
||||
let retyped_ast = checker.check(func_template, arg_types)?;
|
||||
|
||||
// 2. Specialize (Recursive)
|
||||
let sub_registry = Rc::new(EnvFunctionRegistry {
|
||||
registry: func_reg.clone(),
|
||||
});
|
||||
let sub_rtl_lookup =
|
||||
Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args));
|
||||
|
||||
let sub_specializer = Specializer::new(
|
||||
Some(sub_registry),
|
||||
None,
|
||||
Some(sub_rtl_lookup),
|
||||
Some(mono_cache.clone()),
|
||||
);
|
||||
|
||||
let specialized_ast = sub_specializer.specialize(retyped_ast);
|
||||
|
||||
// 3. Optimize (Phase 2: Cracking & Folding)
|
||||
let optimizer = Optimizer::new(optimization)
|
||||
.with_globals(global_values.clone())
|
||||
.with_purity(global_purity.clone());
|
||||
let optimized_ast = optimizer.optimize(specialized_ast);
|
||||
|
||||
// 4. TCO (converts to ExecNode)
|
||||
let tco_ast = TCO::optimize(optimized_ast);
|
||||
|
||||
// 5. Compile to Value (VM)
|
||||
let mut vm = VM::new(global_values.clone());
|
||||
let compiled_val = match vm.run(&tco_ast) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Err(format!("VM Error during specialization: {}", e)),
|
||||
};
|
||||
|
||||
// 6. Determine correct return type from the newly inferred function signature
|
||||
let ret_type = if let StaticType::Function(sig) = &tco_ast.ty.ty {
|
||||
sig.ret.clone()
|
||||
} else {
|
||||
StaticType::Any
|
||||
};
|
||||
|
||||
Ok((compiled_val, ret_type))
|
||||
},
|
||||
);
|
||||
|
||||
let specializer = Specializer::new(
|
||||
Some(registry),
|
||||
Some(compiler),
|
||||
Some(rtl_lookup),
|
||||
Some(self.monomorph_cache.clone()),
|
||||
);
|
||||
|
||||
specializer.specialize(node)
|
||||
}
|
||||
|
||||
/// Runtime: Execute the linked AST in the VM
|
||||
pub fn run(&self, node: &ExecNode) -> Result<Value, String> {
|
||||
let mut vm = VM::new(self.global_values.clone());
|
||||
let mut result = vm.run(node)?;
|
||||
|
||||
// Handle potential script body closure
|
||||
if let Value::Object(obj) = &result
|
||||
&& let Some(closure) = obj.as_any().downcast_ref::<crate::ast::vm::Closure>()
|
||||
{
|
||||
result = vm.run(&closure.exec_node)?;
|
||||
}
|
||||
|
||||
// IMPORTANT: Resolve any pending tail call requests from the top-level execution
|
||||
while let Value::TailCallRequest(payload) = result {
|
||||
let (next_obj, next_args) = *payload;
|
||||
if let Some(closure) = next_obj.as_any().downcast_ref::<crate::ast::vm::Closure>() {
|
||||
result = vm.run_with_args(closure, next_args)?;
|
||||
} else {
|
||||
return Err(format!(
|
||||
"Tail call target is not a closure: {}",
|
||||
next_obj.type_name()
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn run_script(&self, source: &str) -> Result<Value, String> {
|
||||
if self.debug_mode {
|
||||
let (res, logs) = self.run_debug(source)?;
|
||||
for line in logs {
|
||||
println!("{}", line);
|
||||
}
|
||||
res
|
||||
} else {
|
||||
let compiled = self.compile(source)?;
|
||||
let linked = self.link(compiled);
|
||||
self.run(&linked)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_debug(&self, source: &str) -> Result<(Result<Value, String>, Vec<String>), String> {
|
||||
let compiled = self.compile(source)?;
|
||||
let linked = self.link(compiled);
|
||||
|
||||
// Execute with TracingObserver
|
||||
let mut vm = VM::new(self.global_values.clone());
|
||||
let mut observer = TracingObserver::new();
|
||||
let mut result = vm.run_with_observer(&mut observer, &linked);
|
||||
|
||||
// If result is a closure (script entry), execute the body too
|
||||
if let Ok(Value::Object(obj)) = &result
|
||||
&& let Some(closure) = obj.as_any().downcast_ref::<crate::ast::vm::Closure>()
|
||||
{
|
||||
result = vm.run_with_observer(&mut observer, &closure.exec_node);
|
||||
}
|
||||
|
||||
// Resolve top-level tail calls
|
||||
while let Ok(Value::TailCallRequest(payload)) = result {
|
||||
let (next_obj, next_args) = *payload;
|
||||
if let Some(closure) = next_obj.as_any().downcast_ref::<crate::ast::vm::Closure>() {
|
||||
result = vm.run_with_args_observed(&mut observer, closure, next_args);
|
||||
} else {
|
||||
result = Err(format!(
|
||||
"Tail call target is not a closure: {}",
|
||||
next_obj.type_name()
|
||||
));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok((result, observer.logs))
|
||||
}
|
||||
}
|
||||
|
||||
+219
-191
@@ -1,191 +1,219 @@
|
||||
use std::iter::Peekable;
|
||||
use std::str::Chars;
|
||||
use std::rc::Rc;
|
||||
use crate::ast::types::SourceLocation;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum TokenKind {
|
||||
LeftParen, RightParen,
|
||||
LeftBracket, RightBracket,
|
||||
LeftBrace, RightBrace,
|
||||
Quote, Backtick, Tilde, At,
|
||||
Identifier(Rc<str>),
|
||||
Keyword(Rc<str>),
|
||||
Integer(i64),
|
||||
Float(f64),
|
||||
String(Rc<str>),
|
||||
EOF,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Token {
|
||||
pub kind: TokenKind,
|
||||
pub location: SourceLocation,
|
||||
}
|
||||
|
||||
pub struct Lexer<'a> {
|
||||
input: Peekable<Chars<'a>>,
|
||||
line: u32,
|
||||
col: u32,
|
||||
}
|
||||
|
||||
impl<'a> Lexer<'a> {
|
||||
pub fn new(input: &'a str) -> Self {
|
||||
Self {
|
||||
input: input.chars().peekable(),
|
||||
line: 1,
|
||||
col: 1,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_token(&mut self) -> Result<Token, String> {
|
||||
self.skip_whitespace();
|
||||
|
||||
let start_location = SourceLocation { line: self.line, col: self.col };
|
||||
|
||||
let char = match self.input.next() {
|
||||
Some(c) => {
|
||||
let current_char = c;
|
||||
self.col += 1;
|
||||
current_char
|
||||
},
|
||||
None => return Ok(Token { kind: TokenKind::EOF, location: start_location }),
|
||||
};
|
||||
|
||||
let kind = match char {
|
||||
'(' => TokenKind::LeftParen,
|
||||
')' => TokenKind::RightParen,
|
||||
'[' => TokenKind::LeftBracket,
|
||||
']' => TokenKind::RightBracket,
|
||||
'{' => TokenKind::LeftBrace,
|
||||
'}' => TokenKind::RightBrace,
|
||||
'\'' => TokenKind::Quote,
|
||||
'`' => TokenKind::Backtick,
|
||||
'~' => TokenKind::Tilde,
|
||||
'@' => TokenKind::At,
|
||||
':' => {
|
||||
let id = self.read_while(|c| !c.is_whitespace() && !"()[]{}\"'`~@;".contains(c));
|
||||
TokenKind::Keyword(Rc::from(id))
|
||||
}
|
||||
'"' => TokenKind::String(Rc::from(self.read_string()?)),
|
||||
c if c.is_ascii_digit() || (c == '-' && self.input.peek().is_some_and(|p| p.is_ascii_digit())) => {
|
||||
let mut num_str = String::from(c);
|
||||
while let Some(&next) = self.peek() {
|
||||
if next.is_ascii_digit() || next == '.' {
|
||||
num_str.push(self.input.next().unwrap());
|
||||
self.col += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if num_str.contains('.') {
|
||||
TokenKind::Float(num_str.parse().map_err(|_| "Invalid float format")?)
|
||||
} else {
|
||||
TokenKind::Integer(num_str.parse().map_err(|_| "Invalid integer format")?)
|
||||
}
|
||||
}
|
||||
c if is_ident_start(c) => {
|
||||
let mut id = String::from(c);
|
||||
id.push_str(&self.read_while(is_ident_char));
|
||||
TokenKind::Identifier(Rc::from(id))
|
||||
}
|
||||
_ => return Err(format!("Unexpected character: {} at {}:{}", char, self.line, self.col - 1)),
|
||||
};
|
||||
|
||||
Ok(Token { kind, location: start_location })
|
||||
}
|
||||
|
||||
fn peek(&mut self) -> Option<&char> {
|
||||
self.input.peek()
|
||||
}
|
||||
|
||||
fn skip_whitespace(&mut self) {
|
||||
while let Some(&c) = self.peek() {
|
||||
if c.is_whitespace() || is_invisible(c) || c == ',' {
|
||||
if c == '\n' {
|
||||
self.line += 1;
|
||||
self.col = 1;
|
||||
} else {
|
||||
self.col += 1;
|
||||
}
|
||||
self.input.next();
|
||||
} else if c == ';' {
|
||||
for c in self.input.by_ref() {
|
||||
if c == '\n' {
|
||||
self.line += 1;
|
||||
self.col = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_while<F>(&mut self, mut predicate: F) -> String
|
||||
where F: FnMut(char) -> bool {
|
||||
let mut s = String::new();
|
||||
while let Some(&c) = self.peek() {
|
||||
if predicate(c) {
|
||||
s.push(self.input.next().unwrap());
|
||||
self.col += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn read_string(&mut self) -> Result<String, String> {
|
||||
let mut s = String::new();
|
||||
while let Some(c) = self.input.next() {
|
||||
self.col += 1;
|
||||
match c {
|
||||
'"' => return Ok(s),
|
||||
'\\' => {
|
||||
let next = self.input.next().ok_or("Unterminated string escape")?;
|
||||
self.col += 1;
|
||||
match next {
|
||||
'n' => s.push('\n'),
|
||||
'r' => s.push('\r'),
|
||||
't' => s.push('\t'),
|
||||
'\\' => s.push('\\'),
|
||||
'"' => s.push('"'),
|
||||
_ => s.push(next),
|
||||
}
|
||||
}
|
||||
'\n' => {
|
||||
self.line += 1;
|
||||
self.col = 1;
|
||||
s.push(c);
|
||||
}
|
||||
_ => s.push(c),
|
||||
}
|
||||
}
|
||||
Err("Unterminated string".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn is_ident_start(c: char) -> bool {
|
||||
c.is_alphabetic() || "+-*/<=>!$%&_?.".contains(c)
|
||||
}
|
||||
|
||||
fn is_ident_char(c: char) -> bool {
|
||||
is_ident_start(c) || c.is_ascii_digit()
|
||||
}
|
||||
|
||||
/// Returns true if the character is an invisible control character
|
||||
/// that should be ignored by the lexer (like BOM or Zero Width Space).
|
||||
fn is_invisible(c: char) -> bool {
|
||||
match c {
|
||||
'\u{FEFF}' | // BOM
|
||||
'\u{200B}' | // Zero Width Space
|
||||
'\u{200C}' | // Zero Width Non-Joiner
|
||||
'\u{200D}' | // Zero Width Joiner
|
||||
'\u{2060}' // Word Joiner
|
||||
=> true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
use crate::ast::types::SourceLocation;
|
||||
use std::iter::Peekable;
|
||||
use std::rc::Rc;
|
||||
use std::str::Chars;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum TokenKind {
|
||||
LeftParen,
|
||||
RightParen,
|
||||
LeftBracket,
|
||||
RightBracket,
|
||||
LeftBrace,
|
||||
RightBrace,
|
||||
Quote,
|
||||
Backtick,
|
||||
Tilde,
|
||||
At,
|
||||
Identifier(Rc<str>),
|
||||
Keyword(Rc<str>),
|
||||
Integer(i64),
|
||||
Float(f64),
|
||||
String(Rc<str>),
|
||||
EOF,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Token {
|
||||
pub kind: TokenKind,
|
||||
pub location: SourceLocation,
|
||||
}
|
||||
|
||||
pub struct Lexer<'a> {
|
||||
input: Peekable<Chars<'a>>,
|
||||
line: u32,
|
||||
col: u32,
|
||||
}
|
||||
|
||||
impl<'a> Lexer<'a> {
|
||||
pub fn new(input: &'a str) -> Self {
|
||||
Self {
|
||||
input: input.chars().peekable(),
|
||||
line: 1,
|
||||
col: 1,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_token(&mut self) -> Result<Token, String> {
|
||||
self.skip_whitespace();
|
||||
|
||||
let start_location = SourceLocation {
|
||||
line: self.line,
|
||||
col: self.col,
|
||||
};
|
||||
|
||||
let char = match self.input.next() {
|
||||
Some(c) => {
|
||||
let current_char = c;
|
||||
self.col += 1;
|
||||
current_char
|
||||
}
|
||||
None => {
|
||||
return Ok(Token {
|
||||
kind: TokenKind::EOF,
|
||||
location: start_location,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
let kind = match char {
|
||||
'(' => TokenKind::LeftParen,
|
||||
')' => TokenKind::RightParen,
|
||||
'[' => TokenKind::LeftBracket,
|
||||
']' => TokenKind::RightBracket,
|
||||
'{' => TokenKind::LeftBrace,
|
||||
'}' => TokenKind::RightBrace,
|
||||
'\'' => TokenKind::Quote,
|
||||
'`' => TokenKind::Backtick,
|
||||
'~' => TokenKind::Tilde,
|
||||
'@' => TokenKind::At,
|
||||
':' => {
|
||||
let id = self.read_while(|c| !c.is_whitespace() && !"()[]{}\"'`~@;".contains(c));
|
||||
TokenKind::Keyword(Rc::from(id))
|
||||
}
|
||||
'"' => TokenKind::String(Rc::from(self.read_string()?)),
|
||||
c if c.is_ascii_digit()
|
||||
|| (c == '-' && self.input.peek().is_some_and(|p| p.is_ascii_digit())) =>
|
||||
{
|
||||
let mut num_str = String::from(c);
|
||||
while let Some(&next) = self.peek() {
|
||||
if next.is_ascii_digit() || next == '.' {
|
||||
num_str.push(self.input.next().unwrap());
|
||||
self.col += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if num_str.contains('.') {
|
||||
TokenKind::Float(num_str.parse().map_err(|_| "Invalid float format")?)
|
||||
} else {
|
||||
TokenKind::Integer(num_str.parse().map_err(|_| "Invalid integer format")?)
|
||||
}
|
||||
}
|
||||
c if is_ident_start(c) => {
|
||||
let mut id = String::from(c);
|
||||
id.push_str(&self.read_while(is_ident_char));
|
||||
TokenKind::Identifier(Rc::from(id))
|
||||
}
|
||||
_ => {
|
||||
return Err(format!(
|
||||
"Unexpected character: {} at {}:{}",
|
||||
char,
|
||||
self.line,
|
||||
self.col - 1
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Token {
|
||||
kind,
|
||||
location: start_location,
|
||||
})
|
||||
}
|
||||
|
||||
fn peek(&mut self) -> Option<&char> {
|
||||
self.input.peek()
|
||||
}
|
||||
|
||||
fn skip_whitespace(&mut self) {
|
||||
while let Some(&c) = self.peek() {
|
||||
if c.is_whitespace() || is_invisible(c) || c == ',' {
|
||||
if c == '\n' {
|
||||
self.line += 1;
|
||||
self.col = 1;
|
||||
} else {
|
||||
self.col += 1;
|
||||
}
|
||||
self.input.next();
|
||||
} else if c == ';' {
|
||||
for c in self.input.by_ref() {
|
||||
if c == '\n' {
|
||||
self.line += 1;
|
||||
self.col = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_while<F>(&mut self, mut predicate: F) -> String
|
||||
where
|
||||
F: FnMut(char) -> bool,
|
||||
{
|
||||
let mut s = String::new();
|
||||
while let Some(&c) = self.peek() {
|
||||
if predicate(c) {
|
||||
s.push(self.input.next().unwrap());
|
||||
self.col += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn read_string(&mut self) -> Result<String, String> {
|
||||
let mut s = String::new();
|
||||
while let Some(c) = self.input.next() {
|
||||
self.col += 1;
|
||||
match c {
|
||||
'"' => return Ok(s),
|
||||
'\\' => {
|
||||
let next = self.input.next().ok_or("Unterminated string escape")?;
|
||||
self.col += 1;
|
||||
match next {
|
||||
'n' => s.push('\n'),
|
||||
'r' => s.push('\r'),
|
||||
't' => s.push('\t'),
|
||||
'\\' => s.push('\\'),
|
||||
'"' => s.push('"'),
|
||||
_ => s.push(next),
|
||||
}
|
||||
}
|
||||
'\n' => {
|
||||
self.line += 1;
|
||||
self.col = 1;
|
||||
s.push(c);
|
||||
}
|
||||
_ => s.push(c),
|
||||
}
|
||||
}
|
||||
Err("Unterminated string".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn is_ident_start(c: char) -> bool {
|
||||
c.is_alphabetic() || "+-*/<=>!$%&_?.".contains(c)
|
||||
}
|
||||
|
||||
fn is_ident_char(c: char) -> bool {
|
||||
is_ident_start(c) || c.is_ascii_digit()
|
||||
}
|
||||
|
||||
/// Returns true if the character is an invisible control character
|
||||
/// that should be ignored by the lexer (like BOM or Zero Width Space).
|
||||
fn is_invisible(c: char) -> bool {
|
||||
match c {
|
||||
'\u{FEFF}' | // BOM
|
||||
'\u{200B}' | // Zero Width Space
|
||||
'\u{200C}' | // Zero Width Non-Joiner
|
||||
'\u{200D}' | // Zero Width Joiner
|
||||
'\u{2060}' // Word Joiner
|
||||
=> true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
+8
-8
@@ -1,8 +1,8 @@
|
||||
pub mod types;
|
||||
pub mod lexer;
|
||||
pub mod nodes;
|
||||
pub mod parser;
|
||||
pub mod compiler;
|
||||
pub mod environment;
|
||||
pub mod vm;
|
||||
pub mod rtl;
|
||||
pub mod compiler;
|
||||
pub mod environment;
|
||||
pub mod lexer;
|
||||
pub mod nodes;
|
||||
pub mod parser;
|
||||
pub mod rtl;
|
||||
pub mod types;
|
||||
pub mod vm;
|
||||
|
||||
+118
-112
@@ -1,112 +1,118 @@
|
||||
use std::rc::Rc;
|
||||
use std::fmt::Debug;
|
||||
use std::any::Any;
|
||||
use crate::ast::types::{Identity, Value, Object};
|
||||
|
||||
/// A name with an optional context for macro hygiene.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Symbol {
|
||||
pub name: Rc<str>,
|
||||
/// Points to the identity of the Expansion node if this symbol
|
||||
/// was created/referenced inside a macro expansion.
|
||||
pub context: Option<Identity>,
|
||||
}
|
||||
|
||||
impl From<Rc<str>> for Symbol {
|
||||
fn from(name: Rc<str>) -> Self {
|
||||
Self { name, context: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Symbol {
|
||||
fn from(name: &str) -> Self {
|
||||
Self { name: Rc::from(name), context: None }
|
||||
}
|
||||
}
|
||||
|
||||
/// A generic AST Node wrapper to preserve identity and metadata
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Node<K, T = ()> {
|
||||
pub identity: Identity,
|
||||
pub kind: K,
|
||||
pub ty: T,
|
||||
}
|
||||
|
||||
impl Object for Node<UntypedKind> {
|
||||
fn type_name(&self) -> &'static str {
|
||||
"ast-node"
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// The base for custom node types (extensions)
|
||||
pub trait CustomNode: Debug {
|
||||
fn display_name(&self) -> &'static str;
|
||||
fn clone_box(&self) -> Box<dyn CustomNode>;
|
||||
}
|
||||
|
||||
impl Clone for Box<dyn CustomNode> {
|
||||
fn clone(&self) -> Self {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum UntypedKind {
|
||||
Nop,
|
||||
Constant(Value),
|
||||
Identifier(Symbol),
|
||||
Parameter(Symbol),
|
||||
If {
|
||||
cond: Box<Node<UntypedKind>>,
|
||||
then_br: Box<Node<UntypedKind>>,
|
||||
else_br: Option<Box<Node<UntypedKind>>>,
|
||||
},
|
||||
Def {
|
||||
name: Symbol,
|
||||
value: Box<Node<UntypedKind>>,
|
||||
},
|
||||
Assign {
|
||||
target: Box<Node<UntypedKind>>,
|
||||
value: Box<Node<UntypedKind>>,
|
||||
},
|
||||
Lambda {
|
||||
params: Box<Node<UntypedKind>>,
|
||||
body: Rc<Node<UntypedKind>>,
|
||||
},
|
||||
Call {
|
||||
callee: Box<Node<UntypedKind>>,
|
||||
args: Box<Node<UntypedKind>>,
|
||||
},
|
||||
Block {
|
||||
exprs: Vec<Node<UntypedKind>>,
|
||||
},
|
||||
Tuple {
|
||||
elements: Vec<Node<UntypedKind>>,
|
||||
},
|
||||
Record {
|
||||
fields: Vec<(Node<UntypedKind>, Node<UntypedKind>)>,
|
||||
},
|
||||
/// A macro declaration that can be expanded at compile time.
|
||||
MacroDecl {
|
||||
name: Symbol,
|
||||
params: Box<Node<UntypedKind>>,
|
||||
body: Box<Node<UntypedKind>>,
|
||||
},
|
||||
/// A template for AST nodes, allowing for substitutions. (Quasiquote)
|
||||
Template(Box<Node<UntypedKind>>),
|
||||
/// A placeholder inside a template to be replaced by a node or value. (Unquote)
|
||||
Placeholder(Box<Node<UntypedKind>>),
|
||||
/// A placeholder that flattens a sequence or merges a record into its surroundings. (Splice)
|
||||
Splice(Box<Node<UntypedKind>>),
|
||||
/// Represents an expanded macro call, preserving the original call for debugging.
|
||||
Expansion {
|
||||
/// The original call from the source AST.
|
||||
call: Box<Node<UntypedKind>>,
|
||||
/// The resulting AST after macro expansion.
|
||||
expanded: Box<Node<UntypedKind>>,
|
||||
},
|
||||
Extension(Box<dyn CustomNode>),
|
||||
}
|
||||
use crate::ast::types::{Identity, Object, Value};
|
||||
use std::any::Any;
|
||||
use std::fmt::Debug;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A name with an optional context for macro hygiene.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Symbol {
|
||||
pub name: Rc<str>,
|
||||
/// Points to the identity of the Expansion node if this symbol
|
||||
/// was created/referenced inside a macro expansion.
|
||||
pub context: Option<Identity>,
|
||||
}
|
||||
|
||||
impl From<Rc<str>> for Symbol {
|
||||
fn from(name: Rc<str>) -> Self {
|
||||
Self {
|
||||
name,
|
||||
context: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Symbol {
|
||||
fn from(name: &str) -> Self {
|
||||
Self {
|
||||
name: Rc::from(name),
|
||||
context: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A generic AST Node wrapper to preserve identity and metadata
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Node<K, T = ()> {
|
||||
pub identity: Identity,
|
||||
pub kind: K,
|
||||
pub ty: T,
|
||||
}
|
||||
|
||||
impl Object for Node<UntypedKind> {
|
||||
fn type_name(&self) -> &'static str {
|
||||
"ast-node"
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// The base for custom node types (extensions)
|
||||
pub trait CustomNode: Debug {
|
||||
fn display_name(&self) -> &'static str;
|
||||
fn clone_box(&self) -> Box<dyn CustomNode>;
|
||||
}
|
||||
|
||||
impl Clone for Box<dyn CustomNode> {
|
||||
fn clone(&self) -> Self {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum UntypedKind {
|
||||
Nop,
|
||||
Constant(Value),
|
||||
Identifier(Symbol),
|
||||
Parameter(Symbol),
|
||||
If {
|
||||
cond: Box<Node<UntypedKind>>,
|
||||
then_br: Box<Node<UntypedKind>>,
|
||||
else_br: Option<Box<Node<UntypedKind>>>,
|
||||
},
|
||||
Def {
|
||||
name: Symbol,
|
||||
value: Box<Node<UntypedKind>>,
|
||||
},
|
||||
Assign {
|
||||
target: Box<Node<UntypedKind>>,
|
||||
value: Box<Node<UntypedKind>>,
|
||||
},
|
||||
Lambda {
|
||||
params: Box<Node<UntypedKind>>,
|
||||
body: Rc<Node<UntypedKind>>,
|
||||
},
|
||||
Call {
|
||||
callee: Box<Node<UntypedKind>>,
|
||||
args: Box<Node<UntypedKind>>,
|
||||
},
|
||||
Block {
|
||||
exprs: Vec<Node<UntypedKind>>,
|
||||
},
|
||||
Tuple {
|
||||
elements: Vec<Node<UntypedKind>>,
|
||||
},
|
||||
Record {
|
||||
fields: Vec<(Node<UntypedKind>, Node<UntypedKind>)>,
|
||||
},
|
||||
/// A macro declaration that can be expanded at compile time.
|
||||
MacroDecl {
|
||||
name: Symbol,
|
||||
params: Box<Node<UntypedKind>>,
|
||||
body: Box<Node<UntypedKind>>,
|
||||
},
|
||||
/// A template for AST nodes, allowing for substitutions. (Quasiquote)
|
||||
Template(Box<Node<UntypedKind>>),
|
||||
/// A placeholder inside a template to be replaced by a node or value. (Unquote)
|
||||
Placeholder(Box<Node<UntypedKind>>),
|
||||
/// A placeholder that flattens a sequence or merges a record into its surroundings. (Splice)
|
||||
Splice(Box<Node<UntypedKind>>),
|
||||
/// Represents an expanded macro call, preserving the original call for debugging.
|
||||
Expansion {
|
||||
/// The original call from the source AST.
|
||||
call: Box<Node<UntypedKind>>,
|
||||
/// The resulting AST after macro expansion.
|
||||
expanded: Box<Node<UntypedKind>>,
|
||||
},
|
||||
Extension(Box<dyn CustomNode>),
|
||||
}
|
||||
|
||||
+400
-345
@@ -1,345 +1,400 @@
|
||||
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};
|
||||
|
||||
pub struct Parser<'a> {
|
||||
lexer: Lexer<'a>,
|
||||
current_token: Token,
|
||||
}
|
||||
|
||||
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 })
|
||||
}
|
||||
|
||||
fn advance(&mut self) -> Result<Token, String> {
|
||||
let prev = std::mem::replace(&mut self.current_token, self.lexer.next_token()?);
|
||||
Ok(prev)
|
||||
}
|
||||
|
||||
fn peek(&self) -> &TokenKind {
|
||||
&self.current_token.kind
|
||||
}
|
||||
|
||||
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 });
|
||||
|
||||
match self.peek() {
|
||||
TokenKind::LeftParen => self.parse_list(),
|
||||
TokenKind::LeftBracket => self.parse_vector_literal(),
|
||||
TokenKind::LeftBrace => self.parse_record_literal(),
|
||||
TokenKind::Quote => {
|
||||
self.advance()?; // consume '
|
||||
let expr = self.parse_expression()?;
|
||||
Ok(Node {
|
||||
identity: identity.clone(),
|
||||
kind: UntypedKind::Call {
|
||||
callee: Box::new(self.make_id_node("quote", identity.clone())),
|
||||
args: Box::new(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Tuple { elements: vec![expr] },
|
||||
ty: (),
|
||||
}),
|
||||
},
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
TokenKind::Backtick => {
|
||||
self.advance()?; // consume `
|
||||
let expr = self.parse_expression()?;
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Template(Box::new(expr)),
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
TokenKind::Tilde => {
|
||||
self.advance()?; // consume ~
|
||||
if *self.peek() == TokenKind::At {
|
||||
self.advance()?; // consume @
|
||||
let expr = self.parse_expression()?;
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Splice(Box::new(expr)),
|
||||
ty: (),
|
||||
})
|
||||
} else {
|
||||
let expr = self.parse_expression()?;
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Placeholder(Box::new(expr)),
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
}
|
||||
_ => self.parse_atom(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn at_eof(&self) -> bool {
|
||||
matches!(self.current_token.kind, TokenKind::EOF)
|
||||
}
|
||||
|
||||
fn parse_atom(&mut self) -> Result<Node<UntypedKind>, String> {
|
||||
let token = self.advance()?;
|
||||
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() {
|
||||
"..." => UntypedKind::Nop,
|
||||
_ => UntypedKind::Identifier(id.into()),
|
||||
}
|
||||
}
|
||||
_ => return Err(format!("Unexpected token in atom: {:?} at {:?}", token.kind, token.location)),
|
||||
};
|
||||
|
||||
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 });
|
||||
|
||||
if *self.peek() == TokenKind::RightParen {
|
||||
return Err(format!("Empty list () is not a valid expression at {:?}", start_loc));
|
||||
}
|
||||
|
||||
let head = self.parse_expression()?;
|
||||
|
||||
let result = if let UntypedKind::Identifier(ref sym) = head.kind {
|
||||
match sym.name.as_ref() {
|
||||
"if" => self.parse_if(identity),
|
||||
"fn" => self.parse_fn(identity),
|
||||
"def" => self.parse_def(identity),
|
||||
"assign" => self.parse_assign(identity),
|
||||
"do" => self.parse_do(identity),
|
||||
"macro" => self.parse_macro_decl(identity),
|
||||
_ => self.parse_call(head, identity),
|
||||
}
|
||||
} else {
|
||||
self.parse_call(head, identity)
|
||||
};
|
||||
|
||||
self.expect(TokenKind::RightParen)?;
|
||||
result
|
||||
}
|
||||
|
||||
fn parse_if(&mut self, identity: Identity) -> Result<Node<UntypedKind>, String> {
|
||||
let cond = Box::new(self.parse_expression()?);
|
||||
let then_br = Box::new(self.parse_expression()?);
|
||||
let mut else_br = None;
|
||||
|
||||
if *self.peek() != TokenKind::RightParen {
|
||||
else_br = Some(Box::new(self.parse_expression()?));
|
||||
}
|
||||
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::If { cond, then_br, else_br },
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_def(&mut self, identity: Identity) -> Result<Node<UntypedKind>, String> {
|
||||
let name_node = self.parse_expression()?;
|
||||
let name = match name_node.kind {
|
||||
UntypedKind::Identifier(sym) => sym,
|
||||
_ => return Err("Expected identifier for def name".to_string()),
|
||||
};
|
||||
|
||||
let value = Box::new(self.parse_expression()?);
|
||||
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Def { name, value },
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_assign(&mut self, identity: Identity) -> Result<Node<UntypedKind>, String> {
|
||||
// (assign target value)
|
||||
let target = Box::new(self.parse_expression()?);
|
||||
let value = Box::new(self.parse_expression()?);
|
||||
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Assign { target, value },
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_do(&mut self, identity: Identity) -> Result<Node<UntypedKind>, String> {
|
||||
let mut exprs = Vec::new();
|
||||
while *self.peek() != TokenKind::RightParen && *self.peek() != TokenKind::EOF {
|
||||
exprs.push(self.parse_expression()?);
|
||||
}
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Block { exprs },
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_fn(&mut self, identity: Identity) -> Result<Node<UntypedKind>, String> {
|
||||
let params = Box::new(self.parse_param_vector()?);
|
||||
let body = self.parse_expression()?;
|
||||
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Lambda {
|
||||
params,
|
||||
body: Rc::new(body),
|
||||
},
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_macro_decl(&mut self, identity: Identity) -> Result<Node<UntypedKind>, String> {
|
||||
let name_node = self.parse_expression()?;
|
||||
let name = match name_node.kind {
|
||||
UntypedKind::Identifier(sym) => sym,
|
||||
_ => return Err("Expected identifier for macro name".to_string()),
|
||||
};
|
||||
let params = Box::new(self.parse_param_vector()?);
|
||||
let body = self.parse_expression()?;
|
||||
Ok(Node {
|
||||
identity,
|
||||
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()));
|
||||
}
|
||||
let token = self.advance()?;
|
||||
let identity = Rc::new(NodeIdentity { location: token.location });
|
||||
|
||||
let mut elements = Vec::new();
|
||||
while *self.peek() != TokenKind::RightBracket {
|
||||
let next_peek = self.peek();
|
||||
match next_peek {
|
||||
TokenKind::Identifier(name) => {
|
||||
let name = name.clone();
|
||||
let token = self.advance()?;
|
||||
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)),
|
||||
}
|
||||
}
|
||||
self.expect(TokenKind::RightBracket)?;
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Tuple { elements },
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
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 {
|
||||
elements.push(self.parse_expression()?);
|
||||
}
|
||||
|
||||
// The arguments are wrapped in a Tuple node, reusing the call's identity/location.
|
||||
let args_node = Node {
|
||||
identity: identity.clone(),
|
||||
kind: UntypedKind::Tuple { elements },
|
||||
ty: (),
|
||||
};
|
||||
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Call { callee: Box::new(callee), args: Box::new(args_node) },
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_vector_literal(&mut self) -> Result<Node<UntypedKind>, String> {
|
||||
let token = self.advance()?;
|
||||
let mut elements = Vec::new();
|
||||
|
||||
while *self.peek() != TokenKind::RightBracket && *self.peek() != TokenKind::EOF {
|
||||
let expr = self.parse_expression()?;
|
||||
elements.push(expr);
|
||||
}
|
||||
|
||||
self.expect(TokenKind::RightBracket)?;
|
||||
|
||||
Ok(Node {
|
||||
identity: Rc::new(NodeIdentity { location: token.location }),
|
||||
kind: UntypedKind::Tuple { elements },
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_record_literal(&mut self) -> Result<Node<UntypedKind>, String> {
|
||||
let token = self.advance()?;
|
||||
let mut fields = Vec::new();
|
||||
|
||||
while *self.peek() != TokenKind::RightBrace {
|
||||
if *self.peek() == TokenKind::EOF {
|
||||
return Err("Unexpected EOF in record literal".to_string());
|
||||
}
|
||||
|
||||
let key_node = self.parse_expression()?;
|
||||
// We check for keyword kind here (syntactically) to avoid ambiguity, but
|
||||
// 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(_)) => {},
|
||||
_ => return Err("Record keys must be keywords (syntactically)".to_string()),
|
||||
}
|
||||
|
||||
if *self.peek() == TokenKind::RightBrace {
|
||||
return Err("Record literal must have even number of forms".to_string());
|
||||
}
|
||||
let val_node = self.parse_expression()?;
|
||||
|
||||
fields.push((key_node, val_node));
|
||||
}
|
||||
self.expect(TokenKind::RightBrace)?;
|
||||
|
||||
Ok(Node {
|
||||
identity: Rc::new(NodeIdentity { location: token.location }),
|
||||
kind: UntypedKind::Record { fields },
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn expect(&mut self, kind: TokenKind) -> Result<(), String> {
|
||||
let token = self.advance()?;
|
||||
if token.kind == kind {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!("Expected {:?}, but found {:?} at {:?}", kind, token.kind, token.location))
|
||||
}
|
||||
}
|
||||
|
||||
fn make_id_node(&self, name: &str, identity: Identity) -> Node<UntypedKind> {
|
||||
Node {
|
||||
identity,
|
||||
kind: UntypedKind::Identifier(Symbol::from(name)),
|
||||
ty: (),
|
||||
}
|
||||
}
|
||||
}
|
||||
use crate::ast::lexer::{Lexer, Token, TokenKind};
|
||||
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>,
|
||||
current_token: Token,
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
fn advance(&mut self) -> Result<Token, String> {
|
||||
let prev = std::mem::replace(&mut self.current_token, self.lexer.next_token()?);
|
||||
Ok(prev)
|
||||
}
|
||||
|
||||
fn peek(&self) -> &TokenKind {
|
||||
&self.current_token.kind
|
||||
}
|
||||
|
||||
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,
|
||||
});
|
||||
|
||||
match self.peek() {
|
||||
TokenKind::LeftParen => self.parse_list(),
|
||||
TokenKind::LeftBracket => self.parse_vector_literal(),
|
||||
TokenKind::LeftBrace => self.parse_record_literal(),
|
||||
TokenKind::Quote => {
|
||||
self.advance()?; // consume '
|
||||
let expr = self.parse_expression()?;
|
||||
Ok(Node {
|
||||
identity: identity.clone(),
|
||||
kind: UntypedKind::Call {
|
||||
callee: Box::new(self.make_id_node("quote", identity.clone())),
|
||||
args: Box::new(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Tuple {
|
||||
elements: vec![expr],
|
||||
},
|
||||
ty: (),
|
||||
}),
|
||||
},
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
TokenKind::Backtick => {
|
||||
self.advance()?; // consume `
|
||||
let expr = self.parse_expression()?;
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Template(Box::new(expr)),
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
TokenKind::Tilde => {
|
||||
self.advance()?; // consume ~
|
||||
if *self.peek() == TokenKind::At {
|
||||
self.advance()?; // consume @
|
||||
let expr = self.parse_expression()?;
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Splice(Box::new(expr)),
|
||||
ty: (),
|
||||
})
|
||||
} else {
|
||||
let expr = self.parse_expression()?;
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Placeholder(Box::new(expr)),
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
}
|
||||
_ => self.parse_atom(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn at_eof(&self) -> bool {
|
||||
matches!(self.current_token.kind, TokenKind::EOF)
|
||||
}
|
||||
|
||||
fn parse_atom(&mut self) -> Result<Node<UntypedKind>, String> {
|
||||
let token = self.advance()?;
|
||||
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() {
|
||||
"..." => UntypedKind::Nop,
|
||||
_ => UntypedKind::Identifier(id.into()),
|
||||
},
|
||||
_ => {
|
||||
return Err(format!(
|
||||
"Unexpected token in atom: {:?} at {:?}",
|
||||
token.kind, token.location
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
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,
|
||||
});
|
||||
|
||||
if *self.peek() == TokenKind::RightParen {
|
||||
return Err(format!(
|
||||
"Empty list () is not a valid expression at {:?}",
|
||||
start_loc
|
||||
));
|
||||
}
|
||||
|
||||
let head = self.parse_expression()?;
|
||||
|
||||
let result = if let UntypedKind::Identifier(ref sym) = head.kind {
|
||||
match sym.name.as_ref() {
|
||||
"if" => self.parse_if(identity),
|
||||
"fn" => self.parse_fn(identity),
|
||||
"def" => self.parse_def(identity),
|
||||
"assign" => self.parse_assign(identity),
|
||||
"do" => self.parse_do(identity),
|
||||
"macro" => self.parse_macro_decl(identity),
|
||||
_ => self.parse_call(head, identity),
|
||||
}
|
||||
} else {
|
||||
self.parse_call(head, identity)
|
||||
};
|
||||
|
||||
self.expect(TokenKind::RightParen)?;
|
||||
result
|
||||
}
|
||||
|
||||
fn parse_if(&mut self, identity: Identity) -> Result<Node<UntypedKind>, String> {
|
||||
let cond = Box::new(self.parse_expression()?);
|
||||
let then_br = Box::new(self.parse_expression()?);
|
||||
let mut else_br = None;
|
||||
|
||||
if *self.peek() != TokenKind::RightParen {
|
||||
else_br = Some(Box::new(self.parse_expression()?));
|
||||
}
|
||||
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::If {
|
||||
cond,
|
||||
then_br,
|
||||
else_br,
|
||||
},
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_def(&mut self, identity: Identity) -> Result<Node<UntypedKind>, String> {
|
||||
let name_node = self.parse_expression()?;
|
||||
let name = match name_node.kind {
|
||||
UntypedKind::Identifier(sym) => sym,
|
||||
_ => return Err("Expected identifier for def name".to_string()),
|
||||
};
|
||||
|
||||
let value = Box::new(self.parse_expression()?);
|
||||
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Def { name, value },
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_assign(&mut self, identity: Identity) -> Result<Node<UntypedKind>, String> {
|
||||
// (assign target value)
|
||||
let target = Box::new(self.parse_expression()?);
|
||||
let value = Box::new(self.parse_expression()?);
|
||||
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Assign { target, value },
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_do(&mut self, identity: Identity) -> Result<Node<UntypedKind>, String> {
|
||||
let mut exprs = Vec::new();
|
||||
while *self.peek() != TokenKind::RightParen && *self.peek() != TokenKind::EOF {
|
||||
exprs.push(self.parse_expression()?);
|
||||
}
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Block { exprs },
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_fn(&mut self, identity: Identity) -> Result<Node<UntypedKind>, String> {
|
||||
let params = Box::new(self.parse_param_vector()?);
|
||||
let body = self.parse_expression()?;
|
||||
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Lambda {
|
||||
params,
|
||||
body: Rc::new(body),
|
||||
},
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_macro_decl(&mut self, identity: Identity) -> Result<Node<UntypedKind>, String> {
|
||||
let name_node = self.parse_expression()?;
|
||||
let name = match name_node.kind {
|
||||
UntypedKind::Identifier(sym) => sym,
|
||||
_ => return Err("Expected identifier for macro name".to_string()),
|
||||
};
|
||||
let params = Box::new(self.parse_param_vector()?);
|
||||
let body = self.parse_expression()?;
|
||||
Ok(Node {
|
||||
identity,
|
||||
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()
|
||||
));
|
||||
}
|
||||
let token = self.advance()?;
|
||||
let identity = Rc::new(NodeIdentity {
|
||||
location: token.location,
|
||||
});
|
||||
|
||||
let mut elements = Vec::new();
|
||||
while *self.peek() != TokenKind::RightBracket {
|
||||
let next_peek = self.peek();
|
||||
match next_peek {
|
||||
TokenKind::Identifier(name) => {
|
||||
let name = name.clone();
|
||||
let token = self.advance()?;
|
||||
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
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
self.expect(TokenKind::RightBracket)?;
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Tuple { elements },
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
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 {
|
||||
elements.push(self.parse_expression()?);
|
||||
}
|
||||
|
||||
// The arguments are wrapped in a Tuple node, reusing the call's identity/location.
|
||||
let args_node = Node {
|
||||
identity: identity.clone(),
|
||||
kind: UntypedKind::Tuple { elements },
|
||||
ty: (),
|
||||
};
|
||||
|
||||
Ok(Node {
|
||||
identity,
|
||||
kind: UntypedKind::Call {
|
||||
callee: Box::new(callee),
|
||||
args: Box::new(args_node),
|
||||
},
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_vector_literal(&mut self) -> Result<Node<UntypedKind>, String> {
|
||||
let token = self.advance()?;
|
||||
let mut elements = Vec::new();
|
||||
|
||||
while *self.peek() != TokenKind::RightBracket && *self.peek() != TokenKind::EOF {
|
||||
let expr = self.parse_expression()?;
|
||||
elements.push(expr);
|
||||
}
|
||||
|
||||
self.expect(TokenKind::RightBracket)?;
|
||||
|
||||
Ok(Node {
|
||||
identity: Rc::new(NodeIdentity {
|
||||
location: token.location,
|
||||
}),
|
||||
kind: UntypedKind::Tuple { elements },
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_record_literal(&mut self) -> Result<Node<UntypedKind>, String> {
|
||||
let token = self.advance()?;
|
||||
let mut fields = Vec::new();
|
||||
|
||||
while *self.peek() != TokenKind::RightBrace {
|
||||
if *self.peek() == TokenKind::EOF {
|
||||
return Err("Unexpected EOF in record literal".to_string());
|
||||
}
|
||||
|
||||
let key_node = self.parse_expression()?;
|
||||
// We check for keyword kind here (syntactically) to avoid ambiguity, but
|
||||
// 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(_)) => {}
|
||||
_ => return Err("Record keys must be keywords (syntactically)".to_string()),
|
||||
}
|
||||
|
||||
if *self.peek() == TokenKind::RightBrace {
|
||||
return Err("Record literal must have even number of forms".to_string());
|
||||
}
|
||||
let val_node = self.parse_expression()?;
|
||||
|
||||
fields.push((key_node, val_node));
|
||||
}
|
||||
self.expect(TokenKind::RightBrace)?;
|
||||
|
||||
Ok(Node {
|
||||
identity: Rc::new(NodeIdentity {
|
||||
location: token.location,
|
||||
}),
|
||||
kind: UntypedKind::Record { fields },
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
|
||||
fn expect(&mut self, kind: TokenKind) -> Result<(), String> {
|
||||
let token = self.advance()?;
|
||||
if token.kind == kind {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!(
|
||||
"Expected {:?}, but found {:?} at {:?}",
|
||||
kind, token.kind, token.location
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn make_id_node(&self, name: &str, identity: Identity) -> Node<UntypedKind> {
|
||||
Node {
|
||||
identity,
|
||||
kind: UntypedKind::Identifier(Symbol::from(name)),
|
||||
ty: (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+473
-334
@@ -1,334 +1,473 @@
|
||||
use std::rc::Rc;
|
||||
use crate::ast::types::{Value, StaticType, Signature};
|
||||
use crate::ast::environment::Environment;
|
||||
|
||||
|
||||
pub fn register(env: &Environment) {
|
||||
register_constants(env);
|
||||
register_arithmetic(env);
|
||||
register_comparison(env);
|
||||
register_logic(env);
|
||||
}
|
||||
|
||||
fn register_constants(env: &Environment) {
|
||||
// True/False are keywords or literals in parser, but could be exposed as constants too if needed.
|
||||
// In Delphi RTL: CFalse, CTrue, CNaN
|
||||
|
||||
// We register NaN as a value, not a function.
|
||||
env.register_constant("NaN", StaticType::Float, Value::Float(f64::NAN));
|
||||
env.register_constant("true", StaticType::Bool, Value::Bool(true));
|
||||
env.register_constant("false", StaticType::Bool, Value::Bool(false));
|
||||
}
|
||||
|
||||
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 },
|
||||
]);
|
||||
env.register_native("+", add_ty, true, |args| {
|
||||
if args.len() == 2 {
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a + b),
|
||||
(Value::Float(a), Value::Float(b)) => Value::Float(a + b),
|
||||
(Value::Int(a), Value::Float(b)) => Value::Float(*a as f64 + b),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Float(a + *b as f64),
|
||||
(Value::Text(a), Value::Text(b)) => {
|
||||
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,
|
||||
}
|
||||
} else {
|
||||
// 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 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 },
|
||||
]);
|
||||
env.register_native("-", sub_ty, true, |args| {
|
||||
if args.is_empty() { return Value::Void; }
|
||||
if args.len() == 1 {
|
||||
return match args[0] {
|
||||
Value::Int(i) => Value::Int(-i),
|
||||
Value::Float(f) => Value::Float(-f),
|
||||
_ => Value::Void,
|
||||
};
|
||||
}
|
||||
if args.len() == 2 {
|
||||
return match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a - b),
|
||||
(Value::Float(a), Value::Float(b)) => Value::Float(a - b),
|
||||
(Value::Int(a), Value::Float(b)) => Value::Float(*a as f64 - b),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Float(a - *b as f64),
|
||||
(Value::DateTime(a), Value::DateTime(b)) => Value::Int(a - b),
|
||||
(Value::DateTime(a), Value::Int(b)) => Value::DateTime(a - b),
|
||||
_ => Value::Void,
|
||||
};
|
||||
}
|
||||
// Variadic sub
|
||||
let mut acc = match args[0] {
|
||||
Value::Int(i) => i as f64,
|
||||
Value::Float(f) => f,
|
||||
_ => 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 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 },
|
||||
]);
|
||||
env.register_native("*", mul_ty, true, |args| {
|
||||
if args.len() == 2 {
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a * b),
|
||||
(Value::Float(a), Value::Float(b)) => Value::Float(a * b),
|
||||
(Value::Int(a), Value::Float(b)) => Value::Float(*a as f64 * b),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Float(a * *b as f64),
|
||||
_ => Value::Void,
|
||||
}
|
||||
} 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 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 },
|
||||
]);
|
||||
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) }
|
||||
});
|
||||
|
||||
// --- Integer Divide (//) ---
|
||||
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; }
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(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,
|
||||
}
|
||||
});
|
||||
|
||||
// --- Modulus (%) ---
|
||||
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; }
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(b)) => {
|
||||
if *b == 0 { Value::Void } else { Value::Int(a % b) }
|
||||
},
|
||||
_ => Value::Void,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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 },
|
||||
]);
|
||||
|
||||
// --- Greater Than (>) ---
|
||||
env.register_native(">", cmp_ty.clone(), true, |args| {
|
||||
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),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Bool(*a > (*b as f64)),
|
||||
(Value::Float(a), Value::Float(b)) => Value::Bool(a > b),
|
||||
(Value::DateTime(a), Value::DateTime(b)) => Value::Bool(a > b),
|
||||
_ => Value::Bool(false),
|
||||
}
|
||||
});
|
||||
|
||||
// --- Less Than (<) ---
|
||||
env.register_native("<", cmp_ty.clone(), true, |args| {
|
||||
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),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Bool(*a < (*b as f64)),
|
||||
(Value::Float(a), Value::Float(b)) => Value::Bool(a < b),
|
||||
(Value::DateTime(a), Value::DateTime(b)) => Value::Bool(a < b),
|
||||
_ => Value::Bool(false),
|
||||
}
|
||||
});
|
||||
|
||||
// --- Greater Or Equal (>=) ---
|
||||
env.register_native(">=", cmp_ty.clone(), true, |args| {
|
||||
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),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Bool(*a >= (*b as f64)),
|
||||
(Value::Float(a), Value::Float(b)) => Value::Bool(a >= b),
|
||||
(Value::DateTime(a), Value::DateTime(b)) => Value::Bool(a >= b),
|
||||
_ => Value::Bool(false),
|
||||
}
|
||||
});
|
||||
|
||||
// --- Less Or Equal (<=) ---
|
||||
env.register_native("<=", cmp_ty.clone(), true, |args| {
|
||||
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),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Bool(*a <= (*b as f64)),
|
||||
(Value::Float(a), Value::Float(b)) => Value::Bool(a <= b),
|
||||
(Value::DateTime(a), Value::DateTime(b)) => Value::Bool(a <= b),
|
||||
_ => Value::Bool(false),
|
||||
}
|
||||
});
|
||||
|
||||
// --- Equal (=) ---
|
||||
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; }
|
||||
// Simple equality check.
|
||||
// Note: Floating point equality is tricky, but we follow standard behavior for now.
|
||||
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),
|
||||
(Value::Int(a), Value::Float(b)) => Value::Bool((*a as f64 - b).abs() < f64::EPSILON),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Bool((a - *b as f64).abs() < f64::EPSILON),
|
||||
(Value::Text(a), Value::Text(b)) => Value::Bool(a == b),
|
||||
(Value::Bool(a), Value::Bool(b)) => Value::Bool(a == b),
|
||||
(Value::Keyword(a), Value::Keyword(b)) => Value::Bool(a == b),
|
||||
(Value::DateTime(a), Value::DateTime(b)) => Value::Bool(a == b),
|
||||
(Value::Void, Value::Void) => Value::Bool(true),
|
||||
_ => Value::Bool(false),
|
||||
}
|
||||
});
|
||||
|
||||
// --- Not Equal (<>) ---
|
||||
env.register_native("<>", eq_ty, true, |args| {
|
||||
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),
|
||||
(Value::Int(a), Value::Float(b)) => Value::Bool((*a as f64 - b).abs() >= f64::EPSILON),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Bool((a - *b as f64).abs() >= f64::EPSILON),
|
||||
(Value::Text(a), Value::Text(b)) => Value::Bool(a != b),
|
||||
(Value::Bool(a), Value::Bool(b)) => Value::Bool(a != b),
|
||||
(Value::Keyword(a), Value::Keyword(b)) => Value::Bool(a != b),
|
||||
(Value::DateTime(a), Value::DateTime(b)) => Value::Bool(a != b),
|
||||
(Value::Void, Value::Void) => Value::Bool(false),
|
||||
_ => Value::Bool(true),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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 },
|
||||
]);
|
||||
env.register_native("not", not_ty, true, |args| {
|
||||
if args.len() != 1 { return Value::Void; }
|
||||
match &args[0] {
|
||||
Value::Bool(b) => Value::Bool(!b),
|
||||
Value::Int(i) => Value::Int(!i), // Bitwise NOT
|
||||
_ => Value::Void,
|
||||
}
|
||||
});
|
||||
|
||||
// --- 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 },
|
||||
]);
|
||||
env.register_native("and", logic_op_ty.clone(), true, |args| {
|
||||
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),
|
||||
_ => Value::Void,
|
||||
}
|
||||
});
|
||||
|
||||
// --- Or (or) ---
|
||||
env.register_native("or", logic_op_ty.clone(), true, |args| {
|
||||
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),
|
||||
_ => Value::Void,
|
||||
}
|
||||
});
|
||||
|
||||
// --- Xor (xor) ---
|
||||
env.register_native("xor", logic_op_ty.clone(), true, |args| {
|
||||
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),
|
||||
_ => Value::Void,
|
||||
}
|
||||
});
|
||||
|
||||
// --- Shift Left (<<) ---
|
||||
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; }
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a << b),
|
||||
_ => Value::Void,
|
||||
}
|
||||
});
|
||||
|
||||
// --- Shift Right (>>) ---
|
||||
env.register_native(">>", shift_ty, true, |args| {
|
||||
if args.len() != 2 { return Value::Void; }
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a >> b),
|
||||
_ => Value::Void,
|
||||
}
|
||||
});
|
||||
}
|
||||
use crate::ast::environment::Environment;
|
||||
use crate::ast::types::{Signature, StaticType, Value};
|
||||
use std::rc::Rc;
|
||||
|
||||
pub fn register(env: &Environment) {
|
||||
register_constants(env);
|
||||
register_arithmetic(env);
|
||||
register_comparison(env);
|
||||
register_logic(env);
|
||||
}
|
||||
|
||||
fn register_constants(env: &Environment) {
|
||||
// True/False are keywords or literals in parser, but could be exposed as constants too if needed.
|
||||
// In Delphi RTL: CFalse, CTrue, CNaN
|
||||
|
||||
// We register NaN as a value, not a function.
|
||||
env.register_constant("NaN", StaticType::Float, Value::Float(f64::NAN));
|
||||
env.register_constant("true", StaticType::Bool, Value::Bool(true));
|
||||
env.register_constant("false", StaticType::Bool, Value::Bool(false));
|
||||
}
|
||||
|
||||
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,
|
||||
},
|
||||
]);
|
||||
env.register_native("+", add_ty, true, |args| {
|
||||
if args.len() == 2 {
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a + b),
|
||||
(Value::Float(a), Value::Float(b)) => Value::Float(a + b),
|
||||
(Value::Int(a), Value::Float(b)) => Value::Float(*a as f64 + b),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Float(a + *b as f64),
|
||||
(Value::Text(a), Value::Text(b)) => {
|
||||
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,
|
||||
}
|
||||
} else {
|
||||
// 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 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,
|
||||
},
|
||||
]);
|
||||
env.register_native("-", sub_ty, true, |args| {
|
||||
if args.is_empty() {
|
||||
return Value::Void;
|
||||
}
|
||||
if args.len() == 1 {
|
||||
return match args[0] {
|
||||
Value::Int(i) => Value::Int(-i),
|
||||
Value::Float(f) => Value::Float(-f),
|
||||
_ => Value::Void,
|
||||
};
|
||||
}
|
||||
if args.len() == 2 {
|
||||
return match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a - b),
|
||||
(Value::Float(a), Value::Float(b)) => Value::Float(a - b),
|
||||
(Value::Int(a), Value::Float(b)) => Value::Float(*a as f64 - b),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Float(a - *b as f64),
|
||||
(Value::DateTime(a), Value::DateTime(b)) => Value::Int(a - b),
|
||||
(Value::DateTime(a), Value::Int(b)) => Value::DateTime(a - b),
|
||||
_ => Value::Void,
|
||||
};
|
||||
}
|
||||
// Variadic sub
|
||||
let mut acc = match args[0] {
|
||||
Value::Int(i) => i as f64,
|
||||
Value::Float(f) => f,
|
||||
_ => 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 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,
|
||||
},
|
||||
]);
|
||||
env.register_native("*", mul_ty, true, |args| {
|
||||
if args.len() == 2 {
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a * b),
|
||||
(Value::Float(a), Value::Float(b)) => Value::Float(a * b),
|
||||
(Value::Int(a), Value::Float(b)) => Value::Float(*a as f64 * b),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Float(a * *b as f64),
|
||||
_ => Value::Void,
|
||||
}
|
||||
} 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 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,
|
||||
},
|
||||
]);
|
||||
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)
|
||||
}
|
||||
});
|
||||
|
||||
// --- Integer Divide (//) ---
|
||||
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;
|
||||
}
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(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,
|
||||
}
|
||||
});
|
||||
|
||||
// --- Modulus (%) ---
|
||||
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;
|
||||
}
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(b)) => {
|
||||
if *b == 0 {
|
||||
Value::Void
|
||||
} else {
|
||||
Value::Int(a % b)
|
||||
}
|
||||
}
|
||||
_ => Value::Void,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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,
|
||||
},
|
||||
]);
|
||||
|
||||
// --- Greater Than (>) ---
|
||||
env.register_native(">", cmp_ty.clone(), true, |args| {
|
||||
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),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Bool(*a > (*b as f64)),
|
||||
(Value::Float(a), Value::Float(b)) => Value::Bool(a > b),
|
||||
(Value::DateTime(a), Value::DateTime(b)) => Value::Bool(a > b),
|
||||
_ => Value::Bool(false),
|
||||
}
|
||||
});
|
||||
|
||||
// --- Less Than (<) ---
|
||||
env.register_native("<", cmp_ty.clone(), true, |args| {
|
||||
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),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Bool(*a < (*b as f64)),
|
||||
(Value::Float(a), Value::Float(b)) => Value::Bool(a < b),
|
||||
(Value::DateTime(a), Value::DateTime(b)) => Value::Bool(a < b),
|
||||
_ => Value::Bool(false),
|
||||
}
|
||||
});
|
||||
|
||||
// --- Greater Or Equal (>=) ---
|
||||
env.register_native(">=", cmp_ty.clone(), true, |args| {
|
||||
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),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Bool(*a >= (*b as f64)),
|
||||
(Value::Float(a), Value::Float(b)) => Value::Bool(a >= b),
|
||||
(Value::DateTime(a), Value::DateTime(b)) => Value::Bool(a >= b),
|
||||
_ => Value::Bool(false),
|
||||
}
|
||||
});
|
||||
|
||||
// --- Less Or Equal (<=) ---
|
||||
env.register_native("<=", cmp_ty.clone(), true, |args| {
|
||||
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),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Bool(*a <= (*b as f64)),
|
||||
(Value::Float(a), Value::Float(b)) => Value::Bool(a <= b),
|
||||
(Value::DateTime(a), Value::DateTime(b)) => Value::Bool(a <= b),
|
||||
_ => Value::Bool(false),
|
||||
}
|
||||
});
|
||||
|
||||
// --- Equal (=) ---
|
||||
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;
|
||||
}
|
||||
// Simple equality check.
|
||||
// Note: Floating point equality is tricky, but we follow standard behavior for now.
|
||||
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),
|
||||
(Value::Int(a), Value::Float(b)) => Value::Bool((*a as f64 - b).abs() < f64::EPSILON),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Bool((a - *b as f64).abs() < f64::EPSILON),
|
||||
(Value::Text(a), Value::Text(b)) => Value::Bool(a == b),
|
||||
(Value::Bool(a), Value::Bool(b)) => Value::Bool(a == b),
|
||||
(Value::Keyword(a), Value::Keyword(b)) => Value::Bool(a == b),
|
||||
(Value::DateTime(a), Value::DateTime(b)) => Value::Bool(a == b),
|
||||
(Value::Void, Value::Void) => Value::Bool(true),
|
||||
_ => Value::Bool(false),
|
||||
}
|
||||
});
|
||||
|
||||
// --- Not Equal (<>) ---
|
||||
env.register_native("<>", eq_ty, true, |args| {
|
||||
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),
|
||||
(Value::Int(a), Value::Float(b)) => Value::Bool((*a as f64 - b).abs() >= f64::EPSILON),
|
||||
(Value::Float(a), Value::Int(b)) => Value::Bool((a - *b as f64).abs() >= f64::EPSILON),
|
||||
(Value::Text(a), Value::Text(b)) => Value::Bool(a != b),
|
||||
(Value::Bool(a), Value::Bool(b)) => Value::Bool(a != b),
|
||||
(Value::Keyword(a), Value::Keyword(b)) => Value::Bool(a != b),
|
||||
(Value::DateTime(a), Value::DateTime(b)) => Value::Bool(a != b),
|
||||
(Value::Void, Value::Void) => Value::Bool(false),
|
||||
_ => Value::Bool(true),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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,
|
||||
},
|
||||
]);
|
||||
env.register_native("not", not_ty, true, |args| {
|
||||
if args.len() != 1 {
|
||||
return Value::Void;
|
||||
}
|
||||
match &args[0] {
|
||||
Value::Bool(b) => Value::Bool(!b),
|
||||
Value::Int(i) => Value::Int(!i), // Bitwise NOT
|
||||
_ => Value::Void,
|
||||
}
|
||||
});
|
||||
|
||||
// --- 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,
|
||||
},
|
||||
]);
|
||||
env.register_native("and", logic_op_ty.clone(), true, |args| {
|
||||
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),
|
||||
_ => Value::Void,
|
||||
}
|
||||
});
|
||||
|
||||
// --- Or (or) ---
|
||||
env.register_native("or", logic_op_ty.clone(), true, |args| {
|
||||
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),
|
||||
_ => Value::Void,
|
||||
}
|
||||
});
|
||||
|
||||
// --- Xor (xor) ---
|
||||
env.register_native("xor", logic_op_ty.clone(), true, |args| {
|
||||
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),
|
||||
_ => Value::Void,
|
||||
}
|
||||
});
|
||||
|
||||
// --- Shift Left (<<) ---
|
||||
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;
|
||||
}
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a << b),
|
||||
_ => Value::Void,
|
||||
}
|
||||
});
|
||||
|
||||
// --- Shift Right (>>) ---
|
||||
env.register_native(">>", shift_ty, true, |args| {
|
||||
if args.len() != 2 {
|
||||
return Value::Void;
|
||||
}
|
||||
match (&args[0], &args[1]) {
|
||||
(Value::Int(a), Value::Int(b)) => Value::Int(a >> b),
|
||||
_ => Value::Void,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+29
-25
@@ -1,25 +1,29 @@
|
||||
use crate::ast::types::{Value, StaticType, Signature};
|
||||
use crate::ast::environment::Environment;
|
||||
use chrono::{NaiveDate, NaiveDateTime};
|
||||
|
||||
pub fn register(env: &Environment) {
|
||||
let date_ty = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(vec![StaticType::Text]),
|
||||
ret: StaticType::DateTime
|
||||
}));
|
||||
|
||||
env.register_native("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();
|
||||
return Value::DateTime(ts);
|
||||
}
|
||||
// Try parse YYYY-MM-DD HH:MM:SS
|
||||
if let Ok(dt) = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S") {
|
||||
return Value::DateTime(dt.and_utc().timestamp_millis());
|
||||
}
|
||||
}
|
||||
Value::Void
|
||||
});
|
||||
}
|
||||
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,
|
||||
}));
|
||||
|
||||
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();
|
||||
return Value::DateTime(ts);
|
||||
}
|
||||
// Try parse YYYY-MM-DD HH:MM:SS
|
||||
if let Ok(dt) = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S") {
|
||||
return Value::DateTime(dt.and_utc().timestamp_millis());
|
||||
}
|
||||
}
|
||||
Value::Void
|
||||
});
|
||||
}
|
||||
|
||||
+116
-108
@@ -1,108 +1,116 @@
|
||||
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.
|
||||
pub fn lookup(name: &str, args: &[StaticType]) -> Option<(Value, StaticType)> {
|
||||
match (name, args) {
|
||||
// --- Integer Arithmetic ---
|
||||
("+", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Int(a + b)
|
||||
} else {
|
||||
Value::Int(0) // Should not happen if type checker works
|
||||
}
|
||||
})),
|
||||
StaticType::Int
|
||||
)),
|
||||
("-", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Int(a - b)
|
||||
} else {
|
||||
Value::Int(0)
|
||||
}
|
||||
})),
|
||||
StaticType::Int
|
||||
)),
|
||||
("-", [StaticType::Int, StaticType::Int, StaticType::Int]) => Some((
|
||||
// Variadic optimization for 3 args (common in some Lisp dialects, though - usually is binary/unary)
|
||||
// MyC's core.rs supports variadic subtraction.
|
||||
Value::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 };
|
||||
Value::Int(a - b - c)
|
||||
})),
|
||||
StaticType::Int
|
||||
)),
|
||||
("*", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Int(a * b)
|
||||
} else {
|
||||
Value::Int(0)
|
||||
}
|
||||
})),
|
||||
StaticType::Int
|
||||
)),
|
||||
|
||||
// --- Integer Comparison ---
|
||||
("<=", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a <= b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
})),
|
||||
StaticType::Bool
|
||||
)),
|
||||
("<", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a < b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
})),
|
||||
StaticType::Bool
|
||||
)),
|
||||
(">", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a > b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
})),
|
||||
StaticType::Bool
|
||||
)),
|
||||
(">=", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a >= b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
})),
|
||||
StaticType::Bool
|
||||
)),
|
||||
("=", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a == b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
})),
|
||||
StaticType::Bool
|
||||
)),
|
||||
|
||||
// --- Constant Unary for -1 (decrement optimization) ---
|
||||
// Special case: tak uses (- x 1). The specializer sees Call("-", [Int, Int]).
|
||||
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
use crate::ast::types::{StaticType, Value};
|
||||
use std::rc::Rc;
|
||||
|
||||
/// Looks up a specialized intrinsic function for the given operator and argument types.
|
||||
/// Returns (Executable Value, Return Type) if a fast-path exists.
|
||||
pub fn lookup(name: &str, args: &[StaticType]) -> Option<(Value, StaticType)> {
|
||||
match (name, args) {
|
||||
// --- Integer Arithmetic ---
|
||||
("+", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Int(a + b)
|
||||
} else {
|
||||
Value::Int(0) // Should not happen if type checker works
|
||||
}
|
||||
})),
|
||||
StaticType::Int,
|
||||
)),
|
||||
("-", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Int(a - b)
|
||||
} else {
|
||||
Value::Int(0)
|
||||
}
|
||||
})),
|
||||
StaticType::Int,
|
||||
)),
|
||||
("-", [StaticType::Int, StaticType::Int, StaticType::Int]) => Some((
|
||||
// Variadic optimization for 3 args (common in some Lisp dialects, though - usually is binary/unary)
|
||||
// MyC's core.rs supports variadic subtraction.
|
||||
Value::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,
|
||||
};
|
||||
Value::Int(a - b - c)
|
||||
})),
|
||||
StaticType::Int,
|
||||
)),
|
||||
("*", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Int(a * b)
|
||||
} else {
|
||||
Value::Int(0)
|
||||
}
|
||||
})),
|
||||
StaticType::Int,
|
||||
)),
|
||||
|
||||
// --- Integer Comparison ---
|
||||
("<=", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a <= b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
})),
|
||||
StaticType::Bool,
|
||||
)),
|
||||
("<", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a < b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
})),
|
||||
StaticType::Bool,
|
||||
)),
|
||||
(">", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a > b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
})),
|
||||
StaticType::Bool,
|
||||
)),
|
||||
(">=", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a >= b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
})),
|
||||
StaticType::Bool,
|
||||
)),
|
||||
("=", [StaticType::Int, StaticType::Int]) => Some((
|
||||
Value::Function(Rc::new(|args| {
|
||||
if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
|
||||
Value::Bool(a == b)
|
||||
} else {
|
||||
Value::Bool(false)
|
||||
}
|
||||
})),
|
||||
StaticType::Bool,
|
||||
)),
|
||||
|
||||
// --- Constant Unary for -1 (decrement optimization) ---
|
||||
// Special case: tak uses (- x 1). The specializer sees Call("-", [Int, Int]).
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
+11
-11
@@ -1,11 +1,11 @@
|
||||
pub mod core;
|
||||
pub mod datetime;
|
||||
pub mod type_registry;
|
||||
pub mod intrinsics;
|
||||
|
||||
use crate::ast::environment::Environment;
|
||||
|
||||
pub fn register(env: &Environment) {
|
||||
core::register(env);
|
||||
datetime::register(env);
|
||||
}
|
||||
pub mod core;
|
||||
pub mod datetime;
|
||||
pub mod intrinsics;
|
||||
pub mod type_registry;
|
||||
|
||||
use crate::ast::environment::Environment;
|
||||
|
||||
pub fn register(env: &Environment) {
|
||||
core::register(env);
|
||||
datetime::register(env);
|
||||
}
|
||||
|
||||
+276
-249
@@ -1,249 +1,276 @@
|
||||
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 {
|
||||
/// Returns the name of the type for debugging/AST.
|
||||
fn type_name() -> &'static str;
|
||||
|
||||
/// Returns the static type definition (methods, properties) for the AST.
|
||||
fn static_type() -> StaticType;
|
||||
|
||||
/// Wraps the instance into a Value (usually a Record of closures).
|
||||
fn wrap(self) -> Value;
|
||||
}
|
||||
|
||||
/// A registry for tracking registered types and their static definitions.
|
||||
pub struct TypeRegistry {
|
||||
known_types: HashMap<TypeId, StaticType>,
|
||||
}
|
||||
|
||||
impl Default for TypeRegistry {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeRegistry {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
known_types: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Registers a type T. Equivalent to `RegisterType<T>` in Delphi.
|
||||
pub fn register<T: Scriptable>(&mut self) {
|
||||
let id = TypeId::of::<T>();
|
||||
self.known_types.entry(id).or_insert_with(T::static_type);
|
||||
}
|
||||
|
||||
/// Resolves the static type for a Rust type.
|
||||
pub fn resolve_type<T: 'static>(&self) -> StaticType {
|
||||
self.known_types.get(&TypeId::of::<T>()).cloned().unwrap_or(StaticType::Any)
|
||||
}
|
||||
|
||||
/// Creates a factory function value that can be bound in the environment.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `factory_func` - A Rust closure that takes script arguments and returns a Result<T, String>.
|
||||
pub fn create_factory<T, F>(
|
||||
factory_func: F
|
||||
) -> Value
|
||||
where
|
||||
T: Scriptable,
|
||||
F: Fn(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 {
|
||||
match factory_func(args) {
|
||||
Ok(instance) => instance.wrap(),
|
||||
Err(msg) => {
|
||||
// In a real system, we'd propagate this error. For now, panic or return Void.
|
||||
// Delphi returns Void if nil, but raises exception on error.
|
||||
// Since Value doesn't have Error, we panic to stop execution.
|
||||
panic!("Runtime Error in Factory for {}: {}", T::type_name(), msg);
|
||||
}
|
||||
}
|
||||
};
|
||||
Value::Function(Rc::new(closure))
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper to build the shadow record for an instance.
|
||||
/// This is used inside `Scriptable::wrap`.
|
||||
pub struct RecordBuilder {
|
||||
fields: Vec<(Keyword, Value)>,
|
||||
}
|
||||
|
||||
impl Default for RecordBuilder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl RecordBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
fields: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn method<F>(mut self, name: &str, func: F) -> Self
|
||||
where F: Fn(Vec<Value>) -> Value + 'static
|
||||
{
|
||||
let key = Keyword::intern(name);
|
||||
self.fields.push((key, Value::Function(Rc::new(func))));
|
||||
self
|
||||
}
|
||||
|
||||
// Helper for methods that return Result (propagating panics for now)
|
||||
pub fn method_checked<F>(self, name: &str, func: F) -> Self
|
||||
where F: Fn(Vec<Value>) -> Result<Value, String> + 'static
|
||||
{
|
||||
let closure = move |args: Vec<Value>| {
|
||||
match func(args) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("Method call error: {}", e),
|
||||
}
|
||||
};
|
||||
self.method(name, closure)
|
||||
}
|
||||
|
||||
pub fn build(self) -> Value {
|
||||
let mut keys = Vec::with_capacity(self.fields.len());
|
||||
let mut values = Vec::with_capacity(self.fields.len());
|
||||
for (k, v) in self.fields {
|
||||
keys.push(k);
|
||||
values.push(v);
|
||||
}
|
||||
Value::make_record(keys, values)
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper to build the StaticType definition.
|
||||
pub struct TypeBuilder {
|
||||
fields: Vec<(Keyword, StaticType)>,
|
||||
}
|
||||
|
||||
impl Default for TypeBuilder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
fields: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn method(mut self, name: &str, params: Vec<StaticType>, ret: StaticType) -> Self {
|
||||
let key = Keyword::intern(name);
|
||||
let sig = StaticType::Function(Box::new(Signature { params: StaticType::Tuple(params), ret }));
|
||||
self.fields.push((key, sig));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> StaticType {
|
||||
StaticType::Record(Rc::new(self.fields))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ast::types::{Value, StaticType};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Person {
|
||||
name: String,
|
||||
age: u32,
|
||||
}
|
||||
|
||||
impl Scriptable for Person {
|
||||
fn type_name() -> &'static str { "Person" }
|
||||
|
||||
fn static_type() -> StaticType {
|
||||
TypeBuilder::new()
|
||||
.method("greet", vec![], StaticType::Text)
|
||||
.method("older", vec![], StaticType::Int)
|
||||
.build()
|
||||
}
|
||||
|
||||
fn wrap(self) -> Value {
|
||||
let name = self.name.clone();
|
||||
let age = self.age;
|
||||
|
||||
RecordBuilder::new()
|
||||
.method("greet", move |_| Value::Text(format!("Hello {}", name).into()))
|
||||
.method("older", move |_| Value::Int((age + 1) as i64))
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_register_and_wrap() {
|
||||
let mut registry = TypeRegistry::new();
|
||||
registry.register::<Person>();
|
||||
|
||||
let p = Person { name: "Alice".to_string(), age: 30 };
|
||||
let wrapped = p.wrap();
|
||||
|
||||
// Check static type
|
||||
let st = TypeRegistry::resolve_type::<Person>(®istry);
|
||||
if let StaticType::Record(fields) = st {
|
||||
assert!(fields.iter().any(|(k, _)| *k == Keyword::intern("greet")));
|
||||
} else {
|
||||
panic!("Expected Record type");
|
||||
}
|
||||
|
||||
// Check runtime behavior
|
||||
if let Value::Record(r) = wrapped {
|
||||
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 {
|
||||
let res = f(vec![]);
|
||||
if let Value::Text(s) = res {
|
||||
assert_eq!(&*s, "Hello Alice");
|
||||
} else {
|
||||
panic!("Expected Text result");
|
||||
}
|
||||
} else {
|
||||
panic!("Expected Function value for method");
|
||||
}
|
||||
} else {
|
||||
panic!("Expected Record value");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_factory() {
|
||||
let factory_val = TypeRegistry::create_factory(|args: Vec<Value>| {
|
||||
if args.len() != 2 {
|
||||
return Err("Expected 2 args".to_string());
|
||||
}
|
||||
let name = match &args[0] { Value::Text(t) => t.to_string(), _ => return Err("Name must be text".to_string()) };
|
||||
let age = match &args[1] { Value::Int(i) => *i as u32, _ => return Err("Age must be int".to_string()) };
|
||||
Ok(Person { name, age })
|
||||
});
|
||||
|
||||
if let Value::Function(f) = factory_val {
|
||||
let instance = f(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 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!("Factory should return Record"); }
|
||||
} else { panic!("Factory is not a function"); }
|
||||
}
|
||||
}
|
||||
use crate::ast::types::{Keyword, Signature, StaticType, Value};
|
||||
use std::any::TypeId;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// Represents a Rust type that can be exposed to the script environment.
|
||||
pub trait Scriptable: 'static + Sized {
|
||||
/// Returns the name of the type for debugging/AST.
|
||||
fn type_name() -> &'static str;
|
||||
|
||||
/// Returns the static type definition (methods, properties) for the AST.
|
||||
fn static_type() -> StaticType;
|
||||
|
||||
/// Wraps the instance into a Value (usually a Record of closures).
|
||||
fn wrap(self) -> Value;
|
||||
}
|
||||
|
||||
/// A registry for tracking registered types and their static definitions.
|
||||
pub struct TypeRegistry {
|
||||
known_types: HashMap<TypeId, StaticType>,
|
||||
}
|
||||
|
||||
impl Default for TypeRegistry {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeRegistry {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
known_types: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Registers a type T. Equivalent to `RegisterType<T>` in Delphi.
|
||||
pub fn register<T: Scriptable>(&mut self) {
|
||||
let id = TypeId::of::<T>();
|
||||
self.known_types.entry(id).or_insert_with(T::static_type);
|
||||
}
|
||||
|
||||
/// Resolves the static type for a Rust type.
|
||||
pub fn resolve_type<T: 'static>(&self) -> StaticType {
|
||||
self.known_types
|
||||
.get(&TypeId::of::<T>())
|
||||
.cloned()
|
||||
.unwrap_or(StaticType::Any)
|
||||
}
|
||||
|
||||
/// Creates a factory function value that can be bound in the environment.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `factory_func` - A Rust closure that takes script arguments and returns a Result<T, String>.
|
||||
pub fn create_factory<T, F>(factory_func: F) -> Value
|
||||
where
|
||||
T: Scriptable,
|
||||
F: Fn(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 {
|
||||
match factory_func(args) {
|
||||
Ok(instance) => instance.wrap(),
|
||||
Err(msg) => {
|
||||
// In a real system, we'd propagate this error. For now, panic or return Void.
|
||||
// Delphi returns Void if nil, but raises exception on error.
|
||||
// Since Value doesn't have Error, we panic to stop execution.
|
||||
panic!("Runtime Error in Factory for {}: {}", T::type_name(), msg);
|
||||
}
|
||||
}
|
||||
};
|
||||
Value::Function(Rc::new(closure))
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper to build the shadow record for an instance.
|
||||
/// This is used inside `Scriptable::wrap`.
|
||||
pub struct RecordBuilder {
|
||||
fields: Vec<(Keyword, Value)>,
|
||||
}
|
||||
|
||||
impl Default for RecordBuilder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl RecordBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self { fields: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn method<F>(mut self, name: &str, func: F) -> Self
|
||||
where
|
||||
F: Fn(Vec<Value>) -> Value + 'static,
|
||||
{
|
||||
let key = Keyword::intern(name);
|
||||
self.fields.push((key, Value::Function(Rc::new(func))));
|
||||
self
|
||||
}
|
||||
|
||||
// Helper for methods that return Result (propagating panics for now)
|
||||
pub fn method_checked<F>(self, name: &str, func: F) -> Self
|
||||
where
|
||||
F: Fn(Vec<Value>) -> Result<Value, String> + 'static,
|
||||
{
|
||||
let closure = move |args: Vec<Value>| match func(args) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("Method call error: {}", e),
|
||||
};
|
||||
self.method(name, closure)
|
||||
}
|
||||
|
||||
pub fn build(self) -> Value {
|
||||
let mut keys = Vec::with_capacity(self.fields.len());
|
||||
let mut values = Vec::with_capacity(self.fields.len());
|
||||
for (k, v) in self.fields {
|
||||
keys.push(k);
|
||||
values.push(v);
|
||||
}
|
||||
Value::make_record(keys, values)
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper to build the StaticType definition.
|
||||
pub struct TypeBuilder {
|
||||
fields: Vec<(Keyword, StaticType)>,
|
||||
}
|
||||
|
||||
impl Default for TypeBuilder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self { fields: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn method(mut self, name: &str, params: Vec<StaticType>, ret: StaticType) -> Self {
|
||||
let key = Keyword::intern(name);
|
||||
let sig = StaticType::Function(Box::new(Signature {
|
||||
params: StaticType::Tuple(params),
|
||||
ret,
|
||||
}));
|
||||
self.fields.push((key, sig));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> StaticType {
|
||||
StaticType::Record(Rc::new(self.fields))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ast::types::{StaticType, Value};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Person {
|
||||
name: String,
|
||||
age: u32,
|
||||
}
|
||||
|
||||
impl Scriptable for Person {
|
||||
fn type_name() -> &'static str {
|
||||
"Person"
|
||||
}
|
||||
|
||||
fn static_type() -> StaticType {
|
||||
TypeBuilder::new()
|
||||
.method("greet", vec![], StaticType::Text)
|
||||
.method("older", vec![], StaticType::Int)
|
||||
.build()
|
||||
}
|
||||
|
||||
fn wrap(self) -> Value {
|
||||
let name = self.name.clone();
|
||||
let age = self.age;
|
||||
|
||||
RecordBuilder::new()
|
||||
.method("greet", move |_| {
|
||||
Value::Text(format!("Hello {}", name).into())
|
||||
})
|
||||
.method("older", move |_| Value::Int((age + 1) as i64))
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_register_and_wrap() {
|
||||
let mut registry = TypeRegistry::new();
|
||||
registry.register::<Person>();
|
||||
|
||||
let p = Person {
|
||||
name: "Alice".to_string(),
|
||||
age: 30,
|
||||
};
|
||||
let wrapped = p.wrap();
|
||||
|
||||
// Check static type
|
||||
let st = TypeRegistry::resolve_type::<Person>(®istry);
|
||||
if let StaticType::Record(fields) = st {
|
||||
assert!(fields.iter().any(|(k, _)| *k == Keyword::intern("greet")));
|
||||
} else {
|
||||
panic!("Expected Record type");
|
||||
}
|
||||
|
||||
// Check runtime behavior
|
||||
if let Value::Record(r) = wrapped {
|
||||
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 {
|
||||
let res = f(vec![]);
|
||||
if let Value::Text(s) = res {
|
||||
assert_eq!(&*s, "Hello Alice");
|
||||
} else {
|
||||
panic!("Expected Text result");
|
||||
}
|
||||
} else {
|
||||
panic!("Expected Function value for method");
|
||||
}
|
||||
} else {
|
||||
panic!("Expected Record value");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_factory() {
|
||||
let factory_val = TypeRegistry::create_factory(|args: Vec<Value>| {
|
||||
if args.len() != 2 {
|
||||
return Err("Expected 2 args".to_string());
|
||||
}
|
||||
let name = match &args[0] {
|
||||
Value::Text(t) => t.to_string(),
|
||||
_ => return Err("Name must be text".to_string()),
|
||||
};
|
||||
let age = match &args[1] {
|
||||
Value::Int(i) => *i as u32,
|
||||
_ => return Err("Age must be int".to_string()),
|
||||
};
|
||||
Ok(Person { name, age })
|
||||
});
|
||||
|
||||
if let Value::Function(f) = factory_val {
|
||||
let instance = f(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 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!("Factory should return Record");
|
||||
}
|
||||
} else {
|
||||
panic!("Factory is not a function");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+392
-370
@@ -1,370 +1,392 @@
|
||||
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};
|
||||
|
||||
/// Simple source location
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct SourceLocation {
|
||||
pub line: u32,
|
||||
pub col: u32,
|
||||
}
|
||||
|
||||
/// Shared identity for nodes (Location, etc.)
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct NodeIdentity {
|
||||
pub location: SourceLocation,
|
||||
}
|
||||
|
||||
pub type Identity = Rc<NodeIdentity>;
|
||||
|
||||
/// Interned string identifier
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct Keyword(pub u32);
|
||||
|
||||
static KEYWORD_REGISTRY: OnceLock<Mutex<HashMap<String, u32>>> = OnceLock::new();
|
||||
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();
|
||||
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 id = rev.len() as u32;
|
||||
reg.insert(name.to_string(), id);
|
||||
rev.push(name.to_string());
|
||||
Keyword(id)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(&self) -> String {
|
||||
let rev = KEYWORD_REVERSE.get_or_init(|| Mutex::new(Vec::new())).lock().unwrap();
|
||||
rev[self.0 as usize].clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// Interface for custom objects (Closures, Series, Streams)
|
||||
pub trait Object: fmt::Debug {
|
||||
fn type_name(&self) -> &'static str;
|
||||
fn as_any(&self) -> &dyn Any;
|
||||
}
|
||||
|
||||
/// A shared sequence of values, used by both Tuples and Records.
|
||||
pub type ValueList = Rc<Vec<Value>>;
|
||||
|
||||
/// Internal storage for Records to allow sharing schema (keys) between instances.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct RecordData {
|
||||
/// Names for slots.
|
||||
pub keys: Rc<Vec<Keyword>>,
|
||||
/// The actual values, potentially shared with a Tuple.
|
||||
pub values: ValueList,
|
||||
}
|
||||
|
||||
/// Core data value in Myc Script (similar to TDataValue)
|
||||
#[derive(Clone)]
|
||||
pub enum Value {
|
||||
Void,
|
||||
Bool(bool),
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
DateTime(i64),
|
||||
Text(Rc<str>),
|
||||
Keyword(Keyword),
|
||||
Tuple(ValueList),
|
||||
Record(Rc<RecordData>),
|
||||
Function(Rc<dyn Fn(Vec<Value>) -> Value>),
|
||||
Object(Rc<dyn Object>), // For compiled Closures and other opaque types
|
||||
Cell(Rc<RefCell<Value>>), // Boxed value for captures
|
||||
TailCallRequest(Box<(Rc<dyn Object>, Vec<Value>)>), // Internal: For TCO (Boxed to keep Value small)
|
||||
}
|
||||
|
||||
impl PartialEq for Value {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self, other) {
|
||||
(Value::Void, Value::Void) => true,
|
||||
(Value::Bool(a), Value::Bool(b)) => a == b,
|
||||
(Value::Int(a), Value::Int(b)) => a == b,
|
||||
(Value::Float(a), Value::Float(b)) => a == b,
|
||||
(Value::DateTime(a), Value::DateTime(b)) => a == b,
|
||||
(Value::Text(a), Value::Text(b)) => a == b,
|
||||
(Value::Keyword(a), Value::Keyword(b)) => a == b,
|
||||
(Value::Tuple(a), Value::Tuple(b)) => a == b,
|
||||
(Value::Record(a), Value::Record(b)) => a == b,
|
||||
(Value::Function(a), Value::Function(b)) => Rc::ptr_eq(a, b),
|
||||
(Value::Object(a), Value::Object(b)) => Rc::ptr_eq(a, b),
|
||||
(Value::Cell(a), Value::Cell(b)) => Rc::ptr_eq(a, b),
|
||||
(Value::TailCallRequest(a), Value::TailCallRequest(b)) => {
|
||||
Rc::ptr_eq(&a.0, &b.0) && a.1 == b.1
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Signature {
|
||||
pub params: StaticType,
|
||||
pub ret: StaticType,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum StaticType {
|
||||
Any,
|
||||
Void,
|
||||
Bool,
|
||||
Int,
|
||||
Float,
|
||||
DateTime,
|
||||
Text,
|
||||
Keyword,
|
||||
List(Box<StaticType>), // Legacy / Dynamic list
|
||||
Tuple(Vec<StaticType>), // Heterogeneous fixed-size
|
||||
Vector(Box<StaticType>, usize), // Homogeneous fixed-size
|
||||
Matrix(Box<StaticType>, Vec<usize>), // Multi-dimensional homogeneous
|
||||
Record(Rc<Vec<(Keyword, StaticType)>>),
|
||||
Function(Box<Signature>),
|
||||
FunctionOverloads(Vec<Signature>),
|
||||
Object(&'static str),
|
||||
}
|
||||
|
||||
impl fmt::Display for StaticType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
StaticType::Any => write!(f, "any"),
|
||||
StaticType::Void => write!(f, "void"),
|
||||
StaticType::Bool => write!(f, "bool"),
|
||||
StaticType::Int => write!(f, "int"),
|
||||
StaticType::Float => write!(f, "float"),
|
||||
StaticType::DateTime => write!(f, "datetime"),
|
||||
StaticType::Text => write!(f, "text"),
|
||||
StaticType::Keyword => write!(f, "keyword"),
|
||||
StaticType::List(inner) => write!(f, "[{}]", inner),
|
||||
StaticType::Tuple(elements) => {
|
||||
write!(f, "[")?;
|
||||
for (i, el) in elements.iter().enumerate() {
|
||||
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, " ")?; }
|
||||
write!(f, "{}", s)?;
|
||||
}
|
||||
write!(f, "]>")
|
||||
},
|
||||
StaticType::Record(fields) => {
|
||||
write!(f, "{{")?;
|
||||
for (i, (k, v)) in fields.iter().enumerate() {
|
||||
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())
|
||||
}
|
||||
StaticType::Object(name) => write!(f, "{}", name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StaticType {
|
||||
/// Returns true if `other` can be assigned to a location of type `self`.
|
||||
pub fn is_assignable_from(&self, other: &StaticType) -> bool {
|
||||
if self == other || matches!(self, StaticType::Any) || matches!(other, StaticType::Any) {
|
||||
return true;
|
||||
}
|
||||
|
||||
match (self, other) {
|
||||
// A Vector is a Tuple
|
||||
(StaticType::Tuple(elements), StaticType::Vector(inner, len)) => {
|
||||
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.len() == 1 {
|
||||
inner.is_assignable_from(m_inner)
|
||||
} else {
|
||||
// It's a matrix of higher dimension, so inner must be assignable from a sub-matrix
|
||||
let sub_shape = shape[1..].to_vec();
|
||||
inner.is_assignable_from(&StaticType::Matrix(m_inner.clone(), sub_shape))
|
||||
}
|
||||
},
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to resolve a call with the given argument type (usually a Tuple) and returns the return type.
|
||||
pub fn resolve_call(&self, args_ty: &StaticType) -> Option<StaticType> {
|
||||
match self {
|
||||
StaticType::Any => Some(StaticType::Any),
|
||||
StaticType::Function(sig) => {
|
||||
if sig.params.is_assignable_from(args_ty) {
|
||||
Some(sig.ret.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
StaticType::FunctionOverloads(sigs) => {
|
||||
sigs.iter()
|
||||
.find(|sig| sig.params.is_assignable_from(args_ty))
|
||||
.map(|sig| sig.ret.clone())
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if this type and all its recursive elements are scalars (Int, Float, etc.)
|
||||
pub fn is_scalar_pure(&self) -> bool {
|
||||
match self {
|
||||
StaticType::Int | StaticType::Float | StaticType::Bool | StaticType::DateTime => true,
|
||||
StaticType::Tuple(elements) => elements.iter().all(|e| e.is_scalar_pure()),
|
||||
StaticType::Vector(inner, _) => inner.is_scalar_pure(),
|
||||
StaticType::Matrix(inner, _) => inner.is_scalar_pure(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Value {
|
||||
pub fn is_truthy(&self) -> bool {
|
||||
match self {
|
||||
Value::Void => false,
|
||||
Value::Bool(b) => *b,
|
||||
Value::Cell(c) => c.borrow().is_truthy(),
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the underlying values as a slice if this is a Tuple or Record.
|
||||
pub fn as_slice(&self) -> Option<&[Value]> {
|
||||
match self {
|
||||
Value::Tuple(v) => Some(v),
|
||||
Value::Record(r) => Some(&r.values),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn make_tuple(values: Vec<Value>) -> Self {
|
||||
Value::Tuple(Rc::new(values))
|
||||
}
|
||||
|
||||
pub fn make_record(keys: Vec<Keyword>, values: Vec<Value>) -> Self {
|
||||
Value::Record(Rc::new(RecordData {
|
||||
keys: Rc::new(keys),
|
||||
values: Rc::new(values)
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn static_type(&self) -> StaticType {
|
||||
match self {
|
||||
Value::Void => StaticType::Void,
|
||||
Value::Bool(_) => StaticType::Bool,
|
||||
Value::Int(_) => StaticType::Int,
|
||||
Value::Float(_) => StaticType::Float,
|
||||
Value::DateTime(_) => StaticType::DateTime,
|
||||
Value::Text(_) => StaticType::Text,
|
||||
Value::Keyword(_) => StaticType::Keyword,
|
||||
Value::Tuple(values) => {
|
||||
if values.is_empty() {
|
||||
return StaticType::Vector(Box::new(StaticType::Any), 0);
|
||||
}
|
||||
|
||||
let element_types: Vec<_> = values.iter().map(|v| v.static_type()).collect();
|
||||
|
||||
// Check for Homogeneity (Vector)
|
||||
let first_ty = &element_types[0];
|
||||
let all_same = element_types.iter().all(|t| t == first_ty);
|
||||
|
||||
if all_same {
|
||||
match first_ty {
|
||||
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())
|
||||
}
|
||||
} 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(),
|
||||
Value::TailCallRequest(_) => StaticType::Any, // Internal state, but typable as Any
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Value {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Value::Void => write!(f, "void"),
|
||||
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::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, " ")?; }
|
||||
write!(f, "{}", val)?;
|
||||
}
|
||||
write!(f, "]")
|
||||
},
|
||||
Value::Record(r) => {
|
||||
write!(f, "{{")?;
|
||||
for i in 0..r.values.len() {
|
||||
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()),
|
||||
Value::TailCallRequest(_) => write!(f, "<tail call request>"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Value {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Display::fmt(self, f)
|
||||
}
|
||||
}
|
||||
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)]
|
||||
pub struct SourceLocation {
|
||||
pub line: u32,
|
||||
pub col: u32,
|
||||
}
|
||||
|
||||
/// Shared identity for nodes (Location, etc.)
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct NodeIdentity {
|
||||
pub location: SourceLocation,
|
||||
}
|
||||
|
||||
pub type Identity = Rc<NodeIdentity>;
|
||||
|
||||
/// Interned string identifier
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct Keyword(pub u32);
|
||||
|
||||
static KEYWORD_REGISTRY: OnceLock<Mutex<HashMap<String, u32>>> = OnceLock::new();
|
||||
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();
|
||||
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 id = rev.len() as u32;
|
||||
reg.insert(name.to_string(), id);
|
||||
rev.push(name.to_string());
|
||||
Keyword(id)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(&self) -> String {
|
||||
let rev = KEYWORD_REVERSE
|
||||
.get_or_init(|| Mutex::new(Vec::new()))
|
||||
.lock()
|
||||
.unwrap();
|
||||
rev[self.0 as usize].clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// Interface for custom objects (Closures, Series, Streams)
|
||||
pub trait Object: fmt::Debug {
|
||||
fn type_name(&self) -> &'static str;
|
||||
fn as_any(&self) -> &dyn Any;
|
||||
}
|
||||
|
||||
/// A shared sequence of values, used by both Tuples and Records.
|
||||
pub type ValueList = Rc<Vec<Value>>;
|
||||
|
||||
/// Internal storage for Records to allow sharing schema (keys) between instances.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct RecordData {
|
||||
/// Names for slots.
|
||||
pub keys: Rc<Vec<Keyword>>,
|
||||
/// The actual values, potentially shared with a Tuple.
|
||||
pub values: ValueList,
|
||||
}
|
||||
|
||||
/// Core data value in Myc Script (similar to TDataValue)
|
||||
#[derive(Clone)]
|
||||
pub enum Value {
|
||||
Void,
|
||||
Bool(bool),
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
DateTime(i64),
|
||||
Text(Rc<str>),
|
||||
Keyword(Keyword),
|
||||
Tuple(ValueList),
|
||||
Record(Rc<RecordData>),
|
||||
Function(Rc<dyn Fn(Vec<Value>) -> Value>),
|
||||
Object(Rc<dyn Object>), // For compiled Closures and other opaque types
|
||||
Cell(Rc<RefCell<Value>>), // Boxed value for captures
|
||||
TailCallRequest(Box<(Rc<dyn Object>, Vec<Value>)>), // Internal: For TCO (Boxed to keep Value small)
|
||||
}
|
||||
|
||||
impl PartialEq for Value {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self, other) {
|
||||
(Value::Void, Value::Void) => true,
|
||||
(Value::Bool(a), Value::Bool(b)) => a == b,
|
||||
(Value::Int(a), Value::Int(b)) => a == b,
|
||||
(Value::Float(a), Value::Float(b)) => a == b,
|
||||
(Value::DateTime(a), Value::DateTime(b)) => a == b,
|
||||
(Value::Text(a), Value::Text(b)) => a == b,
|
||||
(Value::Keyword(a), Value::Keyword(b)) => a == b,
|
||||
(Value::Tuple(a), Value::Tuple(b)) => a == b,
|
||||
(Value::Record(a), Value::Record(b)) => a == b,
|
||||
(Value::Function(a), Value::Function(b)) => Rc::ptr_eq(a, b),
|
||||
(Value::Object(a), Value::Object(b)) => Rc::ptr_eq(a, b),
|
||||
(Value::Cell(a), Value::Cell(b)) => Rc::ptr_eq(a, b),
|
||||
(Value::TailCallRequest(a), Value::TailCallRequest(b)) => {
|
||||
Rc::ptr_eq(&a.0, &b.0) && a.1 == b.1
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Signature {
|
||||
pub params: StaticType,
|
||||
pub ret: StaticType,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum StaticType {
|
||||
Any,
|
||||
Void,
|
||||
Bool,
|
||||
Int,
|
||||
Float,
|
||||
DateTime,
|
||||
Text,
|
||||
Keyword,
|
||||
List(Box<StaticType>), // Legacy / Dynamic list
|
||||
Tuple(Vec<StaticType>), // Heterogeneous fixed-size
|
||||
Vector(Box<StaticType>, usize), // Homogeneous fixed-size
|
||||
Matrix(Box<StaticType>, Vec<usize>), // Multi-dimensional homogeneous
|
||||
Record(Rc<Vec<(Keyword, StaticType)>>),
|
||||
Function(Box<Signature>),
|
||||
FunctionOverloads(Vec<Signature>),
|
||||
Object(&'static str),
|
||||
}
|
||||
|
||||
impl fmt::Display for StaticType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
StaticType::Any => write!(f, "any"),
|
||||
StaticType::Void => write!(f, "void"),
|
||||
StaticType::Bool => write!(f, "bool"),
|
||||
StaticType::Int => write!(f, "int"),
|
||||
StaticType::Float => write!(f, "float"),
|
||||
StaticType::DateTime => write!(f, "datetime"),
|
||||
StaticType::Text => write!(f, "text"),
|
||||
StaticType::Keyword => write!(f, "keyword"),
|
||||
StaticType::List(inner) => write!(f, "[{}]", inner),
|
||||
StaticType::Tuple(elements) => {
|
||||
write!(f, "[")?;
|
||||
for (i, el) in elements.iter().enumerate() {
|
||||
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, " ")?;
|
||||
}
|
||||
write!(f, "{}", s)?;
|
||||
}
|
||||
write!(f, "]>")
|
||||
}
|
||||
StaticType::Record(fields) => {
|
||||
write!(f, "{{")?;
|
||||
for (i, (k, v)) in fields.iter().enumerate() {
|
||||
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())
|
||||
}
|
||||
StaticType::Object(name) => write!(f, "{}", name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StaticType {
|
||||
/// Returns true if `other` can be assigned to a location of type `self`.
|
||||
pub fn is_assignable_from(&self, other: &StaticType) -> bool {
|
||||
if self == other || matches!(self, StaticType::Any) || matches!(other, StaticType::Any) {
|
||||
return true;
|
||||
}
|
||||
|
||||
match (self, other) {
|
||||
// A Vector is a Tuple
|
||||
(StaticType::Tuple(elements), StaticType::Vector(inner, len)) => {
|
||||
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.len() == 1 {
|
||||
inner.is_assignable_from(m_inner)
|
||||
} else {
|
||||
// It's a matrix of higher dimension, so inner must be assignable from a sub-matrix
|
||||
let sub_shape = shape[1..].to_vec();
|
||||
inner.is_assignable_from(&StaticType::Matrix(m_inner.clone(), sub_shape))
|
||||
}
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to resolve a call with the given argument type (usually a Tuple) and returns the return type.
|
||||
pub fn resolve_call(&self, args_ty: &StaticType) -> Option<StaticType> {
|
||||
match self {
|
||||
StaticType::Any => Some(StaticType::Any),
|
||||
StaticType::Function(sig) => {
|
||||
if sig.params.is_assignable_from(args_ty) {
|
||||
Some(sig.ret.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
StaticType::FunctionOverloads(sigs) => sigs
|
||||
.iter()
|
||||
.find(|sig| sig.params.is_assignable_from(args_ty))
|
||||
.map(|sig| sig.ret.clone()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if this type and all its recursive elements are scalars (Int, Float, etc.)
|
||||
pub fn is_scalar_pure(&self) -> bool {
|
||||
match self {
|
||||
StaticType::Int | StaticType::Float | StaticType::Bool | StaticType::DateTime => true,
|
||||
StaticType::Tuple(elements) => elements.iter().all(|e| e.is_scalar_pure()),
|
||||
StaticType::Vector(inner, _) => inner.is_scalar_pure(),
|
||||
StaticType::Matrix(inner, _) => inner.is_scalar_pure(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Value {
|
||||
pub fn is_truthy(&self) -> bool {
|
||||
match self {
|
||||
Value::Void => false,
|
||||
Value::Bool(b) => *b,
|
||||
Value::Cell(c) => c.borrow().is_truthy(),
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the underlying values as a slice if this is a Tuple or Record.
|
||||
pub fn as_slice(&self) -> Option<&[Value]> {
|
||||
match self {
|
||||
Value::Tuple(v) => Some(v),
|
||||
Value::Record(r) => Some(&r.values),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn make_tuple(values: Vec<Value>) -> Self {
|
||||
Value::Tuple(Rc::new(values))
|
||||
}
|
||||
|
||||
pub fn make_record(keys: Vec<Keyword>, values: Vec<Value>) -> Self {
|
||||
Value::Record(Rc::new(RecordData {
|
||||
keys: Rc::new(keys),
|
||||
values: Rc::new(values),
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn static_type(&self) -> StaticType {
|
||||
match self {
|
||||
Value::Void => StaticType::Void,
|
||||
Value::Bool(_) => StaticType::Bool,
|
||||
Value::Int(_) => StaticType::Int,
|
||||
Value::Float(_) => StaticType::Float,
|
||||
Value::DateTime(_) => StaticType::DateTime,
|
||||
Value::Text(_) => StaticType::Text,
|
||||
Value::Keyword(_) => StaticType::Keyword,
|
||||
Value::Tuple(values) => {
|
||||
if values.is_empty() {
|
||||
return StaticType::Vector(Box::new(StaticType::Any), 0);
|
||||
}
|
||||
|
||||
let element_types: Vec<_> = values.iter().map(|v| v.static_type()).collect();
|
||||
|
||||
// Check for Homogeneity (Vector)
|
||||
let first_ty = &element_types[0];
|
||||
let all_same = element_types.iter().all(|t| t == first_ty);
|
||||
|
||||
if all_same {
|
||||
match first_ty {
|
||||
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()),
|
||||
}
|
||||
} 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(),
|
||||
Value::TailCallRequest(_) => StaticType::Any, // Internal state, but typable as Any
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Value {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Value::Void => write!(f, "void"),
|
||||
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::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, " ")?;
|
||||
}
|
||||
write!(f, "{}", val)?;
|
||||
}
|
||||
write!(f, "]")
|
||||
}
|
||||
Value::Record(r) => {
|
||||
write!(f, "{{")?;
|
||||
for i in 0..r.values.len() {
|
||||
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()),
|
||||
Value::TailCallRequest(_) => write!(f, "<tail call request>"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Value {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Display::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
+712
-465
File diff suppressed because it is too large
Load Diff
+134
-132
@@ -1,132 +1,134 @@
|
||||
use myc::ast::environment::Environment;
|
||||
use myc::utils::tester;
|
||||
use clap::Parser;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about = "MYC AST Compiler & Benchmarker", long_about = None)]
|
||||
struct Cli {
|
||||
/// The script file to run or benchmark
|
||||
#[arg(value_name = "FILE")]
|
||||
file: Option<PathBuf>,
|
||||
|
||||
/// Run a script string directly
|
||||
#[arg(short, long)]
|
||||
eval: Option<String>,
|
||||
|
||||
/// Run benchmarks (Only allowed in Release mode)
|
||||
#[arg(short, long)]
|
||||
bench: bool,
|
||||
|
||||
/// Update the benchmark baseline (Only allowed in Release mode)
|
||||
#[arg(short, long)]
|
||||
update_bench: bool,
|
||||
|
||||
/// Dump the compiled AST
|
||||
#[arg(short, long)]
|
||||
dump: bool,
|
||||
|
||||
/// Run with TracingObserver enabled
|
||||
#[arg(short, long)]
|
||||
trace: bool,
|
||||
|
||||
/// Disable optimization
|
||||
#[arg(long)]
|
||||
no_opt: bool,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
let mut env = Environment::new();
|
||||
env.optimization = !cli.no_opt;
|
||||
|
||||
if cli.bench || cli.update_bench {
|
||||
if cfg!(debug_assertions) {
|
||||
eprintln!("❌ ERROR: Benchmarks must be run in Release mode!");
|
||||
eprintln!(" Use: cargo run --release --bin ast -- --bench");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
println!("🚀 Running benchmarks in RELEASE mode...\n");
|
||||
let results = tester::run_benchmarks(cli.update_bench);
|
||||
for res in results {
|
||||
let diff = res.diff_pct.map_or(String::new(), |d| format!(" ({:+.1}%)", d));
|
||||
println!("{}: {} - {:?}{}", res.status, res.name, res.median, diff);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(script_str) = cli.eval {
|
||||
if cli.dump {
|
||||
match env.dump_ast(&script_str) {
|
||||
Ok(dump) => println!("{}", dump),
|
||||
Err(e) => eprintln!("Error dumping AST: {}", e),
|
||||
}
|
||||
} else if cli.trace {
|
||||
execute_trace(&mut env, &script_str);
|
||||
} else {
|
||||
execute(&env, &script_str);
|
||||
}
|
||||
} else if let Some(file_path) = cli.file {
|
||||
match fs::read_to_string(&file_path) {
|
||||
Ok(content) => {
|
||||
if cli.dump {
|
||||
match env.dump_ast(&content) {
|
||||
Ok(dump) => println!("{}", dump),
|
||||
Err(e) => eprintln!("Error dumping AST: {}", e),
|
||||
}
|
||||
} else if cli.trace {
|
||||
execute_trace(&mut env, &content);
|
||||
} else {
|
||||
execute(&env, &content);
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("Error reading file {:?}: {}", file_path, e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("MYC AST Compiler CLI. Use --help for usage.");
|
||||
}
|
||||
}
|
||||
|
||||
fn execute(env: &Environment, source: &str) {
|
||||
match env.run_script(source) {
|
||||
Ok(result) => println!("{}", result),
|
||||
Err(e) => {
|
||||
eprintln!("Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn execute_trace(env: &mut Environment, source: &str) {
|
||||
match env.compile(source) {
|
||||
Ok(compiled) => {
|
||||
let linked = env.link(compiled);
|
||||
let mut vm = myc::ast::vm::VM::new(env.global_values.clone());
|
||||
let mut observer = myc::ast::vm::TracingObserver::new();
|
||||
match vm.run_with_observer(&mut observer, &linked) {
|
||||
Ok(result) => {
|
||||
for line in observer.logs {
|
||||
println!("{}", line);
|
||||
}
|
||||
println!("Result: {}", result);
|
||||
}
|
||||
Err(e) => {
|
||||
for line in observer.logs {
|
||||
println!("{}", line);
|
||||
}
|
||||
eprintln!("Runtime Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Compilation Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
use clap::Parser;
|
||||
use myc::ast::environment::Environment;
|
||||
use myc::utils::tester;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about = "MYC AST Compiler & Benchmarker", long_about = None)]
|
||||
struct Cli {
|
||||
/// The script file to run or benchmark
|
||||
#[arg(value_name = "FILE")]
|
||||
file: Option<PathBuf>,
|
||||
|
||||
/// Run a script string directly
|
||||
#[arg(short, long)]
|
||||
eval: Option<String>,
|
||||
|
||||
/// Run benchmarks (Only allowed in Release mode)
|
||||
#[arg(short, long)]
|
||||
bench: bool,
|
||||
|
||||
/// Update the benchmark baseline (Only allowed in Release mode)
|
||||
#[arg(short, long)]
|
||||
update_bench: bool,
|
||||
|
||||
/// Dump the compiled AST
|
||||
#[arg(short, long)]
|
||||
dump: bool,
|
||||
|
||||
/// Run with TracingObserver enabled
|
||||
#[arg(short, long)]
|
||||
trace: bool,
|
||||
|
||||
/// Disable optimization
|
||||
#[arg(long)]
|
||||
no_opt: bool,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
let mut env = Environment::new();
|
||||
env.optimization = !cli.no_opt;
|
||||
|
||||
if cli.bench || cli.update_bench {
|
||||
if cfg!(debug_assertions) {
|
||||
eprintln!("❌ ERROR: Benchmarks must be run in Release mode!");
|
||||
eprintln!(" Use: cargo run --release --bin ast -- --bench");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
println!("🚀 Running benchmarks in RELEASE mode...\n");
|
||||
let results = tester::run_benchmarks(cli.update_bench);
|
||||
for res in results {
|
||||
let diff = res
|
||||
.diff_pct
|
||||
.map_or(String::new(), |d| format!(" ({:+.1}%)", d));
|
||||
println!("{}: {} - {:?}{}", res.status, res.name, res.median, diff);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(script_str) = cli.eval {
|
||||
if cli.dump {
|
||||
match env.dump_ast(&script_str) {
|
||||
Ok(dump) => println!("{}", dump),
|
||||
Err(e) => eprintln!("Error dumping AST: {}", e),
|
||||
}
|
||||
} else if cli.trace {
|
||||
execute_trace(&mut env, &script_str);
|
||||
} else {
|
||||
execute(&env, &script_str);
|
||||
}
|
||||
} else if let Some(file_path) = cli.file {
|
||||
match fs::read_to_string(&file_path) {
|
||||
Ok(content) => {
|
||||
if cli.dump {
|
||||
match env.dump_ast(&content) {
|
||||
Ok(dump) => println!("{}", dump),
|
||||
Err(e) => eprintln!("Error dumping AST: {}", e),
|
||||
}
|
||||
} else if cli.trace {
|
||||
execute_trace(&mut env, &content);
|
||||
} else {
|
||||
execute(&env, &content);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error reading file {:?}: {}", file_path, e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("MYC AST Compiler CLI. Use --help for usage.");
|
||||
}
|
||||
}
|
||||
|
||||
fn execute(env: &Environment, source: &str) {
|
||||
match env.run_script(source) {
|
||||
Ok(result) => println!("{}", result),
|
||||
Err(e) => {
|
||||
eprintln!("Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn execute_trace(env: &mut Environment, source: &str) {
|
||||
match env.compile(source) {
|
||||
Ok(compiled) => {
|
||||
let linked = env.link(compiled);
|
||||
let mut vm = myc::ast::vm::VM::new(env.global_values.clone());
|
||||
let mut observer = myc::ast::vm::TracingObserver::new();
|
||||
match vm.run_with_observer(&mut observer, &linked) {
|
||||
Ok(result) => {
|
||||
for line in observer.logs {
|
||||
println!("{}", line);
|
||||
}
|
||||
println!("Result: {}", result);
|
||||
}
|
||||
Err(e) => {
|
||||
for line in observer.logs {
|
||||
println!("{}", line);
|
||||
}
|
||||
eprintln!("Runtime Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Compilation Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+334
-328
@@ -1,328 +1,334 @@
|
||||
use crate::ast::environment::Environment;
|
||||
use regex::Regex;
|
||||
use std::fs;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
pub struct TestResult {
|
||||
pub name: String,
|
||||
pub success: bool,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
pub struct BenchmarkResult {
|
||||
pub name: String,
|
||||
pub median: Duration,
|
||||
pub baseline: Option<Duration>,
|
||||
pub diff_pct: Option<f64>,
|
||||
pub status: String,
|
||||
}
|
||||
|
||||
pub fn run_functional_tests() -> Vec<TestResult> {
|
||||
run_functional_tests_with_optimization(false)
|
||||
}
|
||||
|
||||
pub fn run_functional_tests_with_optimization(enabled: bool) -> Vec<TestResult> {
|
||||
let mut results = Vec::new();
|
||||
let entries = fs::read_dir("examples").unwrap();
|
||||
let output_re = Regex::new(r";; Output: (.*)").unwrap();
|
||||
|
||||
for entry in entries.filter_map(|e| e.ok()) {
|
||||
let mut env = Environment::new(); // Fresh environment per test file
|
||||
env.optimization = enabled;
|
||||
let path = entry.path();
|
||||
if path.extension().is_some_and(|ext| ext == "myc") {
|
||||
let content = fs::read_to_string(&path).unwrap();
|
||||
let name = path.file_name().unwrap().to_string_lossy().to_string();
|
||||
|
||||
let expected_output = output_re
|
||||
.captures(&content)
|
||||
.map(|m| m.get(1).unwrap().as_str().trim().to_string());
|
||||
|
||||
if let Some(expected) = expected_output {
|
||||
match env.run_script(&content) {
|
||||
Ok(val) => {
|
||||
let val_str = format!("{}", val);
|
||||
if val_str == expected {
|
||||
results.push(TestResult {
|
||||
name,
|
||||
success: true,
|
||||
message: format!("OK: {}", val_str),
|
||||
});
|
||||
} else {
|
||||
results.push(TestResult {
|
||||
name,
|
||||
success: false,
|
||||
message: format!(
|
||||
"Opt {}: Expected {}, got {}",
|
||||
if enabled { "ON" } else { "OFF" }, expected, val_str
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
Err(e) => results.push(TestResult {
|
||||
name,
|
||||
success: false,
|
||||
message: format!("Opt {}: Error: {}", if enabled { "ON" } else { "OFF" }, e),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
results
|
||||
}
|
||||
|
||||
pub fn run_benchmarks(update: bool) -> Vec<BenchmarkResult> {
|
||||
let mut results = Vec::new();
|
||||
let entries = fs::read_dir("examples").unwrap();
|
||||
let is_release = !cfg!(debug_assertions);
|
||||
let baseline_re = Regex::new(r";; Benchmark: ([\d\.]+\w+)").unwrap();
|
||||
let repeat_re = Regex::new(r";; Benchmark-Repeat: (\d+)").unwrap();
|
||||
|
||||
for entry in entries.filter_map(|e| e.ok()) {
|
||||
let path = entry.path();
|
||||
if path.extension().is_none_or(|ext| ext != "myc") {
|
||||
continue;
|
||||
}
|
||||
|
||||
let content = fs::read_to_string(&path).unwrap();
|
||||
let name = path.file_name().unwrap().to_string_lossy().to_string();
|
||||
|
||||
let baseline_match = baseline_re.captures(&content);
|
||||
let repeat_match = repeat_re.captures(&content);
|
||||
|
||||
let mut repeats = repeat_match
|
||||
.and_then(|m| m.get(1))
|
||||
.and_then(|m| m.as_str().parse::<u32>().ok())
|
||||
.unwrap_or(1);
|
||||
|
||||
// Compile once for this file (symbols/types are compatible with all fresh environments)
|
||||
let initial_env = Environment::new();
|
||||
let compiled_once = match initial_env.compile(&content) {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: Duration::ZERO,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: format!("COMPILE ERROR: {}", e),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
// Helper to measure sum of VM execution times over N executions in one environment
|
||||
let measure_sum =
|
||||
|n: u32, node: &crate::ast::compiler::TypedNode| -> Result<Duration, String> {
|
||||
let env = Environment::new();
|
||||
// Link once per sample
|
||||
let linked = env.link(node.clone());
|
||||
let mut total = Duration::ZERO;
|
||||
for _ in 0..n {
|
||||
let start = Instant::now();
|
||||
let _ = env.run(&linked)?;
|
||||
total += start.elapsed();
|
||||
}
|
||||
Ok(total)
|
||||
};
|
||||
|
||||
if update {
|
||||
repeats = 1;
|
||||
loop {
|
||||
match measure_sum(repeats, &compiled_once) {
|
||||
Ok(total) => {
|
||||
if total >= Duration::from_millis(2) || repeats >= 100_000 {
|
||||
break;
|
||||
}
|
||||
let nanos = total.as_nanos().max(1) as f64;
|
||||
let factor = 2_000_000.0 / nanos;
|
||||
repeats = (repeats as f64 * factor).ceil() as u32;
|
||||
repeats = repeats.max(repeats + 1);
|
||||
}
|
||||
Err(e) => {
|
||||
results.push(BenchmarkResult {
|
||||
name: name.clone(),
|
||||
median: Duration::ZERO,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: format!("ERROR: {}", e),
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if results.last().is_some_and(|r| r.name == name) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let mut runs = Vec::new();
|
||||
let mut error = None;
|
||||
// Adaptive samples: High repeats need fewer samples for stable median
|
||||
let num_samples = if repeats > 1000 {
|
||||
10
|
||||
} else if repeats > 100 {
|
||||
30
|
||||
} else {
|
||||
100
|
||||
};
|
||||
|
||||
for _ in 0..num_samples {
|
||||
match measure_sum(repeats, &compiled_once) {
|
||||
Ok(d) => runs.push(d),
|
||||
Err(e) => {
|
||||
error = Some(e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(e) = error {
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: Duration::ZERO,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: format!("ERROR: {}", e),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
runs.sort();
|
||||
let median_total = runs[runs.len() / 2];
|
||||
let median_single = median_total / repeats;
|
||||
|
||||
if update {
|
||||
let new_val = format_duration(median_single);
|
||||
let mut updated_content = content.clone();
|
||||
|
||||
// 1. Update/Insert Benchmark
|
||||
let bench_line = format!(";; Benchmark: {}", new_val);
|
||||
if let Some(m) = baseline_match {
|
||||
updated_content = updated_content.replace(m.get(0).unwrap().as_str(), &bench_line);
|
||||
} else {
|
||||
updated_content = format!("{}\n{}", bench_line, updated_content);
|
||||
}
|
||||
|
||||
// 2. Update/Insert/Remove Benchmark-Repeat
|
||||
let repeat_line = if repeats > 1 {
|
||||
Some(format!(";; Benchmark-Repeat: {}", repeats))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let current_repeat_str = repeat_re
|
||||
.captures(&updated_content)
|
||||
.map(|m| m.get(0).unwrap().as_str().to_string());
|
||||
|
||||
if let Some(line) = repeat_line {
|
||||
if let Some(old_line) = current_repeat_str {
|
||||
updated_content = updated_content.replace(&old_line, &line);
|
||||
} else if let Some(m) = baseline_re.captures(&updated_content) {
|
||||
let b_str = m.get(0).unwrap().as_str().to_string();
|
||||
if let Some(pos) = updated_content.find(&b_str) {
|
||||
updated_content.insert_str(pos + b_str.len(), &format!("\n{}", line));
|
||||
}
|
||||
}
|
||||
} else if let Some(old_line) = current_repeat_str {
|
||||
updated_content = updated_content.replace(&format!("{}\n", old_line), "");
|
||||
updated_content = updated_content.replace(&old_line, "");
|
||||
}
|
||||
|
||||
fs::write(&path, updated_content).unwrap();
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: median_single,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: format!("UPDATED: {}", new_val),
|
||||
});
|
||||
} else if let Some(m) = baseline_match {
|
||||
let baseline_str = m.get(1).unwrap().as_str();
|
||||
let baseline = parse_duration(baseline_str).unwrap();
|
||||
|
||||
let diff = (median_single.as_nanos() as f64 / baseline.as_nanos() as f64) - 1.0;
|
||||
|
||||
let threshold = if is_release { 0.15 } else { 0.30 };
|
||||
let status = if median_single > baseline && diff > threshold {
|
||||
"FAILED"
|
||||
} else {
|
||||
"OK"
|
||||
};
|
||||
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: median_single,
|
||||
baseline: Some(baseline),
|
||||
diff_pct: Some(diff * 100.0),
|
||||
status: status.to_string(),
|
||||
});
|
||||
} else {
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: median_single,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: "MISSING BASELINE".to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
results
|
||||
}
|
||||
|
||||
fn format_duration(d: Duration) -> String {
|
||||
if d.as_nanos() < 1000 {
|
||||
format!("{}ns", d.as_nanos())
|
||||
} else if d.as_micros() < 1000 {
|
||||
format!("{:.1}us", d.as_nanos() as f64 / 1000.0)
|
||||
} else if d.as_millis() < 1000 {
|
||||
format!("{:.1}ms", d.as_micros() as f64 / 1000.0)
|
||||
} else {
|
||||
format!("{:.1}s", d.as_millis() as f64 / 1000.0)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_duration(s: &str) -> Option<Duration> {
|
||||
use std::sync::OnceLock;
|
||||
static RE: OnceLock<Regex> = OnceLock::new();
|
||||
let re = RE.get_or_init(|| Regex::new(r"([\d\.]+)(\w+)").unwrap());
|
||||
|
||||
let caps = re.captures(s)?;
|
||||
let val: f64 = caps[1].parse().ok()?;
|
||||
let unit = &caps[2];
|
||||
match unit {
|
||||
"ns" => Some(Duration::from_nanos(val as u64)),
|
||||
"us" => Some(Duration::from_nanos((val * 1000.0) as u64)),
|
||||
"ms" => Some(Duration::from_nanos((val * 1_000_000.0) as u64)),
|
||||
"s" => Some(Duration::from_nanos((val * 1_000_000_000.0) as u64)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn benchmark_regression_test() {
|
||||
use super::*;
|
||||
let results = run_benchmarks(false);
|
||||
let failures: Vec<_> = results.iter().filter(|r| r.status == "FAILED").collect();
|
||||
|
||||
if !failures.is_empty() {
|
||||
let error_msg = failures
|
||||
.iter()
|
||||
.map(|r| {
|
||||
format!(
|
||||
"{}: Median {:?}, Baseline {:?}, Diff {:.2}%",
|
||||
r.name,
|
||||
r.median,
|
||||
r.baseline.unwrap_or_default(),
|
||||
r.diff_pct.unwrap_or(0.0)
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
panic!("Performance regression detected:\n{}", error_msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
use crate::ast::environment::Environment;
|
||||
use regex::Regex;
|
||||
use std::fs;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
pub struct TestResult {
|
||||
pub name: String,
|
||||
pub success: bool,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
pub struct BenchmarkResult {
|
||||
pub name: String,
|
||||
pub median: Duration,
|
||||
pub baseline: Option<Duration>,
|
||||
pub diff_pct: Option<f64>,
|
||||
pub status: String,
|
||||
}
|
||||
|
||||
pub fn run_functional_tests() -> Vec<TestResult> {
|
||||
run_functional_tests_with_optimization(false)
|
||||
}
|
||||
|
||||
pub fn run_functional_tests_with_optimization(enabled: bool) -> Vec<TestResult> {
|
||||
let mut results = Vec::new();
|
||||
let entries = fs::read_dir("examples").unwrap();
|
||||
let output_re = Regex::new(r";; Output: (.*)").unwrap();
|
||||
|
||||
for entry in entries.filter_map(|e| e.ok()) {
|
||||
let mut env = Environment::new(); // Fresh environment per test file
|
||||
env.optimization = enabled;
|
||||
let path = entry.path();
|
||||
if path.extension().is_some_and(|ext| ext == "myc") {
|
||||
let content = fs::read_to_string(&path).unwrap();
|
||||
let name = path.file_name().unwrap().to_string_lossy().to_string();
|
||||
|
||||
let expected_output = output_re
|
||||
.captures(&content)
|
||||
.map(|m| m.get(1).unwrap().as_str().trim().to_string());
|
||||
|
||||
if let Some(expected) = expected_output {
|
||||
match env.run_script(&content) {
|
||||
Ok(val) => {
|
||||
let val_str = format!("{}", val);
|
||||
if val_str == expected {
|
||||
results.push(TestResult {
|
||||
name,
|
||||
success: true,
|
||||
message: format!("OK: {}", val_str),
|
||||
});
|
||||
} else {
|
||||
results.push(TestResult {
|
||||
name,
|
||||
success: false,
|
||||
message: format!(
|
||||
"Opt {}: Expected {}, got {}",
|
||||
if enabled { "ON" } else { "OFF" },
|
||||
expected,
|
||||
val_str
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
Err(e) => results.push(TestResult {
|
||||
name,
|
||||
success: false,
|
||||
message: format!(
|
||||
"Opt {}: Error: {}",
|
||||
if enabled { "ON" } else { "OFF" },
|
||||
e
|
||||
),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
results
|
||||
}
|
||||
|
||||
pub fn run_benchmarks(update: bool) -> Vec<BenchmarkResult> {
|
||||
let mut results = Vec::new();
|
||||
let entries = fs::read_dir("examples").unwrap();
|
||||
let is_release = !cfg!(debug_assertions);
|
||||
let baseline_re = Regex::new(r";; Benchmark: ([\d\.]+\w+)").unwrap();
|
||||
let repeat_re = Regex::new(r";; Benchmark-Repeat: (\d+)").unwrap();
|
||||
|
||||
for entry in entries.filter_map(|e| e.ok()) {
|
||||
let path = entry.path();
|
||||
if path.extension().is_none_or(|ext| ext != "myc") {
|
||||
continue;
|
||||
}
|
||||
|
||||
let content = fs::read_to_string(&path).unwrap();
|
||||
let name = path.file_name().unwrap().to_string_lossy().to_string();
|
||||
|
||||
let baseline_match = baseline_re.captures(&content);
|
||||
let repeat_match = repeat_re.captures(&content);
|
||||
|
||||
let mut repeats = repeat_match
|
||||
.and_then(|m| m.get(1))
|
||||
.and_then(|m| m.as_str().parse::<u32>().ok())
|
||||
.unwrap_or(1);
|
||||
|
||||
// Compile once for this file (symbols/types are compatible with all fresh environments)
|
||||
let initial_env = Environment::new();
|
||||
let compiled_once = match initial_env.compile(&content) {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: Duration::ZERO,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: format!("COMPILE ERROR: {}", e),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
// Helper to measure sum of VM execution times over N executions in one environment
|
||||
let measure_sum =
|
||||
|n: u32, node: &crate::ast::compiler::TypedNode| -> Result<Duration, String> {
|
||||
let env = Environment::new();
|
||||
// Link once per sample
|
||||
let linked = env.link(node.clone());
|
||||
let mut total = Duration::ZERO;
|
||||
for _ in 0..n {
|
||||
let start = Instant::now();
|
||||
let _ = env.run(&linked)?;
|
||||
total += start.elapsed();
|
||||
}
|
||||
Ok(total)
|
||||
};
|
||||
|
||||
if update {
|
||||
repeats = 1;
|
||||
loop {
|
||||
match measure_sum(repeats, &compiled_once) {
|
||||
Ok(total) => {
|
||||
if total >= Duration::from_millis(2) || repeats >= 100_000 {
|
||||
break;
|
||||
}
|
||||
let nanos = total.as_nanos().max(1) as f64;
|
||||
let factor = 2_000_000.0 / nanos;
|
||||
repeats = (repeats as f64 * factor).ceil() as u32;
|
||||
repeats = repeats.max(repeats + 1);
|
||||
}
|
||||
Err(e) => {
|
||||
results.push(BenchmarkResult {
|
||||
name: name.clone(),
|
||||
median: Duration::ZERO,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: format!("ERROR: {}", e),
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if results.last().is_some_and(|r| r.name == name) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let mut runs = Vec::new();
|
||||
let mut error = None;
|
||||
// Adaptive samples: High repeats need fewer samples for stable median
|
||||
let num_samples = if repeats > 1000 {
|
||||
10
|
||||
} else if repeats > 100 {
|
||||
30
|
||||
} else {
|
||||
100
|
||||
};
|
||||
|
||||
for _ in 0..num_samples {
|
||||
match measure_sum(repeats, &compiled_once) {
|
||||
Ok(d) => runs.push(d),
|
||||
Err(e) => {
|
||||
error = Some(e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(e) = error {
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: Duration::ZERO,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: format!("ERROR: {}", e),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
runs.sort();
|
||||
let median_total = runs[runs.len() / 2];
|
||||
let median_single = median_total / repeats;
|
||||
|
||||
if update {
|
||||
let new_val = format_duration(median_single);
|
||||
let mut updated_content = content.clone();
|
||||
|
||||
// 1. Update/Insert Benchmark
|
||||
let bench_line = format!(";; Benchmark: {}", new_val);
|
||||
if let Some(m) = baseline_match {
|
||||
updated_content = updated_content.replace(m.get(0).unwrap().as_str(), &bench_line);
|
||||
} else {
|
||||
updated_content = format!("{}\n{}", bench_line, updated_content);
|
||||
}
|
||||
|
||||
// 2. Update/Insert/Remove Benchmark-Repeat
|
||||
let repeat_line = if repeats > 1 {
|
||||
Some(format!(";; Benchmark-Repeat: {}", repeats))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let current_repeat_str = repeat_re
|
||||
.captures(&updated_content)
|
||||
.map(|m| m.get(0).unwrap().as_str().to_string());
|
||||
|
||||
if let Some(line) = repeat_line {
|
||||
if let Some(old_line) = current_repeat_str {
|
||||
updated_content = updated_content.replace(&old_line, &line);
|
||||
} else if let Some(m) = baseline_re.captures(&updated_content) {
|
||||
let b_str = m.get(0).unwrap().as_str().to_string();
|
||||
if let Some(pos) = updated_content.find(&b_str) {
|
||||
updated_content.insert_str(pos + b_str.len(), &format!("\n{}", line));
|
||||
}
|
||||
}
|
||||
} else if let Some(old_line) = current_repeat_str {
|
||||
updated_content = updated_content.replace(&format!("{}\n", old_line), "");
|
||||
updated_content = updated_content.replace(&old_line, "");
|
||||
}
|
||||
|
||||
fs::write(&path, updated_content).unwrap();
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: median_single,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: format!("UPDATED: {}", new_val),
|
||||
});
|
||||
} else if let Some(m) = baseline_match {
|
||||
let baseline_str = m.get(1).unwrap().as_str();
|
||||
let baseline = parse_duration(baseline_str).unwrap();
|
||||
|
||||
let diff = (median_single.as_nanos() as f64 / baseline.as_nanos() as f64) - 1.0;
|
||||
|
||||
let threshold = if is_release { 0.15 } else { 0.30 };
|
||||
let status = if median_single > baseline && diff > threshold {
|
||||
"FAILED"
|
||||
} else {
|
||||
"OK"
|
||||
};
|
||||
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: median_single,
|
||||
baseline: Some(baseline),
|
||||
diff_pct: Some(diff * 100.0),
|
||||
status: status.to_string(),
|
||||
});
|
||||
} else {
|
||||
results.push(BenchmarkResult {
|
||||
name,
|
||||
median: median_single,
|
||||
baseline: None,
|
||||
diff_pct: None,
|
||||
status: "MISSING BASELINE".to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
results
|
||||
}
|
||||
|
||||
fn format_duration(d: Duration) -> String {
|
||||
if d.as_nanos() < 1000 {
|
||||
format!("{}ns", d.as_nanos())
|
||||
} else if d.as_micros() < 1000 {
|
||||
format!("{:.1}us", d.as_nanos() as f64 / 1000.0)
|
||||
} else if d.as_millis() < 1000 {
|
||||
format!("{:.1}ms", d.as_micros() as f64 / 1000.0)
|
||||
} else {
|
||||
format!("{:.1}s", d.as_millis() as f64 / 1000.0)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_duration(s: &str) -> Option<Duration> {
|
||||
use std::sync::OnceLock;
|
||||
static RE: OnceLock<Regex> = OnceLock::new();
|
||||
let re = RE.get_or_init(|| Regex::new(r"([\d\.]+)(\w+)").unwrap());
|
||||
|
||||
let caps = re.captures(s)?;
|
||||
let val: f64 = caps[1].parse().ok()?;
|
||||
let unit = &caps[2];
|
||||
match unit {
|
||||
"ns" => Some(Duration::from_nanos(val as u64)),
|
||||
"us" => Some(Duration::from_nanos((val * 1000.0) as u64)),
|
||||
"ms" => Some(Duration::from_nanos((val * 1_000_000.0) as u64)),
|
||||
"s" => Some(Duration::from_nanos((val * 1_000_000_000.0) as u64)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn benchmark_regression_test() {
|
||||
use super::*;
|
||||
let results = run_benchmarks(false);
|
||||
let failures: Vec<_> = results.iter().filter(|r| r.status == "FAILED").collect();
|
||||
|
||||
if !failures.is_empty() {
|
||||
let error_msg = failures
|
||||
.iter()
|
||||
.map(|r| {
|
||||
format!(
|
||||
"{}: Median {:?}, Baseline {:?}, Diff {:.2}%",
|
||||
r.name,
|
||||
r.median,
|
||||
r.baseline.unwrap_or_default(),
|
||||
r.diff_pct.unwrap_or(0.0)
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
panic!("Performance regression detected:\n{}", error_msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user