Skip to content

Commit 1ea388f

Browse files
committed
Added (load-file ..), a relatively useful primitive
1 parent 58a1e3f commit 1ea388f

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/environment.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ impl Environment {
7878
let print_string_fn = rust_core::PrintStringFn {};
7979
let assoc_fn = rust_core::AssocFn {};
8080
let get_fn = rust_core::GetFn {};
81+
8182
// Hardcoded fns
8283
let lexical_eval_fn = Value::LexicalEvalFn {};
8384
// Hardcoded macros
@@ -88,7 +89,8 @@ impl Environment {
8889
let defmacro_macro = Value::DefmacroMacro {};
8990
let if_macro = Value::IfMacro {};
9091
let environment = Rc::new(Environment::new_main_environment());
91-
92+
93+
let load_file_fn = rust_core::LoadFileFn::new(Rc::clone(&environment));
9294
let eval_fn = rust_core::EvalFn::new(Rc::clone(&environment));
9395

9496
environment.insert(Symbol::intern("+"), add_fn.to_rc_value());
@@ -115,6 +117,7 @@ impl Environment {
115117
Symbol::intern("lexical-eval"),
116118
lexical_eval_fn.to_rc_value(),
117119
);
120+
environment.insert(Symbol::intern("load-file"), load_file_fn.to_rc_value());
118121
environment.insert(Symbol::intern("nth"), nth_fn.to_rc_value());
119122
environment.insert(Symbol::intern("assoc"), assoc_fn.to_rc_value());
120123
environment.insert(Symbol::intern("get"), get_fn.to_rc_value());

src/rust_core.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,41 @@ impl IFn for GetFn {
317317
Value::Nil
318318
}
319319
}
320+
321+
#[derive(Debug, Clone)]
322+
pub struct LoadFileFn {
323+
enclosing_environment: Rc<Environment>,
324+
}
325+
impl LoadFileFn {
326+
pub fn new(enclosing_environment: Rc<Environment>) -> LoadFileFn {
327+
LoadFileFn {
328+
enclosing_environment,
329+
}
330+
}
331+
}
332+
impl ToValue for LoadFileFn {
333+
fn to_value(&self) -> Value {
334+
Value::IFn(Rc::new(self.clone()))
335+
}
336+
}
337+
impl IFn for LoadFileFn {
338+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
339+
if args.len() != 1 {
340+
Value::Condition(format!(
341+
"Wrong number of arguments given to function (Given: {}, Expected: 1)",
342+
args.len()
343+
))
344+
} else if let Value::String(file) = &**args.get(0).unwrap() {
345+
// @TODO clean this
346+
Repl::new(Rc::clone(&self.enclosing_environment)).try_eval_file(file);
347+
//@TODO remove this placeholder value, return last value evaluated in try_eval_file
348+
Value::Nil
349+
} else {
350+
Value::Condition(format!(
351+
"Type mismatch; Expected instance of {}, Recieved type {}",
352+
TypeTag::String,
353+
args.len()
354+
))
355+
}
356+
}
357+
}

0 commit comments

Comments
 (0)