From 86ba4458cfddf1d8db5231d7e0240c2374abc770 Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Fri, 22 Mar 2019 22:37:11 +0530 Subject: refactor: split eval and input --- src/main.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src') 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() { loop { let mut input = String::new(); stdin().read_line(&mut input).unwrap(); - let input = input.trim(); let input = input.replace(" ", ""); - let input = autobalance_parens(&input[..]).unwrap(); - if input == "exit" { return } - - let lexed = lexer(&input[..]); - let postfixed = to_postfix(lexed.unwrap()); - let evaled = eval_postfix(postfixed.unwrap()); - println!("ans: {}", evaled.unwrap()); - println!(); + println!("ans: {}\n", eval_math_expression(&input[..]).unwrap()); } } @@ -50,3 +42,11 @@ fn autobalance_parens(input: &str) -> Result { Ok(balanced) } } + +fn eval_math_expression(input: &str) -> Result { + let input = autobalance_parens(&input[..])?; + let lexed = lexer(&input[..])?; + let postfixed = to_postfix(lexed)?; + let evaled = eval_postfix(postfixed)?; + Ok(evaled) +} -- cgit v1.2.3