added env function and test

This commit is contained in:
Ava Hahn 2023-03-06 15:50:02 -08:00
parent 79184b18ef
commit f22d807b57
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
5 changed files with 55 additions and 1 deletions

View file

@ -172,6 +172,17 @@ pub fn static_stdlib(syms: &mut SymTable) -> Result<(), String> {
},
);
syms.insert(
"env".to_string(),
Symbol {
name: String::from("env"),
args: Args::None,
conditional_branches: false,
docs: decl::ENV_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(decl::env_callback)),
},
);
Ok(())
}

View file

@ -189,3 +189,24 @@ pub fn isset_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
}
}
}
pub const ENV_DOCSTRING: &str = "takes no arguments
prints out all available symbols and their associated values";
pub fn env_callback(_ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
let mut functions = vec![];
println!("VARIABLES:");
for (name, val) in syms.iter() {
match val.value {
ValueType::VarForm(_) => {
println!(" {}: {}", &name, val.value);
},
_ => functions.push(name.clone()),
}
}
println!("FUNCTIONS:");
for func in functions {
println!(" {}", func);
}
Ok(Ctr::None)
}

View file

@ -87,6 +87,10 @@ impl SymTable {
self.0.remove(arg)
}
pub fn iter(&self) -> std::collections::hash_map::Iter<'_, String, Symbol> {
self.0.iter()
}
pub fn call_symbol(
&mut self,
name: &String,