add more unit tests for function calls
This commit is contained in:
parent
a6abc993a5
commit
9b981921b4
4 changed files with 339 additions and 19 deletions
20
src/eval.rs
20
src/eval.rs
|
|
@ -51,6 +51,8 @@ fn eval_inner(
|
|||
* 5. return new ast
|
||||
*/
|
||||
let ret = new_ast(Ctr::None, Ctr::None);
|
||||
let res_car;
|
||||
let res_cdr;
|
||||
|
||||
match process_ctr(
|
||||
ast.borrow().clone().car,
|
||||
|
|
@ -59,10 +61,10 @@ fn eval_inner(
|
|||
sym_loose,
|
||||
first_item,
|
||||
true,
|
||||
ret.borrow().clone().cdr
|
||||
ast.borrow().clone().cdr
|
||||
) {
|
||||
Ok(ctr) => {
|
||||
ret.borrow_mut().car = ctr;
|
||||
res_car = ctr;
|
||||
},
|
||||
Err(err) => {
|
||||
return Err(err)
|
||||
|
|
@ -79,13 +81,16 @@ fn eval_inner(
|
|||
Ctr::None
|
||||
) {
|
||||
Ok(ctr) => {
|
||||
ret.borrow_mut().cdr = ctr;
|
||||
res_cdr = ctr;
|
||||
},
|
||||
Err(err) => {
|
||||
return Err(err)
|
||||
}
|
||||
}
|
||||
|
||||
ret.borrow_mut().car = res_car;
|
||||
ret.borrow_mut().cdr = res_cdr;
|
||||
|
||||
return Ok(ret)
|
||||
}
|
||||
|
||||
|
|
@ -112,16 +117,18 @@ fn process_ctr(
|
|||
if let Some(s) = vars.borrow().get(&tok) {
|
||||
|
||||
// is function, or variable alias?
|
||||
let mut pass = false;
|
||||
if first_item {
|
||||
if let Ctr::Symbol(t) = &*(s.clone()) {
|
||||
if let Some(_f) = funcs.borrow().get(t) {
|
||||
// leave this set for the function call case below
|
||||
tok = t.clone();
|
||||
pass = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// is a basic value.
|
||||
} else {
|
||||
if !pass {
|
||||
return Ok((*s.clone()).clone())
|
||||
}
|
||||
|
||||
|
|
@ -139,6 +146,7 @@ fn process_ctr(
|
|||
Err(s) => return Err(s)
|
||||
}
|
||||
} else {
|
||||
println!("AC::: {:?}\n", rest);
|
||||
return Err("evaluation: args to function not a list".to_string())
|
||||
}
|
||||
} else {
|
||||
|
|
@ -152,7 +160,7 @@ fn process_ctr(
|
|||
},
|
||||
Ctr::Seg(tree) => {
|
||||
// if list is_car then we need to set first_item to true
|
||||
match eval_inner(tree, vars, funcs, sym_loose, is_car) {
|
||||
match eval_inner(tree, vars, funcs, sym_loose, is_car && first_item) {
|
||||
Ok(ast) => {
|
||||
Ok(Ctr::Seg(ast))
|
||||
},
|
||||
|
|
|
|||
24
src/func.rs
24
src/func.rs
|
|
@ -32,9 +32,9 @@ pub struct ExternalOperation {
|
|||
// TODO: Intermediate evaluation to simplify branches with no argument in them
|
||||
// Simplified branches must not have side effects.
|
||||
// TODO: Apply Memoization?
|
||||
ast: Ast,
|
||||
pub ast: Ast,
|
||||
// list of argument string tokens
|
||||
arg_syms: Vec<String>
|
||||
pub arg_syms: Vec<String>
|
||||
}
|
||||
|
||||
/* A stored function may either be a pointer to a function
|
||||
|
|
@ -94,11 +94,16 @@ pub fn func_call(
|
|||
|
||||
match &called_func.args {
|
||||
Args::Lazy(num) => {
|
||||
if *num < 0 {
|
||||
}
|
||||
|
||||
if !(*num == (list_len(n_args.clone()) as i128 - 1)) {
|
||||
return Err(format!("expected {} args in call to {}", num, called_func.name))
|
||||
let called_arg_count = list_len(n_args.clone()) as i128;
|
||||
if *num > -1 && (*num != called_arg_count) {
|
||||
return Err(
|
||||
format!(
|
||||
"expected {} args in call to {}. Got {}.",
|
||||
num,
|
||||
called_func.name,
|
||||
called_arg_count
|
||||
)
|
||||
)
|
||||
}
|
||||
},
|
||||
|
||||
|
|
@ -151,10 +156,13 @@ pub fn func_call(
|
|||
match &called_func.function {
|
||||
Operation::Internal(f) => Ok((f)(n_args, vars, funcs)),
|
||||
Operation::External(f) => {
|
||||
|
||||
for n in 0..f.arg_syms.len() {
|
||||
let iter_arg = list_idx(n_args.clone(), n as u128);
|
||||
|
||||
vars.borrow_mut().insert(
|
||||
f.arg_syms[n].clone(),
|
||||
Rc::new(list_idx(n_args.clone(), n as u128))
|
||||
Rc::new(iter_arg)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ pub type Ast = Rc<RefCell<Seg>>;
|
|||
|
||||
// Container
|
||||
#[derive(Clone)]
|
||||
#[derive(Debug)]
|
||||
pub enum Ctr {
|
||||
Symbol(String),
|
||||
String(String),
|
||||
|
|
@ -53,6 +54,7 @@ pub enum Type {
|
|||
* how important RefCells were in Rust
|
||||
*/
|
||||
#[derive(Clone)]
|
||||
#[derive(Debug)]
|
||||
pub struct Seg {
|
||||
/* "Contents of Address Register"
|
||||
* Historical way of referring to the first value in a cell.
|
||||
|
|
@ -215,7 +217,7 @@ pub fn list_idx(tree: Ast, idx: u128) -> Ctr {
|
|||
match inner.car {
|
||||
Ctr::None => Ctr::None,
|
||||
_ => {
|
||||
inner.cdr.clone()
|
||||
inner.car.clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue