Formatting

This commit is contained in:
Michael Schimmel
2026-02-24 07:27:13 +01:00
parent 9b7ef5080c
commit eeb6621280
7 changed files with 713 additions and 334 deletions
+353 -223
View File
@@ -1,223 +1,353 @@
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, NodeMetrics, TypedNode}; use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, NodeMetrics, TypedNode};
use crate::ast::types::Purity; use crate::ast::types::Purity;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::rc::Rc; use std::rc::Rc;
pub struct Analyzer<'a> { pub struct Analyzer<'a> {
global_purity: &'a HashMap<u32, Purity>, global_purity: &'a HashMap<u32, Purity>,
/// Stack of currently visiting lambdas to detect direct recursion. /// Stack of currently visiting lambdas to detect direct recursion.
lambda_stack: Vec<crate::ast::types::Identity>, lambda_stack: Vec<crate::ast::types::Identity>,
/// Map of global index to its Lambda identity if known. /// Map of global index to its Lambda identity if known.
globals_to_lambdas: HashMap<u32, crate::ast::types::Identity>, globals_to_lambdas: HashMap<u32, crate::ast::types::Identity>,
/// Set of identities that were found to be recursive. /// Set of identities that were found to be recursive.
recursive_identities: HashSet<crate::ast::types::Identity>, recursive_identities: HashSet<crate::ast::types::Identity>,
} }
impl<'a> Analyzer<'a> { impl<'a> Analyzer<'a> {
pub fn analyze(node: &TypedNode, global_purity: &'a HashMap<u32, Purity>) -> AnalyzedNode { pub fn analyze(node: &TypedNode, global_purity: &'a HashMap<u32, Purity>) -> AnalyzedNode {
let mut analyzer = Self { let mut analyzer = Self {
global_purity, global_purity,
lambda_stack: Vec::new(), lambda_stack: Vec::new(),
globals_to_lambdas: HashMap::new(), globals_to_lambdas: HashMap::new(),
recursive_identities: HashSet::new(), recursive_identities: HashSet::new(),
}; };
// First pass: map globals to their lambda identities // First pass: map globals to their lambda identities
analyzer.collect_globals(node); analyzer.collect_globals(node);
// Second pass: full analysis (decorating TypedNode into AnalyzedNode) // Second pass: full analysis (decorating TypedNode into AnalyzedNode)
analyzer.visit(Rc::new(node.clone())) analyzer.visit(Rc::new(node.clone()))
} }
fn collect_globals(&mut self, node: &TypedNode) { fn collect_globals(&mut self, node: &TypedNode) {
match &node.kind { match &node.kind {
BoundKind::DefGlobal { global_index, value, .. } => { BoundKind::DefGlobal {
if let BoundKind::Lambda { .. } = &value.kind { global_index,
self.globals_to_lambdas.insert(*global_index, value.identity.clone()); value,
} ..
self.collect_globals(value); } => {
} if let BoundKind::Lambda { .. } = &value.kind {
BoundKind::Block { exprs } => { self.globals_to_lambdas
for e in exprs { self.collect_globals(e); } .insert(*global_index, value.identity.clone());
} }
_ => { self.collect_globals(value);
node.kind.for_each_child(|child| self.collect_globals(child)); }
} BoundKind::Block { exprs } => {
} for e in exprs {
} self.collect_globals(e);
}
fn visit(&mut self, node_rc: Rc<TypedNode>) -> AnalyzedNode { }
let node = &*node_rc; _ => {
let mut is_recursive = false; node.kind
.for_each_child(|child| self.collect_globals(child));
let (new_kind, purity) = match &node.kind { }
BoundKind::Constant(v) => (BoundKind::Constant(v.clone()), Purity::Pure), }
BoundKind::Nop => (BoundKind::Nop, Purity::Pure), }
BoundKind::Parameter { name, slot } => {
(BoundKind::Parameter { name: name.clone(), slot: *slot }, Purity::Pure) fn visit(&mut self, node_rc: Rc<TypedNode>) -> AnalyzedNode {
} let node = &*node_rc;
let mut is_recursive = false;
BoundKind::Get { addr, name } => {
let p = match addr { let (new_kind, purity) = match &node.kind {
Address::Global(idx) => self.global_purity.get(idx).cloned().unwrap_or(Purity::Pure), BoundKind::Constant(v) => (BoundKind::Constant(v.clone()), Purity::Pure),
_ => Purity::Pure, BoundKind::Nop => (BoundKind::Nop, Purity::Pure),
}; BoundKind::Parameter { name, slot } => (
(BoundKind::Get { addr: *addr, name: name.clone() }, p) BoundKind::Parameter {
} name: name.clone(),
slot: *slot,
BoundKind::Set { addr, value } => { },
let val_m = self.visit(Rc::new((**value).clone())); Purity::Pure,
(BoundKind::Set { addr: *addr, value: Box::new(val_m) }, Purity::Impure) ),
}
BoundKind::Get { addr, name } => {
BoundKind::DefLocal { name, slot, value, captured_by } => { let p = match addr {
let val_m = self.visit(Rc::new((**value).clone())); Address::Global(idx) => {
let p = val_m.ty.purity; self.global_purity.get(idx).cloned().unwrap_or(Purity::Pure)
(BoundKind::DefLocal { name: name.clone(), slot: *slot, value: Box::new(val_m), captured_by: captured_by.clone() }, p) }
} _ => Purity::Pure,
};
BoundKind::DefGlobal { name, global_index, value } => { (
let val_m = self.visit(Rc::new((**value).clone())); BoundKind::Get {
let p = val_m.ty.purity; addr: *addr,
(BoundKind::DefGlobal { name: name.clone(), global_index: *global_index, value: Box::new(val_m) }, p) name: name.clone(),
} },
p,
BoundKind::If { cond, then_br, else_br } => { )
let cond_m = self.visit(Rc::new((**cond).clone())); }
let then_m = self.visit(Rc::new((**then_br).clone()));
let else_m = else_br.as_ref().map(|e| self.visit(Rc::new((**e).clone()))); BoundKind::Set { addr, value } => {
let val_m = self.visit(Rc::new((**value).clone()));
let mut p = cond_m.ty.purity.min(then_m.ty.purity); (
if let Some(ref em) = else_m { p = p.min(em.ty.purity); } BoundKind::Set {
(BoundKind::If { cond: Box::new(cond_m), then_br: Box::new(then_m), else_br: else_m.map(Box::new) }, p) addr: *addr,
} value: Box::new(val_m),
},
BoundKind::Lambda { params, upvalues, body, positional_count } => { Purity::Impure,
self.lambda_stack.push(node.identity.clone()); )
let params_m = self.visit(params.clone()); }
let body_m = self.visit(body.clone());
self.lambda_stack.pop(); BoundKind::DefLocal {
name,
is_recursive = self.recursive_identities.contains(&node.identity); slot,
(BoundKind::Lambda { params: Rc::new(params_m), upvalues: upvalues.clone(), body: Rc::new(body_m), positional_count: *positional_count }, Purity::Pure) value,
} captured_by,
} => {
BoundKind::Call { callee, args } => { let val_m = self.visit(Rc::new((**value).clone()));
let callee_m = self.visit(Rc::new((**callee).clone())); let p = val_m.ty.purity;
let args_m = self.visit(Rc::new((**args).clone())); (
BoundKind::DefLocal {
if let BoundKind::Get { addr: Address::Global(idx), .. } = &callee.kind name: name.clone(),
&& let Some(lambda_id) = self.globals_to_lambdas.get(idx) slot: *slot,
&& self.lambda_stack.contains(lambda_id) value: Box::new(val_m),
{ captured_by: captured_by.clone(),
self.recursive_identities.insert(lambda_id.clone()); },
is_recursive = true; p,
} )
}
let p_func = if let BoundKind::Get { addr: Address::Global(idx), .. } = &callee.kind {
self.global_purity.get(idx).cloned().unwrap_or(Purity::Impure) BoundKind::DefGlobal {
} else { name,
Purity::Impure global_index,
}; value,
let p = callee_m.ty.purity.min(args_m.ty.purity).min(p_func); } => {
(BoundKind::Call { callee: Box::new(callee_m), args: Box::new(args_m) }, p) let val_m = self.visit(Rc::new((**value).clone()));
} let p = val_m.ty.purity;
(
BoundKind::Again { args } => { BoundKind::DefGlobal {
let args_m = self.visit(Rc::new((**args).clone())); name: name.clone(),
if let Some(lambda_id) = self.lambda_stack.last() { global_index: *global_index,
self.recursive_identities.insert(lambda_id.clone()); value: Box::new(val_m),
is_recursive = true; },
} p,
(BoundKind::Again { args: Box::new(args_m) }, Purity::Impure) )
} }
BoundKind::Block { exprs } => { BoundKind::If {
let mut new_exprs = Vec::with_capacity(exprs.len()); cond,
let mut p = Purity::Pure; then_br,
for e in exprs { else_br,
let em = self.visit(Rc::new(e.clone())); } => {
p = p.min(em.ty.purity); let cond_m = self.visit(Rc::new((**cond).clone()));
new_exprs.push(em); let then_m = self.visit(Rc::new((**then_br).clone()));
} let else_m = else_br.as_ref().map(|e| self.visit(Rc::new((**e).clone())));
(BoundKind::Block { exprs: new_exprs }, p)
} let mut p = cond_m.ty.purity.min(then_m.ty.purity);
if let Some(ref em) = else_m {
BoundKind::Tuple { elements } => { p = p.min(em.ty.purity);
let mut new_elements = Vec::with_capacity(elements.len()); }
let mut p = Purity::Pure; (
for e in elements { BoundKind::If {
let em = self.visit(Rc::new(e.clone())); cond: Box::new(cond_m),
p = p.min(em.ty.purity); then_br: Box::new(then_m),
new_elements.push(em); else_br: else_m.map(Box::new),
} },
(BoundKind::Tuple { elements: new_elements }, p) p,
} )
}
BoundKind::Record { fields } => {
let mut new_fields = Vec::with_capacity(fields.len()); BoundKind::Lambda {
let mut p = Purity::Pure; params,
for (k, v) in fields { upvalues,
let km = self.visit(Rc::new(k.clone())); body,
let vm = self.visit(Rc::new(v.clone())); positional_count,
p = p.min(km.ty.purity).min(vm.ty.purity); } => {
new_fields.push((km, vm)); self.lambda_stack.push(node.identity.clone());
} let params_m = self.visit(params.clone());
(BoundKind::Record { fields: new_fields }, p) let body_m = self.visit(body.clone());
} self.lambda_stack.pop();
BoundKind::Expansion { original_call, bound_expanded } => { is_recursive = self.recursive_identities.contains(&node.identity);
let expanded_m = self.visit(Rc::new((**bound_expanded).clone())); (
(BoundKind::Expansion { original_call: original_call.clone(), bound_expanded: Box::new(expanded_m.clone()) }, expanded_m.ty.purity) BoundKind::Lambda {
} params: Rc::new(params_m),
upvalues: upvalues.clone(),
BoundKind::Extension(_) => (BoundKind::Nop, Purity::Impure), body: Rc::new(body_m),
}; positional_count: *positional_count,
},
crate::ast::nodes::Node { Purity::Pure,
identity: node.identity.clone(), )
kind: new_kind, }
ty: NodeMetrics {
original: node_rc, BoundKind::Call { callee, args } => {
purity, let callee_m = self.visit(Rc::new((**callee).clone()));
is_recursive, let args_m = self.visit(Rc::new((**args).clone()));
},
} if let BoundKind::Get {
} addr: Address::Global(idx),
} ..
} = &callee.kind
trait NodeExt { && let Some(lambda_id) = self.globals_to_lambdas.get(idx)
fn for_each_child<F: FnMut(&TypedNode)>(&self, f: F); && self.lambda_stack.contains(lambda_id)
} {
self.recursive_identities.insert(lambda_id.clone());
impl NodeExt for BoundKind<crate::ast::types::StaticType> { is_recursive = true;
fn for_each_child<F: FnMut(&TypedNode)>(&self, mut f: F) { }
match self {
BoundKind::If { cond, then_br, else_br } => { let p_func = if let BoundKind::Get {
f(cond); f(then_br); if let Some(e) = else_br { f(e); } addr: Address::Global(idx),
} ..
BoundKind::DefLocal { value, .. } | BoundKind::DefGlobal { value, .. } | BoundKind::Set { value, .. } => { } = &callee.kind
f(value); {
} self.global_purity
BoundKind::Lambda { params, body, .. } => { .get(idx)
f(params); f(body); .cloned()
} .unwrap_or(Purity::Impure)
BoundKind::Call { callee, args } => { } else {
f(callee); f(args); Purity::Impure
} };
BoundKind::Block { exprs } => { let p = callee_m.ty.purity.min(args_m.ty.purity).min(p_func);
for e in exprs { f(e); } (
} BoundKind::Call {
BoundKind::Tuple { elements } => { callee: Box::new(callee_m),
for e in elements { f(e); } args: Box::new(args_m),
} },
BoundKind::Record { fields } => { p,
for (k, v) in fields { f(k); f(v); } )
} }
BoundKind::Expansion { bound_expanded, .. } => {
f(bound_expanded); BoundKind::Again { args } => {
} let args_m = self.visit(Rc::new((**args).clone()));
_ => {} if let Some(lambda_id) = self.lambda_stack.last() {
} self.recursive_identities.insert(lambda_id.clone());
} is_recursive = true;
} }
(
BoundKind::Again {
args: Box::new(args_m),
},
Purity::Impure,
)
}
BoundKind::Block { exprs } => {
let mut new_exprs = Vec::with_capacity(exprs.len());
let mut p = Purity::Pure;
for e in exprs {
let em = self.visit(Rc::new(e.clone()));
p = p.min(em.ty.purity);
new_exprs.push(em);
}
(BoundKind::Block { exprs: new_exprs }, p)
}
BoundKind::Tuple { elements } => {
let mut new_elements = Vec::with_capacity(elements.len());
let mut p = Purity::Pure;
for e in elements {
let em = self.visit(Rc::new(e.clone()));
p = p.min(em.ty.purity);
new_elements.push(em);
}
(
BoundKind::Tuple {
elements: new_elements,
},
p,
)
}
BoundKind::Record { fields } => {
let mut new_fields = Vec::with_capacity(fields.len());
let mut p = Purity::Pure;
for (k, v) in fields {
let km = self.visit(Rc::new(k.clone()));
let vm = self.visit(Rc::new(v.clone()));
p = p.min(km.ty.purity).min(vm.ty.purity);
new_fields.push((km, vm));
}
(BoundKind::Record { fields: new_fields }, p)
}
BoundKind::Expansion {
original_call,
bound_expanded,
} => {
let expanded_m = self.visit(Rc::new((**bound_expanded).clone()));
(
BoundKind::Expansion {
original_call: original_call.clone(),
bound_expanded: Box::new(expanded_m.clone()),
},
expanded_m.ty.purity,
)
}
BoundKind::Extension(_) => (BoundKind::Nop, Purity::Impure),
};
crate::ast::nodes::Node {
identity: node.identity.clone(),
kind: new_kind,
ty: NodeMetrics {
original: node_rc,
purity,
is_recursive,
},
}
}
}
trait NodeExt {
fn for_each_child<F: FnMut(&TypedNode)>(&self, f: F);
}
impl NodeExt for BoundKind<crate::ast::types::StaticType> {
fn for_each_child<F: FnMut(&TypedNode)>(&self, mut f: F) {
match self {
BoundKind::If {
cond,
then_br,
else_br,
} => {
f(cond);
f(then_br);
if let Some(e) = else_br {
f(e);
}
}
BoundKind::DefLocal { value, .. }
| BoundKind::DefGlobal { value, .. }
| BoundKind::Set { value, .. } => {
f(value);
}
BoundKind::Lambda { params, body, .. } => {
f(params);
f(body);
}
BoundKind::Call { callee, args } => {
f(callee);
f(args);
}
BoundKind::Block { exprs } => {
for e in exprs {
f(e);
}
}
BoundKind::Tuple { elements } => {
for e in elements {
f(e);
}
}
BoundKind::Record { fields } => {
for (k, v) in fields {
f(k);
f(v);
}
}
BoundKind::Expansion { bound_expanded, .. } => {
f(bound_expanded);
}
_ => {}
}
}
}
+203 -48
View File
@@ -342,7 +342,9 @@ impl Optimizer {
} }
let then_br = Box::new(self.visit_node((**then_br).clone(), sub, path)); let then_br = Box::new(self.visit_node((**then_br).clone(), sub, path));
let else_br = else_br.as_ref().map(|e| Box::new(self.visit_node((**e).clone(), sub, path))); let else_br = else_br
.as_ref()
.map(|e| Box::new(self.visit_node((**e).clone(), sub, path)));
( (
BoundKind::If { BoundKind::If {
cond: Box::new(cond_opt), cond: Box::new(cond_opt),
@@ -361,8 +363,12 @@ impl Optimizer {
} }
} }
for slot in &info.assigned_locals { sub.assigned_locals.insert(*slot); } for slot in &info.assigned_locals {
for idx in &info.assigned_globals { sub.assigned_globals.insert(*idx); } sub.assigned_locals.insert(*slot);
}
for idx in &info.assigned_globals {
sub.assigned_globals.insert(*idx);
}
let mut new_exprs = Vec::with_capacity(exprs.len()); let mut new_exprs = Vec::with_capacity(exprs.len());
let last_idx = exprs.len().saturating_sub(1); let last_idx = exprs.len().saturating_sub(1);
@@ -397,7 +403,9 @@ impl Optimizer {
_ => e.ty.purity >= Purity::SideEffectFree, _ => e.ty.purity >= Purity::SideEffectFree,
}; };
if removable { continue; } if removable {
continue;
}
} }
let opt = self.visit_node(e.clone(), sub, path); let opt = self.visit_node(e.clone(), sub, path);
@@ -435,7 +443,9 @@ impl Optimizer {
let mut info = UsageInfo::default(); let mut info = UsageInfo::default();
self.collect_usage(&node, &mut info); self.collect_usage(&node, &mut info);
for idx in &info.assigned_globals { sub.assigned_globals.insert(*idx); } for idx in &info.assigned_globals {
sub.assigned_globals.insert(*idx);
}
let mut new_upvalues = Vec::new(); let mut new_upvalues = Vec::new();
let mut mapping = Vec::new(); let mut mapping = Vec::new();
@@ -447,11 +457,15 @@ impl Optimizer {
let mut inlined_val = None; let mut inlined_val = None;
match capture_addr { match capture_addr {
Address::Local(slot) => { Address::Local(slot) => {
if let Some(val) = sub.locals.get(slot) { inlined_val = Some(val.clone()); } if let Some(val) = sub.locals.get(slot) {
inlined_val = Some(val.clone());
}
sub.captured_slots.insert(*slot); sub.captured_slots.insert(*slot);
} }
Address::Upvalue(idx) => { Address::Upvalue(idx) => {
if let Some(val) = sub.upvalues.get(idx) { inlined_val = Some(val.clone()); } if let Some(val) = sub.upvalues.get(idx) {
inlined_val = Some(val.clone());
}
} }
_ => {} _ => {}
} }
@@ -470,7 +484,8 @@ impl Optimizer {
} }
} }
let params_node = self.visit_node(params.as_ref().clone(), &mut next_inner_subs, path); let params_node =
self.visit_node(params.as_ref().clone(), &mut next_inner_subs, path);
self.collect_parameter_slots(&params_node, &mut next_inner_subs); self.collect_parameter_slots(&params_node, &mut next_inner_subs);
let body_node = self.visit_node(body.as_ref().clone(), &mut next_inner_subs, path); let body_node = self.visit_node(body.as_ref().clone(), &mut next_inner_subs, path);
@@ -497,7 +512,9 @@ impl Optimizer {
ref value, ref value,
ref captured_by, ref captured_by,
} => { } => {
if !captured_by.is_empty() { sub.captured_slots.insert(slot); } if !captured_by.is_empty() {
sub.captured_slots.insert(slot);
}
let value_opt = Box::new(self.visit_node((**value).clone(), sub, path)); let value_opt = Box::new(self.visit_node((**value).clone(), sub, path));
if let BoundKind::Constant(val) = &value_opt.kind { if let BoundKind::Constant(val) = &value_opt.kind {
sub.add_local(slot, val.clone()); sub.add_local(slot, val.clone());
@@ -563,7 +580,8 @@ impl Optimizer {
ref bound_expanded, ref bound_expanded,
} => { } => {
path.inlining_depth += 1; path.inlining_depth += 1;
let bound_expanded = Box::new(self.visit_node((**bound_expanded).clone(), sub, path)); let bound_expanded =
Box::new(self.visit_node((**bound_expanded).clone(), sub, path));
path.inlining_depth -= 1; path.inlining_depth -= 1;
( (
BoundKind::Expansion { BoundKind::Expansion {
@@ -696,8 +714,14 @@ impl Optimizer {
let mut slot_index = 0; let mut slot_index = 0;
self.map_params_to_args(params, &arg_vals, &mut slot_index, sub); self.map_params_to_args(params, &arg_vals, &mut slot_index, sub);
if slot_index != arg_vals.len() { return None; } if slot_index != arg_vals.len() {
if sub.locals.is_empty() && sub.ast_locals.is_empty() && sub.upvalues.is_empty() && !arg_vals.is_empty() { return None;
}
if sub.locals.is_empty()
&& sub.ast_locals.is_empty()
&& sub.upvalues.is_empty()
&& !arg_vals.is_empty()
{
return None; return None;
} }
@@ -707,10 +731,14 @@ impl Optimizer {
fn flatten_tuple(&self, node: AnalyzedNode, into: &mut Vec<AnalyzedNode>) { fn flatten_tuple(&self, node: AnalyzedNode, into: &mut Vec<AnalyzedNode>) {
match node.kind { match node.kind {
BoundKind::Tuple { elements } => { BoundKind::Tuple { elements } => {
for el in elements { self.flatten_tuple(el, into); } for el in elements {
self.flatten_tuple(el, into);
}
} }
BoundKind::Record { fields } => { BoundKind::Record { fields } => {
for (_, v) in fields { self.flatten_tuple(v, into); } for (_, v) in fields {
self.flatten_tuple(v, into);
}
} }
BoundKind::Nop => {} BoundKind::Nop => {}
_ => into.push(node), _ => into.push(node),
@@ -730,8 +758,14 @@ impl Optimizer {
if let BoundKind::Constant(val) = &arg.kind { if let BoundKind::Constant(val) = &arg.kind {
sub.add_local(*slot, val.clone()); sub.add_local(*slot, val.clone());
} else if let BoundKind::Lambda { upvalues, .. } = &arg.kind { } else if let BoundKind::Lambda { upvalues, .. } = &arg.kind {
if upvalues.is_empty() { sub.add_ast_local(*slot, arg.clone()); } if upvalues.is_empty() {
} else if let BoundKind::Get { addr: Address::Global(_), .. } = &arg.kind { sub.add_ast_local(*slot, arg.clone());
}
} else if let BoundKind::Get {
addr: Address::Global(_),
..
} = &arg.kind
{
sub.add_ast_local(*slot, arg.clone()); sub.add_ast_local(*slot, arg.clone());
} }
} }
@@ -764,7 +798,9 @@ impl Optimizer {
} }
// Fallback: Continue flat matching (original behavior) // Fallback: Continue flat matching (original behavior)
for el in elements { self.map_params_to_args(el, args, offset, sub); } for el in elements {
self.map_params_to_args(el, args, offset, sub);
}
} }
_ => {} _ => {}
} }
@@ -776,19 +812,28 @@ impl Optimizer {
if let Value::Object(obj) = v if let Value::Object(obj) = v
&& let Some(closure) = obj.as_any().downcast_ref::<Closure>() && let Some(closure) = obj.as_any().downcast_ref::<Closure>()
{ {
info.used_identities.insert(closure.function_node.identity.clone()); info.used_identities
.insert(closure.function_node.identity.clone());
self.collect_usage(&closure.function_node, info); self.collect_usage(&closure.function_node, info);
} }
} }
BoundKind::Get { addr, .. } => match addr { BoundKind::Get { addr, .. } => match addr {
Address::Local(slot) => { info.used_locals.insert(*slot); } Address::Local(slot) => {
Address::Global(idx) => { info.used_globals.insert(*idx); } info.used_locals.insert(*slot);
}
Address::Global(idx) => {
info.used_globals.insert(*idx);
}
_ => {} _ => {}
}, },
BoundKind::Set { addr, value } => { BoundKind::Set { addr, value } => {
match addr { match addr {
Address::Local(slot) => { info.assigned_locals.insert(*slot); } Address::Local(slot) => {
Address::Global(idx) => { info.assigned_globals.insert(*idx); } info.assigned_locals.insert(*slot);
}
Address::Global(idx) => {
info.assigned_globals.insert(*idx);
}
_ => {} _ => {}
} }
self.collect_usage(value, info); self.collect_usage(value, info);
@@ -798,17 +843,31 @@ impl Optimizer {
self.collect_usage(params, info); self.collect_usage(params, info);
self.collect_usage(body, info); self.collect_usage(body, info);
} }
BoundKind::Block { exprs } => { for e in exprs { self.collect_usage(e, info); } } BoundKind::Block { exprs } => {
BoundKind::If { cond, then_br, else_br } => { for e in exprs {
self.collect_usage(e, info);
}
}
BoundKind::If {
cond,
then_br,
else_br,
} => {
self.collect_usage(cond, info); self.collect_usage(cond, info);
self.collect_usage(then_br, info); self.collect_usage(then_br, info);
if let Some(e) = else_br { self.collect_usage(e, info); } if let Some(e) = else_br {
self.collect_usage(e, info);
}
} }
BoundKind::Call { callee, args } => { BoundKind::Call { callee, args } => {
self.collect_usage(callee, info); self.collect_usage(callee, info);
self.collect_usage(args, info); self.collect_usage(args, info);
} }
BoundKind::Tuple { elements } => { for e in elements { self.collect_usage(e, info); } } BoundKind::Tuple { elements } => {
for e in elements {
self.collect_usage(e, info);
}
}
BoundKind::Record { fields } => { BoundKind::Record { fields } => {
for (k, v) in fields { for (k, v) in fields {
self.collect_usage(k, info); self.collect_usage(k, info);
@@ -857,46 +916,103 @@ impl SubstitutionMap {
} }
} }
fn add_ast_local(&mut self, slot: u32, node: AnalyzedNode) { self.ast_locals.insert(slot, node); } fn add_ast_local(&mut self, slot: u32, node: AnalyzedNode) {
self.ast_locals.insert(slot, node);
}
fn map_slot(&mut self, old_slot: u32) -> u32 { fn map_slot(&mut self, old_slot: u32) -> u32 {
if let Some(&new_slot) = self.slot_mapping.get(&old_slot) { return new_slot; } if let Some(&new_slot) = self.slot_mapping.get(&old_slot) {
return new_slot;
}
let new_slot = self.next_slot; let new_slot = self.next_slot;
self.slot_mapping.insert(old_slot, new_slot); self.slot_mapping.insert(old_slot, new_slot);
self.next_slot += 1; self.next_slot += 1;
new_slot new_slot
} }
fn add_local(&mut self, slot: u32, val: Value) { self.locals.insert(slot, val); } fn add_local(&mut self, slot: u32, val: Value) {
fn add_upvalue(&mut self, idx: u32, val: Value) { self.upvalues.insert(idx, val); } self.locals.insert(slot, val);
fn add_global(&mut self, idx: u32, val: Value) { self.globals.insert(idx, val); } }
fn add_upvalue(&mut self, idx: u32, val: Value) {
self.upvalues.insert(idx, val);
}
fn add_global(&mut self, idx: u32, val: Value) {
self.globals.insert(idx, val);
}
fn reindex_upvalues(&self, node: AnalyzedNode, mapping: &[Option<u32>]) -> AnalyzedNode { fn reindex_upvalues(&self, node: AnalyzedNode, mapping: &[Option<u32>]) -> AnalyzedNode {
let (new_kind, metrics) = match node.kind { let (new_kind, metrics) = match node.kind {
BoundKind::Get { addr: Address::Upvalue(idx), name } => { BoundKind::Get {
if let Some(res) = mapping.get(idx as usize) && let Some(new_idx) = res { addr: Address::Upvalue(idx),
(BoundKind::Get { addr: Address::Upvalue(*new_idx), name }, node.ty.clone()) name,
} => {
if let Some(res) = mapping.get(idx as usize)
&& let Some(new_idx) = res
{
(
BoundKind::Get {
addr: Address::Upvalue(*new_idx),
name,
},
node.ty.clone(),
)
} else { } else {
(BoundKind::Get { addr: Address::Upvalue(idx), name }, node.ty.clone()) (
BoundKind::Get {
addr: Address::Upvalue(idx),
name,
},
node.ty.clone(),
)
} }
} }
BoundKind::Lambda { params, upvalues, body, positional_count } => { BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
} => {
let mut next_upvalues = Vec::new(); let mut next_upvalues = Vec::new();
for addr in upvalues { for addr in upvalues {
if let Address::Upvalue(idx) = addr && let Some(res) = mapping.get(idx as usize) && let Some(new_idx) = res { if let Address::Upvalue(idx) = addr
&& let Some(res) = mapping.get(idx as usize)
&& let Some(new_idx) = res
{
next_upvalues.push(Address::Upvalue(*new_idx)); next_upvalues.push(Address::Upvalue(*new_idx));
continue; continue;
} }
next_upvalues.push(addr); next_upvalues.push(addr);
} }
(BoundKind::Lambda { params, upvalues: next_upvalues, body, positional_count }, node.ty.clone()) (
BoundKind::Lambda {
params,
upvalues: next_upvalues,
body,
positional_count,
},
node.ty.clone(),
)
} }
BoundKind::If { cond, then_br, else_br } => { BoundKind::If {
cond,
then_br,
else_br,
} => {
let cond = Box::new(self.reindex_upvalues(*cond, mapping)); let cond = Box::new(self.reindex_upvalues(*cond, mapping));
let then_br = Box::new(self.reindex_upvalues(*then_br, mapping)); let then_br = Box::new(self.reindex_upvalues(*then_br, mapping));
let else_br = else_br.map(|e| Box::new(self.reindex_upvalues(*e, mapping))); let else_br = else_br.map(|e| Box::new(self.reindex_upvalues(*e, mapping)));
(BoundKind::If { cond, then_br, else_br }, node.ty.clone()) (
BoundKind::If {
cond,
then_br,
else_br,
},
node.ty.clone(),
)
} }
BoundKind::Block { exprs } => { BoundKind::Block { exprs } => {
let exprs = exprs.into_iter().map(|e| self.reindex_upvalues(e, mapping)).collect(); let exprs = exprs
.into_iter()
.map(|e| self.reindex_upvalues(e, mapping))
.collect();
(BoundKind::Block { exprs }, node.ty.clone()) (BoundKind::Block { exprs }, node.ty.clone())
} }
BoundKind::Call { callee, args } => { BoundKind::Call { callee, args } => {
@@ -904,28 +1020,67 @@ impl SubstitutionMap {
let args = Box::new(self.reindex_upvalues(*args, mapping)); let args = Box::new(self.reindex_upvalues(*args, mapping));
(BoundKind::Call { callee, args }, node.ty.clone()) (BoundKind::Call { callee, args }, node.ty.clone())
} }
BoundKind::DefLocal { name, slot, value, captured_by } => { BoundKind::DefLocal {
name,
slot,
value,
captured_by,
} => {
let value = Box::new(self.reindex_upvalues(*value, mapping)); let value = Box::new(self.reindex_upvalues(*value, mapping));
(BoundKind::DefLocal { name, slot, value, captured_by }, node.ty.clone()) (
BoundKind::DefLocal {
name,
slot,
value,
captured_by,
},
node.ty.clone(),
)
} }
BoundKind::DefGlobal { name, global_index, value } => { BoundKind::DefGlobal {
name,
global_index,
value,
} => {
let value = Box::new(self.reindex_upvalues(*value, mapping)); let value = Box::new(self.reindex_upvalues(*value, mapping));
(BoundKind::DefGlobal { name, global_index, value }, node.ty.clone()) (
BoundKind::DefGlobal {
name,
global_index,
value,
},
node.ty.clone(),
)
} }
BoundKind::Set { addr, value } => { BoundKind::Set { addr, value } => {
let value = Box::new(self.reindex_upvalues(*value, mapping)); let value = Box::new(self.reindex_upvalues(*value, mapping));
(BoundKind::Set { addr, value }, node.ty.clone()) (BoundKind::Set { addr, value }, node.ty.clone())
} }
BoundKind::Tuple { elements } => { BoundKind::Tuple { elements } => {
let elements = elements.into_iter().map(|e| self.reindex_upvalues(e, mapping)).collect(); let elements = elements
.into_iter()
.map(|e| self.reindex_upvalues(e, mapping))
.collect();
(BoundKind::Tuple { elements }, node.ty.clone()) (BoundKind::Tuple { elements }, node.ty.clone())
} }
BoundKind::Record { fields } => { BoundKind::Record { fields } => {
let fields = fields.into_iter().map(|(k, v)| (self.reindex_upvalues(k, mapping), self.reindex_upvalues(v, mapping))).collect(); let fields = fields
.into_iter()
.map(|(k, v)| {
(
self.reindex_upvalues(k, mapping),
self.reindex_upvalues(v, mapping),
)
})
.collect();
(BoundKind::Record { fields }, node.ty.clone()) (BoundKind::Record { fields }, node.ty.clone())
} }
k => (k, node.ty.clone()), k => (k, node.ty.clone()),
}; };
Node { identity: node.identity.clone(), kind: new_kind, ty: metrics } Node {
identity: node.identity.clone(),
kind: new_kind,
ty: metrics,
}
} }
} }
+122 -36
View File
@@ -16,7 +16,9 @@ pub type RtlLookupFunc = Rc<dyn Fn(&str, &[StaticType]) -> Option<(Value, Static
pub trait FunctionRegistry { pub trait FunctionRegistry {
fn resolve(&self, addr: Address) -> Option<BoundNode>; fn resolve(&self, addr: Address) -> Option<BoundNode>;
fn resolve_analyzed(&self, _addr: Address) -> Option<AnalyzedNode> { None } fn resolve_analyzed(&self, _addr: Address) -> Option<AnalyzedNode> {
None
}
} }
pub type MonoCache = HashMap<MonoCacheKey, (Value, StaticType)>; pub type MonoCache = HashMap<MonoCacheKey, (Value, StaticType)>;
@@ -52,7 +54,7 @@ impl Specializer {
BoundKind::Call { callee, args } => { BoundKind::Call { callee, args } => {
let (new_callee, new_args, _ret_ty) = let (new_callee, new_args, _ret_ty) =
self.specialize_call_logic(*callee, *args, node.ty.original.ty.clone()); self.specialize_call_logic(*callee, *args, node.ty.original.ty.clone());
let new_metrics = node.ty.clone(); let new_metrics = node.ty.clone();
( (
BoundKind::Call { BoundKind::Call {
@@ -63,28 +65,76 @@ impl Specializer {
) )
} }
BoundKind::If { cond, then_br, else_br } => { BoundKind::If {
cond,
then_br,
else_br,
} => {
let cond = Box::new(self.visit_node(*cond)); let cond = Box::new(self.visit_node(*cond));
let then_br = Box::new(self.visit_node(*then_br)); let then_br = Box::new(self.visit_node(*then_br));
let else_br = else_br.map(|e| Box::new(self.visit_node(*e))); let else_br = else_br.map(|e| Box::new(self.visit_node(*e)));
(BoundKind::If { cond, then_br, else_br }, node.ty.clone()) (
BoundKind::If {
cond,
then_br,
else_br,
},
node.ty.clone(),
)
} }
BoundKind::Block { exprs } => { BoundKind::Block { exprs } => {
let exprs = exprs.into_iter().map(|e| self.visit_node(e)).collect(); let exprs = exprs.into_iter().map(|e| self.visit_node(e)).collect();
(BoundKind::Block { exprs }, node.ty.clone()) (BoundKind::Block { exprs }, node.ty.clone())
} }
BoundKind::Lambda { params, upvalues, body, positional_count } => { BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
} => {
let params = Rc::new(self.visit_node(params.as_ref().clone())); let params = Rc::new(self.visit_node(params.as_ref().clone()));
let body = Rc::new(self.visit_node((*body).clone())); let body = Rc::new(self.visit_node((*body).clone()));
(BoundKind::Lambda { params, upvalues, body, positional_count }, node.ty.clone()) (
BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
},
node.ty.clone(),
)
} }
BoundKind::DefLocal { name, slot, value, captured_by } => { BoundKind::DefLocal {
name,
slot,
value,
captured_by,
} => {
let value = Box::new(self.visit_node(*value)); let value = Box::new(self.visit_node(*value));
(BoundKind::DefLocal { name, slot, value, captured_by }, node.ty.clone()) (
BoundKind::DefLocal {
name,
slot,
value,
captured_by,
},
node.ty.clone(),
)
} }
BoundKind::DefGlobal { name, global_index, value } => { BoundKind::DefGlobal {
name,
global_index,
value,
} => {
let value = Box::new(self.visit_node(*value)); let value = Box::new(self.visit_node(*value));
(BoundKind::DefGlobal { name, global_index, value }, node.ty.clone()) (
BoundKind::DefGlobal {
name,
global_index,
value,
},
node.ty.clone(),
)
} }
BoundKind::Set { addr, value } => { BoundKind::Set { addr, value } => {
let value = Box::new(self.visit_node(*value)); let value = Box::new(self.visit_node(*value));
@@ -101,9 +151,18 @@ impl Specializer {
.collect(); .collect();
(BoundKind::Record { fields }, node.ty.clone()) (BoundKind::Record { fields }, node.ty.clone())
} }
BoundKind::Expansion { original_call, bound_expanded } => { BoundKind::Expansion {
original_call,
bound_expanded,
} => {
let bound_expanded = Box::new(self.visit_node(*bound_expanded)); let bound_expanded = Box::new(self.visit_node(*bound_expanded));
(BoundKind::Expansion { original_call, bound_expanded }, node.ty.clone()) (
BoundKind::Expansion {
original_call,
bound_expanded,
},
node.ty.clone(),
)
} }
k => (k, node.ty.clone()), k => (k, node.ty.clone()),
}; };
@@ -130,11 +189,12 @@ impl Specializer {
return (new_callee, new_args, original_ty); return (new_callee, new_args, original_ty);
}; };
let arg_types: Vec<StaticType> = if let StaticType::Tuple(elements) = &new_args.ty.original.ty { let arg_types: Vec<StaticType> =
elements.clone() if let StaticType::Tuple(elements) = &new_args.ty.original.ty {
} else { elements.clone()
vec![new_args.ty.original.ty.clone()] } else {
}; vec![new_args.ty.original.ty.clone()]
};
if arg_types.iter().any(|t| matches!(t, StaticType::Any)) { if arg_types.iter().any(|t| matches!(t, StaticType::Any)) {
return (new_callee, new_args, original_ty); return (new_callee, new_args, original_ty);
@@ -146,10 +206,14 @@ impl Specializer {
}; };
if let Some((val, ret_ty)) = self.cache.borrow().get(&key) { if let Some((val, ret_ty)) = self.cache.borrow().get(&key) {
let specialized_callee = self.make_constant_node(val.clone(), StaticType::Function(Box::new(Signature { let specialized_callee = self.make_constant_node(
params: StaticType::Tuple(arg_types), val.clone(),
ret: ret_ty.clone(), StaticType::Function(Box::new(Signature {
})), &new_callee); params: StaticType::Tuple(arg_types),
ret: ret_ty.clone(),
})),
&new_callee,
);
return (specialized_callee, new_args, ret_ty.clone()); return (specialized_callee, new_args, ret_ty.clone());
} }
@@ -157,11 +221,17 @@ impl Specializer {
&& let BoundKind::Get { name, .. } = &new_callee.kind && let BoundKind::Get { name, .. } = &new_callee.kind
&& let Some((val, ret_ty)) = rtl_lookup(&name.name, &arg_types) && let Some((val, ret_ty)) = rtl_lookup(&name.name, &arg_types)
{ {
self.cache.borrow_mut().insert(key.clone(), (val.clone(), ret_ty.clone())); self.cache
let specialized_callee = self.make_constant_node(val.clone(), StaticType::Function(Box::new(Signature { .borrow_mut()
params: StaticType::Tuple(arg_types), .insert(key.clone(), (val.clone(), ret_ty.clone()));
ret: ret_ty.clone(), let specialized_callee = self.make_constant_node(
})), &new_callee); val.clone(),
StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(arg_types),
ret: ret_ty.clone(),
})),
&new_callee,
);
return (specialized_callee, new_args, ret_ty); return (specialized_callee, new_args, ret_ty);
} }
@@ -172,20 +242,27 @@ impl Specializer {
return (new_callee, new_args, original_ty); return (new_callee, new_args, original_ty);
} }
if let Some(compiler) = &self.compiler if let Some(compiler) = &self.compiler
&& let Some(func_node) = self.registry.as_ref().and_then(|r| r.resolve(address)) && let Some(func_node) = self.registry.as_ref().and_then(|r| r.resolve(address))
&& let Ok((compiled_val, ret_ty)) = compiler(func_node, &arg_types) && let Ok((compiled_val, ret_ty)) = compiler(func_node, &arg_types)
{ {
self.cache.borrow_mut().insert(key, (compiled_val.clone(), ret_ty.clone())); self.cache
.borrow_mut()
.insert(key, (compiled_val.clone(), ret_ty.clone()));
let flat_elements = self.flatten_tuple(new_args.clone()); let flat_elements = self.flatten_tuple(new_args.clone());
let flat_types = flat_elements.iter().map(|e| e.ty.original.ty.clone()).collect(); let flat_types = flat_elements
.iter()
.map(|e| e.ty.original.ty.clone())
.collect();
let flattened_args = Node { let flattened_args = Node {
identity: new_args.identity.clone(), identity: new_args.identity.clone(),
kind: BoundKind::Tuple { elements: flat_elements }, kind: BoundKind::Tuple {
elements: flat_elements,
},
ty: NodeMetrics { ty: NodeMetrics {
original: Rc::new(Node { original: Rc::new(Node {
identity: new_args.identity.clone(), identity: new_args.identity.clone(),
kind: BoundKind::Tuple { elements: vec![] }, kind: BoundKind::Tuple { elements: vec![] },
ty: StaticType::Tuple(flat_types), ty: StaticType::Tuple(flat_types),
}), }),
purity: new_args.ty.purity, purity: new_args.ty.purity,
@@ -193,17 +270,26 @@ impl Specializer {
}, },
}; };
let specialized_callee = self.make_constant_node(compiled_val, StaticType::Function(Box::new(Signature { let specialized_callee = self.make_constant_node(
params: flattened_args.ty.original.ty.clone(), compiled_val,
ret: ret_ty.clone(), StaticType::Function(Box::new(Signature {
})), &new_callee); params: flattened_args.ty.original.ty.clone(),
ret: ret_ty.clone(),
})),
&new_callee,
);
return (specialized_callee, flattened_args, ret_ty); return (specialized_callee, flattened_args, ret_ty);
} }
(new_callee, new_args, original_ty) (new_callee, new_args, original_ty)
} }
fn make_constant_node(&self, val: Value, ty: StaticType, template: &AnalyzedNode) -> AnalyzedNode { fn make_constant_node(
&self,
val: Value,
ty: StaticType,
template: &AnalyzedNode,
) -> AnalyzedNode {
let typed_original = Rc::new(Node { let typed_original = Rc::new(Node {
identity: template.identity.clone(), identity: template.identity.clone(),
kind: BoundKind::Constant(val.clone()), kind: BoundKind::Constant(val.clone()),
+1 -1
View File
@@ -1,8 +1,8 @@
use crate::ast::compiler::bound_nodes::{AnalyzedNode, BoundKind}; use crate::ast::compiler::bound_nodes::{AnalyzedNode, BoundKind};
use crate::ast::nodes::Node; use crate::ast::nodes::Node;
use crate::ast::types::StaticType; use crate::ast::types::StaticType;
use std::rc::Rc;
use std::fmt::Debug; use std::fmt::Debug;
use std::rc::Rc;
#[derive(Clone)] #[derive(Clone)]
pub struct RuntimeMetadata { pub struct RuntimeMetadata {
+19 -8
View File
@@ -198,7 +198,7 @@ impl Environment {
pub fn link(&self, node: TypedNode) -> ExecNode { pub fn link(&self, node: TypedNode) -> ExecNode {
// 1. Analyze // 1. Analyze
let analyzed = Analyzer::analyze(&node, &self.global_purity.borrow()); let analyzed = Analyzer::analyze(&node, &self.global_purity.borrow());
// 2. Collect Analyzed Lambdas // 2. Collect Analyzed Lambdas
LambdaCollector::collect(&analyzed, &mut self.typed_function_registry.borrow_mut()); LambdaCollector::collect(&analyzed, &mut self.typed_function_registry.borrow_mut());
@@ -218,13 +218,18 @@ impl Environment {
pub fn instantiate(&self, node: ExecNode) -> Rc<crate::ast::types::NativeFunction> { pub fn instantiate(&self, node: ExecNode) -> Rc<crate::ast::types::NativeFunction> {
let global_values = self.global_values.clone(); let global_values = self.global_values.clone();
if let BoundKind::Lambda { params, upvalues, body, positional_count } = &node.kind if let BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
} = &node.kind
&& upvalues.is_empty() && upvalues.is_empty()
{ {
let closure = Rc::new(crate::ast::vm::Closure::new( let closure = Rc::new(crate::ast::vm::Closure::new(
params.ty.original.clone(), params.ty.original.clone(),
body.ty.original.clone(), body.ty.original.clone(),
body.clone(), body.clone(),
vec![], vec![],
*positional_count, *positional_count,
)); ));
@@ -294,7 +299,8 @@ impl Environment {
registry: untyped_reg.clone(), registry: untyped_reg.clone(),
analyzed_registry: typed_reg.clone(), analyzed_registry: typed_reg.clone(),
}); });
let sub_rtl_lookup = Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args)); let sub_rtl_lookup =
Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args));
let sub_specializer = Specializer::new( let sub_specializer = Specializer::new(
Some(sub_registry), Some(sub_registry),
@@ -335,7 +341,9 @@ impl Environment {
pub fn run_script(&self, source: &str) -> Result<Value, String> { pub fn run_script(&self, source: &str) -> Result<Value, String> {
if self.debug_mode { if self.debug_mode {
let (res, logs) = self.run_debug(source)?; let (res, logs) = self.run_debug(source)?;
for line in logs { println!("{}", line); } for line in logs {
println!("{}", line);
}
res res
} else { } else {
let compiled = self.compile(source)?; let compiled = self.compile(source)?;
@@ -363,7 +371,10 @@ impl Environment {
if let Some(closure) = next_obj.as_any().downcast_ref::<crate::ast::vm::Closure>() { 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); result = vm.run_with_args_observed(&mut observer, closure, next_args);
} else { } else {
result = Err(format!("Tail call target is not a closure: {}", next_obj.type_name())); result = Err(format!(
"Tail call target is not a closure: {}",
next_obj.type_name()
));
break; break;
} }
} }
+4 -15
View File
@@ -262,11 +262,7 @@ impl VM {
} }
#[inline(always)] #[inline(always)]
fn eval_core<O: VMObserver>( fn eval_core<O: VMObserver>(&mut self, obs: &mut O, node: &ExecNode) -> Result<Value, String> {
&mut self,
obs: &mut O,
node: &ExecNode,
) -> Result<Value, String> {
match &node.kind { match &node.kind {
BoundKind::Nop => Ok(Value::Void), BoundKind::Nop => Ok(Value::Void),
BoundKind::Constant(v) => Ok(v.clone()), BoundKind::Constant(v) => Ok(v.clone()),
@@ -380,14 +376,11 @@ impl VM {
if node.ty.is_tail { if node.ty.is_tail {
match func_val { match func_val {
Value::Object(obj) => { Value::Object(obj) => {
return Ok(Value::TailCallRequest(Box::new((obj, arg_vals)))) return Ok(Value::TailCallRequest(Box::new((obj, arg_vals))));
} }
Value::Function(f) => return Ok((f.func)(arg_vals)), Value::Function(f) => return Ok((f.func)(arg_vals)),
_ => { _ => {
return Err(format!( return Err(format!("Tail call target is not a function: {}", func_val));
"Tail call target is not a function: {}",
func_val
))
} }
} }
} }
@@ -423,10 +416,7 @@ impl VM {
res => break res, res => break res,
} }
} else { } else {
break Err(format!( break Err(format!("Object is not a closure: {}", obj.type_name()));
"Object is not a closure: {}",
obj.type_name()
));
} }
} }
_ => break Err(format!("Attempt to call non-function: {}", func_val)), _ => break Err(format!("Attempt to call non-function: {}", func_val)),
@@ -494,7 +484,6 @@ impl VM {
} }
} }
pub fn resolve_tail_calls(&mut self, mut result: Value) -> Value { pub fn resolve_tail_calls(&mut self, mut result: Value) -> Value {
while let Value::TailCallRequest(payload) = result { while let Value::TailCallRequest(payload) = result {
let (next_obj, next_args) = *payload; let (next_obj, next_args) = *payload;
+11 -3
View File
@@ -291,17 +291,25 @@ mod tests {
#[test] #[test]
fn test_nested_destructuring_optimization() { fn test_nested_destructuring_optimization() {
let env = Environment::new(); let env = Environment::new();
// 1. Tuple-to-Tuple // 1. Tuple-to-Tuple
let source_tuple = "((fn [[x y]] (+ x y)) [10 20])"; let source_tuple = "((fn [[x y]] (+ x y)) [10 20])";
assert_eq!(format!("{}", env.run_script(source_tuple).unwrap()), "30"); assert_eq!(format!("{}", env.run_script(source_tuple).unwrap()), "30");
let dump_tuple = env.dump_ast(source_tuple).unwrap(); let dump_tuple = env.dump_ast(source_tuple).unwrap();
assert!(dump_tuple.contains("Constant: 30"), "Nested tuple should be folded to 30. Dump:\n{}", dump_tuple); assert!(
dump_tuple.contains("Constant: 30"),
"Nested tuple should be folded to 30. Dump:\n{}",
dump_tuple
);
// 2. Record-to-Tuple // 2. Record-to-Tuple
let source_record = "((fn [[x y]] (+ x y)) {:a 5 :b 7})"; let source_record = "((fn [[x y]] (+ x y)) {:a 5 :b 7})";
assert_eq!(format!("{}", env.run_script(source_record).unwrap()), "12"); assert_eq!(format!("{}", env.run_script(source_record).unwrap()), "12");
let dump_record = env.dump_ast(source_record).unwrap(); let dump_record = env.dump_ast(source_record).unwrap();
assert!(dump_record.contains("Constant: 12"), "Record-to-Tuple destructuring should be folded to 12. Dump:\n{}", dump_record); assert!(
dump_record.contains("Constant: 12"),
"Record-to-Tuple destructuring should be folded to 12. Dump:\n{}",
dump_record
);
} }
} }