File tree Expand file tree Collapse file tree 3 files changed +57
-4
lines changed Expand file tree Collapse file tree 3 files changed +57
-4
lines changed Original file line number Diff line number Diff line change 1+ (defn hello-world []
2+ (println " Hello, world" ))
3+
4+ (hello-world )
Original file line number Diff line number Diff line change @@ -23,11 +23,26 @@ mod symbol;
2323mod type_tag;
2424mod util;
2525mod value;
26+ mod user_action;
2627
2728fn main ( ) {
28- //
29- // Start repl
30- //
29+ let cli_args : user_action :: Action = user_action :: parse_args ( ) ;
30+
31+ // instantiate the core environment
3132 let repl = repl:: Repl :: default ( ) ;
32- repl. run ( ) ;
33+
34+ match cli_args {
35+ // eval the file/script
36+ user_action:: Action :: RunScript ( script) => {
37+ println ! ( "{:?}" , repl:: Repl :: try_eval_file( & repl, script. as_str( ) ) ) ;
38+ } ,
39+
40+ // eval the expression
41+ user_action:: Action :: Evaluate ( expression) => {
42+ println ! ( "{:?}" , repl:: Repl :: eval( & repl, & repl:: Repl :: read_string( & expression) ) ) ;
43+ } ,
44+
45+ // Start repl
46+ user_action:: Action :: Nothing => { repl. run ( ) ; }
47+ }
3348}
Original file line number Diff line number Diff line change 1+ use std:: env;
2+ use std:: fmt;
3+
4+ pub enum Action {
5+ RunScript ( String ) ,
6+ Evaluate ( String ) ,
7+ Nothing ,
8+ }
9+
10+ impl fmt:: Display for Action {
11+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
12+ match & * self {
13+ Action :: RunScript ( filepath) => write ! ( f, "RunScript" ) ,
14+ Action :: Evaluate ( expression) => write ! ( f, "Evaluate" ) ,
15+ Action :: Nothing => write ! ( f, "Nothing" ) ,
16+ }
17+ }
18+ }
19+
20+ pub fn parse_args ( ) -> Action {
21+
22+ let arguments: Vec < String > = env:: args ( ) . collect ( ) ;
23+
24+ if arguments. len ( ) == 3 {
25+ if arguments[ 1 ] == "-i" || arguments[ 1 ] == "--init" {
26+ return Action :: RunScript ( arguments[ 2 ] . clone ( ) ) ;
27+ } else if arguments[ 1 ] == "-e" || arguments[ 1 ] == "--eval" {
28+ return Action :: Evaluate ( arguments[ 2 ] . clone ( ) ) ;
29+ } else { return Action :: Nothing ; }
30+ } else {
31+ return Action :: Nothing ;
32+ }
33+ }
34+
You can’t perform that action at this time.
0 commit comments