diff --git a/src/eval.rs b/src/eval.rs index 9d4594b..0686b62 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -129,151 +129,3 @@ pub fn eval( return Ok(Ctr::Seg(ret)) } - -/* -fn eval_inner( - ast: Ast, - vars: Rc>, - funcs: Rc>, - sym_loose: bool, - first_item: bool -) -> Result { - /* 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>, - funcs: Rc>, - sym_loose: bool, - first_item: bool, - is_car: bool, - rest: Ctr -) -> Result { - /* 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()) - } -} -*/ diff --git a/src/func.rs b/src/func.rs index 94d522a..b570757 100644 --- a/src/func.rs +++ b/src/func.rs @@ -77,7 +77,7 @@ pub fn func_call( vars: Rc>, funcs: Rc> ) -> Result { - let called_func = function.borrow_mut(); + let called_func = function.borrow(); let mut n_args: Ast = args.clone(); if !called_func.eval_lazy { 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(); loop { if let Ctr::Seg(ast) = iterate.borrow().clone().car { match eval(ast, vars.clone(), funcs.clone(), called_func.loose_syms) { Ok(ctr) => { - if let Ctr::Seg(ast) = ctr { - result = ast.borrow().clone().car; + match ctr { + Ctr::Seg(ast) => result = ast.borrow().clone().car, + _ => result = ctr } }, Err(e) => return Err(e) } + } else { + panic!("function body not in standard form!") } match iterate.clone().borrow().clone().cdr { @@ -192,7 +195,6 @@ pub fn func_call( _ => panic!("function body not in standard form!") } } - for n in 0..f.arg_syms.len() { vars.borrow_mut().remove(&f.arg_syms[n].clone()); } diff --git a/src/lex.rs b/src/lex.rs index 60ebdad..5999232 100644 --- a/src/lex.rs +++ b/src/lex.rs @@ -164,7 +164,7 @@ fn process(document: String) -> Result { } else if let Some(s) = tok_is_symbol(&token) { obj = Ctr::Symbol(s); } else { - return Err(format!("Unparsable token:{}", token)); + return Err(format!("Unparsable token: {}", token)); } token = String::new(); @@ -195,14 +195,14 @@ fn process(document: String) -> Result { /* Returns true if token - * - is all alphanumeric + * - is all alphanumeric except dash and underscore * * else returns false */ fn tok_is_symbol(token: &String) -> Option { let tok = token.as_str(); 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 } } diff --git a/tests/test_eval.rs b/tests/test_eval.rs index 93033ec..75786ed 100644 --- a/tests/test_eval.rs +++ b/tests/test_eval.rs @@ -1,4 +1,4 @@ -mod func_tests { +mod eval_tests { use std::rc::Rc; use std::cell::RefCell; use relish::ast::{Ctr, Function, Operation, ExternalOperation}; @@ -117,12 +117,30 @@ mod func_tests { } } - /* #[test] 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 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) => { @@ -137,14 +155,22 @@ mod func_tests { assert!(false) }, - Ok(reduced_ast) => { - // write tests here + 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 = "1".to_string(); diff --git a/tests/test_func.rs b/tests/test_func.rs index 611ebac..688fbc8 100644 --- a/tests/test_func.rs +++ b/tests/test_func.rs @@ -3,7 +3,7 @@ mod func_tests { 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}; + use relish::ast::{Function, Operation, FTable, Args, func_declare, func_call, ExternalOperation, lex}; #[test] fn decl_and_call_internal_func() { @@ -58,131 +58,111 @@ mod func_tests { #[test] fn decl_and_call_external_func_singlet() { - 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::Symbol("input".to_string()), Ctr::None) + 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 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>; - if let Some(f) = ft.borrow().get(&"echo".to_string()) { - func = f.clone(); - } else { - print!("failed to retrieve function!"); - assert!(false); - return; - } + let func: Rc>; + 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); + 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); } } - }, - Err(e) => { - print!("Call to function failed: {}\n", e); - assert!(false); } } } #[test] - fn decl_and_call_external_func_dual_ret() { - let test_external_func: Function = Function{ - name: String::from("echox2"), - 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::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())); - 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>; - if let Some(f) = ft.borrow().get(&"echox2".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::Seg(b) => { - match b.borrow().clone().car { - Ctr::String(s) => assert!(s == "test"), - _ => { - print!("first elem wrong. expected \"test\", got: {:?}\n", b.borrow().car); - assert!(false); - } + 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); + } - 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) - } - } + let func: Rc>; + if let Some(f) = ft.borrow().get(&"echo_2".to_string()) { + func = f.clone(); + } else { + print!("failed to retrieve function!"); + assert!(false); + return; + } - match b2.borrow().cdr { - Ctr::None => (), - _ => { - print!("there should not be a third element\n"); - assert!(false) - } - } + match func_call(func, args, vt, ft) { + Ok(ret) => { + match ret { + Ctr::String(s) => { + assert!(s == "test"); }, _ => { - print!("first elem cdr should be a list. got: {:?}\n", b.borrow().cdr) + print!("Invalid return from function {:#?}. Should have recieved single string", ret); + assert!(false); + return } } }, - _ => { - print!("Invalid return from func. Got {:?}\n", ret); + Err(e) => { + print!("Call to function failed: {}\n", e); assert!(false); } } - }, - Err(e) => { - print!("Call to function failed: {}\n", e); - assert!(false); } } } @@ -210,58 +190,61 @@ mod func_tests { ) }; - 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: new_ast(Ctr::Symbol("test_inner".to_string()), - Ctr::Seg(new_ast(Ctr::Symbol("input".to_string()), - 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 ft = Rc::new(RefCell::new(FTable::new())); - let vt = Rc::new(RefCell::new(VTable::new())); - let args = new_ast(Ctr::Bool(true), Ctr::None); + let func: Rc>; + if let Some(f) = ft.borrow().get(&"test_outer".to_string()) { + func = f.clone(); + } else { + print!("failed to retrieve function!"); + assert!(false); + return; + } - 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>; - 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); + 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); } } - }, - Err(e) => { - print!("Call to function failed: {}\n", e); - assert!(false); } } } @@ -350,9 +333,10 @@ mod func_tests { assert!(false); } } - } - */ + }*/ + /* + // TODO: These tests need completion! #[test] fn eval_lazy_func_call() { @@ -381,10 +365,5 @@ mod func_tests { #[test] fn bad_eval_fn_body() { - } + }*/ } - -/* TESTING TODO: - * - Test functions that eval_lazy - * - Test functions that sym_loose - */ diff --git a/tests/test_lex.rs b/tests/test_lex.rs index 0e9fb5f..9b5c643 100644 --- a/tests/test_lex.rs +++ b/tests/test_lex.rs @@ -46,7 +46,7 @@ mod lex_tests { #[test] fn test_bad_symbol() { let document: &str = "(as;dd)"; - let output: &str = "Problem lexing document: \"Unparsable token:as;dd\""; + let output: &str = "Problem lexing document: \"Unparsable token: as;dd\""; match lex(document.to_string()) { Ok(tree) => { print!("Bad token yielded: {}\n", ast_to_string(tree));