WIP commit:

* Fix up project structures
* combine vars and funcs table
* make a place for old code that may be useful to reference
* singleton pattern for sym table

Commentary:
When this change is finally finished I promise to use feature branches
from here on out
This commit is contained in:
Ava Hahn 2023-02-15 23:27:00 -08:00
parent b680e3ca9a
commit ca4c557d95
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
32 changed files with 1092 additions and 616 deletions

View file

@ -0,0 +1,245 @@
mod eval_tests {
use relish::ast::{ast_to_string, eval, lex, new_ast, FTable, VTable};
use relish::ast::{func_declare, Args};
use relish::ast::{Ctr, ExternalOperation, Function, Operation};
use std::cell::RefCell;
use std::rc::Rc;
// TODO: write generalized testing routine on top of list of inputs
#[test]
fn eval_singlet() {
let test_doc = "(1)".to_string();
let ft = Rc::new(RefCell::new(FTable::new()));
let vt = Rc::new(RefCell::new(VTable::new()));
match lex(test_doc.clone()) {
Err(e) => {
println!("Lexing error: {}\n", e);
assert!(false)
}
Ok(initial_ast) => match eval(initial_ast.clone(), vt.clone(), ft.clone(), false) {
Err(e) => {
println!("Evaluation error: {}\n", e);
assert!(false)
}
Ok(reduced) => {
if let Ctr::Seg(reduced_ast) = reduced {
assert_eq!(ast_to_string(reduced_ast), test_doc)
}
}
},
}
}
#[test]
fn eval_embedded_lists_no_funcs() {
let test_doc = "(1 (1 2 3 4 5) 5)".to_string();
let ft = Rc::new(RefCell::new(FTable::new()));
let vt = Rc::new(RefCell::new(VTable::new()));
match lex(test_doc.clone()) {
Err(e) => {
println!("Lexing error: {}\n", e);
assert!(false)
}
Ok(initial_ast) => match eval(initial_ast.clone(), vt.clone(), ft.clone(), false) {
Err(e) => {
println!("Evaluation error: {}\n", e);
assert!(false)
}
Ok(reduced) => {
if let Ctr::Seg(reduced_ast) = reduced {
assert_eq!(ast_to_string(reduced_ast), test_doc)
}
}
},
}
}
#[test]
fn eval_function_call() {
let test_doc = "('one' (echo 'unwrap_me'))".to_string();
let output = "('one' 'unwrap_me')";
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: new_ast(
Ctr::Seg(new_ast(Ctr::Symbol("input".to_string()), Ctr::None)),
Ctr::None,
),
}),
};
let ft = Rc::new(RefCell::new(FTable::new()));
let vt = Rc::new(RefCell::new(VTable::new()));
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(test_external_func))) {
print!("Error declaring external func: {}", s);
assert!(false);
}
match lex(test_doc) {
Err(e) => {
println!("Lexing error: {}\n", e);
assert!(false)
}
Ok(initial_ast) => match eval(initial_ast.clone(), vt.clone(), ft.clone(), false) {
Err(e) => {
println!("Evaluation error: {}\n", e);
assert!(false)
}
Ok(reduced) => {
if let Ctr::Seg(reduced_ast) = reduced {
let out_doc = ast_to_string(reduced_ast);
if out_doc != output {
print!("Erroneous output: {}\n", out_doc);
assert!(false)
}
}
}
},
}
}
#[test]
fn eval_embedded_func_calls() {
let test_doc = "('one' (echo (echo 'unwrap_me')))".to_string();
let output = "('one' 'unwrap_me')";
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: new_ast(
Ctr::Seg(new_ast(Ctr::Symbol("input".to_string()), Ctr::None)),
Ctr::None,
),
}),
};
let ft = Rc::new(RefCell::new(FTable::new()));
let vt = Rc::new(RefCell::new(VTable::new()));
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(test_external_func))) {
print!("Error declaring external func: {}", s);
assert!(false);
}
match lex(test_doc) {
Err(e) => {
println!("Lexing error: {}\n", e);
assert!(false)
}
Ok(initial_ast) => match eval(initial_ast.clone(), vt.clone(), ft.clone(), false) {
Err(e) => {
println!("Evaluation error: {}\n", e);
assert!(false)
}
Ok(reduced) => {
if let Ctr::Seg(reduced_ast) = reduced {
let out_doc = ast_to_string(reduced_ast);
if out_doc != output {
print!("Erroneous output: {}\n", out_doc);
assert!(false)
}
}
}
},
}
}
/*
#[test]
fn eval_bad_vars() {
let test_doc = "".to_string();
let ft = Rc::new(RefCell::new(FTable::new()));
let vt = Rc::new(RefCell::new(VTable::new()));
match lex(test_doc) {
Err(e) => {
println!("Lexing error: {}\n", e);
assert!(false)
},
Ok(initial_ast) => {
match eval(initial_ast.clone(), vt.clone(), ft.clone(), false) {
Err(e) => {
println!("Evaluation error: {}\n", e);
assert!(false)
},
Ok(reduced_ast) => {
// write tests here
}
}
}
}
}
#[test]
fn eval_bad_func() {
let test_doc = "".to_string();
let ft = Rc::new(RefCell::new(FTable::new()));
let vt = Rc::new(RefCell::new(VTable::new()));
match lex(test_doc) {
Err(e) => {
println!("Lexing error: {}\n", e);
assert!(false)
},
Ok(initial_ast) => {
match eval(initial_ast.clone(), vt.clone(), ft.clone(), false) {
Err(e) => {
println!("Evaluation error: {}\n", e);
assert!(false)
},
Ok(reduced_ast) => {
// write tests here
}
}
}
}
}
#[test]
fn eval_verify_all_elems_cloned() {
let test_doc = "".to_string();
let ft = Rc::new(RefCell::new(FTable::new()));
let vt = Rc::new(RefCell::new(VTable::new()));
match lex(test_doc) {
Err(e) => {
println!("Lexing error: {}\n", e);
assert!(false)
},
Ok(initial_ast) => {
match eval(initial_ast.clone(), vt.clone(), ft.clone(), false) {
Err(e) => {
println!("Evaluation error: {}\n", e);
assert!(false)
},
Ok(reduced_ast) => {
// write tests here
}
}
}
}
}*/
}

