From 66790c32e9cc80eed512d67fe3630bbe124c6c61 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Sun, 26 Dec 2021 14:02:19 +0100 Subject: Add `exp2` function and ensure it's correctly lexed --- src/lex.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/lex.rs b/src/lex.rs index 4bdd7b6..1c686a1 100644 --- a/src/lex.rs +++ b/src/lex.rs @@ -102,6 +102,7 @@ lazy_static! { m.insert("asec", Function::token_from_fn("asec".into(), |x| (1./x).acos())); m.insert("acot", Function::token_from_fn("acot".into(), |x| (1./x).atan())); m.insert("exp", Function::token_from_fn("exp".into(), |x| x.exp())); + m.insert("exp2", Function::token_from_fn("exp2".into(), |x| x.exp2())); // single arg function s can be added here m }; @@ -135,19 +136,23 @@ pub fn lexer(input: &str, prev_ans: Option) -> Result, CalcError '0'..='9' | '.' => { if !char_vec.is_empty() { if FUNCTIONS.get(&char_vec[..]).is_some() { - return Err(CalcError::Syntax(format!( - "Function '{}' expected parentheses", - char_vec - ))); + char_vec.push(letter); + if !FUNCTIONS.get(&char_vec[..]).is_some() { + return Err(CalcError::Syntax(format!( + "Function '{}' expected parentheses", + &char_vec[..char_vec.chars().count()-1] + ))); + } } else { return Err(CalcError::Syntax(format!( "Unexpected character '{}'", char_vec ))); } + } else { + num_vec.push(letter); + last_char_is_op = false; } - num_vec.push(letter); - last_char_is_op = false; } '_' => { if prev_ans.is_none() { -- cgit v1.2.3