diff --git a/README.md b/README.md index 39f48ab..86cd0d2 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,8 @@ - [x] Negate `-X` - [x] Parentheses `(X+Y)*Z` - [ ] Logical boolean operators - - [ ] Equal `==` - - [ ] Not equal `!=` + - [x] Equal `==` + - [x] Not equal `!=` - [ ] Greater than `>` - [ ] Less than `<` - [ ] Greater than or equal `>=` diff --git a/src/interpreter.rs b/src/interpreter.rs index 64f4498..fe9e274 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -54,6 +54,8 @@ impl Interpreter { BinOpType::BXor => Value::I64(lhs ^ rhs), BinOpType::Shr => Value::I64(lhs >> rhs), BinOpType::Shl => Value::I64(lhs << rhs), + BinOpType::EquEqu => Value::I64(if lhs == rhs { 1 } else { 0 }), + BinOpType::NotEqu => Value::I64(if lhs != rhs { 1 } else { 0 }), }, // _ => panic!("Value types are not compatible"), } diff --git a/src/lexer.rs b/src/lexer.rs index f28bf6c..9fb5116 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -28,6 +28,12 @@ pub enum Token { /// Percent (%) Mod, + /// Equal Equal (==) + EquEqu, + + /// Exclamationmark Equal (!=) + NotEqu, + /// Pipe (|) BOr, @@ -100,6 +106,15 @@ impl<'a> Lexer<'a> { self.next(); tokens.push(Token::Shl); } + '=' if matches!(self.peek(), Some('=')) => { + self.next(); + tokens.push(Token::EquEqu); + } + '!' if matches!(self.peek(), Some('=')) => { + self.next(); + tokens.push(Token::NotEqu); + } + '+' => tokens.push(Token::Add), '-' => tokens.push(Token::Sub), '*' => tokens.push(Token::Mul), @@ -155,6 +170,10 @@ impl Token { Token::Shl => BinOpType::Shl, Token::Shr => BinOpType::Shr, + + Token::EquEqu => BinOpType::EquEqu, + Token::NotEqu => BinOpType::NotEqu, + _ => return None, }) } diff --git a/src/parser.rs b/src/parser.rs index 11f9e96..c4b9980 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -20,6 +20,12 @@ pub enum BinOpType { /// Modulo Mod, + /// Compare Equal + EquEqu, + + /// Compare Not Equal + NotEqu, + /// Bitwise OR (inclusive or) BOr, @@ -177,9 +183,10 @@ impl BinOpType { BinOpType::BOr => 0, BinOpType::BXor => 1, BinOpType::BAnd => 2, - BinOpType::Shl | BinOpType::Shr => 3, - BinOpType::Add | BinOpType::Sub => 4, - BinOpType::Mul | BinOpType::Div | BinOpType::Mod => 5, + BinOpType::EquEqu | BinOpType::NotEqu => 3, + BinOpType::Shl | BinOpType::Shr => 4, + BinOpType::Add | BinOpType::Sub => 5, + BinOpType::Mul | BinOpType::Div | BinOpType::Mod => 6, } } }