View file

@ -0,0 +1,356 @@
mod func_tests {
use relish::ast::VTable;
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"),
loose_syms: false,
eval_lazy: false,
args: Args::Strict(vec![Type::Bool]),
function: Operation::Internal(Box::new(
|a: Ast, _b: Rc<RefCell<VTable>>, _c: Rc<RefCell<FTable>>| -> Ctr {
let inner = a.borrow();
let mut is_bool = false;
if let Ctr::Bool(_) = &inner.car {
is_bool = true;
}
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))) {
print!("Error declaring internal func: {}", 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(ret) = func_call(func, args, vt, ft) {
match ret {
Ctr::Bool(b) => assert!(b),
_ => {
print!("invalid return from func!");
assert!(false);
}
}
} else {
print!("call to function failed!");
assert!(false);
}
}
#[test]
fn decl_and_call_external_func_singlet() {
match lex("((input))".to_string()) {
Err(e) => panic!("{}", e),
Ok(finner) => {
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,
}),
};
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)))
{
print!("Error declaring external func: {}", s);
assert!(false);
}
let func: Rc<RefCell<Function>>;
if let Some(f) = ft.borrow().get(&"echo".to_string()) {
func = f.clone();
} else {
print!("failed to retrieve function!");
assert!(false);
return;
}
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);
}
},
Err(e) => {
print!("Call to function failed: {}\n", e);
assert!(false);
}
}
}
}
}
#[test]
fn decl_and_call_external_func_multi_body() {
match lex("((input) (input))".to_string()) {
Err(e) => panic!("{}", e),
Ok(finner) => {
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,
}),
};
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)))
{
print!("Error declaring external func: {}", s);
assert!(false);
}
let func: Rc<RefCell<Function>>;
if let Some(f) = ft.borrow().get(&"echo_2".to_string()) {
func = f.clone();
} else {
print!("failed to retrieve function!");
assert!(false);
return;
}
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;
}
},
Err(e) => {
print!("Call to function failed: {}\n", e);
assert!(false);
}
}
}
}
}
#[test]
fn decl_and_call_func_with_nested_call() {
let inner_func: Function = Function {
name: String::from("test_inner"),
loose_syms: false,
eval_lazy: false,
args: Args::Strict(vec![Type::Bool]),
function: Operation::Internal(Box::new(
|a: Ast, _b: Rc<RefCell<VTable>>, _c: Rc<RefCell<FTable>>| -> Ctr {
let inner = a.borrow();
if let Ctr::Bool(b) = &inner.car {
if *b {
Ctr::String("test".to_string())
} else {
Ctr::None
}
} else {
Ctr::None
}
},
)),
};
match lex("((test_inner true))".to_string()) {
Err(e) => panic!("{}", e),
Ok(finner) => {
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,
}),
};
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))) {
print!("Error declaring inner func: {}", s);
assert!(false);
}
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(outer_func))) {
print!("Error declaring outer func: {}", s);
assert!(false);
}
let func: Rc<RefCell<Function>>;
if let Some(f) = ft.borrow().get(&"test_outer".to_string()) {
func = f.clone();
} else {
print!("failed to retrieve function!");
assert!(false);
return;
}
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);
}
},
Err(e) => {
print!("Call to function failed: {}\n", e);
assert!(false);
}
}
}
}
}
/* This test removed because it would make more sense to test this functionality in eval tests
* this function tested the evaluation of a complex argument ast that could be reduced in eval
#[test]
fn decl_and_call_func_eval_arg() {
let is_true_func: Function = Function{
name: String::from("is_true?"),
loose_syms: false,
eval_lazy: false,
args: Args::Strict(vec![Type::Bool]),
function: Operation::Internal(
|a: Ast, _b: Rc<RefCell<VTable>>, _c: Rc<RefCell<FTable>>| -> Ctr {
let inner = a.borrow();
if let Ctr::Bool(b) = &inner.car {
if *b {
Ctr::String("test".to_string())
} else {
Ctr::None
}
} else {
Ctr::None
}
}
)
};
let echo_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: new_ast(Ctr::Symbol("input".to_string()), Ctr::None)
}
)
};
let ft = Rc::new(RefCell::new(FTable::new()));
let vt = Rc::new(RefCell::new(VTable::new()));
let args = new_ast(
Ctr::Seg(new_ast(
Ctr::Symbol("is_true?".to_string()),
Ctr::Seg(new_ast(
Ctr::Bool(true),
Ctr::None)))),
Ctr::None);
if let Some(s) = func_declare(ft.clone(),
Rc::new(RefCell::new(is_true_func))) {
print!("Error declaring inner func: {}", s);
assert!(false);
}
if let Some(s) = func_declare(ft.clone(),
Rc::new(RefCell::new(echo_func))) {
print!("Error declaring outer func: {}", s);
assert!(false);
}
let func: Rc<RefCell<Function>>;
if let Some(f) = ft.borrow().get(&"echo".to_string()) {
func = f.clone();
} else {
print!("failed to retrieve function!");
assert!(false);
return;
}
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);
}
}
},
Err(e) => {
print!("Call to function failed: {}\n", e);
assert!(false);
}
}
}*/
/*
// TODO: These tests need completion!
#[test]
fn eval_lazy_func_call() {
}
#[test]
fn sym_loose_func_call() {
}
#[test]
fn too_many_args() {
}
#[test]
fn not_enough_args() {
}
#[test]
fn bad_eval_arg() {
}
#[test]
fn bad_eval_fn_body() {
}*/
}

View file

@ -0,0 +1,178 @@
mod lex_tests {
use relish::ast::lex;
#[test]
fn test_lex_basic_pair() {
let document: &str = "(hello 'world')";
match lex(document) {
Ok(tree) => {
assert_eq!(tree, document);
}
Err(s) => {
print!("{}\n", s);
assert!(false);
}
}
}
#[test]
fn test_lex_basic_list() {
let document: &str = "(hello 'world' 1 2 3)";
match lex(document) {
Ok(tree) => {
assert_eq!(tree, document);
}
Err(s) => {
print!("{}\n", s);
assert!(false);
}
}
}
#[test]
fn test_lex_complex_list() {
let document: &str = "(hello 'world' (1 2 (1 2 3)) 1 2 3)";
match lex(document) {
Ok(tree) => {
assert_eq!(tree, document);
}
Err(s) => {
print!("{}\n", s);
assert!(false);
}
}
}
#[test]
fn test_bad_symbol() {
let document: &str = "(as;dd)";
let output: &str = "Problem lexing document: \"Unparsable token: as;dd\"";
match lex(document) {
Ok(tree) => {
print!("Bad token yielded: {}\n", tree);
assert!(false);
}
Err(s) => {
assert_eq!(s, output);
}
}
}
#[test]
fn test_list_delim_in_str() {
let document: &str = "('(')";
match lex(document) {
Ok(tree) => {
assert_eq!(tree, document);
}
Err(s) => {
print!("{}\n", s);
assert!(false);
}
}
}
#[test]
fn test_empty_string() {
let document: &str = "('')";
match lex(document) {
Ok(tree) => {
assert_eq!(tree, document);
}
Err(s) => {
print!("{}\n", s);
assert!(false);
}
}
}
#[test]
fn test_unmatched_list_delim_flat() {
let document: &str = "(one two";
let output: &str = "Problem lexing document: \"Unmatched list delimiter in input\"";
match lex(document) {
Ok(tree) => {
print!("Bad token yielded: {}\n", tree);
assert!(false);
}
Err(s) => {
assert_eq!(s, output);
}
}
}
#[test]
fn test_unmatched_list_delim_complex() {
let document: &str = "(one two (three)";
let output: &str = "Problem lexing document: \"Unmatched list delimiter in input\"";
match lex(document) {
Ok(tree) => {
print!("Bad token yielded: {}\n", tree);
assert!(false);
}
Err(s) => {
assert_eq!(s, output);
}
}
}
#[test]
fn test_comment() {
let document: &str = "#!/bin/relish\n(one two)";
let output: &str = "(one two)";
match lex(document) {
Ok(tree) => {
assert_eq!(tree, output);
}
Err(s) => {
print!("{}\n", s);
assert!(false);
}
}
}
#[test]
fn test_postline_comment() {
let document: &str = "#!/bin/relish\n((one two)# another doc comment\n(three four))";
let output: &str = "((one two) (three four))";
match lex(document) {
Ok(tree) => {
assert_eq!(tree, output.to_string());
}
Err(s) => {
print!("{}\n", s);
assert!(false);
}
}
}
#[test]
fn test_inline_comment() {
let document: &str = "#!/bin/relish\n((one two)\n# another doc comment\nthree)";
let output: &str = "((one two) three)";
match lex(document) {
Ok(tree) => {
assert_eq!(tree, output);
}
Err(s) => {
print!("{}\n", s);
assert!(false);
}
}
}
#[test]
fn test_bad_token_list() {
let document: &str = "(one t(wo)";
let output: &str = "Problem lexing document: \"list started in middle of another token\"";
match lex(document) {
Ok(tree) => {
print!("Bad token yielded: {}\n", tree);
assert!(false);
}
Err(s) => {
assert_eq!(s, output);
}
}
}
}

