Skip to content

Commit

Permalink
finish quick path of no spreading
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Feb 23, 2024
1 parent e71b44c commit 477a3b9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/cirru/calcit-core.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,8 @@
&let
x0 $ &list:first xs
if (list? x0)
recur (append x0 base) & $ &list:rest xs
recur ([] x0 base) & $ &list:rest xs
&call-spread recur (append x0 base) & $ &list:rest xs
&call-spread recur ([] x0 base) & $ &list:rest xs
|/ $ %{} :CodeEntry (:doc |dividing)
:code $ quote
defn / (x & ys)
Expand Down
59 changes: 9 additions & 50 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,65 +531,24 @@ pub fn evaluate_lines(
Ok(ret)
}

/// TODO: simplify this
/// evaluate symbols before calling a function
/// notice that `&` is used to spread a list
/// quick path evaluate symbols before calling a function, not need to check `&` for spreading
pub fn evaluate_args(
items: CalcitList,
scope: &CalcitScope,
file_ns: &str,
call_stack: &CallStackList,
) -> Result<Vec<Calcit>, CalcitErr> {
let mut ret: Vec<Calcit> = Vec::with_capacity(items.len());
let mut spreading = false;
for item in &items {
match item {
Calcit::Syntax(CalcitSyntax::ArgSpread, _) => {
spreading = true;
}
_ => {
if item.is_expr_evaluated() {
if spreading {
match item {
Calcit::List(xs) => {
for x in &**xs {
ret.push((*x).to_owned());
}
spreading = false
}
a => {
return Err(CalcitErr::use_msg_stack(
format!("expected list for spreading, got: {a}"),
call_stack,
))
}
}
} else {
ret.push(item.to_owned());
}
} else {
let v = evaluate_expr(item, scope, file_ns, call_stack)?;
// if let Calcit::Syntax(CalcitSyntax::ArgSpread, _) = item {
// unreachable!("unexpected spread in args: {items}, should be handled before calling this")
// }

if spreading {
match v {
Calcit::List(xs) => {
for x in &*xs {
ret.push((*x).to_owned());
}
spreading = false
}
a => {
return Err(CalcitErr::use_msg_stack(
format!("expected list for spreading, got: {a}"),
call_stack,
))
}
}
} else {
ret.push(v);
}
}
}
if item.is_expr_evaluated() {
ret.push(item.to_owned());
} else {
let v = evaluate_expr(item, scope, file_ns, call_stack)?;
ret.push(v);
}
}
// println!("Evaluated args: {}", ret);
Expand Down

0 comments on commit 477a3b9

Please sign in to comment.