another tweak to eval/quote equivalency, more tests

This commit is contained in:
Ava Apples Affine 2023-06-08 17:11:10 -07:00
parent 33791c7f3b
commit 0a01f9178c
3 changed files with 42 additions and 57 deletions

View file

@ -45,11 +45,20 @@ fn eval_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, Traceback> {
match *ast.car { match *ast.car {
Ctr::Seg(ref s) => { Ctr::Seg(ref s) => {
match eval(s, syms) { match eval(s, syms) {
Err(e) => Err(e.with_trace(
("eval", "evaluation failure")
.into())),
Ok(s) => if let Ctr::Seg(ref inner) = *s {
match eval(inner, syms) {
Err(e) => Err(e.with_trace( Err(e) => Err(e.with_trace(
("eval", "evaluation failure") ("eval", "evaluation failure")
.into())), .into())),
Ok(s) => Ok(*s), Ok(s) => Ok(*s),
} }
} else {
Ok(*s)
},
}
} }
Ctr::Symbol(ref sym) => { Ctr::Symbol(ref sym) => {
@ -73,25 +82,6 @@ fn eval_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, Traceback> {
}, },
_ => Ok(*ast.car.clone()) _ => Ok(*ast.car.clone())
} }
/* this bit removed because it was determined eval shouldnt do things twice
* kept here for reference purposes since I have gone back and forth on this
* a bit
*
* thank you for your patience (ava)
match arguments {
Ctr::Seg(ref s) => Ok(*eval(s, syms)?.clone()),
Ctr::Symbol(ref sym) => {
let intermediate = syms.call_symbol(sym, &Seg::new(), true)?;
if let Ctr::Seg(ref s) = *intermediate {
Ok(*eval(s, syms)?.clone())
} else {
Ok(*intermediate)
}
},
_ => Ok(arguments)
}*/
} }
} }

View file

@ -164,7 +164,6 @@ impl SymTable {
_ => return Ok(val.clone()), _ => return Ok(val.clone()),
} }
} }
if call_func { if call_func {
symbol.call(args, self) symbol.call(args, self)
} else { } else {

View file

@ -92,9 +92,11 @@ mod decl_lib_tests {
fn test_variable_def_redef_via_reference_and_lookup() { fn test_variable_def_redef_via_reference_and_lookup() {
let doc1 = "(def test 'my test var' 1)"; let doc1 = "(def test 'my test var' 1)";
let doc2 = "(def ref 'references test' (quote test))"; let doc2 = "(def ref 'references test' (quote test))";
let doc3 = "(def (eval ref) 'my test var' '2')"; let doc3 = "(def ref 'my test var' '2')";
let doc4 = "(test)"; let test = "(test)";
let result = "('2')"; let res1 = "(1)";
let doc4 = "(def (eval ref) 'my test var' '2')";
let res2 = "('2')";
let mut syms = SymTable::new(); let mut syms = SymTable::new();
static_stdlib(&mut syms); static_stdlib(&mut syms);
@ -103,8 +105,12 @@ mod decl_lib_tests {
eval(&lex(&doc1.to_string()).unwrap(), &mut syms).unwrap(); eval(&lex(&doc1.to_string()).unwrap(), &mut syms).unwrap();
eval(&lex(&doc2.to_string()).unwrap(), &mut syms).unwrap(); eval(&lex(&doc2.to_string()).unwrap(), &mut syms).unwrap();
eval(&lex(&doc3.to_string()).unwrap(), &mut syms).unwrap(); eval(&lex(&doc3.to_string()).unwrap(), &mut syms).unwrap();
let res = *eval(&lex(&doc4.to_string()).unwrap(), &mut syms).unwrap(); let r1 = *eval(&lex(&test.to_string()).unwrap(), &mut syms).unwrap();
assert_eq!(res.to_string(), result); assert_eq!(r1.to_string(), res1);
eval(&lex(&doc2.to_string()).unwrap(), &mut syms).unwrap();
eval(&lex(&doc4.to_string()).unwrap(), &mut syms).unwrap();
let r2 = *eval(&lex(&test.to_string()).unwrap(), &mut syms).unwrap();
assert_eq!(r2.to_string(), res2);
} }
#[test] #[test]
@ -235,27 +241,6 @@ mod decl_lib_tests {
); );
} }
/* THIS TEST REMOVED BECAUSE EVAL SHOULDNT ARBITRARILY DO THINGS TWICE
* KEPT FOR REFERENCE PURPOSES JUST IN CASE.
#[test]
fn test_eval_var_deref() {
let def1 = "(def one '' 1)";
let def2 = "(def two '' (quote one))";
let document = "(eval two)";
let result = "1";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
eval(&lex(&def1.to_string()).unwrap(), &mut syms).unwrap();
eval(&lex(&def2.to_string()).unwrap(), &mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}*/
#[test] #[test]
fn test_lambda_str_equivalency_list() { fn test_lambda_str_equivalency_list() {
let document = "(lambda (x y) (add x y))"; let document = "(lambda (x y) (add x y))";
@ -375,4 +360,15 @@ mod decl_lib_tests {
} }
} }
#[test]
fn test_eval_quote() {
let doc = "(eval (quote (add 1 1)))";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
dynamic_stdlib(&mut syms, None);
assert_eq!(
*eval(&lex(&doc.to_string()).unwrap(), &mut syms).unwrap().to_string(),
2.to_string()
)
}
} }