more complex tests
This commit is contained in:
parent
df5cb47cb4
commit
2c30975571
6 changed files with 179 additions and 320 deletions
148
src/eval.rs
148
src/eval.rs
|
|
@ -129,151 +129,3 @@ pub fn eval(
|
||||||
|
|
||||||
return Ok(Ctr::Seg(ret))
|
return Ok(Ctr::Seg(ret))
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
fn eval_inner(
|
|
||||||
ast: Ast,
|
|
||||||
vars: Rc<RefCell<VTable>>,
|
|
||||||
funcs: Rc<RefCell<FTable>>,
|
|
||||||
sym_loose: bool,
|
|
||||||
first_item: bool
|
|
||||||
) -> Result<Ast, String> {
|
|
||||||
/* LOGIC:
|
|
||||||
* 1. make new ast
|
|
||||||
* 2. call process_ctr on ast.car
|
|
||||||
* 3. set new.car to result
|
|
||||||
* 4. else call process_ctr on cdr
|
|
||||||
* (set first_item to false)
|
|
||||||
* (and set new.cdr to result)
|
|
||||||
* 5. return new ast
|
|
||||||
*/
|
|
||||||
let ret = new_ast(Ctr::None, Ctr::None);
|
|
||||||
let res_car;
|
|
||||||
let res_cdr;
|
|
||||||
|
|
||||||
// Handle Function Case
|
|
||||||
|
|
||||||
match process_ctr(
|
|
||||||
ast.borrow().clone().car,
|
|
||||||
vars.clone(),
|
|
||||||
funcs.clone(),
|
|
||||||
sym_loose,
|
|
||||||
first_item,
|
|
||||||
true,
|
|
||||||
ast.borrow().clone().cdr
|
|
||||||
) {
|
|
||||||
Ok(ctr) => {
|
|
||||||
res_car = ctr;
|
|
||||||
},
|
|
||||||
Err(err) => {
|
|
||||||
return Err(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match process_ctr(
|
|
||||||
ast.borrow().clone().cdr,
|
|
||||||
vars.clone(),
|
|
||||||
funcs.clone(),
|
|
||||||
sym_loose,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
Ctr::None
|
|
||||||
) {
|
|
||||||
Ok(ctr) => {
|
|
||||||
res_cdr = ctr;
|
|
||||||
},
|
|
||||||
Err(err) => {
|
|
||||||
return Err(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ret.borrow_mut().car = res_car;
|
|
||||||
ret.borrow_mut().cdr = res_cdr;
|
|
||||||
|
|
||||||
return Ok(ret)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn process_ctr(
|
|
||||||
ctr: Ctr,
|
|
||||||
vars: Rc<RefCell<VTable>>,
|
|
||||||
funcs: Rc<RefCell<FTable>>,
|
|
||||||
sym_loose: bool,
|
|
||||||
first_item: bool,
|
|
||||||
is_car: bool,
|
|
||||||
rest: Ctr
|
|
||||||
) -> Result<Ctr, String> {
|
|
||||||
/* LOGIC:
|
|
||||||
* 1. if symbol, unwrap (DEEP COPY)
|
|
||||||
* 2. if first_item and symbol, call function?
|
|
||||||
* (else: return var copy)
|
|
||||||
* 3. if list return result of eval()
|
|
||||||
* 4. finally, return a clone (shallow copy) of datum
|
|
||||||
*/
|
|
||||||
|
|
||||||
match ctr.clone() {
|
|
||||||
Ctr::Symbol(token) => {
|
|
||||||
print!("[+] Detected Symbol: {}\n", token);
|
|
||||||
let mut tok = token;
|
|
||||||
if let Some(s) = vars.borrow().get(&tok) {
|
|
||||||
print!("[+] Found symbol value\n");
|
|
||||||
|
|
||||||
// is function, or variable alias?
|
|
||||||
let mut pass = false;
|
|
||||||
if first_item {
|
|
||||||
if let Ctr::Symbol(t) = &*(s.clone()) {
|
|
||||||
if let Some(_f) = funcs.borrow().get(t) {
|
|
||||||
// leave this set for the function call case below
|
|
||||||
tok = t.clone();
|
|
||||||
pass = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !pass {
|
|
||||||
return Ok((*s.clone()).clone())
|
|
||||||
}
|
|
||||||
|
|
||||||
// variable not found case
|
|
||||||
} else if !first_item && !sym_loose {
|
|
||||||
return Err(format!("variable '{}' not found", tok).to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
// function call case
|
|
||||||
if first_item {
|
|
||||||
if let Some(f) = funcs.borrow().get(&tok) {
|
|
||||||
if let Ctr::Seg(args) = rest {
|
|
||||||
print!("[+] Calling function: {}\n", tok);
|
|
||||||
match func_call(f.clone(), args, vars.clone(), funcs.clone()) {
|
|
||||||
Ok(a) => {
|
|
||||||
print!("[+] Function Call Result: {:#?}\n", a);
|
|
||||||
return Ok(a.clone())
|
|
||||||
},
|
|
||||||
Err(s) => return Err(s)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Err("evaluation: args to function not a list".to_string())
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Err(format!("function '{}' not defined", tok).to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
// sym_loose and not function call
|
|
||||||
} else {
|
|
||||||
return Ok(ctr.clone())
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Ctr::Seg(tree) => {
|
|
||||||
// if list is_car then we need to set first_item to true
|
|
||||||
match eval_inner(tree, vars, funcs, sym_loose, is_car) {
|
|
||||||
Ok(ast) => {
|
|
||||||
Ok(Ctr::Seg(ast))
|
|
||||||
},
|
|
||||||
Err(e) => {
|
|
||||||
Err(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => Ok(ctr.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
|
||||||
12
src/func.rs
12
src/func.rs
|
|
@ -77,7 +77,7 @@ pub fn func_call(
|
||||||
vars: Rc<RefCell<VTable>>,
|
vars: Rc<RefCell<VTable>>,
|
||||||
funcs: Rc<RefCell<FTable>>
|
funcs: Rc<RefCell<FTable>>
|
||||||
) -> Result<Ctr, String> {
|
) -> Result<Ctr, String> {
|
||||||
let called_func = function.borrow_mut();
|
let called_func = function.borrow();
|
||||||
let mut n_args: Ast = args.clone();
|
let mut n_args: Ast = args.clone();
|
||||||
if !called_func.eval_lazy {
|
if !called_func.eval_lazy {
|
||||||
match eval(args, vars.clone(), funcs.clone(), called_func.loose_syms) {
|
match eval(args, vars.clone(), funcs.clone(), called_func.loose_syms) {
|
||||||
|
|
@ -172,18 +172,21 @@ pub fn func_call(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut result = Ctr::None;
|
let mut result: Ctr;
|
||||||
let mut iterate = f.ast.clone();
|
let mut iterate = f.ast.clone();
|
||||||
loop {
|
loop {
|
||||||
if let Ctr::Seg(ast) = iterate.borrow().clone().car {
|
if let Ctr::Seg(ast) = iterate.borrow().clone().car {
|
||||||
match eval(ast, vars.clone(), funcs.clone(), called_func.loose_syms) {
|
match eval(ast, vars.clone(), funcs.clone(), called_func.loose_syms) {
|
||||||
Ok(ctr) => {
|
Ok(ctr) => {
|
||||||
if let Ctr::Seg(ast) = ctr {
|
match ctr {
|
||||||
result = ast.borrow().clone().car;
|
Ctr::Seg(ast) => result = ast.borrow().clone().car,
|
||||||
|
_ => result = ctr
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(e) => return Err(e)
|
Err(e) => return Err(e)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
panic!("function body not in standard form!")
|
||||||
}
|
}
|
||||||
|
|
||||||
match iterate.clone().borrow().clone().cdr {
|
match iterate.clone().borrow().clone().cdr {
|
||||||
|
|
@ -192,7 +195,6 @@ pub fn func_call(
|
||||||
_ => panic!("function body not in standard form!")
|
_ => panic!("function body not in standard form!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for n in 0..f.arg_syms.len() {
|
for n in 0..f.arg_syms.len() {
|
||||||
vars.borrow_mut().remove(&f.arg_syms[n].clone());
|
vars.borrow_mut().remove(&f.arg_syms[n].clone());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -195,14 +195,14 @@ fn process(document: String) -> Result<Ast, String> {
|
||||||
|
|
||||||
|
|
||||||
/* Returns true if token
|
/* Returns true if token
|
||||||
* - is all alphanumeric
|
* - is all alphanumeric except dash and underscore
|
||||||
*
|
*
|
||||||
* else returns false
|
* else returns false
|
||||||
*/
|
*/
|
||||||
fn tok_is_symbol(token: &String) -> Option<String> {
|
fn tok_is_symbol(token: &String) -> Option<String> {
|
||||||
let tok = token.as_str();
|
let tok = token.as_str();
|
||||||
for t in tok.chars() {
|
for t in tok.chars() {
|
||||||
if !t.is_alphabetic() && !t.is_digit(10) {
|
if !t.is_alphabetic() && !t.is_digit(10) && !(t == '-') && !(t == '_') {
|
||||||
return None
|
return None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
mod func_tests {
|
mod eval_tests {
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use relish::ast::{Ctr, Function, Operation, ExternalOperation};
|
use relish::ast::{Ctr, Function, Operation, ExternalOperation};
|
||||||
|
|
@ -117,12 +117,30 @@ mod func_tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
#[test]
|
#[test]
|
||||||
fn eval_embedded_func_calls() {
|
fn eval_embedded_func_calls() {
|
||||||
let test_doc = "1".to_string();
|
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 ft = Rc::new(RefCell::new(FTable::new()));
|
||||||
let vt = Rc::new(RefCell::new(VTable::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) {
|
match lex(test_doc) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
@ -137,14 +155,22 @@ mod func_tests {
|
||||||
assert!(false)
|
assert!(false)
|
||||||
},
|
},
|
||||||
|
|
||||||
Ok(reduced_ast) => {
|
Ok(reduced) => {
|
||||||
// write tests here
|
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]
|
#[test]
|
||||||
fn eval_bad_vars() {
|
fn eval_bad_vars() {
|
||||||
let test_doc = "1".to_string();
|
let test_doc = "1".to_string();
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ mod func_tests {
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use relish::ast::{Ast, Type, Ctr, new_ast};
|
use relish::ast::{Ast, Type, Ctr, new_ast};
|
||||||
use relish::ast::VTable;
|
use relish::ast::VTable;
|
||||||
use relish::ast::{Function, Operation, FTable, Args, func_declare, func_call, ExternalOperation};
|
use relish::ast::{Function, Operation, FTable, Args, func_declare, func_call, ExternalOperation, lex};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn decl_and_call_internal_func() {
|
fn decl_and_call_internal_func() {
|
||||||
|
|
@ -58,6 +58,9 @@ mod func_tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn decl_and_call_external_func_singlet() {
|
fn decl_and_call_external_func_singlet() {
|
||||||
|
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"),
|
name: String::from("echo"),
|
||||||
loose_syms: false,
|
loose_syms: false,
|
||||||
|
|
@ -66,7 +69,7 @@ mod func_tests {
|
||||||
function: Operation::External(
|
function: Operation::External(
|
||||||
ExternalOperation{
|
ExternalOperation{
|
||||||
arg_syms: vec!["input".to_string()],
|
arg_syms: vec!["input".to_string()],
|
||||||
ast: new_ast(Ctr::Symbol("input".to_string()), Ctr::None)
|
ast: finner
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
@ -104,20 +107,23 @@ mod func_tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn decl_and_call_external_func_dual_ret() {
|
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{
|
let test_external_func: Function = Function{
|
||||||
name: String::from("echox2"),
|
name: String::from("echo_2"),
|
||||||
loose_syms: false,
|
loose_syms: false,
|
||||||
eval_lazy: false,
|
eval_lazy: false,
|
||||||
args: Args::Lazy(1),
|
args: Args::Lazy(1),
|
||||||
function: Operation::External(
|
function: Operation::External(
|
||||||
ExternalOperation{
|
ExternalOperation{
|
||||||
arg_syms: vec!["input".to_string()],
|
arg_syms: vec!["input".to_string()],
|
||||||
ast: new_ast(Ctr::Seg(new_ast(Ctr::Symbol("input".to_string()),
|
ast: finner
|
||||||
Ctr::Seg(new_ast(Ctr::Symbol("input".to_string()), Ctr::None)))),
|
|
||||||
Ctr::None)
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
@ -131,7 +137,7 @@ mod func_tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
let func: Rc<RefCell<Function>>;
|
let func: Rc<RefCell<Function>>;
|
||||||
if let Some(f) = ft.borrow().get(&"echox2".to_string()) {
|
if let Some(f) = ft.borrow().get(&"echo_2".to_string()) {
|
||||||
func = f.clone();
|
func = f.clone();
|
||||||
} else {
|
} else {
|
||||||
print!("failed to retrieve function!");
|
print!("failed to retrieve function!");
|
||||||
|
|
@ -142,41 +148,13 @@ mod func_tests {
|
||||||
match func_call(func, args, vt, ft) {
|
match func_call(func, args, vt, ft) {
|
||||||
Ok(ret) => {
|
Ok(ret) => {
|
||||||
match ret {
|
match ret {
|
||||||
Ctr::Seg(b) => {
|
Ctr::String(s) => {
|
||||||
match b.borrow().clone().car {
|
assert!(s == "test");
|
||||||
Ctr::String(s) => assert!(s == "test"),
|
|
||||||
_ => {
|
|
||||||
print!("first elem wrong. expected \"test\", got: {:?}\n", b.borrow().car);
|
|
||||||
assert!(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match b.borrow().clone().cdr {
|
|
||||||
Ctr::Seg(b2) => {
|
|
||||||
match b2.borrow().clone().car {
|
|
||||||
Ctr::String(s) => assert!(s == "test"),
|
|
||||||
_ => {
|
|
||||||
print!("second elem wrong. expected \"test\", got: {:?}\n", b2.borrow().car);
|
|
||||||
assert!(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match b2.borrow().cdr {
|
|
||||||
Ctr::None => (),
|
|
||||||
_ => {
|
|
||||||
print!("there should not be a third element\n");
|
|
||||||
assert!(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
print!("first elem cdr should be a list. got: {:?}\n", b.borrow().cdr)
|
print!("Invalid return from function {:#?}. Should have recieved single string", ret);
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
print!("Invalid return from func. Got {:?}\n", ret);
|
|
||||||
assert!(false);
|
assert!(false);
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -186,6 +164,8 @@ mod func_tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn decl_and_call_func_with_nested_call() {
|
fn decl_and_call_func_with_nested_call() {
|
||||||
|
|
@ -210,6 +190,9 @@ mod func_tests {
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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"),
|
name: String::from("test_outer"),
|
||||||
loose_syms: false,
|
loose_syms: false,
|
||||||
|
|
@ -218,9 +201,7 @@ mod func_tests {
|
||||||
function: Operation::External(
|
function: Operation::External(
|
||||||
ExternalOperation{
|
ExternalOperation{
|
||||||
arg_syms: vec!["input".to_string()],
|
arg_syms: vec!["input".to_string()],
|
||||||
ast: new_ast(Ctr::Symbol("test_inner".to_string()),
|
ast: finner
|
||||||
Ctr::Seg(new_ast(Ctr::Symbol("input".to_string()),
|
|
||||||
Ctr::None)))
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
@ -265,6 +246,8 @@ mod func_tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* This test removed because it would make more sense to test this functionality in eval tests
|
/* 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
|
* this function tested the evaluation of a complex argument ast that could be reduced in eval
|
||||||
|
|
@ -350,9 +333,10 @@ mod func_tests {
|
||||||
assert!(false);
|
assert!(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
*/
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
// TODO: These tests need completion!
|
||||||
#[test]
|
#[test]
|
||||||
fn eval_lazy_func_call() {
|
fn eval_lazy_func_call() {
|
||||||
|
|
||||||
|
|
@ -381,10 +365,5 @@ mod func_tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn bad_eval_fn_body() {
|
fn bad_eval_fn_body() {
|
||||||
|
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* TESTING TODO:
|
|
||||||
* - Test functions that eval_lazy
|
|
||||||
* - Test functions that sym_loose
|
|
||||||
*/
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue