This commit is contained in:
Aidan Hahn 2022-01-16 22:02:40 -08:00
parent f805290a4b
commit be73b0b828
No known key found for this signature in database
GPG key ID: 327711E983899316
17 changed files with 588 additions and 675 deletions

View file

@ -15,14 +15,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
use crate::func::{FTable, Function, Args, Operation}; use crate::func::{Args, FTable, Function, Operation};
use crate::vars::{VTable}; use crate::segment::{circuit, list_append, list_idx, new_ast, Ast, Ctr};
use crate::segment::{Ctr, Ast, circuit, list_idx, list_append, new_ast}; use crate::vars::VTable;
use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc;
pub fn get_append() -> Function { pub fn get_append() -> Function {
return Function{ return Function {
name: String::from("append"), name: String::from("append"),
loose_syms: false, loose_syms: false,
eval_lazy: false, eval_lazy: false,
@ -40,16 +40,18 @@ pub fn get_append() -> Function {
list_append(c.clone(), arg.clone()); list_append(c.clone(), arg.clone());
return true; return true;
}) { }) {
eprintln!("get_append circuit loop should not have returned false"); eprintln!(
"get_append circuit loop should not have returned false"
);
} }
}, }
_ => { _ => {
eprintln!("get_append args somehow not in standard form"); eprintln!("get_append args somehow not in standard form");
} }
} }
return ptr; return ptr;
}, }
_ => { _ => {
let n = new_ast(Ctr::None, Ctr::None); let n = new_ast(Ctr::None, Ctr::None);
if !circuit(a, &mut |arg: &Ctr| -> bool { if !circuit(a, &mut |arg: &Ctr| -> bool {
@ -62,7 +64,7 @@ pub fn get_append() -> Function {
return Ctr::Seg(n); return Ctr::Seg(n);
} }
} }
} },
)) )),
}; };
} }

View file

