finished inc and dec, and tests

This commit is contained in:
Ava Hahn 2023-03-07 22:16:57 -08:00
parent c74d6f5ddf
commit 8bf4b3c368
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
4 changed files with 304 additions and 17 deletions

View file

@ -201,21 +201,6 @@ Optionally return a list of new variables and/or functions?
Will need a concatenate function for tables Will need a concatenate function for tables
*** TODO Main shell calls Load function on arg and exits *** TODO Main shell calls Load function on arg and exits
*** TODO Can enter multiple lines of text, with formatting in repl *** TODO Can enter multiple lines of text, with formatting in repl
*** TODO arithmetic operations
**** DONE typecast (int)
**** DONE typecast (float)
**** DONE add
**** DONE sub
**** DONE div
**** DONE mul
**** DONE exp
**** DONE mod
***** DONE Test for Let Destructuring
**** TODO inc
**** TODO dec
**** DONE gt?
**** DONE lt?
**** DONE snippets for gte and lte
*** TODO string operations *** TODO string operations
**** TODO typecast (string) **** TODO typecast (string)
**** TODO contains **** TODO contains

View file

@ -349,6 +349,28 @@ pub fn static_stdlib(syms: &mut SymTable) -> Result<(), String> {
}, },
); );
syms.insert(
"inc".to_string(),
Symbol {
name: String::from("inc"),
args: Args::Lazy(1),
conditional_branches: true,
docs: math::INC_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::inc_callback)),
},
);
syms.insert(
"dec".to_string(),
Symbol {
name: String::from("dec"),
args: Args::Lazy(1),
conditional_branches: true,
docs: math::DEC_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::dec_callback)),
},
);
Ok(()) Ok(())
} }

View file

@ -15,7 +15,7 @@
*/ */
use crate::segment::{Ctr, Seg}; use crate::segment::{Ctr, Seg};
use crate::sym::SymTable; use crate::sym::{SymTable, ValueType};
fn isnumeric(arg: &Ctr) -> bool { fn isnumeric(arg: &Ctr) -> bool {
match arg { match arg {
@ -358,3 +358,77 @@ pub fn islte_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
Err("impossible state: islt returned non-bool".to_string()) Err("impossible state: islt returned non-bool".to_string())
} }
} }
pub const INC_DOCSTRING: &str = "Accepts a single argument, expects it to be a symbol.
The symbol is fetched from the symbol table.
If the symbol is not an integer an error is returned.
The symbol is redefined as symbol + 1.
This call is similar to the following:
(def counter '' (add counter 1))
with the caveat that your docstring is preserved.";
pub fn inc_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
let var_name: String;
if let Ctr::Symbol(ref s) = *ast.car {
var_name = s.clone();
} else {
return Err("argument should be a symbol".to_string());
}
let mut sym = syms
.remove(&var_name)
.expect(&format!("symbol {var_name} is not defined"));
if let ValueType::VarForm(ref var) = sym.value {
if let Ctr::Integer(ref b) = **var {
sym.value = ValueType::VarForm(Box::new(Ctr::Integer(b + 1)));
} else {
syms.insert(var_name, sym);
return Err("can only increment an integer".to_string());
}
} else {
syms.insert(var_name, sym);
return Err("cannot increment a function".to_string());
}
syms.insert(var_name, sym);
Ok(Ctr::None)
}
pub const DEC_DOCSTRING: &str = "Accepts a single argument, expects it to be a symbol.
The symbol is fetched from the symbol table.
If the symbol is not an integer an error is returned.
The symbol is redefined as symbol - 1.
This call is similar to the following:
(def counter '' (sub counter 1))
with the caveat that your docstring is preserved.";
pub fn dec_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
let var_name: String;
if let Ctr::Symbol(ref s) = *ast.car {
var_name = s.clone();
} else {
return Err("argument should be a symbol".to_string());
}
let mut sym = syms
.remove(&var_name)
.expect(&format!("symbol {var_name} is not defined"));
if let ValueType::VarForm(ref var) = sym.value {
if let Ctr::Integer(ref b) = **var {
sym.value = ValueType::VarForm(Box::new(Ctr::Integer(b - 1)));
} else {
syms.insert(var_name, sym);
return Err("can only decrement an integer".to_string());
}
} else {
syms.insert(var_name, sym);
return Err("cannot decrement a function".to_string());
}
syms.insert(var_name, sym);
Ok(Ctr::None)
}

View file

