diff options
Diffstat (limited to 'src/lex/mod.rs')
-rw-r--r-- | src/lex/mod.rs | 7 |
1 files changed, 5 insertions, 2 deletions
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 { | |||
123 | n.signum() * (1..=n.abs() as u64).fold(1, |p, n| p * n) as f64 | 123 | n.signum() * (1..=n.abs() as u64).fold(1, |p, n| p * n) as f64 |
124 | } | 124 | } |
125 | 125 | ||
126 | pub fn lexer(input: &str, prev_ans: f64) -> Result<Vec<Token>, CalcError> { | 126 | pub fn lexer(input: &str, prev_ans: Option<f64>) -> Result<Vec<Token>, CalcError> { |
127 | let mut num_vec: String = String::new(); | 127 | let mut num_vec: String = String::new(); |
128 | let mut char_vec: String = String::new(); | 128 | let mut char_vec: String = String::new(); |
129 | let mut result: Vec<Token> = vec![]; | 129 | let mut result: Vec<Token> = vec![]; |
@@ -149,6 +149,9 @@ pub fn lexer(input: &str, prev_ans: f64) -> Result<Vec<Token>, CalcError> { | |||
149 | last_char_is_op = false; | 149 | last_char_is_op = false; |
150 | } | 150 | } |
151 | '_' => { | 151 | '_' => { |
152 | if prev_ans.is_none() { | ||
153 | return Err(CalcError::Syntax("No previous answer!".into())); | ||
154 | } | ||
152 | if !char_vec.is_empty() { | 155 | if !char_vec.is_empty() { |
153 | if FUNCTIONS.get(&char_vec[..]).is_some() { | 156 | if FUNCTIONS.get(&char_vec[..]).is_some() { |
154 | return Err(CalcError::Syntax(format!( | 157 | return Err(CalcError::Syntax(format!( |
@@ -169,7 +172,7 @@ pub fn lexer(input: &str, prev_ans: f64) -> Result<Vec<Token>, CalcError> { | |||
169 | num_vec.clear(); | 172 | num_vec.clear(); |
170 | } | 173 | } |
171 | last_char_is_op = false; | 174 | last_char_is_op = false; |
172 | result.push(Token::Num(prev_ans)); | 175 | result.push(Token::Num(prev_ans.unwrap())); |
173 | } | 176 | } |
174 | 'a'...'z' | 'A'...'Z' => { | 177 | 'a'...'z' | 'A'...'Z' => { |
175 | let parse_num = num_vec.parse::<f64>().ok(); | 178 | let parse_num = num_vec.parse::<f64>().ok(); |