more complex tests

This commit is contained in:
Aidan Hahn 2021-07-19 23:59:03 -07:00
parent df5cb47cb4
commit 2c30975571
No known key found for this signature in database
GPG key ID: 327711E983899316
6 changed files with 179 additions and 320 deletions

View file

@ -129,151 +129,3 @@ pub fn eval(
return Ok(Ctr::Seg(ret))
}
/*
fn eval_inner(
ast: Ast,
vars: Rc<RefCell<VTable>>,
funcs: Rc<RefCell<FTable>>,
sym_loose: bool,
first_item: bool
) -> Result<Ast, String> {
/* LOGIC:
* 1. make new ast
* 2. call process_ctr on ast.car
* 3. set new.car to result
* 4. else call process_ctr on cdr
* (set first_item to false)
* (and set new.cdr to result)
* 5. return new ast
*/
let ret = new_ast(Ctr::None, Ctr::None);
let res_car;
let res_cdr;
// Handle Function Case
match process_ctr(
ast.borrow().clone().car,
vars.clone(),
funcs.clone(),
sym_loose,
first_item,
true,
ast.borrow().clone().cdr
) {
Ok(ctr) => {
res_car = ctr;
},
Err(err) => {
return Err(err)
}
}
match process_ctr(
ast.borrow().clone().cdr,
vars.clone(),
funcs.clone(),
sym_loose,
false,
false,
Ctr::None
) {
Ok(ctr) => {
res_cdr = ctr;
},
Err(err) => {
return Err(err)
}
}
ret.borrow_mut().car = res_car;
ret.borrow_mut().cdr = res_cdr;
return Ok(ret)
}
fn process_ctr(
ctr: Ctr,
vars: Rc<RefCell<VTable>>,
funcs: Rc<RefCell<FTable>>,
sym_loose: bool,
first_item: bool,
is_car: bool,
rest: Ctr
) -> Result<Ctr, String> {
/* LOGIC:
* 1. if symbol, unwrap (DEEP COPY)
* 2. if first_item and symbol, call function?
* (else: return var copy)
* 3. if list return result of eval()
* 4. finally, return a clone (shallow copy) of datum
*/
match ctr.clone() {
Ctr::Symbol(token) => {
print!("[+] Detected Symbol: {}\n", token);
let mut tok = token;
if let Some(s) = vars.borrow().get(&tok) {
print!("[+] Found symbol value\n");
// 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;
}
}
}
if !pass {
return Ok((*s.clone()).clone())
}
// variable not found case
} else if !first_item && !sym_loose {
return Err(format!("variable '{}' not found", tok).to_string())
}
// function call case
if first_item {
if let Some(f) = funcs.borrow().get(&tok) {
if let Ctr::Seg(args) = rest {
print!("[+] Calling function: {}\n", tok);
match func_call(f.clone(), args, vars.clone(), funcs.clone()) {
Ok(a) => {
print!("[+] Function Call Result: {:#?}\n", 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) => {
// 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(Ctr::Seg(ast))
},
Err(e) => {
Err(e)
}
}
},
_ => Ok(ctr.clone())
}
}
*/