Skip to content

Commit 6fc04e0

Browse files
authored
Merge pull request #45 from zackteo/feature/run-clj-file-as-script
Run clj file as script & evaluate expression
2 parents c272638 + ee27d1a commit 6fc04e0

File tree

5 files changed

+119
-6
lines changed

5 files changed

+119
-6
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( std::env::args().collect() );
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::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/repl.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use std::io::Write;
66

77
use crate::environment::Environment;
88
use crate::reader;
9-
use crate::value::Evaluable;
10-
use crate::value::Value;
9+
use crate::value::{Value,Evaluable,ToValue};
1110
use std::rc::Rc;
1211

1312
pub struct Repl {
@@ -84,6 +83,9 @@ impl Repl {
8483
last_val = Repl::read(&mut reader);
8584
}
8685
}
86+
pub fn eval_file(&self, filepath: &str) -> Value {
87+
self.try_eval_file(filepath).to_value()
88+
}
8789
}
8890

8991
impl Default for Repl {

src/user_action.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use std::fmt;
2+
3+
#[derive(PartialEq,Eq)]
4+
pub enum Action{
5+
RunScript(String),
6+
Evaluate(String),
7+
Nothing,
8+
}
9+
10+
impl fmt::Debug for Action {
11+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
12+
match &*self {
13+
Action::RunScript(filepath) => write!(f, "RunScript: {}", filepath),
14+
Action::Evaluate(expression) => write!(f, "Evaluate: {}", expression),
15+
Action::Nothing => write!(f, "Nothing"),
16+
}
17+
}
18+
}
19+
20+
pub fn parse_args(arguments: Vec<String>) -> Action{
21+
22+
if arguments.len() >= 2 {
23+
if arguments[1] == "-i" || arguments[1] == "--init" {
24+
return Action::RunScript(arguments[2].clone());
25+
}else if arguments[1] == "-e" || arguments[1] == "--eval" {
26+
return Action::Evaluate(arguments[2].clone());
27+
}else { Action::RunScript(arguments[1].clone()) } // for path as argument
28+
}else {
29+
return Action::Nothing;
30+
}
31+
}
32+
33+
#[cfg(test)]
34+
mod tests {
35+
mod parse_args_test {
36+
use crate::user_action;
37+
38+
#[test]
39+
fn parses_args_given_path() {
40+
let arguments = vec!["target/debug/rust_clojure".to_string(), "examples/hello_world.clj".to_string()];
41+
assert_eq!(user_action::Action::RunScript("examples/hello_world.clj".to_string()),
42+
user_action::parse_args(arguments));
43+
}
44+
45+
#[test]
46+
fn parses_args_given_i() {
47+
let arguments = vec!["target/debug/rust_clojure".to_string(), "-i".to_string(), "test.clj".to_string()];
48+
49+
assert_eq!(user_action::Action::RunScript("test.clj".to_string()),
50+
user_action::parse_args(arguments));
51+
}
52+
53+
#[test]
54+
fn parses_args_given_init() {
55+
let arguments = vec!["target/debug/rust_clojure".to_string(), "--init".to_string(), "testing.clj".to_string()];
56+
57+
assert_eq!(user_action::Action::RunScript("testing.clj".to_string()),
58+
user_action::parse_args(arguments));
59+
}
60+
61+
#[test]
62+
fn parses_args_given_e() {
63+
let arguments = vec!["target/debug/rust_clojure".to_string(), "-e".to_string(), "(+ 1 2 3)".to_string()];
64+
65+
assert_eq!(user_action::Action::Evaluate("(+ 1 2 3)".to_string()),
66+
user_action::parse_args(arguments));
67+
}
68+
69+
#[test]
70+
fn parses_args_given_eval() {
71+
let arguments = vec!["target/debug/rust_clojure".to_string(), "--eval".to_string(), "(println \"eh\")".to_string()];
72+
73+
assert_eq!(user_action::Action::Evaluate("(println \"eh\")".to_string()),
74+
user_action::parse_args(arguments));
75+
}
76+
77+
#[test]
78+
fn parses_args_given_nil() {
79+
assert_eq!(user_action::Action::Nothing, user_action::parse_args(vec!["target/debug/rust_clojure".to_string()]));
80+
}
81+
}
82+
}
83+

src/value.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::fmt::Display;
12
use crate::environment::Environment;
23
use crate::ifn::IFn;
34
use crate::keyword::Keyword;
@@ -611,6 +612,14 @@ impl ToValue for PersistentListMap {
611612
Value::PersistentListMap(self.clone())
612613
}
613614
}
615+
impl<T: Display> ToValue for Result<Value,T> {
616+
fn to_value(&self) -> Value {
617+
match self {
618+
Ok(val) => val.clone(),
619+
Err(err) => Value::Condition(err.to_string())
620+
}
621+
}
622+
}
614623

615624
/// Allows a type to be evaluated, abstracts evaluation
616625
///

0 commit comments

Comments
 (0)