diff options
Diffstat (limited to 'src/lex')
-rw-r--r-- | src/lex/mod.rs | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/src/lex/mod.rs b/src/lex/mod.rs index b21bafb..3222c12 100644 --- a/src/lex/mod.rs +++ b/src/lex/mod.rs | |||
@@ -129,7 +129,8 @@ pub fn lexer(input: &str, prev_ans: Option<f64>) -> Result<Vec<Token>, CalcError | |||
129 | let mut result: Vec<Token> = vec![]; | 129 | let mut result: Vec<Token> = vec![]; |
130 | let mut last_char_is_op = true; | 130 | let mut last_char_is_op = true; |
131 | 131 | ||
132 | for letter in input.chars() { | 132 | let mut chars = input.chars().peekable(); |
133 | while let Some(mut letter) = chars.next() { | ||
133 | match letter { | 134 | match letter { |
134 | '0'..='9' | '.' => { | 135 | '0'..='9' | '.' => { |
135 | if !char_vec.is_empty() { | 136 | if !char_vec.is_empty() { |
@@ -209,6 +210,11 @@ pub fn lexer(input: &str, prev_ans: Option<f64>) -> Result<Vec<Token>, CalcError | |||
209 | } | 210 | } |
210 | '/' | '*' | '%' | '^' | '!' => { | 211 | '/' | '*' | '%' | '^' | '!' => { |
211 | drain_stack(&mut num_vec, &mut char_vec, &mut result); | 212 | drain_stack(&mut num_vec, &mut char_vec, &mut result); |
213 | if letter == '*' && chars.peek() == Some(&'*') { | ||
214 | // Accept `**` operator as meaning `^` (exponentation). | ||
215 | let _ = chars.next(); | ||
216 | letter = '^'; | ||
217 | } | ||
212 | let operator_token: Token = OPERATORS.get(&letter).unwrap().clone(); | 218 | let operator_token: Token = OPERATORS.get(&letter).unwrap().clone(); |
213 | result.push(operator_token); | 219 | result.push(operator_token); |
214 | last_char_is_op = true; | 220 | last_char_is_op = true; |