implement basic control flow, error handling from functions, many tests

Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
Ava Hahn 2023-02-27 22:53:54 -08:00
parent ae365ad63c
commit 09e3546ba6
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
14 changed files with 315 additions and 488 deletions

View file

@ -1,62 +0,0 @@
/* relish: versatile lisp shell
* Copyright (C) 2021 Aidan Hahn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::append::get_append;
use crate::func::{func_declare, FTable};
use crate::segment::Ctr;
use crate::str::{get_concat, get_echo};
use crate::control::{get_if};
use crate::vars::{get_export, VTable};
use std::cell::RefCell;
use std::rc::Rc;
pub fn get_stdlib(conf: Rc<RefCell<VTable>>) -> Result<Rc<RefCell<FTable>>, String> {
let ft = Rc::new(RefCell::new(FTable::new()));
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_echo()))) {
return Err(s);
}
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_append()))) {
return Err(s);
}
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_concat()))) {
return Err(s);
}
let mut cfg_env = true;
match conf.borrow().get(&String::from("CFG_RELISH_ENV")) {
None => {
println!("CFG_RELISH_ENV not defined. defaulting to ON.")
}
Some(ctr) => match (**ctr).clone() {
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");
}
},
}
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_export(cfg_env)))) {
return Err(s);
}
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_if()))) {
return Err(s);
}
return Ok(ft);
}

View file

@ -1,4 +1,5 @@
mod func_tests {
use std::rc::Rc;
use relish::ast::lex;
use relish::ast::{SymTable, Type, UserFn};
use relish::ast::{Args, Symbol, Ctr, Seg, ValueType};
@ -10,14 +11,14 @@ mod func_tests {
name: String::from("test_func_in"),
conditional_branches: false,
args: Args::Strict(vec![Type::Bool]),
value: ValueType::Internal(Box::new(
|a: &Seg, _: &mut SymTable| -> Ctr {
value: ValueType::Internal(Rc::new(
|a: &Seg, _: &mut SymTable| -> Result<Ctr, String> {
let inner = a;
let mut is_bool = false;
if let Ctr::Bool(_) = *inner.car {
is_bool = true;
}
Ctr::Bool(is_bool)
Ok(Ctr::Bool(is_bool))
},
)),
};
@ -123,17 +124,17 @@ mod func_tests {
name: String::from("test_inner"),
conditional_branches: false,
args: Args::Strict(vec![Type::Bool]),
value: ValueType::Internal(Box::new(
|a: &Seg, _: &mut SymTable| -> Ctr {
value: ValueType::Internal(Rc::new(
|a: &Seg, _: &mut SymTable| -> Result<Ctr, String> {
let inner = a;
if let Ctr::Bool(b) = *inner.car {
if b {
Ctr::String("test".to_string())
Ok(Ctr::String("test".to_string()))
} else {
Ctr::None
Ok(Ctr::None)
}
} else {
Ctr::None
Err("not a bool".to_string())
}
},
)),
@ -184,14 +185,14 @@ mod func_tests {
name: String::from("test_func_in"),
conditional_branches: false,
args: Args::Strict(vec![Type::Bool]),
value: ValueType::Internal(Box::new(
|a: &Seg, _: &mut SymTable| -> Ctr {
value: ValueType::Internal(Rc::new(
|a: &Seg, _: &mut SymTable| -> Result<Ctr, String> {
let inner = a;
let mut is_bool = false;
if let Ctr::Bool(_) = *inner.car {
is_bool = true;
}
Ctr::Bool(is_bool)
Ok(Ctr::Bool(is_bool))
},
)),
};
@ -279,14 +280,14 @@ mod func_tests {
name: String::from("test_func_in"),
conditional_branches: false,
args: Args::Strict(vec![Type::Bool]),
value: ValueType::Internal(Box::new(
|a: &Seg, _: &mut SymTable| -> Ctr {
value: ValueType::Internal(Rc::new(
|a: &Seg, _: &mut SymTable| -> Result<Ctr, String> {
let inner = a;
let mut is_bool = false;
if let Ctr::Bool(_) = *inner.car {
is_bool = true;
}
Ctr::Bool(is_bool)
Ok(Ctr::Bool(is_bool))
},
)),
};

View file

@ -1,46 +1,22 @@
mod append_lib_tests {
use relish::ast::{ast_to_string, eval, lex, Ctr, FTable, VTable};
use relish::stdlib::get_stdlib;
use std::cell::RefCell;
use std::rc::Rc;
use relish::ast::{Ctr, eval, lex, SymTable};
use relish::stdlib::{static_stdlib, dynamic_stdlib};
#[test]
fn test_append_to_empty_list() {
let document = "(append () 1 2 3)";
let result = "(1 2 3)";
let vt = Rc::new(RefCell::new(VTable::new()));
let ft: Rc<RefCell<FTable>>;
match get_stdlib(vt.clone()) {
Ok(f) => ft = f,
Err(s) => {
ft = Rc::new(RefCell::new(FTable::new()));
println!("Couldnt get stdlib: {}!", s);
assert!(false)
let document = "(append () 1)";
let result = "(1)";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(false, &mut syms).unwrap();
if let Ok(tree) = lex(&document.to_string()) {
if let Ctr::Seg(ref s) = *eval(&tree, &mut syms).unwrap() {
assert_eq!(s.to_string(), result);
}
}
match lex(document.to_string()) {
Err(s) => {
println!("Couldnt lex {}: {}\n", document, s);
assert!(false);
}
Ok(tree) => match eval(tree, vt.clone(), ft.clone(), false) {
Err(s) => {
println!("Couldnt eval {}: {}\n", document, s);
assert!(false);
}
Ok(ctr) => match ctr {
Ctr::Symbol(_) => assert!(false),
Ctr::String(_) => assert!(false),
Ctr::Integer(_) => assert!(false),
Ctr::Float(_) => assert!(false),
Ctr::Bool(_) => assert!(false),
Ctr::Seg(s) => assert_eq!(ast_to_string(s), result),
Ctr::None => assert!(false),
},
},
} else {
assert!(false)
}
}
@ -48,79 +24,35 @@ mod append_lib_tests {
fn test_append_to_full_list() {
let document = "(append (1 2) 3)";
let result = "(1 2 3)";
let vt = Rc::new(RefCell::new(VTable::new()));
let ft: Rc<RefCell<FTable>>;
match get_stdlib(vt.clone()) {
Ok(f) => ft = f,
Err(s) => {
ft = Rc::new(RefCell::new(FTable::new()));
println!("Couldnt get stdlib: {}!", s);
assert!(false)
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(false, &mut syms).unwrap();
if let Ok(tree) = lex(&document.to_string()) {
if let Ctr::Seg(ref s) = *eval(&tree, &mut syms).unwrap() {
assert_eq!(s.to_string(), result);
}
}
match lex(document.to_string()) {
Err(s) => {
println!("Couldnt lex {}: {}\n", document, s);
assert!(false);
}
Ok(tree) => match eval(tree, vt.clone(), ft.clone(), false) {
Err(s) => {
println!("Couldnt eval {}: {}\n", document, s);
assert!(false);
}
Ok(ctr) => match ctr {
Ctr::Symbol(_) => assert!(false),
Ctr::String(_) => assert!(false),
Ctr::Integer(_) => assert!(false),
Ctr::Float(_) => assert!(false),
Ctr::Bool(_) => assert!(false),
Ctr::Seg(s) => assert_eq!(ast_to_string(s), result),
Ctr::None => assert!(false),
},
},
} else {
assert!(false);
}
}
#[test]
fn test_mono_append() {
let document = "(append)";
let result = "()";
let vt = Rc::new(RefCell::new(VTable::new()));
let ft: Rc<RefCell<FTable>>;
match get_stdlib(vt.clone()) {
Ok(f) => ft = f,
Err(s) => {
ft = Rc::new(RefCell::new(FTable::new()));
println!("Couldnt get stdlib: {}!", s);
assert!(false)
let result = "(<nil>)";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(false, &mut syms).unwrap();
if let Ok(tree) = lex(&document.to_string()) {
if let Ctr::Seg(ref s) = *eval(&tree, &mut syms).unwrap() {
assert_eq!(s.to_string(), result);
}
}
match lex(document.to_string()) {
Err(s) => {
println!("Couldnt lex {}: {}\n", document, s);
assert!(false);
}
Ok(tree) => match eval(tree, vt.clone(), ft.clone(), false) {
Err(s) => {
println!("Couldnt eval {}: {}\n", document, s);
assert!(false);
}
Ok(ctr) => match ctr {
Ctr::Symbol(_) => assert!(false),
Ctr::String(_) => assert!(false),
Ctr::Integer(_) => assert!(false),
Ctr::Float(_) => assert!(false),
Ctr::Bool(_) => assert!(false),
Ctr::Seg(s) => assert_eq!(ast_to_string(s), result),
Ctr::None => assert!(false),
},
},
} else {
assert!(false);
}
}
@ -128,39 +60,17 @@ mod append_lib_tests {
fn test_append_no_list() {
let document = "(append 'test' 1 2 3)";
let result = "('test' 1 2 3)";
let vt = Rc::new(RefCell::new(VTable::new()));
let ft: Rc<RefCell<FTable>>;
match get_stdlib(vt.clone()) {
Ok(f) => ft = f,
Err(s) => {
ft = Rc::new(RefCell::new(FTable::new()));
println!("Couldnt get stdlib: {}!", s);
assert!(false)
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(false, &mut syms).unwrap();
if let Ok(tree) = lex(&document.to_string()) {
if let Ctr::Seg(ref s) = *eval(&tree, &mut syms).unwrap() {
assert_eq!(s.to_string(), result);
}
}
match lex(document.to_string()) {
Err(s) => {
println!("Couldnt lex {}: {}\n", document, s);
assert!(false);
}
Ok(tree) => match eval(tree, vt.clone(), ft.clone(), false) {
Err(s) => {
println!("Couldnt eval {}: {}\n", document, s);
assert!(false);
}
Ok(ctr) => match ctr {
Ctr::Symbol(_) => assert!(false),
Ctr::String(_) => assert!(false),
Ctr::Integer(_) => assert!(false),
Ctr::Float(_) => assert!(false),
Ctr::Bool(_) => assert!(false),
Ctr::Seg(s) => assert_eq!(ast_to_string(s), result),
Ctr::None => assert!(false),
},
},
} else {
assert!(false);
}
}
}

64
tests/test_lib_control.rs Normal file
View file

@ -0,0 +1,64 @@
mod control_lib_tests {
use relish::ast::{Ctr, eval, lex, SymTable};
use relish::stdlib::{static_stdlib, dynamic_stdlib};
#[test]
fn test_if_first_case_singlet() {
let document = "(if true 1 2)";
let result = 1;
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(false, &mut syms).unwrap();
if let Ok(tree) = lex(&document.to_string()) {
if let Ctr::Integer(i) = *eval(&tree, &mut syms).unwrap() {
assert_eq!(i, result);
} else {
assert!(false);
}
} else {
assert!(false);
}
}
#[test]
fn test_if_second_case_singlet() {
let document = "(if false 1 2)";
let result = 2;
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(false, &mut syms).unwrap();
if let Ok(tree) = lex(&document.to_string()) {
if let Ctr::Integer(i) = *eval(&tree, &mut syms).unwrap() {
assert_eq!(i, result);
} else {
assert!(false);
}
} else {
assert!(false);
}
}
#[test]
fn test_complex_case_call() {
let document = "(if true (append () 1) 2)";
let result = "(1)";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(false, &mut syms).unwrap();
if let Ok(tree) = lex(&document.to_string()) {
if let Ctr::Seg(ref i) = *eval(&tree, &mut syms).unwrap() {
assert_eq!(i.to_string(), result);
} else {
assert!(false);
}
} else {
assert!(false);
}
}
}

View file

@ -1,4 +1,4 @@
mod str_lib_tests {
/*mod str_lib_tests {
use relish::ast::{eval, lex, Ctr, FTable, VTable};
use relish::stdlib::get_stdlib;
use std::cell::RefCell;
@ -124,3 +124,4 @@ mod str_lib_tests {
}
}
}
*/

