undid eval tweaks, made tests for weird def cases

This commit is contained in:
Ava Apples Affine 2023-03-17 12:21:42 -07:00
parent 2dfe73de6b
commit a01df6b7b2
Signed by: affine
GPG key ID: 3A4645B8CF806069
2 changed files with 60 additions and 11 deletions

View file

@ -39,19 +39,25 @@ pub fn eval_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
if ast.len() > 1 {
Err("do not eval more than one thing at a time".to_string())
} else {
let arguments: Ctr;
match *ast.car {
Ctr::Seg(ref s) => arguments = *eval(s, syms)?.clone(),
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 {
arguments = *eval(s, syms)?.clone()
Ok(*eval(s, syms)?.clone())
} else {
arguments = *intermediate
Ok(*intermediate)
}
},
_ => arguments = *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
*
* thanks you for your patience (ava)
match arguments {
Ctr::Seg(ref s) => Ok(*eval(s, syms)?.clone()),
Ctr::Symbol(ref sym) => {
@ -63,7 +69,7 @@ pub fn eval_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
}
},
_ => Ok(arguments)
}
}*/
}
}
@ -267,9 +273,12 @@ pub fn store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<C
Ctr::Seg(ref s) => match *eval(s, syms)? {
Ctr::String(ref s) => name = s.clone(),
Ctr::Symbol(ref s) => name = s.clone(),
_ => return Err("new symbol name doesnt make sense".to_string()),
_ => {
println!("{}", *eval(s, syms)?);
return Err("evaluated symbol name doesnt make sense".to_string());
},
_ => return Err("new symbol name doesnt make sense".to_string()),
},
_ => return Err("symbol name doesnt make sense".to_string()),
}
// remove var case
@ -348,8 +357,6 @@ pub fn store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<C
return Ok(Ctr::None)
}
println!("{}", args);
let mut arg_list = vec![];
if !args.circuit(&mut |c: &Ctr| -> bool {
if let Ctr::Symbol(s) = c {

View file

@ -88,6 +88,46 @@ mod decl_lib_tests {
}
}
#[test]
fn test_variable_def_redef_via_reference_and_lookup() {
let doc1 = "(def test 'my test var' 1)";
let doc2 = "(def ref 'references test' (quote test))";
let doc3 = "(def (eval ref) 'my test var' '2')";
let doc4 = "(test)";
let result = "('2')";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
eval(&lex(&doc1.to_string()).unwrap(), &mut syms).unwrap();
eval(&lex(&doc2.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();
assert_eq!(res.to_string(), result);
}
#[test]
fn test_variable_doc_dynamic() {
let doc1 = "(def test-doc 'docs for test' 'test tests tests test')";
let doc2 = "(def test test-doc 'one')";
let doc3 = "(eq? (and
(eq? (get-doc test) test-doc)
(eq? test 'one')))";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
eval(&lex(&doc1.to_string()).unwrap(), &mut syms).unwrap();
eval(&lex(&doc2.to_string()).unwrap(), &mut syms).unwrap();
if let Ctr::Bool(b) = *eval(&lex(&doc3.to_string()).unwrap(), &mut syms).unwrap() {
assert!(b);
} else {
assert!(false);
}
}
#[test]
fn test_func_def_no_args() {
let doc1 = "(def test 'my test func' () 1)";
@ -195,6 +235,8 @@ 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)";
@ -212,7 +254,7 @@ mod decl_lib_tests {
.to_string(),
result.to_string(),
);
}
}*/
#[test]
fn test_lambda_str_equivalency_list() {