diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 20 |
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 | ||
165 | fn 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 | |||
175 | fn to_postfix(tokens: Vec<Token>) -> Result<Vec<Token>, String> { | 165 | fn 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 | } |