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

@ -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(())
}

View file

@ -15,7 +15,7 @@
*/
use crate::segment::{Ctr, Seg};
use crate::sym::SymTable;
use crate::sym::{SymTable, ValueType};
fn isnumeric(arg: &Ctr) -> bool {
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())
}
}
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)
}