Several changes, see commit msg

* clean up all tests
* bugfix for zero value functions, and test
* removed expand function, put in snippets
* added doc strings to Symbol type
* added doc strings to symbol declarations
* implemented display for Args type
* wrote a help function
* wrote docstrings for all builtins and config vars
This commit is contained in:
Ava Hahn 2023-03-05 22:18:49 -08:00
parent 4b587f11ab
commit dc6342bc74
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
16 changed files with 575 additions and 677 deletions

View file

@ -1,66 +1,44 @@
mod control_lib_tests {
use relish::ast::{eval, lex, Ctr, SymTable};
use relish::ast::{eval, lex, SymTable};
use relish::stdlib::{dynamic_stdlib, static_stdlib};
#[test]
fn test_if_first_case_singlet() {
let document = "(if true 1 2)";
let result = 1;
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
if let Ok(tree) = lex(&document.to_string()) {
if let Ctr::Integer(i) = *eval(&tree, &mut syms).unwrap() {
assert_eq!(i, result);
} else {
assert!(false);
}
} else {
assert!(false);
}
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
result.to_string(),
);
}
#[test]
fn test_if_second_case_singlet() {
let document = "(if false 1 2)";
let result = 2;
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
if let Ok(tree) = lex(&document.to_string()) {
if let Ctr::Integer(i) = *eval(&tree, &mut syms).unwrap() {
assert_eq!(i, result);
} else {
eprintln!("{}", *eval(&tree, &mut syms).unwrap());
assert!(false);
}
} else {
assert!(false);
}
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
result.to_string(),
);
}
#[test]
fn test_complex_case_call() {
let document = "(if true (append () 1) 2)";
let result = "(1)";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
if let Ok(tree) = lex(&document.to_string()) {
if let Ctr::Seg(ref i) = *eval(&tree, &mut syms).unwrap() {
assert_eq!(i.to_string(), result);
} else {
assert!(false);
}
} else {
assert!(false);
}
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
result.to_string(),
);
}
#[test]
@ -70,45 +48,30 @@ mod control_lib_tests {
(temp (append () temp '2')))
temp)";
let result = "('1' '2')";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
if let Ok(tree) = lex(&document.to_string()) {
if let Ctr::Seg(ref i) = *eval(&tree, &mut syms).unwrap() {
assert_eq!(i.to_string(), result);
} else {
assert!(false);
}
} else {
assert!(false);
}
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
result.to_string(),
);
}
#[test]
fn test_let_multibody_evals() {
let document = "(let ((temp '1')) temp (append () temp '2'))";
let result = "('1' '2')";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
if let Ok(tree) = lex(&document.to_string()) {
if let Ctr::Seg(ref i) = *eval(&tree, &mut syms).unwrap() {
assert_eq!(i.to_string(), result);
} else {
assert!(false);
}
} else {
assert!(false);
}
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
result.to_string(),
);
}
#[test]
fn test_let_multiphase_local_multibody_evals() {
// prints 'first body' and then returns ('1' '2' '3')
let document = "(let (
(temp '1')
(temp (append () temp '2')))
@ -116,33 +79,26 @@ mod control_lib_tests {
(append temp '3'))";
let result = "('1' '2' '3')";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
if let Ok(tree) = lex(&document.to_string()) {
if let Ctr::Seg(ref i) = *eval(&tree, &mut syms).unwrap() {
assert_eq!(i.to_string(), result);
} else {
assert!(false);
}
} else {
assert!(false);
}
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
result.to_string(),
);
}
#[test]
fn test_while_basic() {
let switch_dec = "(def switch true)";
let switch_dec = "(def switch '' true)";
// if prev is true, switch looped once and only once
// else prev will have a problematic type
let while_loop = "
(while switch
(def prev switch)
(def prev '' switch)
(toggle switch)
(if switch
(def prev)
(def '' prev)
()))";
let test_check = "prev";
@ -161,15 +117,15 @@ mod control_lib_tests {
#[test]
fn test_while_eval_cond() {
let switch_dec = "(def switch true)";
let switch_dec = "(def switch '' true)";
// if prev is true, switch looped once and only once
// else prev will have a problematic type
let while_loop = "
(while (or switch switch)
(def prev switch)
(def prev '' switch)
(toggle switch)
(if switch
(def prev)
(def '' prev)
()))";
let test_check = "prev";
@ -188,15 +144,15 @@ mod control_lib_tests {
#[test]
fn test_while_2_iter() {
let additional = "(def sw1 true)";
let switch_dec = "(def sw2 true)";
let additional = "(def sw1 '' true)";
let switch_dec = "(def sw2 '' true)";
// while should loop twice and define result
let while_loop = "
(while sw1
(toggle sw2)
(if (and sw1 sw2)
(def sw1 false)
(def result 'yay')))";
(def sw1 '' false)
(def result '' 'yay')))";
let test_check = "result";
let another_tree = lex(&additional.to_string()).unwrap();
@ -216,7 +172,7 @@ mod control_lib_tests {
#[test]
fn test_circuit_basic() {
let document = "(if (circuit true (and true true) true) (def result 'passed') ())";
let document = "(if (circuit true (and true true) true) (def result '' 'passed') ())";
let test = "result";
let doc_tree = lex(&document.to_string()).unwrap();
@ -228,13 +184,12 @@ mod control_lib_tests {
eval(&doc_tree, &mut syms).unwrap();
let res = eval(&test_tree, &mut syms);
println!("{:#?}", res);
res.unwrap();
}
#[test]
fn test_circuit_fail() {
let document = "(if (circuit true (and false true) true) (def result 'passed') ())";
let document = "(if (circuit true (and false true) true) (def result '' 'passed') ())";
let test = "result";
let doc_tree = lex(&document.to_string()).unwrap();
@ -245,13 +200,9 @@ mod control_lib_tests {
dynamic_stdlib(&mut syms).unwrap();
eval(&doc_tree, &mut syms).unwrap();
if let Err(s) = eval(&test_tree, &mut syms) {
assert_eq!(
s,
"error in call to result: undefined symbol: result".to_string()
);
} else {
panic!();
}
assert_eq!(
eval(&test_tree, &mut syms).err().unwrap(),
"error in call to result: undefined symbol: result".to_string()
);
}
}