2023-02-27 22:53:54 -08:00
|
|
|
mod control_lib_tests {
|
2023-03-01 11:38:02 -08:00
|
|
|
use relish::ast::{eval, lex, Ctr, SymTable};
|
|
|
|
|
use relish::stdlib::{dynamic_stdlib, static_stdlib};
|
2023-02-27 22:53:54 -08:00
|
|
|
|
|
|
|
|
#[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();
|
2023-03-01 11:14:42 -08:00
|
|
|
dynamic_stdlib(&mut syms).unwrap();
|
2023-02-27 22:53:54 -08:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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();
|
2023-03-01 11:14:42 -08:00
|
|
|
dynamic_stdlib(&mut syms).unwrap();
|
2023-02-27 22:53:54 -08:00
|
|
|
|
|
|
|
|
if let Ok(tree) = lex(&document.to_string()) {
|
|
|
|
|
if let Ctr::Integer(i) = *eval(&tree, &mut syms).unwrap() {
|
|
|
|
|
assert_eq!(i, result);
|
|
|
|
|
} else {
|
2023-02-28 11:12:27 -08:00
|
|
|
eprintln!("{}", *eval(&tree, &mut syms).unwrap());
|
2023-02-27 22:53:54 -08:00
|
|
|
assert!(false);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
assert!(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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();
|
2023-03-01 11:14:42 -08:00
|
|
|
dynamic_stdlib(&mut syms).unwrap();
|
2023-02-27 22:53:54 -08:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-01 15:17:50 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_let_multiphase_locals() {
|
|
|
|
|
let document = "(let (
|
|
|
|
|
(temp '1')
|
|
|
|
|
(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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_let_multiphase_local_multibody_evals() {
|
2023-03-02 09:43:12 -08:00
|
|
|
// prints 'first body' and then returns ('1' '2' '3')
|
2023-03-01 15:17:50 -08:00
|
|
|
let document = "(let (
|
|
|
|
|
(temp '1')
|
|
|
|
|
(temp (append () temp '2')))
|
|
|
|
|
(echo 'first body')
|
|
|
|
|
(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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-02 15:29:50 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_while_basic() {
|
|
|
|
|
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)
|
|
|
|
|
(toggle switch)
|
|
|
|
|
(if switch
|
|
|
|
|
(def prev)
|
|
|
|
|
()))";
|
|
|
|
|
let test_check = "prev";
|
|
|
|
|
|
|
|
|
|
let switch_tree = lex(&switch_dec.to_string()).unwrap();
|
|
|
|
|
let while_tree = lex(&while_loop.to_string()).unwrap();
|
|
|
|
|
let check_tree = lex(&test_check.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
|
|
|
|
dynamic_stdlib(&mut syms).unwrap();
|
|
|
|
|
|
|
|
|
|
eval(&switch_tree, &mut syms).unwrap();
|
|
|
|
|
eval(&while_tree, &mut syms).unwrap();
|
|
|
|
|
eval(&check_tree, &mut syms).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_while_eval_cond() {
|
|
|
|
|
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)
|
|
|
|
|
(toggle switch)
|
|
|
|
|
(if switch
|
|
|
|
|
(def prev)
|
|
|
|
|
()))";
|
|
|
|
|
let test_check = "prev";
|
|
|
|
|
|
|
|
|
|
let switch_tree = lex(&switch_dec.to_string()).unwrap();
|
|
|
|
|
let while_tree = lex(&while_loop.to_string()).unwrap();
|
|
|
|
|
let check_tree = lex(&test_check.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
|
|
|
|
dynamic_stdlib(&mut syms).unwrap();
|
|
|
|
|
|
|
|
|
|
eval(&switch_tree, &mut syms).unwrap();
|
|
|
|
|
eval(&while_tree, &mut syms).unwrap();
|
|
|
|
|
eval(&check_tree, &mut syms).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_while_2_iter() {
|
|
|
|
|
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')))";
|
|
|
|
|
let test_check = "result";
|
|
|
|
|
|
|
|
|
|
let another_tree = lex(&additional.to_string()).unwrap();
|
|
|
|
|
let switch_tree = lex(&switch_dec.to_string()).unwrap();
|
|
|
|
|
let while_tree = lex(&while_loop.to_string()).unwrap();
|
|
|
|
|
let check_tree = lex(&test_check.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
|
|
|
|
dynamic_stdlib(&mut syms).unwrap();
|
|
|
|
|
|
|
|
|
|
eval(&another_tree, &mut syms).unwrap();
|
|
|
|
|
eval(&switch_tree, &mut syms).unwrap();
|
|
|
|
|
eval(&while_tree, &mut syms).unwrap();
|
|
|
|
|
eval(&check_tree, &mut syms).unwrap();
|
|
|
|
|
}
|
2023-03-03 14:29:53 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_circuit_basic() {
|
|
|
|
|
let document = "(if (circuit true (and true true) true) (def result 'passed') ())";
|
|
|
|
|
let test = "result";
|
|
|
|
|
|
|
|
|
|
let doc_tree = lex(&document.to_string()).unwrap();
|
|
|
|
|
let test_tree = lex(&test.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
|
|
|
|
dynamic_stdlib(&mut syms).unwrap();
|
|
|
|
|
|
|
|
|
|
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 test = "result";
|
|
|
|
|
|
|
|
|
|
let doc_tree = lex(&document.to_string()).unwrap();
|
|
|
|
|
let test_tree = lex(&test.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
|
|
|
|
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!();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-27 22:53:54 -08:00
|
|
|
}
|