more complex tests
This commit is contained in:
parent
df5cb47cb4
commit
2c30975571
6 changed files with 179 additions and 320 deletions
148
src/eval.rs
148
src/eval.rs
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
|
|
|||
12
src/func.rs
12
src/func.rs
|
|
@ -77,7 +77,7 @@ pub fn func_call(
|
|||
vars: Rc<RefCell<VTable>>,
|
||||
funcs: Rc<RefCell<FTable>>
|
||||
) -> Result<Ctr, String> {
|
||||
let called_func = function.borrow_mut();
|
||||
let called_func = function.borrow();
|
||||
let mut n_args: Ast = args.clone();
|
||||
if !called_func.eval_lazy {
|
||||
match eval(args, vars.clone(), funcs.clone(), called_func.loose_syms) {
|
||||
|
|
@ -172,18 +172,21 @@ pub fn func_call(
|
|||
);
|
||||
}
|
||||
|
||||
let mut result = Ctr::None;
|
||||
let mut result: Ctr;
|
||||
let mut iterate = f.ast.clone();
|
||||
loop {
|
||||
if let Ctr::Seg(ast) = iterate.borrow().clone().car {
|
||||
match eval(ast, vars.clone(), funcs.clone(), called_func.loose_syms) {
|
||||
Ok(ctr) => {
|
||||
if let Ctr::Seg(ast) = ctr {
|
||||
result = ast.borrow().clone().car;
|
||||
match ctr {
|
||||
Ctr::Seg(ast) => result = ast.borrow().clone().car,
|
||||
_ => result = ctr
|
||||
}
|
||||
},
|
||||
Err(e) => return Err(e)
|
||||
}
|
||||
} else {
|
||||
panic!("function body not in standard form!")
|
||||
}
|
||||
|
||||
match iterate.clone().borrow().clone().cdr {
|
||||
|
|
@ -192,7 +195,6 @@ pub fn func_call(
|
|||
_ => panic!("function body not in standard form!")
|
||||
}
|
||||
}
|
||||
|
||||
for n in 0..f.arg_syms.len() {
|
||||
vars.borrow_mut().remove(&f.arg_syms[n].clone());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -164,7 +164,7 @@ fn process(document: String) -> Result<Ast, String> {
|
|||
} else if let Some(s) = tok_is_symbol(&token) {
|
||||
obj = Ctr::Symbol(s);
|
||||
} else {
|
||||
return Err(format!("Unparsable token:{}", token));
|
||||
return Err(format!("Unparsable token: {}", token));
|
||||
}
|
||||
|
||||
token = String::new();
|
||||
|
|
@ -195,14 +195,14 @@ fn process(document: String) -> Result<Ast, String> {
|
|||
|
||||
|
||||
/* Returns true if token
|
||||
* - is all alphanumeric
|
||||
* - is all alphanumeric except dash and underscore
|
||||
*
|
||||
* else returns false
|
||||
*/
|
||||
fn tok_is_symbol(token: &String) -> Option<String> {
|
||||
let tok = token.as_str();
|
||||
for t in tok.chars() {
|
||||
if !t.is_alphabetic() && !t.is_digit(10) {
|
||||
if !t.is_alphabetic() && !t.is_digit(10) && !(t == '-') && !(t == '_') {
|
||||
return None
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue