Eval enhancements. Rewrote store to be significantly better

This commit is contained in:
Ava Apples Affine 2023-03-17 11:42:36 -07:00
parent 20821057f2
commit 3848d3bcfa
Signed by: affine
GPG key ID: 3A4645B8CF806069
6 changed files with 363 additions and 327 deletions

View file

@ -5,76 +5,46 @@ mod decl_lib_tests {
#[test]
fn test_variable_def_and_lookup() {
let doc1 = "(def test 'my test var' 1)";
let doc2 = "(test)";
let doc2 = "test";
let result = "(1)";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
if let Ok(tree) = lex(&doc1.to_string()) {
let eval_result = *eval(&tree, &mut syms).unwrap();
if let Ctr::None = eval_result {
// pass
} else {
eprintln!("bad: {eval_result}");
assert!(false);
}
} else {
eprintln!("couldn't lex doc1");
assert!(false);
}
eval(&lex(&doc1.to_string()).unwrap(), &mut syms).unwrap();
let res = *eval(&lex(&doc2.to_string()).unwrap(), &mut syms).unwrap();
assert_eq!(res.to_string(), result);
}
if let Ok(tree) = lex(&doc2.to_string()) {
println!("tree: {tree}");
let eval_result = *eval(&tree, &mut syms).unwrap();
if let Ctr::Seg(ref i) = eval_result {
assert_eq!(i.to_string(), result);
} else {
eprintln!("bad: {eval_result}");
assert!(false);
}
} else {
eprintln!("couldn't lex doc2");
assert!(false);
}
#[test]
fn test_variable_def_and_lookup_list() {
let doc1 = "(def test 'my test var' (1))";
let doc2 = "test";
let result = "((1))";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
eval(&lex(&doc1.to_string()).unwrap(), &mut syms).unwrap();
let res = *eval(&lex(&doc2.to_string()).unwrap(), &mut syms).unwrap();
assert_eq!(res.to_string(), result);
}
#[test]
fn test_func_def_and_lookup() {
let doc1 = "(def test 'my test func' (hello) hello)";
let doc2 = "(test '1')";
let doc2 = "(test 1)";
let result = "1";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
if let Ok(tree) = lex(&doc1.to_string()) {
let eval_result = *eval(&tree, &mut syms).unwrap();
if let Ctr::None = eval_result {
// pass
} else {
eprintln!("bad: {eval_result}");
assert!(false);
}
} else {
eprintln!("couldn't lex doc1");
assert!(false);
}
if let Ok(tree) = lex(&doc2.to_string()) {
let eval_result = *eval(&tree, &mut syms).unwrap();
if let Ctr::String(ref i) = eval_result {
assert_eq!(i.to_string(), result);
} else {
eprintln!("bad: {eval_result}");
assert!(false);
}
} else {
eprintln!("couldn't lex doc2");
assert!(false);
}
eval(&lex(&doc1.to_string()).unwrap(), &mut syms).unwrap();
let res = *eval(&lex(&doc2.to_string()).unwrap(), &mut syms).unwrap();
assert_eq!(res.to_string(), result);
}
#[test]
@ -88,46 +58,10 @@ mod decl_lib_tests {
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
if let Ok(tree) = lex(&doc1.to_string()) {
let eval_result = *eval(&tree, &mut syms).unwrap();
if let Ctr::None = eval_result {
// pass
} else {
eprintln!("bad: {eval_result}");
assert!(false);
}
} else {
eprintln!("couldn't lex doc1");
assert!(false);
}
if let Ok(tree) = lex(&doc2.to_string()) {
println!("tree: {tree}");
let eval_result = *eval(&tree, &mut syms).unwrap();
if let Ctr::None = eval_result {
// pass
} else {
eprintln!("bad: {eval_result}");
assert!(false);
}
} else {
eprintln!("couldn't lex doc2");
assert!(false);
}
if let Ok(tree) = lex(&doc3.to_string()) {
println!("tree: {tree}");
let eval_result = *eval(&tree, &mut syms).unwrap();
if let Ctr::Seg(ref i) = eval_result {
assert_eq!(i.to_string(), result);
} else {
eprintln!("bad: {eval_result}");
assert!(false);
}
} else {
eprintln!("couldn't lex doc3");
assert!(false);
}
eval(&lex(&doc1.to_string()).unwrap(), &mut syms).unwrap();
eval(&lex(&doc2.to_string()).unwrap(), &mut syms).unwrap();
let res = *eval(&lex(&doc3.to_string()).unwrap(), &mut syms).unwrap();
assert_eq!(res.to_string(), result);
}
#[test]
@ -140,55 +74,23 @@ mod decl_lib_tests {
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
if let Ok(tree) = lex(&doc1.to_string()) {
let eval_result = *eval(&tree, &mut syms).unwrap();
if let Ctr::None = eval_result {
// pass
} else {
eprintln!("bad: {eval_result}");
assert!(false);
}
} else {
eprintln!("couldn't lex doc1");
assert!(false);
}
eval(&lex(&doc1.to_string()).unwrap(), &mut syms).unwrap();
eval(&lex(&doc2.to_string()).unwrap(), &mut syms).unwrap();
if let Ok(tree) = lex(&doc2.to_string()) {
println!("tree: {tree}");
let eval_result = *eval(&tree, &mut syms).unwrap();
if let Ctr::None = eval_result {
// pass
} else {
eprintln!("bad: {eval_result}");
assert!(false);
}
let eval_result = eval(&lex(&doc3.to_string()).unwrap(), &mut syms);
if let Err(s) = eval_result {
assert_eq!(
s.to_string(),
"error in call to test: undefined symbol: test".to_string()
);
} else {
eprintln!("couldn't lex doc2");
assert!(false);
}
if let Ok(tree) = lex(&doc3.to_string()) {
println!("tree: {tree}");
let eval_result = eval(&tree, &mut syms);
if let Err(s) = eval_result {
assert_eq!(
s.to_string(),
"error in call to test: undefined symbol: test".to_string()
);
} else {
let res = eval_result.unwrap();
eprintln!("shouldn't have suceeded: {res}");
assert!(false);
}
} else {
eprintln!("couldn't lex doc3");
assert!(false);
}
}
#[test]
fn test_func_def_no_args() {
let doc1 = "(def test 'my test func' () '1')";
let doc1 = "(def test 'my test func' () 1)";
let doc2 = "(test)";
let result = "1";
@ -196,40 +98,20 @@ mod decl_lib_tests {
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
if let Ok(tree) = lex(&doc1.to_string()) {
let eval_result = *eval(&tree, &mut syms).unwrap();
if let Ctr::None = eval_result {
// pass
} else {
eprintln!("bad: {eval_result}");
assert!(false);
}
} else {
eprintln!("couldn't lex doc1");
assert!(false);
}
if let Ok(tree) = lex(&doc2.to_string()) {
let eval_result = *eval(&tree, &mut syms).unwrap();
if let Ctr::String(ref i) = eval_result {
assert_eq!(i.to_string(), result);
} else {
eprintln!("bad: {eval_result}");
assert!(false);
}
} else {
eprintln!("couldn't lex doc2");
assert!(false);
}
eval(&lex(&doc1.to_string()).unwrap(), &mut syms).unwrap();
let res = *eval(&lex(&doc2.to_string()).unwrap(), &mut syms).unwrap();
assert_eq!(res.to_string(), result);
}
#[test]
fn test_isset_true() {
let doc1 = "(def test '' 1)";
let doc2 = "(set? test)";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
let def_tree = lex(&doc1.to_string()).unwrap();
let set_tree = lex(&doc2.to_string()).unwrap();
eval(&def_tree, &mut syms).unwrap();
@ -298,6 +180,40 @@ mod decl_lib_tests {
);
}
#[test]
fn test_eval_basic() {
let document = "(eval (1 2 3))";
let result = "(1 2 3)";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[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]
fn test_lambda_str_equivalency_list() {
let document = "(lambda (x y) (add x y))";
@ -335,7 +251,6 @@ mod decl_lib_tests {
let it = *eval(
&lex(&document.to_string()).unwrap(),
&mut syms).unwrap();
println!("final return: {}", it);
if let Ctr::Integer(i) = it {
assert_eq!(i, 3)
} else {
@ -353,7 +268,6 @@ mod decl_lib_tests {
let it = *eval(
&lex(&document.to_string()).unwrap(),
&mut syms).unwrap();
println!("{}", it);
if let Ctr::Integer(i) = it {
assert_eq!(i, 3)
} else {
@ -372,11 +286,31 @@ mod decl_lib_tests {
let it = *eval(
&lex(&document.to_string()).unwrap(),
&mut syms).unwrap();
println!("{}", it);
if let Ctr::Integer(i) = it {
assert_eq!(i, 3)
} else {
panic!()
}
}
#[test]
fn test_setget_doc_string() {
let highly_inadvisable = "(set-doc help 'help')";
let document = "(get-doc help)";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
let _ = *eval(
&lex(&highly_inadvisable.to_string()).unwrap(),
&mut syms).unwrap();
let it = *eval(
&lex(&document.to_string()).unwrap(),
&mut syms).unwrap();
if let Ctr::String(i) = it {
assert_eq!(i, "help".to_string())
} else {
panic!()
}
}
}