@ -15,15 +15,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
use dirs::home_dir;
use relish::ast::{ast_to_string, eval, func_call, lex, new_ast, Ctr, FTable, VTable};
use relish::aux::configure;
use relish::stdlib::get_stdlib;
use rustyline::error::ReadlineError; use rustyline::error::ReadlineError;
use rustyline::Editor; use rustyline::Editor;
use dirs::home_dir;
use std::rc::Rc;
use std::env;
use std::cell::RefCell; use std::cell::RefCell;
use relish::ast::{VTable, FTable, Ctr, lex, eval, ast_to_string, new_ast, func_call}; use std::env;
use relish::aux::configure; use std::rc::Rc;
use relish::stdlib::{get_stdlib};
fn main() { fn main() {
let mut rl = Editor::<()>::new(); let mut rl = Editor::<()>::new();
@ -52,12 +52,12 @@ fn main() {
match env::var("RELISH_CFG_FILE") { match env::var("RELISH_CFG_FILE") {
Ok(s) => configure(s, vt.clone(), ft.clone()), Ok(s) => configure(s, vt.clone(), ft.clone()),
Err(_) => configure(cfg, vt.clone(), ft.clone()) Err(_) => configure(cfg, vt.clone(), ft.clone()),
} }
match get_stdlib(vt.clone()) { match get_stdlib(vt.clone()) {
Ok(f) => ft = f, Ok(f) => ft = f,
Err(s) => println!("{}", s) Err(s) => println!("{}", s),
} }
loop { loop {
@ -68,23 +68,26 @@ fn main() {
let t_ft_c_b = tmp_ft_clone.borrow(); let t_ft_c_b = tmp_ft_clone.borrow();
let pfunc = t_ft_c_b.get("CFG_RELISH_PROMPT"); let pfunc = t_ft_c_b.get("CFG_RELISH_PROMPT");
if let Some(fnc) = pfunc { if let Some(fnc) = pfunc {
match func_call(fnc.clone(), new_ast(Ctr::None, Ctr::None), vt.clone(), ft.clone()) { match func_call(
fnc.clone(),
new_ast(Ctr::None, Ctr::None),
vt.clone(),
ft.clone(),
) {
Err(s) => { Err(s) => {
eprintln!("Couldnt generate prompt: {}", s); eprintln!("Couldnt generate prompt: {}", s);
readline = rl.readline(""); readline = rl.readline("");
},
Ok(c) => {
match c {
Ctr::Symbol(s) => readline = rl.readline(&s.to_owned()),
Ctr::String(s) => readline = rl.readline(&s),
Ctr::Integer(i) => readline = rl.readline(&format!("{}", i)),
Ctr::Float(f) => readline = rl.readline(&format!("{}", f)),
Ctr::Bool(b) => readline = rl.readline(&format!("{}", b)),
Ctr::Seg(c) => readline = rl.readline(&ast_to_string(c.clone())),
Ctr::None => readline = rl.readline("")
}
} }
Ok(c) => match c {
Ctr::Symbol(s) => readline = rl.readline(&s.to_owned()),
Ctr::String(s) => readline = rl.readline(&s),
Ctr::Integer(i) => readline = rl.readline(&format!("{}", i)),
Ctr::Float(f) => readline = rl.readline(&format!("{}", f)),
Ctr::Bool(b) => readline = rl.readline(&format!("{}", b)),
Ctr::Seg(c) => readline = rl.readline(&ast_to_string(c.clone())),
Ctr::None => readline = rl.readline(""),
},
} }
} else { } else {
readline = rl.readline(""); readline = rl.readline("");
@ -103,40 +106,32 @@ fn main() {
} }
match lex(l) { match lex(l) {
Ok(a) => { Ok(a) => match eval(a.clone(), vt.clone(), ft.clone(), false) {
match eval(a.clone(), vt.clone(), ft.clone(), false) { Ok(a) => match a {
Ok(a) => { Ctr::Symbol(s) => println!("{}", s),
match a { Ctr::String(s) => println!("{}", s),
Ctr::Symbol(s) => println!("{}", s), Ctr::Integer(i) => println!("{}", i),
Ctr::String(s) => println!("{}", s), Ctr::Float(f) => println!("{}", f),
Ctr::Integer(i) => println!("{}", i), Ctr::Bool(b) => println!("{}", b),
Ctr::Float(f) => println!("{}", f), Ctr::Seg(c) => println!("{}", ast_to_string(c.clone())),
Ctr::Bool(b) => println!("{}", b), Ctr::None => (),
Ctr::Seg(c) => println!("{}", ast_to_string(c.clone())), },
Ctr::None => () Err(s) => {
} println!("{}", s);
},
Err(s) => {
println!("{}", s);
}
} }
}, },
Err(s) => { Err(s) => {
println!("{}", s); println!("{}", s);
} }
} }
}, }
Err(ReadlineError::Interrupted) => { Err(ReadlineError::Interrupted) => break,
break
},
Err(ReadlineError::Eof) => { Err(ReadlineError::Eof) => return,
return
},
Err(err) => { Err(err) => {
eprintln!("Prompt error: {:?}", err); eprintln!("Prompt error: {:?}", err);
break break;
} }
} }
} }

View file

@ -15,16 +15,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
use crate::vars::{VTable, define};
use crate::func::{FTable, Args, Function, Operation, func_declare};
use crate::segment::{Ast, Ctr};
use crate::lex::lex;
use crate::eval::eval; use crate::eval::eval;
use crate::func::{func_declare, Args, FTable, Function, Operation};
use crate::lex::lex;
use crate::segment::{Ast, Ctr};
use crate::stl::get_stdlib; use crate::stl::get_stdlib;
use std::rc::Rc; use crate::vars::{define, VTable};
use std::cell::RefCell; use std::cell::RefCell;
use std::fs; use std::fs;
use std::rc::Rc;
fn prompt_default_callback(_: Ast, _: Rc<RefCell<VTable>>, _: Rc<RefCell<FTable>>) -> Ctr { fn prompt_default_callback(_: Ast, _: Rc<RefCell<VTable>>, _: Rc<RefCell<FTable>>) -> Ctr {
print!("λ "); print!("λ ");
@ -32,52 +31,56 @@ fn prompt_default_callback(_: Ast, _: Rc<RefCell<VTable>>, _: Rc<RefCell<FTable>
} }
pub fn configure(filename: String, vars: Rc<RefCell<VTable>>, mut funcs: Rc<RefCell<FTable>>) { pub fn configure(filename: String, vars: Rc<RefCell<VTable>>, mut funcs: Rc<RefCell<FTable>>) {
define(vars.clone(), String::from("CFG_RELISH_POSIX"), Rc::new(Ctr::String(String::from("0")))); define(
define(vars.clone(), String::from("CFG_RELISH_ENV"), Rc::new(Ctr::String(String::from("1")))); vars.clone(),
String::from("CFG_RELISH_POSIX"),
Rc::new(Ctr::String(String::from("0"))),
);
define(
vars.clone(),
String::from("CFG_RELISH_ENV"),
Rc::new(Ctr::String(String::from("1"))),
);
match get_stdlib(vars.clone()) { match get_stdlib(vars.clone()) {
Ok(f) => funcs = f, Ok(f) => funcs = f,
Err(s) => println!("{}", s) Err(s) => println!("{}", s),
} }
func_declare(
func_declare(funcs.clone(), Rc::new(RefCell::new(Function{ funcs.clone(),
name: String::from("CFG_RELISH_PROMPT"), Rc::new(RefCell::new(Function {
loose_syms: false, name: String::from("CFG_RELISH_PROMPT"),
eval_lazy: false, loose_syms: false,
args: Args::Lazy(0), eval_lazy: false,
function: Operation::Internal(Box::new(prompt_default_callback)) args: Args::Lazy(0),
}))); function: Operation::Internal(Box::new(prompt_default_callback)),
})),
);
match fs::read_to_string(filename) { match fs::read_to_string(filename) {
Err(s) => { Err(s) => {
eprintln!("Couldnt open configuration file: {}", s); eprintln!("Couldnt open configuration file: {}", s);
return return;
},
Ok(raw_config) => {
match lex(raw_config) {
Err(s) => {
println!("Error in configuration: {}", s);
return
},
Ok(config) => {
match eval(config, vars, funcs, false) {
Err(s) => {
println!("Error in applying configuration: {}", s);
return
},
Ok(ctr) => {
match ctr {
Ctr:: String(s) => println!("{}", s),
_ => ()
}
}
}
}
}
} }
Ok(raw_config) => match lex(raw_config) {
Err(s) => {
println!("Error in configuration: {}", s);
return;
}
Ok(config) => match eval(config, vars, funcs, false) {
Err(s) => {
println!("Error in applying configuration: {}", s);
return;
}
Ok(ctr) => match ctr {
Ctr::String(s) => println!("{}", s),
_ => (),
},
},
},
} }
} }

View file

@ -15,11 +15,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
use std::rc::Rc; use crate::func::{func_call, FTable};
use std::cell::RefCell; use crate::segment::{new_ast, Ast, Ctr};
use crate::segment::{Ast, Ctr, new_ast};
use crate::func::{FTable, func_call};
use crate::vars::VTable; use crate::vars::VTable;
use std::cell::RefCell;
use std::rc::Rc;
/* iterates over a syntax tree /* iterates over a syntax tree
* returns a NEW LIST of values * returns a NEW LIST of values
@ -29,7 +29,7 @@ pub fn eval(
ast: Ast, ast: Ast,
vars: Rc<RefCell<VTable>>, vars: Rc<RefCell<VTable>>,
funcs: Rc<RefCell<FTable>>, funcs: Rc<RefCell<FTable>>,
sym_loose: bool sym_loose: bool,
) -> Result<Ctr, String> { ) -> Result<Ctr, String> {
let mut car = ast.borrow().clone().car; let mut car = ast.borrow().clone().car;
let mut cdr = ast.borrow().clone().cdr; let mut cdr = ast.borrow().clone().cdr;
@ -49,19 +49,24 @@ pub fn eval(
match cdr.clone() { match cdr.clone() {
Ctr::Seg(ast) => { Ctr::Seg(ast) => {
if let Some(func) = funcs.borrow().get(tok) { if let Some(func) = funcs.borrow().get(tok) {
return func_call(func.clone(), ast.clone(), vars.clone(), funcs.clone()) return func_call(func.clone(), ast.clone(), vars.clone(), funcs.clone());
} else if !sym_loose { } else if !sym_loose {
return Err(format!("Couldnt find definition of {}.", tok)) return Err(format!("Couldnt find definition of {}.", tok));
} }
}, }
Ctr::None => { Ctr::None => {
if let Some(func) = funcs.borrow().get(tok) { if let Some(func) = funcs.borrow().get(tok) {
return func_call(func.clone(), new_ast(Ctr::None, Ctr::None), vars.clone(), funcs.clone()) return func_call(
func.clone(),
new_ast(Ctr::None, Ctr::None),
vars.clone(),
funcs.clone(),
);
} else if !sym_loose { } else if !sym_loose {
return Err(format!("Couldnt find definition of {}.", tok)) return Err(format!("Couldnt find definition of {}.", tok));
} }
}, }
_ => return Err(format!("Arguments to function not a list!")) _ => return Err(format!("Arguments to function not a list!")),
} }
} }
@ -72,9 +77,9 @@ pub fn eval(
Ctr::Seg(ref inner) => { Ctr::Seg(ref inner) => {
match eval(inner.clone(), vars.clone(), funcs.clone(), sym_loose) { match eval(inner.clone(), vars.clone(), funcs.clone(), sym_loose) {
Ok(res) => (*iter).borrow_mut().car = res, Ok(res) => (*iter).borrow_mut().car = res,
Err(e) => return Err(format!("Evaluation error: {}", e)) Err(e) => return Err(format!("Evaluation error: {}", e)),
} }
}, }
// if SYMBOL: unwrap naively // if SYMBOL: unwrap naively
Ctr::Symbol(ref tok) => { Ctr::Symbol(ref tok) => {
@ -83,9 +88,9 @@ pub fn eval(
} else if sym_loose { } else if sym_loose {
(*iter).borrow_mut().car = Ctr::Symbol(tok.to_string()); (*iter).borrow_mut().car = Ctr::Symbol(tok.to_string());
} else { } else {
return Err(format!("Undefined variable: {}", tok)) return Err(format!("Undefined variable: {}", tok));
} }
}, }
// if OTHER: clone and set // if OTHER: clone and set
_ => { _ => {
@ -101,11 +106,11 @@ pub fn eval(
} else if sym_loose { } else if sym_loose {
(*iter).borrow_mut().car = Ctr::Symbol(tok.to_string()); (*iter).borrow_mut().car = Ctr::Symbol(tok.to_string());
} else { } else {
return Err(format!("Undefined variable: {}", tok)) return Err(format!("Undefined variable: {}", tok));
} }
none = true; none = true;
}, }
// if LIST: // if LIST:
// - iter.cdr = new_ast(None, None) // - iter.cdr = new_ast(None, None)
@ -135,5 +140,5 @@ pub fn eval(
} }
} }
return Ok(Ctr::Seg(ret)) return Ok(Ctr::Seg(ret));
} }

View file

@ -15,13 +15,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
use std::rc::Rc;
use std::cell::RefCell;
use std::convert::TryInto;
use std::collections::HashMap;
use crate::segment::{Ctr, Type, circuit, list_len, list_idx, Ast};
use crate::vars::{VTable};
use crate::eval::eval; use crate::eval::eval;
use crate::segment::{circuit, list_idx, list_len, Ast, Ctr, Type};
use crate::vars::VTable;
use std::cell::RefCell;
use std::collections::HashMap;
use std::convert::TryInto;
use std::rc::Rc;
pub type FTable = HashMap<String, Rc<RefCell<Function>>>; pub type FTable = HashMap<String, Rc<RefCell<Function>>>;
@ -34,15 +34,15 @@ pub struct ExternalOperation {
// TODO: Apply Memoization? // TODO: Apply Memoization?
pub ast: Ast, pub ast: Ast,
// list of argument string tokens // list of argument string tokens
pub arg_syms: Vec<String> pub arg_syms: Vec<String>,
} }
/* A stored function may either be a pointer to a function /* A stored function may either be a pointer to a function
* or a syntax tree to eval with the arguments * or a syntax tree to eval with the arguments
*/ */
pub enum Operation { pub enum Operation {
Internal(Box<dyn Fn(Ast, Rc<RefCell<VTable>>, Rc<RefCell<FTable>>)->Ctr>), Internal(Box<dyn Fn(Ast, Rc<RefCell<VTable>>, Rc<RefCell<FTable>>) -> Ctr>),
External(ExternalOperation) External(ExternalOperation),
} }
/* Function Args /* Function Args
@ -52,7 +52,7 @@ pub enum Operation {
pub enum Args { pub enum Args {
// signed: -1 denotes infinite args // signed: -1 denotes infinite args
Lazy(i128), Lazy(i128),
Strict(Vec<Type>) Strict(Vec<Type>),
} }
// function which does not need args checked // function which does not need args checked
@ -65,7 +65,7 @@ pub struct Function {
pub loose_syms: bool, pub loose_syms: bool,
// dont evaluate args at all. leave that to the function // dont evaluate args at all. leave that to the function
pub eval_lazy: bool pub eval_lazy: bool,
} }
/* call /* call
@ -75,7 +75,7 @@ pub fn func_call(
function: Rc<RefCell<Function>>, function: Rc<RefCell<Function>>,
args: Ast, args: Ast,
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(); let called_func = function.borrow();
let mut n_args: Ast = args.clone(); let mut n_args: Ast = args.clone();
@ -85,16 +85,15 @@ pub fn func_call(
if let Ctr::Seg(ast) = rc_seg { if let Ctr::Seg(ast) = rc_seg {
n_args = ast n_args = ast
} else { } else {
return Err("Panicking: eval returned not a list for function args.".to_string()) return Err("Panicking: eval returned not a list for function args.".to_string());
} }
}, }
Err(s) => return Err( Err(s) => {
format!( return Err(format!(
"error evaluating args to {}: {}", "error evaluating args to {}: {}",
called_func.name, called_func.name, s
s ))
) }
)
} }
} }
@ -102,16 +101,12 @@ pub fn func_call(
Args::Lazy(num) => { Args::Lazy(num) => {
let called_arg_count = list_len(n_args.clone()) as i128; let called_arg_count = list_len(n_args.clone()) as i128;
if *num > -1 && (*num != called_arg_count) { if *num > -1 && (*num != called_arg_count) {
return Err( return Err(format!(
format!( "expected {} args in call to {}. Got {}.",
"expected {} args in call to {}. Got {}.", num, called_func.name, called_arg_count
num, ));
called_func.name,
called_arg_count
)
)
} }
}, }
Args::Strict(arg_types) => { Args::Strict(arg_types) => {
let mut idx: usize = 0; let mut idx: usize = 0;
@ -160,17 +155,13 @@ pub fn func_call(
} }
match &called_func.function { match &called_func.function {
Operation::Internal(f) => { Operation::Internal(f) => return Ok(f(n_args, vars, funcs)),
return Ok(f(n_args, vars, funcs))
},
Operation::External(f) => { Operation::External(f) => {
for n in 0..f.arg_syms.len() { for n in 0..f.arg_syms.len() {
let iter_arg = list_idx(n_args.clone(), n as u128); let iter_arg = list_idx(n_args.clone(), n as u128);
vars.borrow_mut().insert( vars.borrow_mut()
f.arg_syms[n].clone(), .insert(f.arg_syms[n].clone(), Rc::new(iter_arg));
Rc::new(iter_arg)
);
} }
let mut result: Ctr; let mut result: Ctr;
@ -178,13 +169,11 @@ pub fn func_call(
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) => match ctr {
match ctr { Ctr::Seg(ast) => result = ast.borrow().clone().car,
Ctr::Seg(ast) => result = ast.borrow().clone().car, _ => result = ctr,
_ => result = ctr
}
}, },
Err(e) => return Err(e) Err(e) => return Err(e),
} }
} else { } else {
panic!("function body not in standard form!") panic!("function body not in standard form!")
@ -193,22 +182,19 @@ pub fn func_call(
match iterate.clone().borrow().clone().cdr { match iterate.clone().borrow().clone().cdr {
Ctr::Seg(next) => iterate = next.clone(), Ctr::Seg(next) => iterate = next.clone(),
Ctr::None => break, Ctr::None => break,
_ => 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());
} }
return Ok(result) return Ok(result);
} }
} }
} }
pub fn func_declare( pub fn func_declare(ft: Rc<RefCell<FTable>>, f: Rc<RefCell<Function>>) -> Option<String> {
ft: Rc<RefCell<FTable>>,
f: Rc<RefCell<Function>>
) -> Option<String> {
let func = f.borrow(); let func = f.borrow();
let name = func.name.clone(); let name = func.name.clone();
if let Operation::External(fun) = &func.function { if let Operation::External(fun) = &func.function {
@ -216,14 +202,11 @@ pub fn func_declare(
if fun.arg_syms.len() != i.try_into().unwrap() { if fun.arg_syms.len() != i.try_into().unwrap() {
return Some( return Some(
"external function must have lazy args equal to declared arg_syms length" "external function must have lazy args equal to declared arg_syms length"
.to_string() .to_string(),
); );
} }
} else { } else {
return Some( return Some("external function must have lazy args".to_string());
"external function must have lazy args"
.to_string()
);
} }
} }

View file

@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
use crate::segment::{Ctr, Ast, list_append, new_ast}; use crate::segment::{list_append, new_ast, Ast, Ctr};
const UNMATCHED_STR_DELIM: &str = "Unmatched string delimiter in input"; const UNMATCHED_STR_DELIM: &str = "Unmatched string delimiter in input";
const UNMATCHED_LIST_DELIM: &str = "Unmatched list delimiter in input"; const UNMATCHED_LIST_DELIM: &str = "Unmatched list delimiter in input";
@ -34,8 +34,8 @@ pub fn lex(document: String) -> Result<Ast, String> {
// To represent the multiple passable outcomes // To represent the multiple passable outcomes
return match tree { return match tree {
Err(e) => Err(format!("Problem lexing document: {:?}", e)), Err(e) => Err(format!("Problem lexing document: {:?}", e)),
Ok(t) => Ok(t) Ok(t) => Ok(t),
} };
} }
/* The logic used in lex /* The logic used in lex
@ -113,27 +113,27 @@ fn process(document: String) -> Result<Ast, String> {
'(' => { '(' => {
if is_str { if is_str {
token.push(c); token.push(c);
continue continue;
} }
if token != "" { if token != "" {
return Err("list started in middle of another token".to_string()); return Err("list started in middle of another token".to_string());
} }
ref_stack.push(new_ast(Ctr::None, Ctr::None)); ref_stack.push(new_ast(Ctr::None, Ctr::None));
delim_stack.push(')'); delim_stack.push(')');
}, }
// begin parsing a string // begin parsing a string
'"' | '\'' | '`' => { '"' | '\'' | '`' => {
is_str = true; is_str = true;
delim_stack.push(c); delim_stack.push(c);
}, }
// eat the whole line // eat the whole line
'#' => { '#' => {
ign = true; ign = true;
delim_stack.push('\n'); delim_stack.push('\n');
}, }
// escape next char // escape next char
'\\' => { '\\' => {
delim_stack.push('*'); delim_stack.push('*');
@ -200,7 +200,6 @@ fn process(document: String) -> Result<Ast, String> {
return Err(UNMATCHED_LIST_DELIM.to_string()); return Err(UNMATCHED_LIST_DELIM.to_string());
} }
/* Returns true if token /* Returns true if token
* - is all alphanumeric except dash and underscore * - is all alphanumeric except dash and underscore
* *
@ -210,9 +209,9 @@ 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) && !(t == '-') && !(t == '_') { if !t.is_alphabetic() && !t.is_digit(10) && !(t == '-') && !(t == '_') {
return None return None;
} }
} }
return Some(String::from(tok)) return Some(String::from(tok));
} }

View file

@ -15,31 +15,31 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
mod segment;
mod lex;
mod func;
mod eval;
mod vars;
mod stl;
mod str;
mod append; mod append;
mod config; mod config;
mod eval;
mod func;
mod lex;
mod segment;
mod stl;
mod str;
mod vars;
pub mod ast { pub mod ast {
pub use crate::segment::{Seg, Ctr, ast_to_string, Type, Ast, new_ast};
pub use crate::lex::lex;
pub use crate::func::{Function, Operation, FTable, Args,
ExternalOperation, func_declare, func_call};
pub use crate::vars::{VTable, define};
pub use crate::eval::eval; pub use crate::eval::eval;
pub use crate::func::{
func_call, func_declare, Args, ExternalOperation, FTable, Function, Operation,
};
pub use crate::lex::lex;
pub use crate::segment::{ast_to_string, new_ast, Ast, Ctr, Seg, Type};
pub use crate::vars::{define, VTable};
} }
pub mod stdlib { pub mod stdlib {
pub use crate::stl::{get_stdlib}; pub use crate::append::get_append;
pub use crate::str::{get_echo, get_concat}; pub use crate::stl::get_stdlib;
pub use crate::append::{get_append}; pub use crate::str::{get_concat, get_echo};
pub use crate::vars::{get_export}; pub use crate::vars::get_export;
} }
pub mod aux { pub mod aux {

View file

@ -15,15 +15,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc;
// Recursive data type for a tree of Segments // Recursive data type for a tree of Segments
pub type Ast = Rc<RefCell<Seg>>; pub type Ast = Rc<RefCell<Seg>>;
// Container // Container
#[derive(Clone)] #[derive(Clone, Debug)]
#[derive(Debug)]
pub enum Ctr { pub enum Ctr {
Symbol(String), Symbol(String),
String(String), String(String),
@ -31,12 +30,11 @@ pub enum Ctr {
Float(f64), Float(f64),
Bool(bool), Bool(bool),
Seg(Ast), Seg(Ast),
None None,
} }
// Type of Container // Type of Container
#[derive(PartialEq)] #[derive(PartialEq, Clone)]
#[derive(Clone)]
pub enum Type { pub enum Type {
Symbol, Symbol,
String, String,
@ -44,7 +42,7 @@ pub enum Type {
Float, Float,
Bool, Bool,
Seg, Seg,
None None,
} }
/* Segment /* Segment
@ -53,8 +51,7 @@ pub enum Type {
* I was going to call it Cell and then I learned about * I was going to call it Cell and then I learned about
* how important RefCells were in Rust * how important RefCells were in Rust
*/ */
#[derive(Clone)] #[derive(Clone, Debug)]
#[derive(Debug)]
pub struct Seg { pub struct Seg {
/* "Contents of Address Register" /* "Contents of Address Register"
* Historical way of referring to the first value in a cell. * Historical way of referring to the first value in a cell.
@ -64,7 +61,7 @@ pub struct Seg {
/* "Contents of Decrement Register" /* "Contents of Decrement Register"
* Historical way of referring to the second value in a cell. * Historical way of referring to the second value in a cell.
*/ */
pub cdr: Ctr pub cdr: Ctr,
} }
impl Ctr { impl Ctr {
@ -76,7 +73,7 @@ impl Ctr {
Ctr::Float(_s) => Type::Float, Ctr::Float(_s) => Type::Float,
Ctr::Bool(_s) => Type::Bool, Ctr::Bool(_s) => Type::Bool,
Ctr::Seg(_s) => Type::Seg, Ctr::Seg(_s) => Type::Seg,
Ctr::None => Type::None Ctr::None => Type::None,
} }
} }
} }
@ -91,7 +88,7 @@ impl Type {
Type::Float => ret = "float", Type::Float => ret = "float",
Type::Bool => ret = "bool", Type::Bool => ret = "bool",
Type::Seg => ret = "segment", Type::Seg => ret = "segment",
Type::None => ret = "none" Type::None => ret = "none",
} }
ret.to_owned() ret.to_owned()
@ -105,17 +102,17 @@ pub fn ast_as_string(c: Ast, with_parens: bool) -> String {
let mut prn_space = true; let mut prn_space = true;
let seg = c.borrow(); let seg = c.borrow();
match &seg.car { match &seg.car {
Ctr::Symbol(s) => string.push_str(&s), Ctr::Symbol(s) => string.push_str(&s),
Ctr::String(s) => { Ctr::String(s) => {
string.push('\''); string.push('\'');
string.push_str(&s); string.push_str(&s);
string.push('\''); string.push('\'');
}, }
Ctr::Integer(i) => string = string + &i.to_string(), Ctr::Integer(i) => string = string + &i.to_string(),
Ctr::Float(f) => string = string + &f.to_string(), Ctr::Float(f) => string = string + &f.to_string(),
Ctr::Bool(b) => string = string + &b.to_string(), Ctr::Bool(b) => string = string + &b.to_string(),
Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), true).as_str()), Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), true).as_str()),
Ctr::None => prn_space = false Ctr::None => prn_space = false,
} }
if prn_space { if prn_space {
@ -123,17 +120,17 @@ pub fn ast_as_string(c: Ast, with_parens: bool) -> String {
} }
match &seg.cdr { match &seg.cdr {
Ctr::Symbol(s) => string.push_str(&s), Ctr::Symbol(s) => string.push_str(&s),
Ctr::String(s) => { Ctr::String(s) => {
string.push('\''); string.push('\'');
string.push_str(&s); string.push_str(&s);
string.push('\''); string.push('\'');
}, }
Ctr::Integer(i) => string = string + &i.to_string(), Ctr::Integer(i) => string = string + &i.to_string(),
Ctr::Float(f) => string = string + &f.to_string(), Ctr::Float(f) => string = string + &f.to_string(),
Ctr::Bool(b) => string = string + &b.to_string(), Ctr::Bool(b) => string = string + &b.to_string(),
Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), false).as_str()), Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), false).as_str()),
Ctr::None => { Ctr::None => {
if prn_space { if prn_space {
string.pop(); string.pop();
} }
@ -147,7 +144,7 @@ pub fn ast_as_string(c: Ast, with_parens: bool) -> String {
extra.push(')'); extra.push(')');
string = extra string = extra
} }
return string return string;
} }
pub fn ast_to_string(c: Ast) -> String { pub fn ast_to_string(c: Ast) -> String {
@ -162,10 +159,7 @@ pub fn ast_to_string(c: Ast) -> String {
/* Initializes a new ast node with segment car and cdr passed in /* Initializes a new ast node with segment car and cdr passed in
*/ */
pub fn new_ast(car: Ctr, cdr: Ctr) -> Ast { pub fn new_ast(car: Ctr, cdr: Ctr) -> Ast {
Rc::new(RefCell::new(Seg{ Rc::new(RefCell::new(Seg { car: car, cdr: cdr }))
car: car,
cdr: cdr
}))
} }
/* applies a function across a list in standard form /* applies a function across a list in standard form
@ -173,13 +167,13 @@ pub fn new_ast(car: Ctr, cdr: Ctr) -> Ast {
* short circuits on the first false returned. * short circuits on the first false returned.
* also returns false on a non standard form list * also returns false on a non standard form list
*/ */
pub fn circuit<F: FnMut(&Ctr)-> bool>(tree: Ast, func: &mut F) -> bool{ pub fn circuit<F: FnMut(&Ctr) -> bool>(tree: Ast, func: &mut F) -> bool {
let inner = tree.borrow(); let inner = tree.borrow();
if func(&inner.car) { if func(&inner.car) {
match &inner.cdr { match &inner.cdr {
Ctr::None => true, Ctr::None => true,
Ctr::Seg(c) => circuit(c.clone(), func), Ctr::Seg(c) => circuit(c.clone(), func),
_ => false _ => false,
} }
} else { } else {
false false
@ -192,7 +186,7 @@ pub fn circuit<F: FnMut(&Ctr)-> bool>(tree: Ast, func: &mut F) -> bool{
pub fn list_len(tree: Ast) -> u128 { pub fn list_len(tree: Ast) -> u128 {
match &tree.borrow().cdr { match &tree.borrow().cdr {
Ctr::Seg(c) => list_len(c.clone()) + 1, Ctr::Seg(c) => list_len(c.clone()) + 1,
_ => 1 _ => 1,
} }
} }
@ -216,9 +210,7 @@ pub fn list_idx(tree: Ast, idx: u128) -> Ctr {
} else { } else {
match inner.car { match inner.car {
Ctr::None => Ctr::None, Ctr::None => Ctr::None,
_ => { _ => inner.car.clone(),
inner.car.clone()
}
} }
} }
} }
@ -231,17 +223,15 @@ pub fn list_append(tree: Ast, obj: Ctr) {
match &inner.car { match &inner.car {
Ctr::None => { Ctr::None => {
inner.car = obj; inner.car = obj;
},
_ => {
match &inner.cdr {
Ctr::None => {
inner.cdr = Ctr::Seg(new_ast(obj, Ctr::None));
},
Ctr::Seg(tr) => {
list_append(tr.clone(), obj);
},
_ => ()
}
} }
_ => match &inner.cdr {
Ctr::None => {
inner.cdr = Ctr::Seg(new_ast(obj, Ctr::None));
}
Ctr::Seg(tr) => {
list_append(tr.clone(), obj);
}
_ => (),
},
} }
} }

View file

@ -15,24 +15,24 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
use crate::segment::{Ctr};
use crate::str::{get_echo, get_concat};
use crate::append::get_append; use crate::append::get_append;
use crate::func::{FTable, func_declare}; use crate::func::{func_declare, FTable};
use crate::vars::{VTable, get_export}; use crate::segment::Ctr;
use std::rc::Rc; use crate::str::{get_concat, get_echo};
use crate::vars::{get_export, VTable};
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc;
pub fn get_stdlib(conf: Rc<RefCell<VTable>>) -> Result<Rc<RefCell<FTable>>, String> { pub fn get_stdlib(conf: Rc<RefCell<VTable>>) -> Result<Rc<RefCell<FTable>>, String> {
let ft = Rc::new(RefCell::new(FTable::new())); let ft = Rc::new(RefCell::new(FTable::new()));
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_echo()))) { if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_echo()))) {
return Err(s) return Err(s);
} }
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_append()))) { if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_append()))) {
return Err(s) return Err(s);
} }
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_concat()))) { if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_concat()))) {
return Err(s) return Err(s);
} }
let mut cfg_env = true; let mut cfg_env = true;
@ -40,22 +40,18 @@ pub fn get_stdlib(conf: Rc<RefCell<VTable>>) -> Result<Rc<RefCell<FTable>>, Stri
None => { None => {
println!("CFG_RELISH_ENV not defined. defaulting to ON.") println!("CFG_RELISH_ENV not defined. defaulting to ON.")
} }
Some(ctr) => { Some(ctr) => match (**ctr).clone() {
match (**ctr).clone() { Ctr::String(ref s) => cfg_env = s.eq("0"),
Ctr::String(ref s) => { _ => {
cfg_env = s.eq("0") println!("Invalid value for CFG_RELISH_ENV. must be a string (0 or 1).");
} println!("Defaulting CFG_RELISH_ENV to ON");
_ => {
println!("Invalid value for CFG_RELISH_ENV. must be a string (0 or 1).");
println!("Defaulting CFG_RELISH_ENV to ON");
}
} }
} },
} }
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_export(cfg_env)))) { if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_export(cfg_env)))) {
return Err(s) return Err(s);
} }
return Ok(ft) return Ok(ft);
} }