View file

@ -1,62 +1,35 @@
/*mod var_lib_tests {
use relish::ast::{eval, lex, SYM_TABLE};
use relish::ast::{Args, Symbol, Ctr, Seg, ValueType, UserFn};
mod var_lib_tests {
use relish::ast::{eval, lex, SymTable, Ctr};
use relish::stdlib::{static_stdlib, dynamic_stdlib};
#[test]
fn test_variable_export_and_lookup() {
let doc1 = "(export test 1)";
let doc2 = "(concat test)";
let result = "1";
let doc2 = "test";
let result = 1;
match get_stdlib(vt.clone()) {
Ok(f) => ft = f,
Err(s) => {
ft = Rc::new(RefCell::new(FTable::new()));
println!("Couldnt get stdlib: {}!", s);
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(false, &mut syms).unwrap();
if let Ok(tree) = lex(&doc1.to_string()) {
if let Ctr::None = *eval(&tree, &mut syms).unwrap() {
// pass
} else {
assert!(false);
}
} else {
assert!(false);
}
match lex(doc1.to_string()) {
Err(s) => {
println!("Couldnt lex {}: {}", doc1, s);
if let Ok(tree) = lex(&doc2.to_string()) {
if let Ctr::Integer(i) = *eval(&tree, &mut syms).unwrap() {
assert_eq!(i, result);
} else {
assert!(false);
}
Ok(tree) => match eval(tree, vt.clone(), ft.clone(), false) {
Err(s) => {
println!("Couldnt eval {}: {}", doc2, s);
assert!(false);
}
Ok(ctr) => {
println!("{:#?}", vt);
match ctr {
Ctr::None => assert!(true),
_ => assert!(false),
}
}
},
}
match lex(doc2.to_string()) {
Err(s) => {
println!("Couldnt lex {}: {}", doc2, s);
assert!(false);
}
Ok(tree) => match eval(tree, vt.clone(), ft.clone(), false) {
Err(s) => {
println!("Couldnt eval {}: {}", doc2, s);
assert!(false);
}
Ok(ctr) => match ctr {
Ctr::String(s) => assert_eq!(s, result),
_ => assert!(false),
},
},
} else {
assert!(false);
}
}
}
*/