diff options
author | NerdyPepper <[email protected]> | 2019-03-28 02:40:29 +0000 |
---|---|---|
committer | NerdyPepper <[email protected]> | 2019-03-28 02:40:29 +0000 |
commit | b717e52505c4a36424f118073c856f6e565cd7d4 (patch) | |
tree | 3472c7f0ec995b33aa26bc0eb0b421b8b4de7b58 /src | |
parent | 308aac0af7aa82ea64d8808c022efaa7f4b5d6cb (diff) |
feat: run eva in command mode
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 83 |
1 files changed, 49 insertions, 34 deletions
diff --git a/src/main.rs b/src/main.rs index 4e9a4e9..37b8d04 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,4 +1,5 @@ | |||
1 | use std::f64; | 1 | use std::f64; |
2 | use std::env; | ||
2 | 3 | ||
3 | mod lex; | 4 | mod lex; |
4 | use crate::lex::*; | 5 | use crate::lex::*; |
@@ -11,46 +12,60 @@ use crate::error::{ CalcError, handler }; | |||
11 | 12 | ||
12 | use rustyline::error::ReadlineError; | 13 | use rustyline::error::ReadlineError; |
13 | use rustyline::Editor; | 14 | use rustyline::Editor; |
14 | use rustyline::config::{ Config, Builder, ColorMode, EditMode }; | 15 | use rustyline::config::{ Builder, ColorMode, EditMode }; |
16 | |||
15 | 17 | ||
16 | fn main() { | 18 | fn main() { |
17 | let config_builder = Builder::new(); | 19 | let args: Vec<String> = env::args().collect(); |
18 | let config = config_builder.color_mode(ColorMode::Enabled) | 20 | if args.len() > 1 { |
19 | .edit_mode(EditMode::Emacs) | 21 | let mut expr = String::new(); |
20 | .history_ignore_space(true) | 22 | for arg in args[1..].iter() { |
21 | .max_history_size(1000) | 23 | expr.push_str(&arg[..]); |
22 | .build(); | 24 | } |
23 | let mut rl = Editor::<()>::with_config(config); | 25 | let evaled = eval_math_expression(&expr[..]); |
24 | if rl.load_history("history.txt").is_err() { | 26 | match evaled { |
25 | println!("No previous history."); | 27 | Ok(ans) => println!("{}", ans), |
26 | } | 28 | Err(e) => handler(e), |
29 | }; | ||
30 | } else { | ||
31 | let config_builder = Builder::new(); | ||
32 | let config = config_builder.color_mode(ColorMode::Enabled) | ||
33 | .edit_mode(EditMode::Emacs) | ||
34 | .history_ignore_space(true) | ||
35 | .max_history_size(1000) | ||
36 | .build(); | ||
37 | let mut rl = Editor::<()>::with_config(config); | ||
38 | if rl.load_history("history.txt").is_err() { | ||
39 | println!("No previous history."); | ||
40 | } | ||
27 | 41 | ||
28 | loop { | 42 | loop { |
29 | let readline = rl.readline("> "); | 43 | let readline = rl.readline("> "); |
30 | match readline { | 44 | match readline { |
31 | Ok(line) => { | 45 | Ok(line) => { |
32 | rl.add_history_entry(line.as_ref()); | 46 | rl.add_history_entry(line.as_ref()); |
33 | let evaled = eval_math_expression(&line[..]); | 47 | let evaled = eval_math_expression(&line[..]); |
34 | match evaled { | 48 | match evaled { |
35 | Ok(ans) => println!("{}", ans), | 49 | Ok(ans) => println!("{}", ans), |
36 | Err(e) => handler(e), | 50 | Err(e) => handler(e), |
37 | }; | 51 | }; |
38 | }, | 52 | }, |
39 | Err(ReadlineError::Interrupted) => { | 53 | Err(ReadlineError::Interrupted) => { |
40 | println!("CTRL-C"); | 54 | println!("CTRL-C"); |
41 | break | 55 | break |
42 | }, | 56 | }, |
43 | Err(ReadlineError::Eof) => { | 57 | Err(ReadlineError::Eof) => { |
44 | println!("CTRL-D"); | 58 | println!("CTRL-D"); |
45 | break | 59 | break |
46 | }, | 60 | }, |
47 | Err(err) => { | 61 | Err(err) => { |
48 | println!("Error: {:?}", err); | 62 | println!("Error: {:?}", err); |
49 | break | 63 | break |
64 | } | ||
50 | } | 65 | } |
51 | } | 66 | } |
67 | rl.save_history("history.txt").unwrap(); | ||
52 | } | 68 | } |
53 | rl.save_history("history.txt").unwrap(); | ||
54 | } | 69 | } |
55 | 70 | ||
56 | fn autobalance_parens(input: &str) -> Result<String, CalcError> { | 71 | fn autobalance_parens(input: &str) -> Result<String, CalcError> { |