Skip to content

Commit ee27d1a

Browse files
committed
Added 'eval_file' (in place of try_eval_file) to command line eval file (and
fixed some formatting)
1 parent 24e1b69 commit ee27d1a

File tree

4 files changed

+77
-67
lines changed

4 files changed

+77
-67
lines changed

src/main.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ fn main() {
3232
let repl = repl::Repl::default();
3333

3434
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-
},
35+
// eval the file/script
36+
user_action::Action::RunScript(script) => {
37+
println!("{}", repl::Repl::eval_file(&repl, script.as_str()));
38+
},
3939

40-
// eval the expression
41-
user_action::Action::Evaluate(expression) => {
42-
println!("{}", repl::Repl::eval(&repl, &repl::Repl::read_string(&expression)));
43-
},
40+
// eval the expression
41+
user_action::Action::Evaluate(expression) => {
42+
println!("{}", repl::Repl::eval(&repl, &repl::Repl::read_string(&expression)));
43+
},
4444

45-
// Start repl
46-
user_action::Action::Nothing => { repl.run(); }
45+
// Start repl
46+
user_action::Action::Nothing => { repl.run(); }
4747
}
4848
}

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: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,81 +4,80 @@ use std::fmt;
44
pub enum Action{
55
RunScript(String),
66
Evaluate(String),
7-
Nothing,
7+
Nothing,
88
}
99

1010
impl fmt::Debug for Action {
1111
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-
}
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+
}
1717
}
1818
}
1919

2020
pub fn parse_args(arguments: Vec<String>) -> Action{
21-
21+
2222
if arguments.len() >= 2 {
23-
if arguments[1] == "-i" || arguments[1] == "--init" {
24-
return Action::RunScript(String::from(&arguments[2]));
25-
}else if arguments[1] == "-e" || arguments[1] == "--eval" {
26-
return Action::Evaluate(String::from(&arguments[2]));
27-
}else { Action::RunScript(String::from(&arguments[1])) } // for path as argument
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
2828
}else {
29-
return Action::Nothing;
30-
}
29+
return Action::Nothing;
30+
}
3131
}
32-
32+
3333
#[cfg(test)]
3434
mod tests {
3535
mod parse_args_test {
36-
use crate::user_action;
36+
use crate::user_action;
3737

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-
42-
assert_eq!(user_action::Action::RunScript("examples/hello_world.clj".to_string()),
43-
user_action::parse_args(arguments));
44-
}
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+
}
4544

46-
#[test]
47-
fn parses_args_given_i() {
48-
let arguments = vec!["target/debug/rust_clojure".to_string(), "-i".to_string(), "test.clj".to_string()];
49-
50-
assert_eq!(user_action::Action::RunScript("test.clj".to_string()),
51-
user_action::parse_args(arguments));
52-
}
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+
}
5352

54-
#[test]
55-
fn parses_args_given_init() {
56-
let arguments = vec!["target/debug/rust_clojure".to_string(), "--init".to_string(), "testing.clj".to_string()];
57-
58-
assert_eq!(user_action::Action::RunScript("testing.clj".to_string()),
59-
user_action::parse_args(arguments));
60-
}
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+
}
6160

62-
#[test]
63-
fn parses_args_given_e() {
64-
let arguments = vec!["target/debug/rust_clojure".to_string(), "-e".to_string(), "(+ 1 2 3)".to_string()];
65-
66-
assert_eq!(user_action::Action::Evaluate("(+ 1 2 3)".to_string()),
67-
user_action::parse_args(arguments));
68-
}
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+
}
6968

70-
#[test]
71-
fn parses_args_given_eval() {
72-
let arguments = vec!["target/debug/rust_clojure".to_string(), "--eval".to_string(), "(println \"eh\")".to_string()];
73-
74-
assert_eq!(user_action::Action::Evaluate("(println \"eh\")".to_string()),
75-
user_action::parse_args(arguments));
76-
}
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+
}
7776

78-
#[test]
79-
fn parses_args_given_nil() {
80-
assert_eq!(user_action::Action::Nothing, user_action::parse_args(vec!["target/debug/rust_clojure".to_string()]));
81-
}
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+
}
8281
}
8382
}
8483

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)