diff options
author | Maximilian Bosch <[email protected]> | 2021-12-26 13:02:19 +0000 |
---|---|---|
committer | Maximilian Bosch <[email protected]> | 2021-12-30 17:06:49 +0000 |
commit | 66790c32e9cc80eed512d67fe3630bbe124c6c61 (patch) | |
tree | ca4ead6510961788619b6658f41eb3cf1a3a6a0d /src | |
parent | 27c5539c0aa8e7df2947d9addbab90b2df8c3a3d (diff) |
Add `exp2` function and ensure it's correctly lexed
Diffstat (limited to 'src')
-rw-r--r-- | src/lex.rs | 17 |
1 files changed, 11 insertions, 6 deletions
@@ -102,6 +102,7 @@ lazy_static! { | |||
102 | m.insert("asec", Function::token_from_fn("asec".into(), |x| (1./x).acos())); | 102 | m.insert("asec", Function::token_from_fn("asec".into(), |x| (1./x).acos())); |
103 | m.insert("acot", Function::token_from_fn("acot".into(), |x| (1./x).atan())); | 103 | m.insert("acot", Function::token_from_fn("acot".into(), |x| (1./x).atan())); |
104 | m.insert("exp", Function::token_from_fn("exp".into(), |x| x.exp())); | 104 | m.insert("exp", Function::token_from_fn("exp".into(), |x| x.exp())); |
105 | m.insert("exp2", Function::token_from_fn("exp2".into(), |x| x.exp2())); | ||
105 | // single arg function s can be added here | 106 | // single arg function s can be added here |
106 | m | 107 | m |
107 | }; | 108 | }; |
@@ -135,19 +136,23 @@ pub fn lexer(input: &str, prev_ans: Option<f64>) -> Result<Vec<Token>, CalcError | |||
135 | '0'..='9' | '.' => { | 136 | '0'..='9' | '.' => { |
136 | if !char_vec.is_empty() { | 137 | if !char_vec.is_empty() { |
137 | if FUNCTIONS.get(&char_vec[..]).is_some() { | 138 | if FUNCTIONS.get(&char_vec[..]).is_some() { |
138 | return Err(CalcError::Syntax(format!( | 139 | char_vec.push(letter); |
139 | "Function '{}' expected parentheses", | 140 | if !FUNCTIONS.get(&char_vec[..]).is_some() { |
140 | char_vec | 141 | return Err(CalcError::Syntax(format!( |
141 | ))); | 142 | "Function '{}' expected parentheses", |
143 | &char_vec[..char_vec.chars().count()-1] | ||
144 | ))); | ||
145 | } | ||
142 | } else { | 146 | } else { |
143 | return Err(CalcError::Syntax(format!( | 147 | return Err(CalcError::Syntax(format!( |
144 | "Unexpected character '{}'", | 148 | "Unexpected character '{}'", |
145 | char_vec | 149 | char_vec |
146 | ))); | 150 | ))); |
147 | } | 151 | } |
152 | } else { | ||
153 | num_vec.push(letter); | ||
154 | last_char_is_op = false; | ||
148 | } | 155 | } |
149 | num_vec.push(letter); | ||
150 | last_char_is_op = false; | ||
151 | } | 156 | } |
152 | '_' => { | 157 | '_' => { |
153 | if prev_ans.is_none() { | 158 | if prev_ans.is_none() { |