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

@ -26,7 +26,7 @@ use crate::eval::eval;
pub type FTable = HashMap<String, Rc<RefCell<Function>>>;
// Standardized function signature for stdlib functions
pub type InternalOperation = fn(Ast, Rc<RefCell<VTable>>, Rc<RefCell<FTable>>) -> Ast;
pub type InternalOperation = fn(Ast, Rc<RefCell<VTable>>, Rc<RefCell<FTable>>) -> Ctr;
pub struct ExternalOperation {
// Un-evaluated abstract syntax tree
// TODO: Intermediate evaluation to simplify branches with no argument in them
@ -76,7 +76,7 @@ pub fn func_call(
args: Ast,
vars: Rc<RefCell<VTable>>,
funcs: Rc<RefCell<FTable>>
) -> Result<Ast, String> {
) -> Result<Ctr, String> {
let called_func = function.borrow_mut();
let mut n_args: Ast = args.clone();
if !called_func.eval_lazy {
@ -151,15 +151,22 @@ pub fn func_call(
match &called_func.function {
Operation::Internal(f) => Ok((f)(n_args, vars, funcs)),
Operation::External(f) => {
let mut temp = vars.borrow().clone();
for n in 0..f.arg_syms.len() {
temp.insert(
vars.borrow().insert(
f.arg_syms[n].clone(),
Rc::new(list_idx(n_args.clone(), n as u128))
);
}
eval(f.ast.clone(), Rc::new(RefCell::new(temp)), funcs, called_func.loose_syms)
let result = eval(f.ast.clone(), vars.clone(), funcs, called_func.loose_syms);
for n in 0..f.arg_syms.len() {
vars.borrow().remove(&f.arg_syms[n].clone());
}
match result {
Ok(r) => Ok(r.borrow().car),
Err(e) => Err(e)
}
}
}
}