Skip to content

Commit 58a1e3f

Browse files
committed
Add get function
1 parent fe0ae54 commit 58a1e3f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/environment.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ impl Environment {
7777
let concat_fn = rust_core::ConcatFn {};
7878
let print_string_fn = rust_core::PrintStringFn {};
7979
let assoc_fn = rust_core::AssocFn {};
80+
let get_fn = rust_core::GetFn {};
8081
// Hardcoded fns
8182
let lexical_eval_fn = Value::LexicalEvalFn {};
8283
// Hardcoded macros
@@ -116,6 +117,7 @@ impl Environment {
116117
);
117118
environment.insert(Symbol::intern("nth"), nth_fn.to_rc_value());
118119
environment.insert(Symbol::intern("assoc"), assoc_fn.to_rc_value());
120+
environment.insert(Symbol::intern("get"), get_fn.to_rc_value());
119121
environment.insert(Symbol::intern("concat"), concat_fn.to_rc_value());
120122
environment.insert(
121123
Symbol::intern("print-string"),

src/rust_core.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,31 @@ impl IFn for AssocFn {
289289
Value::Nil
290290
}
291291
}
292+
293+
// General assoc fn; however, currently just implemented
294+
// for our one map type, PersistentListMap
295+
#[derive(Debug, Clone)]
296+
pub struct GetFn {}
297+
impl ToValue for GetFn {
298+
fn to_value(&self) -> Value {
299+
Value::IFn(Rc::new(self.clone()))
300+
}
301+
}
302+
impl IFn for GetFn {
303+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
304+
if args.len() != 2 {
305+
return Value::Condition(format!(
306+
"Wrong number of arguments given to function (Given: {}, Expected: 2)",
307+
args.len()
308+
));
309+
}
310+
311+
if let Value::PersistentListMap(pmap) = &*(args.get(0).unwrap().clone()) {
312+
let key = args.get(1).unwrap();
313+
return pmap.get(key).to_value();
314+
}
315+
// @TODO add error in here with erkk's new error tools
316+
317+
Value::Nil
318+
}
319+
}

0 commit comments

Comments
 (0)