- RC+RefCell pattern used... everywhere - Ast type implemented - unit tests for func_call - more changes, but this commit scope has grown significantly and I cannot list them all
58 lines
1.9 KiB
Rust
58 lines
1.9 KiB
Rust
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};
|
|
|
|
#[test]
|
|
fn decl_and_call_internal_func() {
|
|
let test_internal_func: Function = Function{
|
|
name: String::from("test_func_in"),
|
|
loose_syms: false,
|
|
eval_lazy: true,
|
|
args: Args::Strict(vec![Type::Bool]),
|
|
function: Operation::Internal(
|
|
|a: Ast, _b: Rc<RefCell<VTable>>, _c: Rc<RefCell<FTable>>| -> Ast {
|
|
let inner = a.borrow();
|
|
let mut is_bool = false;
|
|
if let Ctr::Bool(_) = &inner.car {
|
|
is_bool = true;
|
|
}
|
|
|
|
new_ast(Ctr::Bool(is_bool), Ctr::None)
|
|
}
|
|
)
|
|
};
|
|
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))) {
|
|
print!("{}", s);
|
|
assert!(false);
|
|
}
|
|
|
|
let func: Rc<RefCell<Function>>;
|
|
if let Some(f) = ft.borrow().get(&"test_func_in".to_string()) {
|
|
func = f.clone();
|
|
} else {
|
|
print!("failed to retrieve function!");
|
|
assert!(false);
|
|
return;
|
|
}
|
|
|
|
if let Ok(ast) = func_call(func, args, vt, ft) {
|
|
match &ast.borrow().car {
|
|
Ctr::Bool(b) => assert!(b),
|
|
_ => {
|
|
print!("invalid return from func!");
|
|
assert!(false);
|
|
}
|
|
}
|
|
} else {
|
|
print!("call to function failed!");
|
|
assert!(false);
|
|
}
|
|
}
|
|
}
|