@ -1,5 +1,5 @@
mod math_lib_tests { mod math_lib_tests {
use relish::ast::{eval, lex, SymTable}; use relish::ast::{eval, lex, Ctr, SymTable};
use relish::stdlib::{dynamic_stdlib, static_stdlib}; use relish::stdlib::{dynamic_stdlib, static_stdlib};
#[test] #[test]
@ -608,4 +608,210 @@ mod math_lib_tests {
result.to_string(), result.to_string(),
); );
} }
#[test]
fn test_inc() {
let document = "(def tester '' 1)";
let change = "(inc tester)";
let check = "(tester)";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
let doc_tree = lex(&document.to_string()).unwrap();
let change_tree = lex(&change.to_string()).unwrap();
let check_tree = lex(&check.to_string()).unwrap();
eval(&doc_tree, &mut syms).unwrap();
eval(&change_tree, &mut syms).unwrap();
if let Ctr::Seg(ref s) = *eval(&check_tree, &mut syms).unwrap() {
if let Ctr::Integer(ref b) = *s.car {
assert_eq!(2, *b)
} else {
panic!()
}
} else {
panic!()
}
eval(&change_tree, &mut syms).unwrap();
if let Ctr::Seg(ref s) = *eval(&check_tree, &mut syms).unwrap() {
if let Ctr::Integer(ref b) = *s.car {
assert_eq!(3, *b)
} else {
panic!()
}
} else {
panic!()
}
}
#[test]
fn test_inc_errors_dont_lose_vars() {
let document = "(def tester '' 'oops')";
let change = "(inc tester)";
let check = "(tester)";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
let doc_tree = lex(&document.to_string()).unwrap();
let change_tree = lex(&change.to_string()).unwrap();
let check_tree = lex(&check.to_string()).unwrap();
eval(&doc_tree, &mut syms).unwrap();
if let Err(s) = eval(&change_tree, &mut syms) {
assert_eq!(
s,
"error in call to inc: can only increment an integer".to_string()
);
let intermediate = *eval(&check_tree, &mut syms).unwrap();
if let Ctr::Seg(ref s) = intermediate {
assert_eq!(s.to_string(), "('oops')".to_string());
} else {
eprintln!("did not expect: {}", intermediate);
panic!()
}
} else {
eprintln!("shouldn't have succeeded!");
panic!()
}
}
#[test]
fn test_inc_errors_dont_lose_funcs() {
let document = "(def tester '' (oops) oops)";
let change = "(inc tester)";
let check = "(tester '1')";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
let doc_tree = lex(&document.to_string()).unwrap();
let change_tree = lex(&change.to_string()).unwrap();
let check_tree = lex(&check.to_string()).unwrap();
eval(&doc_tree, &mut syms).unwrap();
if let Err(s) = eval(&change_tree, &mut syms) {
assert_eq!(
s,
"error in call to inc: cannot increment a function".to_string()
);
if let Ctr::String(ref s) = *eval(&check_tree, &mut syms).unwrap() {
assert_eq!(*s, "1".to_string());
} else {
panic!()
}
} else {
eprintln!("shouldn't have succeeded!");
panic!()
}
}
#[test]
fn test_dec() {
let document = "(def tester '' 1)";
let change = "(dec tester)";
let check = "(tester)";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
let doc_tree = lex(&document.to_string()).unwrap();
let change_tree = lex(&change.to_string()).unwrap();
let check_tree = lex(&check.to_string()).unwrap();
eval(&doc_tree, &mut syms).unwrap();
eval(&change_tree, &mut syms).unwrap();
if let Ctr::Seg(ref s) = *eval(&check_tree, &mut syms).unwrap() {
if let Ctr::Integer(ref b) = *s.car {
assert_eq!(0, *b)
} else {
panic!()
}
} else {
panic!()
}
eval(&change_tree, &mut syms).unwrap();
if let Ctr::Seg(ref s) = *eval(&check_tree, &mut syms).unwrap() {
if let Ctr::Integer(ref b) = *s.car {
assert_eq!(-1, *b)
} else {
panic!()
}
} else {
panic!()
}
}
#[test]
fn test_dec_errors_dont_lose_vars() {
let document = "(def tester '' 'oops')";
let change = "(dec tester)";
let check = "(tester)";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
let doc_tree = lex(&document.to_string()).unwrap();
let change_tree = lex(&change.to_string()).unwrap();
let check_tree = lex(&check.to_string()).unwrap();
eval(&doc_tree, &mut syms).unwrap();
if let Err(s) = eval(&change_tree, &mut syms) {
assert_eq!(
s,
"error in call to dec: can only decrement an integer".to_string()
);
let intermediate = *eval(&check_tree, &mut syms).unwrap();
if let Ctr::Seg(ref s) = intermediate {
assert_eq!(s.to_string(), "('oops')".to_string());
} else {
eprintln!("did not expect: {}", intermediate);
panic!()
}
} else {
eprintln!("shouldn't have succeeded!");
panic!()
}
}
#[test]
fn test_dec_errors_dont_lose_funcs() {
let document = "(def tester '' (oops) oops)";
let change = "(dec tester)";
let check = "(tester '1')";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
let doc_tree = lex(&document.to_string()).unwrap();
let change_tree = lex(&change.to_string()).unwrap();
let check_tree = lex(&check.to_string()).unwrap();
eval(&doc_tree, &mut syms).unwrap();
if let Err(s) = eval(&change_tree, &mut syms) {
assert_eq!(
s,
"error in call to dec: cannot decrement a function".to_string()
);
if let Ctr::String(ref s) = *eval(&check_tree, &mut syms).unwrap() {
assert_eq!(*s, "1".to_string());
} else {
panic!()
}
} else {
eprintln!("shouldn't have succeeded!");
panic!()
}
}
} }