This commit is contained in:
Aidan Hahn 2022-01-16 22:02:40 -08:00
parent f805290a4b
commit be73b0b828
No known key found for this signature in database
GPG key ID: 327711E983899316
17 changed files with 588 additions and 675 deletions

View file

@ -1,14 +1,16 @@
mod func_tests {
use std::rc::Rc;
use std::cell::RefCell;
use relish::ast::{Ast, Type, Ctr, new_ast};
use relish::ast::VTable;
use relish::ast::{Function, Operation, FTable, Args, func_declare, func_call, ExternalOperation, lex};
use relish::ast::{
func_call, func_declare, lex, Args, ExternalOperation, FTable, Function, Operation,
};
use relish::ast::{new_ast, Ast, Ctr, Type};
use std::cell::RefCell;
use std::rc::Rc;
#[test]
fn decl_and_call_internal_func() {
let test_internal_func: Function = Function{
name: String::from("test_func_in"),
let test_internal_func: Function = Function {
name: String::from("test_func_in"),
loose_syms: false,
eval_lazy: false,
args: Args::Strict(vec![Type::Bool]),
@ -21,14 +23,13 @@ mod func_tests {
}
Ctr::Bool(is_bool)
}
))
},
)),
};
let ft = Rc::new(RefCell::new(FTable::new()));
let vt = Rc::new(RefCell::new(VTable::new()));
let args = new_ast(Ctr::Bool(true), Ctr::None);
if let Some(s) = func_declare(ft.clone(),
Rc::new(RefCell::new(test_internal_func))) {
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(test_internal_func))) {
print!("Error declaring internal func: {}", s);
assert!(false);
}
@ -61,23 +62,21 @@ mod func_tests {
match lex("((input))".to_string()) {
Err(e) => panic!("{}", e),
Ok(finner) => {
let test_external_func: Function = Function{
let test_external_func: Function = Function {
name: String::from("echo"),
loose_syms: false,
eval_lazy: false,
args: Args::Lazy(1),
function: Operation::External(
ExternalOperation{
arg_syms: vec!["input".to_string()],
ast: finner
}
)
function: Operation::External(ExternalOperation {
arg_syms: vec!["input".to_string()],
ast: finner,
}),
};
let ft = Rc::new(RefCell::new(FTable::new()));
let vt = Rc::new(RefCell::new(VTable::new()));
let args = new_ast(Ctr::String("test".to_string()), Ctr::None);
if let Some(s) = func_declare(ft.clone(),
Rc::new(RefCell::new(test_external_func))) {
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(test_external_func)))
{
print!("Error declaring external func: {}", s);
assert!(false);
}
@ -92,13 +91,11 @@ mod func_tests {
}
match func_call(func, args, vt, ft) {
Ok(ret) => {
match ret {
Ctr::String(b) => assert!(b == "test"),
_ => {
print!("Invalid return from func. Got {:?}\n", ret);
assert!(false);
}
Ok(ret) => match ret {
Ctr::String(b) => assert!(b == "test"),
_ => {
print!("Invalid return from func. Got {:?}\n", ret);
assert!(false);
}
},
Err(e) => {
@ -115,23 +112,21 @@ mod func_tests {
match lex("((input) (input))".to_string()) {
Err(e) => panic!("{}", e),
Ok(finner) => {
let test_external_func: Function = Function{
let test_external_func: Function = Function {
name: String::from("echo_2"),
loose_syms: false,
eval_lazy: false,
args: Args::Lazy(1),
function: Operation::External(
ExternalOperation{
arg_syms: vec!["input".to_string()],
ast: finner
}
)
function: Operation::External(ExternalOperation {
arg_syms: vec!["input".to_string()],
ast: finner,
}),
};
let ft = Rc::new(RefCell::new(FTable::new()));
let vt = Rc::new(RefCell::new(VTable::new()));
let args = new_ast(Ctr::String("test".to_string()), Ctr::None);
if let Some(s) = func_declare(ft.clone(),
Rc::new(RefCell::new(test_external_func))) {
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(test_external_func)))
{
print!("Error declaring external func: {}", s);
assert!(false);
}
@ -146,16 +141,14 @@ mod func_tests {
}
match func_call(func, args, vt, ft) {
Ok(ret) => {
match ret {
Ctr::String(s) => {
assert!(s == "test");
},
_ => {
print!("Invalid return from function {:#?}. Should have recieved single string", ret);
assert!(false);
return
}
Ok(ret) => match ret {
Ctr::String(s) => {
assert!(s == "test");
}
_ => {
print!("Invalid return from function {:#?}. Should have recieved single string", ret);
assert!(false);
return;
}
},
Err(e) => {
@ -169,7 +162,7 @@ mod func_tests {
#[test]
fn decl_and_call_func_with_nested_call() {
let inner_func: Function = Function{
let inner_func: Function = Function {
name: String::from("test_inner"),
loose_syms: false,
eval_lazy: false,
@ -186,37 +179,33 @@ mod func_tests {
} else {
Ctr::None
}
}
))
},
)),
};
match lex("((test_inner true))".to_string()) {
Err(e) => panic!("{}", e),
Ok(finner) => {
let outer_func: Function = Function{
let outer_func: Function = Function {
name: String::from("test_outer"),
loose_syms: false,
eval_lazy: false,
args: Args::Lazy(1),
function: Operation::External(
ExternalOperation{
arg_syms: vec!["input".to_string()],
ast: finner
}
)
function: Operation::External(ExternalOperation {
arg_syms: vec!["input".to_string()],
ast: finner,
}),
};
let ft = Rc::new(RefCell::new(FTable::new()));
let vt = Rc::new(RefCell::new(VTable::new()));
let args = new_ast(Ctr::Bool(true), Ctr::None);
if let Some(s) = func_declare(ft.clone(),
Rc::new(RefCell::new(inner_func))) {
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(inner_func))) {
print!("Error declaring inner func: {}", s);
assert!(false);
}
if let Some(s) = func_declare(ft.clone(),
Rc::new(RefCell::new(outer_func))) {
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(outer_func))) {
print!("Error declaring outer func: {}", s);
assert!(false);
}
@ -231,13 +220,11 @@ mod func_tests {
}
match func_call(func, args, vt, ft) {
Ok(ret) => {
match ret {
Ctr::String(b) => assert!(b == "test"),
_ => {
print!("Invalid return from func. Got {:?}\n", ret);
assert!(false);
}
Ok(ret) => match ret {
Ctr::String(b) => assert!(b == "test"),
_ => {
print!("Invalid return from func. Got {:?}\n", ret);
assert!(false);
}
},
Err(e) => {