Skip to content

Commit

Permalink
fix: allow floats in number parsing (#6)
Browse files Browse the repository at this point in the history
* fix: switch return type to `f64`

* chore: clean up unused code

* chore: clean up irrelevant tests
  • Loading branch information
elcharitas authored Feb 12, 2023
1 parent 374dbf8 commit 7361aba
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 42 deletions.
1 change: 0 additions & 1 deletion src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub enum Expression {
Asterisk(Box<Expression>, Box<Expression>),
Slash(Box<Expression>, Box<Expression>),
Variable(String),
Decimal(Box<Expression>),
Power(Box<Expression>, Box<Expression>),
Factorial(Box<Expression>),
Group(Box<Expression>),
Expand Down
18 changes: 9 additions & 9 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::HashMap;

pub struct Interpreter<'a> {
parser: &'a mut Parser<'a>,
pub variables: HashMap<String, i32>,
pub variables: HashMap<String, f64>,
}

impl<'a> Interpreter<'a> {
Expand Down Expand Up @@ -35,10 +35,10 @@ impl<'a> Interpreter<'a> {
Ok(())
}

fn evaluate_expression(&mut self, expression: Expression) -> Result<i32, String> {
fn evaluate_expression(&mut self, expression: Expression) -> Result<f64, String> {
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)
Expand Down Expand Up @@ -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),
}
}
}
Expand All @@ -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]
Expand All @@ -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));
}
}
29 changes: 0 additions & 29 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ impl<'a> Lexer<'a> {
chars.next();
return Some(Token::Factorial);
}
Some(&'.') => {
chars.next();
return Some(Token::Dot);
}
_ => return None,
}
}
Expand Down Expand Up @@ -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");
Expand Down
3 changes: 0 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 7361aba

Please sign in to comment.