From ac352e26b6b0ff9b90004208abf4644f0b2c2f4e Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Tue, 6 Aug 2019 20:59:25 +0530 Subject: improve highlight for `_`, store program state --- src/lex/mod.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/lex') diff --git a/src/lex/mod.rs b/src/lex/mod.rs index 48e07be..fe9c421 100644 --- a/src/lex/mod.rs +++ b/src/lex/mod.rs @@ -123,7 +123,7 @@ fn factorial(n: f64) -> f64 { n.signum() * (1..=n.abs() as u64).fold(1, |p, n| p * n) as f64 } -pub fn lexer(input: &str, prev_ans: f64) -> Result, CalcError> { +pub fn lexer(input: &str, prev_ans: Option) -> Result, CalcError> { let mut num_vec: String = String::new(); let mut char_vec: String = String::new(); let mut result: Vec = vec![]; @@ -149,6 +149,9 @@ pub fn lexer(input: &str, prev_ans: f64) -> Result, CalcError> { last_char_is_op = false; } '_' => { + if prev_ans.is_none() { + return Err(CalcError::Syntax("No previous answer!".into())); + } if !char_vec.is_empty() { if FUNCTIONS.get(&char_vec[..]).is_some() { return Err(CalcError::Syntax(format!( @@ -169,7 +172,7 @@ pub fn lexer(input: &str, prev_ans: f64) -> Result, CalcError> { num_vec.clear(); } last_char_is_op = false; - result.push(Token::Num(prev_ans)); + result.push(Token::Num(prev_ans.unwrap())); } 'a'...'z' | 'A'...'Z' => { let parse_num = num_vec.parse::().ok(); -- cgit v1.2.3