From 18a1ce8f0343f7865f8a01741fb96f98945645d0 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Sun, 26 Dec 2021 15:41:47 +0100 Subject: Allow expressions such as `e2` as multiplication of `e * 2` --- src/lex.rs | 6 ++++++ src/main.rs | 5 +++++ 2 files changed, 11 insertions(+) (limited to 'src') diff --git a/src/lex.rs b/src/lex.rs index 1c686a1..da542d5 100644 --- a/src/lex.rs +++ b/src/lex.rs @@ -143,6 +143,12 @@ pub fn lexer(input: &str, prev_ans: Option) -> Result, CalcError &char_vec[..char_vec.chars().count()-1] ))); } + } else if CONSTANTS.get(&char_vec[..]).is_some() { + result.push(CONSTANTS.get(&char_vec[..]).unwrap().clone()); + result.push(OPERATORS.get(&'*').unwrap().clone()); + char_vec.clear(); + num_vec.push(letter); + last_char_is_op = false; } else { return Err(CalcError::Syntax(format!( "Unexpected character '{}'", diff --git a/src/main.rs b/src/main.rs index 75c33bb..f5e5c59 100644 --- a/src/main.rs +++ b/src/main.rs @@ -259,4 +259,9 @@ mod tests { let evaled = eval_math_expression("9 + _ ", Some(0f64)).unwrap(); assert_eq!(9., evaled); } + #[test] + fn eval_const_multiplication() { + let evaled = eval_math_expression("e2", None).unwrap(); + assert_eq!(5.4365636569, evaled); + } } -- cgit v1.2.3