aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNerdyPepper <[email protected]>2019-03-22 17:07:11 +0000
committerNerdyPepper <[email protected]>2019-03-22 17:07:11 +0000
commit86ba4458cfddf1d8db5231d7e0240c2374abc770 (patch)
tree0a9c97cd756e7f2b62a60ceb8dd24efc20f7bf80
parentbb97540abf26ea14881a4243b7c4b105b1a40075 (diff)
refactor: split eval and input
-rw-r--r--src/main.rs18
1 files changed, 9 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs
index f3f9d2d..0cd192d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -11,20 +11,12 @@ fn main() {
11 loop { 11 loop {
12 let mut input = String::new(); 12 let mut input = String::new();
13 stdin().read_line(&mut input).unwrap(); 13 stdin().read_line(&mut input).unwrap();
14
15 let input = input.trim(); 14 let input = input.trim();
16 let input = input.replace(" ", ""); 15 let input = input.replace(" ", "");
17 let input = autobalance_parens(&input[..]).unwrap();
18
19 if input == "exit" { 16 if input == "exit" {
20 return 17 return
21 } 18 }
22 19 println!("ans: {}\n", eval_math_expression(&input[..]).unwrap());
23 let lexed = lexer(&input[..]);
24 let postfixed = to_postfix(lexed.unwrap());
25 let evaled = eval_postfix(postfixed.unwrap());
26 println!("ans: {}", evaled.unwrap());
27 println!();
28 } 20 }
29} 21}
30 22
@@ -50,3 +42,11 @@ fn autobalance_parens(input: &str) -> Result<String, String> {
50 Ok(balanced) 42 Ok(balanced)
51 } 43 }
52} 44}
45
46fn eval_math_expression(input: &str) -> Result<f64, String> {
47 let input = autobalance_parens(&input[..])?;
48 let lexed = lexer(&input[..])?;
49 let postfixed = to_postfix(lexed)?;
50 let evaled = eval_postfix(postfixed)?;
51 Ok(evaled)
52}