From 7361aba5aa1842d56c6a034e58d82031957c576e Mon Sep 17 00:00:00 2001 From: Jonathan Irhodia Date: Sun, 12 Feb 2023 08:00:14 +0100 Subject: [PATCH] fix: allow floats in number parsing (#6) * fix: switch return type to `f64` * chore: clean up unused code * chore: clean up irrelevant tests --- src/enums.rs | 1 - src/interpreter.rs | 18 +++++++++--------- src/lexer.rs | 29 ----------------------------- src/parser.rs | 3 --- 4 files changed, 9 insertions(+), 42 deletions(-) diff --git a/src/enums.rs b/src/enums.rs index 9ef24b5..1aa5b7c 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -24,7 +24,6 @@ pub enum Expression { Asterisk(Box, Box), Slash(Box, Box), Variable(String), - Decimal(Box), Power(Box, Box), Factorial(Box), Group(Box), diff --git a/src/interpreter.rs b/src/interpreter.rs index b19ec98..d7886b2 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; pub struct Interpreter<'a> { parser: &'a mut Parser<'a>, - pub variables: HashMap, + pub variables: HashMap, } impl<'a> Interpreter<'a> { @@ -35,10 +35,10 @@ impl<'a> Interpreter<'a> { Ok(()) } - fn evaluate_expression(&mut self, expression: Expression) -> Result { + fn evaluate_expression(&mut self, expression: Expression) -> Result { match expression { Expression::Group(expression) => self.evaluate_expression(*expression), - Expression::Number(value) => Ok(value as i32), + Expression::Number(value) => Ok(value), Expression::Variable(name) => { if let Some(value) = self.variables.get(&name) { Ok(*value) @@ -67,12 +67,12 @@ impl<'a> Interpreter<'a> { Ok(left / right) } Expression::Factorial(factor) => { - let factor = self.evaluate_expression(*factor)?; - Ok((1..=factor).product()) + let factor = self.evaluate_expression(*factor)? as i32; + let result: i32 = (1..=factor).product(); + Ok(result as f64) } - Expression::Decimal(_) => todo!(), Expression::Power(_, _) => todo!(), - Expression::None => Ok(0), + Expression::None => Ok(0.0), } } } @@ -87,7 +87,7 @@ mod tests { let mut parser = Parser::new(&mut lexer); let mut interpreter = Interpreter::new(&mut parser); interpreter.interpret().unwrap(); - assert_eq!(interpreter.variables.get("a"), Some(&3)); + assert_eq!(interpreter.variables.get("a"), Some(&3.0)); } #[test] @@ -111,6 +111,6 @@ mod tests { let mut parser = Parser::new(&mut lexer); let mut interpreter = Interpreter::new(&mut parser); interpreter.interpret().unwrap(); - assert_eq!(interpreter.variables.get("a"), Some(&120)); + assert_eq!(interpreter.variables.get("a"), Some(&120.0)); } } diff --git a/src/lexer.rs b/src/lexer.rs index fe1c1f1..f6dd25c 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -105,10 +105,6 @@ impl<'a> Lexer<'a> { chars.next(); return Some(Token::Factorial); } - Some(&'.') => { - chars.next(); - return Some(Token::Dot); - } _ => return None, } } @@ -183,31 +179,6 @@ mod tests { assert_eq!(lexer.next_token(), None); } - #[test] - fn test_next_token_with_dot() { - let mut lexer = Lexer::new("1.0 + 2.0 - 3.0 * 4.0 / 5.0"); - assert_eq!(lexer.next_token(), Some(Token::Number(1.0))); - assert_eq!(lexer.next_token(), Some(Token::Dot)); - assert_eq!(lexer.next_token(), Some(Token::Number(0.0))); - assert_eq!(lexer.next_token(), Some(Token::Plus)); - assert_eq!(lexer.next_token(), Some(Token::Number(2.0))); - assert_eq!(lexer.next_token(), Some(Token::Dot)); - assert_eq!(lexer.next_token(), Some(Token::Number(0.0))); - assert_eq!(lexer.next_token(), Some(Token::Minus)); - assert_eq!(lexer.next_token(), Some(Token::Number(3.0))); - assert_eq!(lexer.next_token(), Some(Token::Dot)); - assert_eq!(lexer.next_token(), Some(Token::Number(0.0))); - assert_eq!(lexer.next_token(), Some(Token::Star)); - assert_eq!(lexer.next_token(), Some(Token::Number(4.0))); - assert_eq!(lexer.next_token(), Some(Token::Dot)); - assert_eq!(lexer.next_token(), Some(Token::Number(0.0))); - assert_eq!(lexer.next_token(), Some(Token::Slash)); - assert_eq!(lexer.next_token(), Some(Token::Number(5.0))); - assert_eq!(lexer.next_token(), Some(Token::Dot)); - assert_eq!(lexer.next_token(), Some(Token::Number(0.0))); - assert_eq!(lexer.next_token(), None); - } - #[test] fn test_next_token_with_parentheses() { let mut lexer = Lexer::new("(1 + 2) - (3 * 4) / 5"); diff --git a/src/parser.rs b/src/parser.rs index e07ad54..d3375b4 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -96,9 +96,6 @@ impl<'a> Parser<'a> { Token::Number(number) => { value = Expression::Number(number); } - Token::Dot => { - value = Expression::Decimal(Box::new(value)); - } Token::Identifier(name) => { value = Expression::Variable(name); }