View file

@ -0,0 +1,166 @@
mod append_lib_tests {
use relish::ast::{ast_to_string, eval, lex, Ctr, FTable, VTable};
use relish::stdlib::get_stdlib;
use std::cell::RefCell;
use std::rc::Rc;
#[test]
fn test_append_to_empty_list() {
let document = "(append () 1 2 3)";
let result = "(1 2 3)";
let vt = Rc::new(RefCell::new(VTable::new()));
let ft: Rc<RefCell<FTable>>;
match get_stdlib(vt.clone()) {
Ok(f) => ft = f,
Err(s) => {
ft = Rc::new(RefCell::new(FTable::new()));
println!("Couldnt get stdlib: {}!", s);
assert!(false)
}
}
match lex(document.to_string()) {
Err(s) => {
println!("Couldnt lex {}: {}\n", document, s);
assert!(false);
}
Ok(tree) => match eval(tree, vt.clone(), ft.clone(), false) {
Err(s) => {
println!("Couldnt eval {}: {}\n", document, s);
assert!(false);
}
Ok(ctr) => match ctr {
Ctr::Symbol(_) => assert!(false),
Ctr::String(_) => assert!(false),
Ctr::Integer(_) => assert!(false),
Ctr::Float(_) => assert!(false),
Ctr::Bool(_) => assert!(false),
Ctr::Seg(s) => assert_eq!(ast_to_string(s), result),
Ctr::None => assert!(false),
},
},
}
}
#[test]
fn test_append_to_full_list() {
let document = "(append (1 2) 3)";
let result = "(1 2 3)";
let vt = Rc::new(RefCell::new(VTable::new()));
let ft: Rc<RefCell<FTable>>;
match get_stdlib(vt.clone()) {
Ok(f) => ft = f,
Err(s) => {
ft = Rc::new(RefCell::new(FTable::new()));
println!("Couldnt get stdlib: {}!", s);
assert!(false)
}
}
match lex(document.to_string()) {
Err(s) => {
println!("Couldnt lex {}: {}\n", document, s);
assert!(false);
}
Ok(tree) => match eval(tree, vt.clone(), ft.clone(), false) {
Err(s) => {
println!("Couldnt eval {}: {}\n", document, s);
assert!(false);
}
Ok(ctr) => match ctr {
Ctr::Symbol(_) => assert!(false),
Ctr::String(_) => assert!(false),
Ctr::Integer(_) => assert!(false),
Ctr::Float(_) => assert!(false),
Ctr::Bool(_) => assert!(false),
Ctr::Seg(s) => assert_eq!(ast_to_string(s), result),
Ctr::None => assert!(false),
},
},
}
}
#[test]
fn test_mono_append() {
let document = "(append)";
let result = "()";
let vt = Rc::new(RefCell::new(VTable::new()));
let ft: Rc<RefCell<FTable>>;
match get_stdlib(vt.clone()) {
Ok(f) => ft = f,
Err(s) => {
ft = Rc::new(RefCell::new(FTable::new()));
println!("Couldnt get stdlib: {}!", s);
assert!(false)
}
}
match lex(document.to_string()) {
Err(s) => {
println!("Couldnt lex {}: {}\n", document, s);
assert!(false);
}
Ok(tree) => match eval(tree, vt.clone(), ft.clone(), false) {
Err(s) => {
println!("Couldnt eval {}: {}\n", document, s);
assert!(false);
}
Ok(ctr) => match ctr {
Ctr::Symbol(_) => assert!(false),
Ctr::String(_) => assert!(false),
Ctr::Integer(_) => assert!(false),
Ctr::Float(_) => assert!(false),
Ctr::Bool(_) => assert!(false),
Ctr::Seg(s) => assert_eq!(ast_to_string(s), result),
Ctr::None => assert!(false),
},
},
}
}
#[test]
fn test_append_no_list() {
let document = "(append 'test' 1 2 3)";
let result = "('test' 1 2 3)";
let vt = Rc::new(RefCell::new(VTable::new()));
let ft: Rc<RefCell<FTable>>;
match get_stdlib(vt.clone()) {
Ok(f) => ft = f,
Err(s) => {
ft = Rc::new(RefCell::new(FTable::new()));
println!("Couldnt get stdlib: {}!", s);
assert!(false)
}
}
match lex(document.to_string()) {
Err(s) => {
println!("Couldnt lex {}: {}\n", document, s);
assert!(false);
}
Ok(tree) => match eval(tree, vt.clone(), ft.clone(), false) {
Err(s) => {
println!("Couldnt eval {}: {}\n", document, s);
assert!(false);
}
Ok(ctr) => match ctr {
Ctr::Symbol(_) => assert!(false),
Ctr::String(_) => assert!(false),
Ctr::Integer(_) => assert!(false),
Ctr::Float(_) => assert!(false),
Ctr::Bool(_) => assert!(false),
Ctr::Seg(s) => assert_eq!(ast_to_string(s), result),
Ctr::None => assert!(false),
},
},
}
}
}