View file

@ -15,16 +15,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
use crate::func::{FTable, Function, Args, Operation}; use crate::func::{Args, FTable, Function, Operation};
use crate::vars::{VTable}; use crate::segment::{ast_as_string, circuit, Ast, Ctr};
use crate::segment::{Ctr, Ast, circuit, ast_as_string}; use crate::vars::VTable;
use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc;
// Current primitive is to use a get_NNNNN function defined for each library function which returns a not-previously-owned // Current primitive is to use a get_NNNNN function defined for each library function which returns a not-previously-owned
// copy of the Function struct. // copy of the Function struct.
pub fn get_echo() -> Function { pub fn get_echo() -> Function {
return Function{ return Function {
name: String::from("echo"), name: String::from("echo"),
loose_syms: false, loose_syms: false,
eval_lazy: false, eval_lazy: false,
@ -35,13 +35,13 @@ pub fn get_echo() -> Function {
if !circuit(a, &mut |arg: &Ctr| { if !circuit(a, &mut |arg: &Ctr| {
match arg { match arg {
// should be a thing here // should be a thing here
Ctr::Symbol(_) => return false, Ctr::Symbol(_) => return false,
Ctr::String(s) => string.push_str(&s), Ctr::String(s) => string.push_str(&s),
Ctr::Integer(i) => string.push_str(&i.to_string()), Ctr::Integer(i) => string.push_str(&i.to_string()),
Ctr::Float(f) => string.push_str(&f.to_string()), Ctr::Float(f) => string.push_str(&f.to_string()),
Ctr::Bool(b) => string.push_str(&b.to_string()), Ctr::Bool(b) => string.push_str(&b.to_string()),
Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), true).as_str()), Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), true).as_str()),
Ctr::None => () Ctr::None => (),
} }
println!("{}", string); println!("{}", string);
return true; return true;
@ -49,13 +49,13 @@ pub fn get_echo() -> Function {
eprintln!("circuit loop in echo should not have returned false") eprintln!("circuit loop in echo should not have returned false")
} }
return Ctr::None; return Ctr::None;
} },
)) )),
}; };
} }
pub fn get_concat() -> Function { pub fn get_concat() -> Function {
return Function{ return Function {
name: String::from("concat"), name: String::from("concat"),
loose_syms: false, loose_syms: false,
eval_lazy: false, eval_lazy: false,
@ -66,20 +66,20 @@ pub fn get_concat() -> Function {
if !circuit(a, &mut |arg: &Ctr| { if !circuit(a, &mut |arg: &Ctr| {
match arg { match arg {
// should be a thing here // should be a thing here
Ctr::Symbol(_) => return false, Ctr::Symbol(_) => return false,
Ctr::String(s) => string.push_str(&s), Ctr::String(s) => string.push_str(&s),
Ctr::Integer(i) => string.push_str(&i.to_string()), Ctr::Integer(i) => string.push_str(&i.to_string()),
Ctr::Float(f) => string.push_str(&f.to_string()), Ctr::Float(f) => string.push_str(&f.to_string()),
Ctr::Bool(b) => string.push_str(&b.to_string()), Ctr::Bool(b) => string.push_str(&b.to_string()),
Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), true).as_str()), Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), true).as_str()),
Ctr::None => () Ctr::None => (),
} }
return true; return true;
}) { }) {
eprintln!("circuit loop in concat should not have returned false") eprintln!("circuit loop in concat should not have returned false")
} }
return Ctr::String(string); return Ctr::String(string);
} },
)) )),
}; };
} }

