Big referencing refactor

- 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
This commit is contained in:
Aidan 2021-03-14 16:14:57 -07:00
parent 76b12a8214
commit 3434a49cc1
No known key found for this signature in database
GPG key ID: 327711E983899316
9 changed files with 446 additions and 391 deletions

58
tests/test_func.rs Normal file
View file

@ -0,0 +1,58 @@
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);
}
}
}

View file

@ -1,12 +1,12 @@
mod lex_tests {
use relish::ast::{lex};
use relish::ast::{lex, ast_to_string};
#[test]
fn test_lex_basic_pair() {
let document: &str = "(hello 'world')";
match lex(document.to_string()) {
Ok(box_cell) => {
assert_eq!(format!("{}", *box_cell), document);
Ok(tree) => {
assert_eq!(ast_to_string(tree), document);
},
Err(s) => {
print!("{}\n", s);
@ -19,8 +19,8 @@ mod lex_tests {
fn test_lex_basic_list() {
let document: &str = "(hello 'world' 1 2 3)";
match lex(document.to_string()) {
Ok(box_cell) => {
assert_eq!(format!("{}", *box_cell), document);
Ok(tree) => {
assert_eq!(ast_to_string(tree), document);
},
Err(s) => {
print!("{}\n", s);
@ -33,8 +33,8 @@ mod lex_tests {
fn test_lex_complex_list() {
let document: &str = "(hello 'world' (1 2 (1 2 3)) 1 2 3)";
match lex(document.to_string()) {
Ok(box_cell) => {
assert_eq!(format!("{}", *box_cell), document);
Ok(tree) => {
assert_eq!(ast_to_string(tree), document);
},
Err(s) => {
print!("{}\n", s);
@ -48,8 +48,8 @@ mod lex_tests {
let document: &str = "(as;dd)";
let output: &str = "Problem lexing document: \"Unparsable token:as;dd\"";
match lex(document.to_string()) {
Ok(box_cell) => {
print!("Bad token yielded: {}\n", *box_cell);
Ok(tree) => {
print!("Bad token yielded: {}\n", ast_to_string(tree));
assert!(false);
},
Err(s) => {
@ -63,8 +63,8 @@ mod lex_tests {
let document: &str = "(one two";
let output: &str = "Problem lexing document: \"Unmatched list delimiter in input\"";
match lex(document.to_string()) {
Ok(box_cell) => {
print!("Bad token yielded: {}\n", *box_cell);
Ok(tree) => {
print!("Bad token yielded: {}\n", ast_to_string(tree));
assert!(false);
},
Err(s) => {
@ -78,8 +78,8 @@ mod lex_tests {
let document: &str = "(one two (three)";
let output: &str = "Problem lexing document: \"Unmatched list delimiter in input\"";
match lex(document.to_string()) {
Ok(box_cell) => {
print!("Bad token yielded: {}\n", *box_cell);
Ok(tree) => {
print!("Bad token yielded: {}\n", ast_to_string(tree));
assert!(false);
},
Err(s) => {
@ -93,8 +93,8 @@ mod lex_tests {
let document: &str = "#!/bin/relish\n(one two)";
let output: &str = "(one two)";
match lex(document.to_string()) {
Ok(box_cell) => {
assert_eq!(format!("{}", *box_cell), output.to_string());
Ok(tree) => {
assert_eq!(ast_to_string(tree), output.to_string());
},
Err(s) => {
print!("{}\n", s);
@ -108,8 +108,8 @@ mod lex_tests {
let document: &str = "#!/bin/relish\n((one two)# another doc comment\n(three four))";
let output: &str = "((one two) (three four))";
match lex(document.to_string()) {
Ok(box_cell) => {
assert_eq!(format!("{}", *box_cell), output.to_string());
Ok(tree) => {
assert_eq!(ast_to_string(tree), output.to_string());
},
Err(s) => {
print!("{}\n", s);
@ -123,8 +123,8 @@ mod lex_tests {
let document: &str = "#!/bin/relish\n((one two)\n# another doc comment\nthree)";
let output: &str = "((one two) three)";
match lex(document.to_string()) {
Ok(box_cell) => {
assert_eq!(format!("{}", *box_cell), output.to_string());
Ok(tree) => {
assert_eq!(ast_to_string(tree), output.to_string());
},
Err(s) => {
print!("{}\n", s);
@ -138,8 +138,8 @@ mod lex_tests {
let document: &str = "(one t(wo)";
let output: &str = "Problem lexing document: \"list started in middle of another token\"";
match lex(document.to_string()) {
Ok(box_cell) => {
print!("Bad token yielded: {}\n", *box_cell);
Ok(tree) => {
print!("Bad token yielded: {}\n", ast_to_string(tree));
assert!(false);
},
Err(s) => {