aboutsummaryrefslogtreecommitdiff
path: root/src/lex
diff options
context:
space:
mode:
Diffstat (limited to 'src/lex')
-rw-r--r--src/lex/mod.rs23
1 files changed, 19 insertions, 4 deletions
diff --git a/src/lex/mod.rs b/src/lex/mod.rs
index caafd4b..2793699 100644
--- a/src/lex/mod.rs
+++ b/src/lex/mod.rs
@@ -77,6 +77,13 @@ pub enum Token {
77 RParen 77 RParen
78} 78}
79 79
80fn get_constants() -> HashMap<&'static str, Token> {
81 return [
82 ("e", Token::Num(std::f64::consts::E)),
83 ("pi", Token::Num(std::f64::consts::PI)),
84 ].iter().cloned().collect();
85}
86
80fn get_functions() -> HashMap<&'static str, Token> { 87fn get_functions() -> HashMap<&'static str, Token> {
81 return [ 88 return [
82 ("sin", Function::token_from_fn("sin".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).sin())), 89 ("sin", Function::token_from_fn("sin".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).sin())),
@@ -123,6 +130,7 @@ fn factorial (n: f64) -> f64 {
123} 130}
124 131
125pub fn lexer(input: &str, prev_ans: f64) -> Result<Vec<Token>, CalcError> { 132pub fn lexer(input: &str, prev_ans: f64) -> Result<Vec<Token>, CalcError> {
133 let constants: HashMap<&str, Token> = get_constants();
126 let functions: HashMap<&str, Token> = get_functions(); 134 let functions: HashMap<&str, Token> = get_functions();
127 let operators: HashMap<char, Token> = get_operators(); 135 let operators: HashMap<char, Token> = get_operators();
128 136
@@ -179,6 +187,10 @@ pub fn lexer(input: &str, prev_ans: f64) -> Result<Vec<Token>, CalcError> {
179 result.push(Token::Num(x)); 187 result.push(Token::Num(x));
180 num_vec.clear(); 188 num_vec.clear();
181 last_char_is_op = true; 189 last_char_is_op = true;
190 } else if let Some(token) = constants.get(&char_vec[..]) {
191 result.push(token.clone());
192 char_vec.clear();
193 last_char_is_op = true;
182 } 194 }
183 result.push(op_token); 195 result.push(op_token);
184 } else if last_char_is_op { 196 } else if last_char_is_op {
@@ -189,7 +201,7 @@ pub fn lexer(input: &str, prev_ans: f64) -> Result<Vec<Token>, CalcError> {
189 } 201 }
190 }, 202 },
191 '/' | '*' | '%' | '^' | '!' => { 203 '/' | '*' | '%' | '^' | '!' => {
192 drain_num_stack(&mut num_vec, &mut result); 204 drain_stack(&mut num_vec, &mut char_vec, &mut result);
193 let operator_token: Token = operators.get(&letter).unwrap().clone(); 205 let operator_token: Token = operators.get(&letter).unwrap().clone();
194 result.push(operator_token); 206 result.push(operator_token);
195 last_char_is_op = true; 207 last_char_is_op = true;
@@ -227,7 +239,7 @@ pub fn lexer(input: &str, prev_ans: f64) -> Result<Vec<Token>, CalcError> {
227 last_char_is_op = true; 239 last_char_is_op = true;
228 }, 240 },
229 ')' => { 241 ')' => {
230 drain_num_stack(&mut num_vec, &mut result); 242 drain_stack(&mut num_vec, &mut char_vec, &mut result);
231 result.push(Token::RParen); 243 result.push(Token::RParen);
232 last_char_is_op = false; 244 last_char_is_op = false;
233 }, 245 },
@@ -238,15 +250,18 @@ pub fn lexer(input: &str, prev_ans: f64) -> Result<Vec<Token>, CalcError> {
238 } 250 }
239 } 251 }
240 // println!("{:?}", result); 252 // println!("{:?}", result);
241 drain_num_stack(&mut num_vec, &mut result); 253 drain_stack(&mut num_vec, &mut char_vec, &mut result);
242 Ok(result) 254 Ok(result)
243} 255}
244 256
245fn drain_num_stack(num_vec: &mut String, result: &mut Vec<Token>) { 257fn drain_stack(num_vec: &mut String, char_vec: &mut String, result: &mut Vec<Token>) {
246 let parse_num = num_vec.parse::<f64>().ok(); 258 let parse_num = num_vec.parse::<f64>().ok();
247 if let Some(x) = parse_num { 259 if let Some(x) = parse_num {
248 result.push(Token::Num(x)); 260 result.push(Token::Num(x));
249 num_vec.clear(); 261 num_vec.clear();
262 } else if let Some(token) = get_constants().get(&char_vec[..]) {
263 result.push(token.clone());
264 char_vec.clear();
250 } 265 }
251} 266}
252 267