diff options
author | Ivan Tham <[email protected]> | 2022-01-05 04:01:48 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2022-01-05 04:01:48 +0000 |
commit | 9fa2823b3916184dbfbb9c704ab34ae79c0c1038 (patch) | |
tree | 597a3ac1bad860fa62737faa58aeb71184c249be /src/lex.rs | |
parent | 3f52f6f2ce8408350fcdc180d416e63dd627d93f (diff) | |
parent | 3f0f8d9f0f910b4c9477297049d4a508eacc3734 (diff) |
Merge pull request #51 from Ma27/exp-and-round
Add functions `exp()`, `exp2()` & `round()`; fix minor readline issues
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() { |