simplify func return type, complete eval and process logic

This commit is contained in:
Aidan 2021-06-05 01:14:38 -07:00
parent 0adda0109c
commit 4712d5466c
No known key found for this signature in database
GPG key ID: 327711E983899316
3 changed files with 49 additions and 24 deletions

View file

@ -60,10 +60,11 @@ fn eval_inner(
funcs.clone(),
sym_loose,
first_item,
true,
mret.cdr
) {
Ok(ctr) => {
mret.car = ctr
mret.car = ctr;
},
Err(err) => {
return Err(err)
@ -76,15 +77,18 @@ fn eval_inner(
funcs.clone(),
sym_loose,
false,
RefCell::new(Rc::clone(Ctr::None))
false,
Ctr::None
) {
Ok(ctr) => {
mret.cdr = ctr
mret.cdr = ctr;
},
Err(err) => {
return Err(err)
}
}
return Ok(ret)
}
fn process_ctr(
@ -93,7 +97,8 @@ fn process_ctr(
funcs: Rc<RefCell<FTable>>,
sym_loose: bool,
first_item: bool,
rest: Ast
is_car: bool,
rest: Ctr
) -> Result<Ctr, String> {
/* LOGIC:
* 1. if symbol, unwrap (DEEP COPY)
@ -106,39 +111,52 @@ fn process_ctr(
match ctr {
Ctr::Symbol(token) => {
let mut tok = token;
if let Some(s) = vars.borrow().get(token) {
if let Some(s) = vars.borrow().get(&tok) {
// is function, or variable alias?
if first_item {
if let Ctr::Symbol(t) = s {
if let Some(f) = funcs.borrow().get(t) {
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;
}
}
// is a basic value.
} else {
return Ok((*s).clone())
return Ok(*s.clone())
}
// else call function
// variable not found case
} else if !first_item && !sym_loose {
return Err("variable not found")
return Err(format!("variable '{}' not found", tok).to_string())
}
// function call case
if first_item {
if let Some(f) = funcs.borrow().get(tok) {
match func_call(f, rest, vars.clone(), funcs.clone()) {
Ok(a) => Ok(Ctr::Seg(a.borrow().clone())),
Err(s) => Err(s)
if let Some(f) = funcs.borrow().get(&tok) {
if let Ctr::Seg(args) = rest {
match func_call(*f, args, vars.clone(), funcs.clone()) {
Ok(a) => return Ok(a.clone()),
Err(s) => return Err(s)
}
} else {
return Err("evaluation: args to function not a list".to_string())
}
} else {
return Err(format!("function '{}' not defined", tok).to_string())
}
// sym_loose and not function call
} else {
return Ok(ctr.clone())
}
},
Ctr::Seg(tree) => {
match eval(tree, vars, funcs, sym_loose) {
// if list is_car then we need to set first_item to true
match eval_inner(tree, vars, funcs, sym_loose, is_car) {
Ok(ast) => {
Ok(ast.borrow())
Ok(Ctr::Seg(ast))
},
Err(e) => {
Err(e)