aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs35
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 @@
1use std::io::{ stdin };
2use std::f64; 1use std::f64;
3 2
4mod lex; 3mod lex;
@@ -7,17 +6,37 @@ use crate::lex::*;
7mod parse; 6mod parse;
8use crate::parse::*; 7use crate::parse::*;
9 8
9use rustyline::error::ReadlineError;
10use rustyline::Editor;
11
10fn main() { 12fn main() {
13let 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
23fn autobalance_parens(input: &str) -> Result<String, String> { 42fn autobalance_parens(input: &str) -> Result<String, String> {