aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNerdyPepper <[email protected]>2019-03-20 17:10:26 +0000
committerNerdyPepper <[email protected]>2019-03-20 17:10:26 +0000
commitcc42fe2c84ce7f8af83670f85dfe9bc93d5098b3 (patch)
tree3e48c11c83fd8c44f6234ee478a19458b0b95c7e
parent38d3446d46ece5a993b797fdb7b56d280c2e9422 (diff)
simplify complicated construct
-rw-r--r--src/main.rs20
1 files changed, 9 insertions, 11 deletions
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<Vec<Token>, String> {
162 Ok(result) 162 Ok(result)
163} 163}
164 164
165fn tilt_until(operators: &mut Vec<Token>, output: &mut Vec<Token>, stop: Token) -> bool {
166 while let Some(token) = operators.pop() {
167 if token == stop {
168 return true;
169 }
170 output.push(token)
171 }
172 false
173}
174
175fn to_postfix(tokens: Vec<Token>) -> Result<Vec<Token>, String> { 165fn to_postfix(tokens: Vec<Token>) -> Result<Vec<Token>, String> {
176 let mut postfixed: Vec<Token> = vec![]; 166 let mut postfixed: Vec<Token> = vec![];
177 let mut op_stack: Vec<Token> = vec![]; 167 let mut op_stack: Vec<Token> = vec![];
@@ -212,7 +202,15 @@ fn to_postfix(tokens: Vec<Token>) -> Result<Vec<Token>, String> {
212 op_stack.push(token); 202 op_stack.push(token);
213 }, 203 },
214 Token::RParen => { 204 Token::RParen => {
215 if !tilt_until(&mut op_stack, &mut postfixed, Token::LParen) { 205 let mut push_until: bool = false;
206 while let Some(token) = op_stack.pop() {
207 if token == Token::LParen {
208 push_until = true;
209 break;
210 }
211 postfixed.push(token)
212 }
213 if !push_until {
216 return Err(String::from("Mismatched ')'")); 214 return Err(String::from("Mismatched ')'"));
217 } 215 }
218 } 216 }