View file

@ -0,0 +1,126 @@
mod str_lib_tests {
use relish::ast::{eval, lex, Ctr, FTable, VTable};
use relish::stdlib::get_stdlib;
use std::cell::RefCell;
use std::rc::Rc;
#[test]
fn test_simple_concat() {
let document = "(concat 'test')";
let result = "test";
let vt = Rc::new(RefCell::new(VTable::new()));
let ft: Rc<RefCell<FTable>>;
match get_stdlib(vt.clone()) {
Ok(f) => ft = f,
Err(s) => {
ft = Rc::new(RefCell::new(FTable::new()));
println!("Couldnt get stdlib: {}!", s);
assert!(false)
}
}
match lex(document.to_string()) {
Err(s) => {
println!("Couldnt lex {}: {}\n", document, s);
assert!(false);
}
Ok(tree) => match eval(tree, vt.clone(), ft.clone(), false) {
Err(s) => {
println!("Couldnt eval {}: {}\n", document, s);
assert!(false);
}
Ok(ctr) => match ctr {
Ctr::Symbol(_) => assert!(false),
Ctr::String(s) => assert_eq!(s, result),
Ctr::Integer(_) => assert!(false),
Ctr::Float(_) => assert!(false),
Ctr::Bool(_) => assert!(false),
Ctr::Seg(_) => assert!(false),
Ctr::None => assert!(false),
},
},
}
}
#[test]
fn test_poly_concat() {
let document = "(concat 'test' 1 2 3)";
let result = "test123";
let vt = Rc::new(RefCell::new(VTable::new()));
let ft: Rc<RefCell<FTable>>;
match get_stdlib(vt.clone()) {
Ok(f) => ft = f,
Err(s) => {
ft = Rc::new(RefCell::new(FTable::new()));
println!("Couldnt get stdlib: {}!", s);
assert!(false)
}
}
match lex(document.to_string()) {
Err(s) => {
println!("Couldnt lex {}: {}\n", document, s);
assert!(false);
}
Ok(tree) => match eval(tree, vt.clone(), ft.clone(), false) {
Err(s) => {
println!("Couldnt eval {}: {}\n", document, s);
assert!(false);
}
Ok(ctr) => match ctr {
Ctr::Symbol(_) => assert!(false),
Ctr::String(s) => assert_eq!(s, result),
Ctr::Integer(_) => assert!(false),
Ctr::Float(_) => assert!(false),
Ctr::Bool(_) => assert!(false),
Ctr::Seg(_) => assert!(false),
Ctr::None => assert!(false),
},
},
}
}
#[test]
fn test_empty_concat() {
let document = "(concat)";
let result = "";
let vt = Rc::new(RefCell::new(VTable::new()));
let ft: Rc<RefCell<FTable>>;
match get_stdlib(vt.clone()) {
Ok(f) => ft = f,
Err(s) => {
ft = Rc::new(RefCell::new(FTable::new()));
println!("Couldnt get stdlib: {}!", s);
assert!(false)
}
}
match lex(document.to_string()) {
Err(s) => {
println!("Couldnt lex {}: {}\n", document, s);
assert!(false);
}
Ok(tree) => match eval(tree, vt.clone(), ft.clone(), false) {
Err(s) => {
println!("Couldnt eval {}: {}\n", document, s);
assert!(false);
}
Ok(ctr) => match ctr {
Ctr::Symbol(_) => assert!(false),
Ctr::String(s) => assert_eq!(s, result),
Ctr::Integer(_) => assert!(false),
Ctr::Float(_) => assert!(false),
Ctr::Bool(_) => assert!(false),
Ctr::Seg(_) => assert!(false),
Ctr::None => assert!(false),
},
},
}
}
}