View file

@ -15,13 +15,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
use crate::eval::eval;
use crate::func::{Args, FTable, Function, Operation};
use crate::segment::{ast_to_string, Ast, Ctr};
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use crate::segment::{Ctr, Ast, ast_to_string}; use std::rc::Rc;
use crate::eval::eval;
use crate::func::{Function, Operation, Args, FTable};
/* Mapping between a string token and a tree of Segments /* Mapping between a string token and a tree of Segments
* The string token can be found in any Ctr::Symbol value * The string token can be found in any Ctr::Symbol value
* it is expected that the trees stored are already evaluated * it is expected that the trees stored are already evaluated
@ -29,11 +29,7 @@ use crate::func::{Function, Operation, Args, FTable};
pub type VTable = HashMap<String, Rc<Ctr>>; pub type VTable = HashMap<String, Rc<Ctr>>;
// WARNING: make sure var_tree is properly evaluated before storing // WARNING: make sure var_tree is properly evaluated before storing
pub fn define( pub fn define(vt: Rc<RefCell<VTable>>, identifier: String, var_tree: Rc<Ctr>) {
vt: Rc<RefCell<VTable>>,
identifier: String,
var_tree: Rc<Ctr>
) {
if let Some(rc_segment) = vt.borrow_mut().insert(identifier, var_tree) { if let Some(rc_segment) = vt.borrow_mut().insert(identifier, var_tree) {
drop(rc_segment); drop(rc_segment);
} }
@ -43,58 +39,58 @@ pub fn get_export(env_cfg: bool) -> Function {
// This is the most hilarious way to work around the lifetime of env_cfg // This is the most hilarious way to work around the lifetime of env_cfg
// TODO: figure out the RUSTEY way of doing this // TODO: figure out the RUSTEY way of doing this
if env_cfg { if env_cfg {
return Function{ return Function {
name: String::from("export"), name: String::from("export"),
loose_syms: true, loose_syms: true,
eval_lazy: true, eval_lazy: true,
args: Args::Lazy(2), args: Args::Lazy(2),
function: Operation::Internal(Box::new(|a: Ast, b: Rc<RefCell<VTable>>, c: Rc<RefCell<FTable>>| -> Ctr { function: Operation::Internal(Box::new(
export_callback(true, a, b, c) |a: Ast, b: Rc<RefCell<VTable>>, c: Rc<RefCell<FTable>>| -> Ctr {
})), export_callback(true, a, b, c)
},
)),
}; };
} else { } else {
return Function{ return Function {
name: String::from("export"), name: String::from("export"),
loose_syms: true, loose_syms: true,
eval_lazy: true, eval_lazy: true,
args: Args::Lazy(2), args: Args::Lazy(2),
function: Operation::Internal(Box::new(|a: Ast, b: Rc<RefCell<VTable>>, c: Rc<RefCell<FTable>>| -> Ctr { function: Operation::Internal(Box::new(
export_callback(false, a, b, c) |a: Ast, b: Rc<RefCell<VTable>>, c: Rc<RefCell<FTable>>| -> Ctr {
})), export_callback(false, a, b, c)
},
)),
}; };
} }
} }
fn export_callback(env_cfg: bool, a: Ast, b: Rc<RefCell<VTable>>, c: Rc<RefCell<FTable>>) -> Ctr{ fn export_callback(env_cfg: bool, a: Ast, b: Rc<RefCell<VTable>>, c: Rc<RefCell<FTable>>) -> Ctr {
let inner = a.borrow_mut(); let inner = a.borrow_mut();
match &inner.car { match &inner.car {
Ctr::Symbol(identifier) => { Ctr::Symbol(identifier) => {
match &inner.cdr { match &inner.cdr {
Ctr::Seg(tree) => { Ctr::Seg(tree) => match eval(tree.clone(), b.clone(), c.clone(), false) {
match eval(tree.clone(), b.clone(), c.clone(), false) { Ok(seg) => match seg {
Ok(seg) => { Ctr::Seg(val) => {
match seg { define(b, identifier.to_string(), Rc::new(val.borrow().clone().car));
Ctr::Seg(val) => { if env_cfg {
define(b, identifier.to_string(), Rc::new(val.borrow().clone().car)); match val.borrow().clone().car {
if env_cfg { Ctr::Symbol(s) => env::set_var(identifier, s),
match val.borrow().clone().car { Ctr::String(s) => env::set_var(identifier, s),
Ctr::Symbol(s) => env::set_var(identifier, s), Ctr::Integer(i) => env::set_var(identifier, format!("{}", i)),
Ctr::String(s) => env::set_var(identifier, s), Ctr::Float(f) => env::set_var(identifier, format!("{}", f)),
Ctr::Integer(i) => env::set_var(identifier, format!("{}", i)), Ctr::Bool(b) => env::set_var(identifier, format!("{}", b)),
Ctr::Float(f) => env::set_var(identifier, format!("{}", f)), Ctr::Seg(c) => env::set_var(identifier, ast_to_string(c)),
Ctr::Bool(b) => env::set_var(identifier, format!("{}", b)), Ctr::None => (),
Ctr::Seg(c) => env::set_var(identifier, ast_to_string(c)), }
Ctr::None => ()
}
}
},
_ => eprintln!("impossible args to export")
} }
}, }
Err(e) => eprintln!("couldnt eval symbol: {}", e) _ => eprintln!("impossible args to export"),
} },
Err(e) => eprintln!("couldnt eval symbol: {}", e),
}, },
Ctr::None => { Ctr::None => {
@ -102,13 +98,13 @@ fn export_callback(env_cfg: bool, a: Ast, b: Rc<RefCell<VTable>>, c: Rc<RefCell<
if env_cfg { if env_cfg {
// TODO: unset variable // TODO: unset variable
} }
}, }
_ => eprintln!("args not in standard form") _ => eprintln!("args not in standard form"),
} }
}, }
_ => eprintln!("first argument to export must be a symbol") _ => eprintln!("first argument to export must be a symbol"),
} }
return Ctr::None; return Ctr::None;

View file

@ -1,9 +1,9 @@
mod eval_tests { mod eval_tests {
use std::rc::Rc; use relish::ast::{ast_to_string, eval, lex, new_ast, FTable, VTable};
use std::cell::RefCell;
use relish::ast::{Ctr, Function, Operation, ExternalOperation};
use relish::ast::{new_ast, lex, eval, VTable, FTable, ast_to_string};
use relish::ast::{func_declare, Args}; 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 // TODO: write generalized testing routine on top of list of inputs
@ -17,22 +17,20 @@ mod eval_tests {
Err(e) => { Err(e) => {
println!("Lexing error: {}\n", e); println!("Lexing error: {}\n", e);
assert!(false) assert!(false)
}, }
Ok(initial_ast) => { Ok(initial_ast) => match eval(initial_ast.clone(), vt.clone(), ft.clone(), false) {
match eval(initial_ast.clone(), vt.clone(), ft.clone(), false) { Err(e) => {
Err(e) => { println!("Evaluation error: {}\n", e);
println!("Evaluation error: {}\n", e); assert!(false)
assert!(false) }
},
Ok(reduced) => { Ok(reduced) => {
if let Ctr::Seg(reduced_ast) = reduced { if let Ctr::Seg(reduced_ast) = reduced {
assert_eq!(ast_to_string(reduced_ast), test_doc) assert_eq!(ast_to_string(reduced_ast), test_doc)
}
} }
} }
} },
} }
} }
@ -46,22 +44,20 @@ mod eval_tests {
Err(e) => { Err(e) => {
println!("Lexing error: {}\n", e); println!("Lexing error: {}\n", e);
assert!(false) assert!(false)
}, }
Ok(initial_ast) => { Ok(initial_ast) => match eval(initial_ast.clone(), vt.clone(), ft.clone(), false) {
match eval(initial_ast.clone(), vt.clone(), ft.clone(), false) { Err(e) => {
Err(e) => { println!("Evaluation error: {}\n", e);
println!("Evaluation error: {}\n", e); assert!(false)
assert!(false) }
},
Ok(reduced) => { Ok(reduced) => {
if let Ctr::Seg(reduced_ast) = reduced { if let Ctr::Seg(reduced_ast) = reduced {
assert_eq!(ast_to_string(reduced_ast), test_doc) assert_eq!(ast_to_string(reduced_ast), test_doc)
}
} }
} }
} },
} }
} }
@ -69,23 +65,23 @@ mod eval_tests {
fn eval_function_call() { fn eval_function_call() {
let test_doc = "('one' (echo 'unwrap_me'))".to_string(); let test_doc = "('one' (echo 'unwrap_me'))".to_string();
let output = "('one' 'unwrap_me')"; let output = "('one' 'unwrap_me')";
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,
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(
ast: new_ast(Ctr::Seg(new_ast(Ctr::Symbol("input".to_string()), Ctr::None)), Ctr::None) 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(), if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(test_external_func))) {
Rc::new(RefCell::new(test_external_func))) {
print!("Error declaring external func: {}", s); print!("Error declaring external func: {}", s);
assert!(false); assert!(false);
} }
@ -94,26 +90,24 @@ mod eval_tests {
Err(e) => { Err(e) => {
println!("Lexing error: {}\n", e); println!("Lexing error: {}\n", e);
assert!(false) assert!(false)
}, }
Ok(initial_ast) => { Ok(initial_ast) => match eval(initial_ast.clone(), vt.clone(), ft.clone(), false) {
match eval(initial_ast.clone(), vt.clone(), ft.clone(), false) { Err(e) => {
Err(e) => { println!("Evaluation error: {}\n", e);
println!("Evaluation error: {}\n", e); assert!(false)
assert!(false) }
},
Ok(reduced) => { Ok(reduced) => {
if let Ctr::Seg(reduced_ast) = reduced { if let Ctr::Seg(reduced_ast) = reduced {
let out_doc = ast_to_string(reduced_ast); let out_doc = ast_to_string(reduced_ast);
if out_doc != output { if out_doc != output {
print!("Erroneous output: {}\n", out_doc); print!("Erroneous output: {}\n", out_doc);
assert!(false) assert!(false)
}
} }
} }
} }
} },
} }
} }
@ -121,23 +115,23 @@ mod eval_tests {
fn eval_embedded_func_calls() { fn eval_embedded_func_calls() {
let test_doc = "('one' (echo (echo 'unwrap_me')))".to_string(); let test_doc = "('one' (echo (echo 'unwrap_me')))".to_string();
let output = "('one' 'unwrap_me')"; let output = "('one' 'unwrap_me')";
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,
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(
ast: new_ast(Ctr::Seg(new_ast(Ctr::Symbol("input".to_string()), Ctr::None)), Ctr::None) 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(), if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(test_external_func))) {
Rc::new(RefCell::new(test_external_func))) {
print!("Error declaring external func: {}", s); print!("Error declaring external func: {}", s);
assert!(false); assert!(false);
} }
@ -146,28 +140,25 @@ mod eval_tests {
Err(e) => { Err(e) => {
println!("Lexing error: {}\n", e); println!("Lexing error: {}\n", e);
assert!(false) assert!(false)
}, }
Ok(initial_ast) => { Ok(initial_ast) => match eval(initial_ast.clone(), vt.clone(), ft.clone(), false) {
match eval(initial_ast.clone(), vt.clone(), ft.clone(), false) { Err(e) => {
Err(e) => { println!("Evaluation error: {}\n", e);
println!("Evaluation error: {}\n", e); assert!(false)
assert!(false) }
},
Ok(reduced) => { Ok(reduced) => {
if let Ctr::Seg(reduced_ast) = reduced { if let Ctr::Seg(reduced_ast) = reduced {
let out_doc = ast_to_string(reduced_ast); let out_doc = ast_to_string(reduced_ast);
if out_doc != output { if out_doc != output {
print!("Erroneous output: {}\n", out_doc); print!("Erroneous output: {}\n", out_doc);
assert!(false) assert!(false)
}
} }
} }
} }
} },
} }
} }
/* /*

View file

@ -1,14 +1,16 @@
mod func_tests { 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::VTable;
use relish::ast::{Function, Operation, FTable, Args, func_declare, func_call, ExternalOperation, lex}; 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] #[test]
fn decl_and_call_internal_func() { fn decl_and_call_internal_func() {
let test_internal_func: Function = Function{ let test_internal_func: Function = Function {
name: String::from("test_func_in"), name: String::from("test_func_in"),
loose_syms: false, loose_syms: false,
eval_lazy: false, eval_lazy: false,
args: Args::Strict(vec![Type::Bool]), args: Args::Strict(vec![Type::Bool]),
@ -21,14 +23,13 @@ mod func_tests {
} }
Ctr::Bool(is_bool) Ctr::Bool(is_bool)
} },
)) )),
}; };
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()));
let args = new_ast(Ctr::Bool(true), Ctr::None); let args = new_ast(Ctr::Bool(true), Ctr::None);
if let Some(s) = func_declare(ft.clone(), if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(test_internal_func))) {
Rc::new(RefCell::new(test_internal_func))) {
print!("Error declaring internal func: {}", s); print!("Error declaring internal func: {}", s);
assert!(false); assert!(false);
} }
@ -61,23 +62,21 @@ mod func_tests {
match lex("((input))".to_string()) { match lex("((input))".to_string()) {
Err(e) => panic!("{}", e), Err(e) => panic!("{}", e),
Ok(finner) => { 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,
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: finner,
ast: finner }),
}
)
}; };
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()));
let args = new_ast(Ctr::String("test".to_string()), Ctr::None); let args = new_ast(Ctr::String("test".to_string()), Ctr::None);
if let Some(s) = func_declare(ft.clone(), if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(test_external_func)))
Rc::new(RefCell::new(test_external_func))) { {
print!("Error declaring external func: {}", s); print!("Error declaring external func: {}", s);
assert!(false); assert!(false);
} }
@ -92,13 +91,11 @@ 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::String(b) => assert!(b == "test"),
Ctr::String(b) => assert!(b == "test"), _ => {
_ => { print!("Invalid return from func. Got {:?}\n", ret);
print!("Invalid return from func. Got {:?}\n", ret); assert!(false);
assert!(false);
}
} }
}, },
Err(e) => { Err(e) => {
@ -115,23 +112,21 @@ mod func_tests {
match lex("((input) (input))".to_string()) { match lex("((input) (input))".to_string()) {
Err(e) => panic!("{}", e), Err(e) => panic!("{}", e),
Ok(finner) => { Ok(finner) => {
let test_external_func: Function = Function{ let test_external_func: Function = Function {
name: String::from("echo_2"), 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: finner,
ast: finner }),
}
)
}; };
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()));
let args = new_ast(Ctr::String("test".to_string()), Ctr::None); let args = new_ast(Ctr::String("test".to_string()), Ctr::None);
if let Some(s) = func_declare(ft.clone(), if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(test_external_func)))
Rc::new(RefCell::new(test_external_func))) { {
print!("Error declaring external func: {}", s); print!("Error declaring external func: {}", s);
assert!(false); assert!(false);
} }
@ -146,16 +141,14 @@ 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::String(s) => {
Ctr::String(s) => { assert!(s == "test");
assert!(s == "test"); }
}, _ => {
_ => { print!("Invalid return from function {:#?}. Should have recieved single string", ret);
print!("Invalid return from function {:#?}. Should have recieved single string", ret); assert!(false);
assert!(false); return;
return
}
} }
}, },
Err(e) => { Err(e) => {
@ -169,7 +162,7 @@ mod func_tests {
#[test] #[test]
fn decl_and_call_func_with_nested_call() { fn decl_and_call_func_with_nested_call() {
let inner_func: Function = Function{ let inner_func: Function = Function {
name: String::from("test_inner"), name: String::from("test_inner"),
loose_syms: false, loose_syms: false,
eval_lazy: false, eval_lazy: false,
@ -186,37 +179,33 @@ mod func_tests {
} else { } else {
Ctr::None Ctr::None
} }
} },
)) )),
}; };
match lex("((test_inner true))".to_string()) { match lex("((test_inner true))".to_string()) {
Err(e) => panic!("{}", e), Err(e) => panic!("{}", e),
Ok(finner) => { 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,
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: finner,
ast: finner }),
}
)
}; };
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()));
let args = new_ast(Ctr::Bool(true), Ctr::None); let args = new_ast(Ctr::Bool(true), Ctr::None);
if let Some(s) = func_declare(ft.clone(), if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(inner_func))) {
Rc::new(RefCell::new(inner_func))) {
print!("Error declaring inner func: {}", s); print!("Error declaring inner func: {}", s);
assert!(false); assert!(false);
} }
if let Some(s) = func_declare(ft.clone(), if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(outer_func))) {
Rc::new(RefCell::new(outer_func))) {
print!("Error declaring outer func: {}", s); print!("Error declaring outer func: {}", s);
assert!(false); assert!(false);
} }
@ -231,13 +220,11 @@ 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::String(b) => assert!(b == "test"),
Ctr::String(b) => assert!(b == "test"), _ => {
_ => { print!("Invalid return from func. Got {:?}\n", ret);
print!("Invalid return from func. Got {:?}\n", ret); assert!(false);
assert!(false);
}
} }
}, },
Err(e) => { Err(e) => {

View file

@ -1,5 +1,5 @@
mod lex_tests { mod lex_tests {
use relish::ast::{lex, ast_to_string}; use relish::ast::{ast_to_string, lex};
#[test] #[test]
fn test_lex_basic_pair() { fn test_lex_basic_pair() {
@ -7,7 +7,7 @@ mod lex_tests {
match lex(document.to_string()) { match lex(document.to_string()) {
Ok(tree) => { Ok(tree) => {
assert_eq!(ast_to_string(tree), document); assert_eq!(ast_to_string(tree), document);
}, }
Err(s) => { Err(s) => {
print!("{}\n", s); print!("{}\n", s);
assert!(false); assert!(false);
@ -21,7 +21,7 @@ mod lex_tests {
match lex(document.to_string()) { match lex(document.to_string()) {
Ok(tree) => { Ok(tree) => {
assert_eq!(ast_to_string(tree), document); assert_eq!(ast_to_string(tree), document);
}, }
Err(s) => { Err(s) => {
print!("{}\n", s); print!("{}\n", s);
assert!(false); assert!(false);
@ -35,7 +35,7 @@ mod lex_tests {
match lex(document.to_string()) { match lex(document.to_string()) {
Ok(tree) => { Ok(tree) => {
assert_eq!(ast_to_string(tree), document); assert_eq!(ast_to_string(tree), document);
}, }
Err(s) => { Err(s) => {
print!("{}\n", s); print!("{}\n", s);
assert!(false); assert!(false);
@ -51,7 +51,7 @@ mod lex_tests {
Ok(tree) => { Ok(tree) => {
print!("Bad token yielded: {}\n", ast_to_string(tree)); print!("Bad token yielded: {}\n", ast_to_string(tree));
assert!(false); assert!(false);
}, }
Err(s) => { Err(s) => {
assert_eq!(s, output.to_string()); assert_eq!(s, output.to_string());
} }
@ -64,7 +64,7 @@ mod lex_tests {
match lex(document.to_string()) { match lex(document.to_string()) {
Ok(tree) => { Ok(tree) => {
assert_eq!(ast_to_string(tree), document); assert_eq!(ast_to_string(tree), document);
}, }
Err(s) => { Err(s) => {
print!("{}\n", s); print!("{}\n", s);
assert!(false); assert!(false);
@ -78,7 +78,7 @@ mod lex_tests {
match lex(document.to_string()) { match lex(document.to_string()) {
Ok(tree) => { Ok(tree) => {
assert_eq!(ast_to_string(tree), document); assert_eq!(ast_to_string(tree), document);
}, }
Err(s) => { Err(s) => {
print!("{}\n", s); print!("{}\n", s);
assert!(false); assert!(false);
@ -94,7 +94,7 @@ mod lex_tests {
Ok(tree) => { Ok(tree) => {
print!("Bad token yielded: {}\n", ast_to_string(tree)); print!("Bad token yielded: {}\n", ast_to_string(tree));
assert!(false); assert!(false);
}, }
Err(s) => { Err(s) => {
assert_eq!(s, output.to_string()); assert_eq!(s, output.to_string());
} }
@ -109,7 +109,7 @@ mod lex_tests {
Ok(tree) => { Ok(tree) => {
print!("Bad token yielded: {}\n", ast_to_string(tree)); print!("Bad token yielded: {}\n", ast_to_string(tree));
assert!(false); assert!(false);
}, }
Err(s) => { Err(s) => {
assert_eq!(s, output.to_string()); assert_eq!(s, output.to_string());
} }
@ -123,7 +123,7 @@ mod lex_tests {
match lex(document.to_string()) { match lex(document.to_string()) {
Ok(tree) => { Ok(tree) => {
assert_eq!(ast_to_string(tree), output.to_string()); assert_eq!(ast_to_string(tree), output.to_string());
}, }
Err(s) => { Err(s) => {
print!("{}\n", s); print!("{}\n", s);
assert!(false); assert!(false);
@ -138,7 +138,7 @@ mod lex_tests {
match lex(document.to_string()) { match lex(document.to_string()) {
Ok(tree) => { Ok(tree) => {
assert_eq!(ast_to_string(tree), output.to_string()); assert_eq!(ast_to_string(tree), output.to_string());
}, }
Err(s) => { Err(s) => {
print!("{}\n", s); print!("{}\n", s);
assert!(false); assert!(false);
@ -153,7 +153,7 @@ mod lex_tests {
match lex(document.to_string()) { match lex(document.to_string()) {
Ok(tree) => { Ok(tree) => {
assert_eq!(ast_to_string(tree), output.to_string()); assert_eq!(ast_to_string(tree), output.to_string());
}, }
Err(s) => { Err(s) => {
print!("{}\n", s); print!("{}\n", s);
assert!(false); assert!(false);
@ -169,7 +169,7 @@ mod lex_tests {
Ok(tree) => { Ok(tree) => {
print!("Bad token yielded: {}\n", ast_to_string(tree)); print!("Bad token yielded: {}\n", ast_to_string(tree));
assert!(false); assert!(false);
}, }
Err(s) => { Err(s) => {
assert_eq!(s, output.to_string()); assert_eq!(s, output.to_string());
} }

View file

@ -1,8 +1,8 @@
mod append_lib_tests { mod append_lib_tests {
use relish::stdlib::{get_stdlib}; use relish::ast::{ast_to_string, eval, lex, Ctr, FTable, VTable};
use relish::ast::{lex, eval, ast_to_string, VTable, FTable, Ctr}; use relish::stdlib::get_stdlib;
use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc;
#[test] #[test]
fn test_append_to_empty_list() { fn test_append_to_empty_list() {
@ -23,28 +23,24 @@ mod append_lib_tests {
Err(s) => { Err(s) => {
println!("Couldnt lex {}: {}\n", document, s); println!("Couldnt lex {}: {}\n", document, s);
assert!(false); 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)
}
}
}
} }
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),
},
},
} }
} }
@ -67,28 +63,24 @@ mod append_lib_tests {
Err(s) => { Err(s) => {
println!("Couldnt lex {}: {}\n", document, s); println!("Couldnt lex {}: {}\n", document, s);
assert!(false); 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)
}
}
}
} }
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),
},
},
} }
} }
@ -111,28 +103,24 @@ mod append_lib_tests {
Err(s) => { Err(s) => {
println!("Couldnt lex {}: {}\n", document, s); println!("Couldnt lex {}: {}\n", document, s);
assert!(false); 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)
}
}
}
} }
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),
},
},
} }
} }
@ -155,28 +143,24 @@ mod append_lib_tests {
Err(s) => { Err(s) => {
println!("Couldnt lex {}: {}\n", document, s); println!("Couldnt lex {}: {}\n", document, s);
assert!(false); 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)
}
}
}
} }
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

@ -1,8 +1,8 @@
mod str_lib_tests { mod str_lib_tests {
use relish::stdlib::{get_stdlib}; use relish::ast::{eval, lex, Ctr, FTable, VTable};
use relish::ast::{lex, eval, VTable, FTable, Ctr}; use relish::stdlib::get_stdlib;
use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc;
#[test] #[test]
fn test_simple_concat() { fn test_simple_concat() {
@ -23,28 +23,24 @@ mod str_lib_tests {
Err(s) => { Err(s) => {
println!("Couldnt lex {}: {}\n", document, s); println!("Couldnt lex {}: {}\n", document, s);
assert!(false); 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)
}
}
}
} }
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),
},
},
} }
} }
@ -67,28 +63,24 @@ mod str_lib_tests {
Err(s) => { Err(s) => {
println!("Couldnt lex {}: {}\n", document, s); println!("Couldnt lex {}: {}\n", document, s);
assert!(false); 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)
}
}
}
} }
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),
},
},
} }
} }
@ -111,28 +103,24 @@ mod str_lib_tests {
Err(s) => { Err(s) => {
println!("Couldnt lex {}: {}\n", document, s); println!("Couldnt lex {}: {}\n", document, s);
assert!(false); 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)
}
}
}
} }
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

@ -1,8 +1,8 @@
mod var_lib_tests { mod var_lib_tests {
use relish::stdlib::{get_stdlib}; use relish::ast::{eval, lex, Ctr, FTable, VTable};
use relish::ast::{lex, eval, VTable, FTable, Ctr}; use relish::stdlib::get_stdlib;
use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc;
#[test] #[test]
fn test_variable_export_and_lookup() { fn test_variable_export_and_lookup() {
@ -24,47 +24,41 @@ mod var_lib_tests {
Err(s) => { Err(s) => {
println!("Couldnt lex {}: {}", doc1, s); println!("Couldnt lex {}: {}", doc1, s);
assert!(false); assert!(false);
}, }
Ok(tree) => { Ok(tree) => match eval(tree, vt.clone(), ft.clone(), false) {
match eval(tree, vt.clone(), ft.clone(), false) { Err(s) => {
Err(s) => { println!("Couldnt eval {}: {}", doc2, s);
println!("Couldnt eval {}: {}", doc2, s); assert!(false);
assert!(false); }
},
Ok(ctr) => { Ok(ctr) => {
println!("{:#?}", vt); println!("{:#?}", vt);
match ctr { match ctr {
Ctr::None => assert!(true), Ctr::None => assert!(true),
_ => assert!(false) _ => assert!(false),
}
} }
} }
} },
} }
match lex(doc2.to_string()) { match lex(doc2.to_string()) {
Err(s) => { Err(s) => {
println!("Couldnt lex {}: {}", doc2, s); println!("Couldnt lex {}: {}", doc2, s);
assert!(false); 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)
}
}
}
} }
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),
},
},
} }
} }
} }