diff options
-rw-r--r-- | src/main.rs | 18 |
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 | |||
46 | fn 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 | } | ||