View file

@ -0,0 +1,64 @@
mod var_lib_tests {
use relish::ast::{eval, lex, Ctr, FTable, VTable};
use relish::stdlib::get_stdlib;
use std::cell::RefCell;
use std::rc::Rc;
#[test]
fn test_variable_export_and_lookup() {
let doc1 = "(export test 1)";
let doc2 = "(concat test)";
let result = "1";
let vt = Rc::new(RefCell::new(VTable::new()));
let ft: Rc<RefCell<FTable>>;
match get_stdlib(vt.clone()) {
Ok(f) => ft = f,
Err(s) => {
ft = Rc::new(RefCell::new(FTable::new()));
println!("Couldnt get stdlib: {}!", s);
assert!(false);
}
}
match lex(doc1.to_string()) {
Err(s) => {
println!("Couldnt lex {}: {}", doc1, s);
assert!(false);
}
Ok(tree) => match eval(tree, vt.clone(), ft.clone(), false) {
Err(s) => {
println!("Couldnt eval {}: {}", doc2, s);
assert!(false);
}
Ok(ctr) => {
println!("{:#?}", vt);
match ctr {
Ctr::None => assert!(true),
_ => assert!(false),
}
}
},
}
match lex(doc2.to_string()) {
Err(s) => {
println!("Couldnt lex {}: {}", doc2, s);
assert!(false);
}
Ok(tree) => match eval(tree, vt.clone(), ft.clone(), false) {
Err(s) => {
println!("Couldnt eval {}: {}", doc2, s);
assert!(false);
}
Ok(ctr) => match ctr {
Ctr::String(s) => assert_eq!(s, result),
_ => assert!(false),
},
},
}
}
}