diff options
Diffstat (limited to 'src/lex.rs')
-rw-r--r-- | src/lex.rs | 25 |
1 files changed, 19 insertions, 6 deletions
@@ -101,6 +101,9 @@ lazy_static! { | |||
101 | m.insert("acsc", Function::token_from_fn("acsc".into(), |x| (1./x).asin())); | 101 | m.insert("acsc", Function::token_from_fn("acsc".into(), |x| (1./x).asin())); |
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())); | ||
105 | m.insert("exp2", Function::token_from_fn("exp2".into(), |x| x.exp2())); | ||
106 | m.insert("round", Function::token_from_fn("round".into(), |x| x.round())); | ||
104 | // single arg function s can be added here | 107 | // single arg function s can be added here |
105 | m | 108 | m |
106 | }; | 109 | }; |
@@ -134,19 +137,29 @@ pub fn lexer(input: &str, prev_ans: Option<f64>) -> Result<Vec<Token>, CalcError | |||
134 | '0'..='9' | '.' => { | 137 | '0'..='9' | '.' => { |
135 | if !char_vec.is_empty() { | 138 | if !char_vec.is_empty() { |
136 | if FUNCTIONS.get(&char_vec[..]).is_some() { | 139 | if FUNCTIONS.get(&char_vec[..]).is_some() { |
137 | return Err(CalcError::Syntax(format!( | 140 | char_vec.push(letter); |
138 | "Function '{}' expected parentheses", | 141 | if !FUNCTIONS.get(&char_vec[..]).is_some() { |
139 | char_vec | 142 | return Err(CalcError::Syntax(format!( |
140 | ))); | 143 | "Function '{}' expected parentheses", |
144 | &char_vec[..char_vec.chars().count()-1] | ||
145 | ))); | ||
146 | } | ||
147 | } else if CONSTANTS.get(&char_vec[..]).is_some() { | ||
148 | result.push(CONSTANTS.get(&char_vec[..]).unwrap().clone()); | ||
149 | result.push(OPERATORS.get(&'*').unwrap().clone()); | ||
150 | char_vec.clear(); | ||
151 | num_vec.push(letter); | ||
152 | last_char_is_op = false; | ||
141 | } else { | 153 | } else { |
142 | return Err(CalcError::Syntax(format!( | 154 | return Err(CalcError::Syntax(format!( |
143 | "Unexpected character '{}'", | 155 | "Unexpected character '{}'", |
144 | char_vec | 156 | char_vec |
145 | ))); | 157 | ))); |
146 | } | 158 | } |
159 | } else { | ||
160 | num_vec.push(letter); | ||
161 | last_char_is_op = false; | ||
147 | } | 162 | } |
148 | num_vec.push(letter); | ||
149 | last_char_is_op = false; | ||
150 | } | 163 | } |
151 | '_' => { | 164 | '_' => { |
152 | if prev_ans.is_none() { | 165 | if prev_ans.is_none() { |