Skip to content

Commit a2c552e

Browse files
zackteoTko1
authored andcommitted
Run clj file as script & evaluate expression
Hardcoding based on arguments.len() == 3 for now Examples: --init/-i for script or --eval/-e for evaluate expression cargo run -- --init examples/hello_world.clj cargo run -- --eval "(println \"eh\")" cargo run -- --eval "(+ 1 2)"
1 parent c272638 commit a2c552e

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

examples/hello_world.clj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(defn hello-world[]
2+
(println "Hello, world"))
3+
4+
(hello-world)

src/main.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,26 @@ mod symbol;
2323
mod type_tag;
2424
mod util;
2525
mod value;
26+
mod user_action;
2627

2728
fn 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
}

src/user_action.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+

0 commit comments

Comments
 (0)