add isset

Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
Ava Hahn 2023-03-06 15:25:23 -08:00
parent de29bbf950
commit 8d6915f69a
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
2 changed files with 57 additions and 0 deletions

View file

@ -162,6 +162,17 @@ pub fn static_stdlib(syms: &mut SymTable) -> Result<(), String> {
}, },
); );
syms.insert(
"set?".to_string(),
Symbol {
name: String::from("set?"),
args: Args::Strict(vec![Type::Symbol]),
conditional_branches: true,
docs: ISSET_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(_isset_callback)),
},
);
Ok(()) Ok(())
} }
@ -356,3 +367,22 @@ fn _store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr,
} }
Ok(Ctr::None) Ok(Ctr::None)
} }
pub const ISSET_DOCSTRING: &str = "accepts a single argument: a symbol.
returns true or false according to whether or not the symbol is found in the symbol table.";
fn _isset_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
if ast.len() != 1 {
Err("help only takes a single argument".to_string())
} else {
if let Ctr::Symbol(ref symbol) = *ast.car {
if let Some(_) = syms.get(symbol) {
Ok(Ctr::Bool(true))
} else {
Ok(Ctr::Bool(false))
}
} else {
Err("help should only be called on a symbol".to_string())
}
}
}

View file

@ -222,4 +222,31 @@ mod var_lib_tests {
assert!(false); assert!(false);
} }
} }
#[test]
fn test_isset_true() {
let doc1 = "(def test '' 1)";
let doc2 = "(set? test)";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
let def_tree = lex(&doc1.to_string()).unwrap();
let set_tree = lex(&doc2.to_string()).unwrap();
eval(&def_tree, &mut syms).unwrap();
if let Ctr::Bool(b) = *eval(&set_tree, &mut syms).unwrap() {
assert!(b);
}
}
#[test]
fn test_isset_false() {
let doc = "(set? test)";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
let set_tree = lex(&doc.to_string()).unwrap();
if let Ctr::Bool(b) = *eval(&set_tree, &mut syms).unwrap() {
assert!(!b);
}
}
} }