diff options
author | NerdyPepper <[email protected]> | 2019-03-23 05:14:11 +0000 |
---|---|---|
committer | NerdyPepper <[email protected]> | 2019-03-23 05:14:11 +0000 |
commit | 00706416e7872c6e4925770af900d9a24466d5fb (patch) | |
tree | 92eda547813b6f6b4e2df96a5e147ef3cbb4d4e6 /src | |
parent | 682da5a12de141502c6c1edc778b9ccc3d55d134 (diff) |
basic readline is ready
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs index 0cd192d..50e2af8 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,4 +1,3 @@ | |||
1 | use std::io::{ stdin }; | ||
2 | use std::f64; | 1 | use std::f64; |
3 | 2 | ||
4 | mod lex; | 3 | mod lex; |
@@ -7,17 +6,37 @@ use crate::lex::*; | |||
7 | mod parse; | 6 | mod parse; |
8 | use crate::parse::*; | 7 | use crate::parse::*; |
9 | 8 | ||
9 | use rustyline::error::ReadlineError; | ||
10 | use rustyline::Editor; | ||
11 | |||
10 | fn main() { | 12 | fn main() { |
13 | let mut rl = Editor::<()>::new(); | ||
14 | if rl.load_history("history.txt").is_err() { | ||
15 | println!("No previous history."); | ||
16 | } | ||
11 | loop { | 17 | loop { |
12 | let mut input = String::new(); | 18 | let readline = rl.readline("> "); |
13 | stdin().read_line(&mut input).unwrap(); | 19 | match readline { |
14 | let input = input.trim(); | 20 | Ok(line) => { |
15 | let input = input.replace(" ", ""); | 21 | rl.add_history_entry(line.as_ref()); |
16 | if input == "exit" { | 22 | let evaled = eval_math_expression(&line[..]).unwrap(); |
17 | return | 23 | println!("{}", evaled); |
24 | }, | ||
25 | Err(ReadlineError::Interrupted) => { | ||
26 | println!("CTRL-C"); | ||
27 | break | ||
28 | }, | ||
29 | Err(ReadlineError::Eof) => { | ||
30 | println!("CTRL-D"); | ||
31 | break | ||
32 | }, | ||
33 | Err(err) => { | ||
34 | println!("Error: {:?}", err); | ||
35 | break | ||
36 | } | ||
18 | } | 37 | } |
19 | println!("ans: {}\n", eval_math_expression(&input[..]).unwrap()); | ||
20 | } | 38 | } |
39 | rl.save_history("history.txt").unwrap(); | ||
21 | } | 40 | } |
22 | 41 | ||
23 | fn autobalance_parens(input: &str) -> Result<String, String> { | 42 | fn autobalance_parens(input: &str) -> Result<String, String> { |