File tree Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -78,6 +78,7 @@ impl Environment {
78
78
let print_string_fn = rust_core:: PrintStringFn { } ;
79
79
let assoc_fn = rust_core:: AssocFn { } ;
80
80
let get_fn = rust_core:: GetFn { } ;
81
+
81
82
// Hardcoded fns
82
83
let lexical_eval_fn = Value :: LexicalEvalFn { } ;
83
84
// Hardcoded macros
@@ -88,7 +89,8 @@ impl Environment {
88
89
let defmacro_macro = Value :: DefmacroMacro { } ;
89
90
let if_macro = Value :: IfMacro { } ;
90
91
let environment = Rc :: new ( Environment :: new_main_environment ( ) ) ;
91
-
92
+
93
+ let load_file_fn = rust_core:: LoadFileFn :: new ( Rc :: clone ( & environment) ) ;
92
94
let eval_fn = rust_core:: EvalFn :: new ( Rc :: clone ( & environment) ) ;
93
95
94
96
environment. insert ( Symbol :: intern ( "+" ) , add_fn. to_rc_value ( ) ) ;
@@ -115,6 +117,7 @@ impl Environment {
115
117
Symbol :: intern ( "lexical-eval" ) ,
116
118
lexical_eval_fn. to_rc_value ( ) ,
117
119
) ;
120
+ environment. insert ( Symbol :: intern ( "load-file" ) , load_file_fn. to_rc_value ( ) ) ;
118
121
environment. insert ( Symbol :: intern ( "nth" ) , nth_fn. to_rc_value ( ) ) ;
119
122
environment. insert ( Symbol :: intern ( "assoc" ) , assoc_fn. to_rc_value ( ) ) ;
120
123
environment. insert ( Symbol :: intern ( "get" ) , get_fn. to_rc_value ( ) ) ;
Original file line number Diff line number Diff line change @@ -317,3 +317,41 @@ impl IFn for GetFn {
317
317
Value :: Nil
318
318
}
319
319
}
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
+ }
You can’t perform that action at this time.
0 commit comments