From cc42fe2c84ce7f8af83670f85dfe9bc93d5098b3 Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Wed, 20 Mar 2019 22:40:26 +0530 Subject: simplify complicated construct --- src/main.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 5f75843..bff6201 100644 --- a/src/main.rs +++ b/src/main.rs @@ -162,16 +162,6 @@ fn lexer(input: &str) -> Result, String> { Ok(result) } -fn tilt_until(operators: &mut Vec, output: &mut Vec, stop: Token) -> bool { - while let Some(token) = operators.pop() { - if token == stop { - return true; - } - output.push(token) - } - false -} - fn to_postfix(tokens: Vec) -> Result, String> { let mut postfixed: Vec = vec![]; let mut op_stack: Vec = vec![]; @@ -212,7 +202,15 @@ fn to_postfix(tokens: Vec) -> Result, String> { op_stack.push(token); }, Token::RParen => { - if !tilt_until(&mut op_stack, &mut postfixed, Token::LParen) { + let mut push_until: bool = false; + while let Some(token) = op_stack.pop() { + if token == Token::LParen { + push_until = true; + break; + } + postfixed.push(token) + } + if !push_until { return Err(String::from("Mismatched ')'")); } } -- cgit v1.2.3