diff --git a/src/interpreter.rs b/src/interpreter.rs index 05a73c5..89ed5f2 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -44,6 +44,8 @@ impl Interpreter { BinOpType::BXor => Value::I64(lhs ^ rhs), BinOpType::Shr => Value::I64(lhs >> rhs), BinOpType::Shl => Value::I64(lhs << rhs), + BinOpType::Equ => Value::I64(if lhs == rhs { 1 } else { 0 }), + BinOpType::Neq => 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 fbd8ab5..994ba27 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -43,6 +43,12 @@ pub enum Token { /// Shift Right (>>) Shr, + /// Equal sign (==) + Equ, + + /// Not Equal sign (!=) + Neq, + /// End of file EoF, } @@ -97,6 +103,15 @@ impl<'a> Lexer<'a> { self.next(); tokens.push(Token::Shl); } + '=' if matches!(self.peek(), Some('=')) => { + self.next(); + tokens.push(Token::Equ); + } + '!' if matches!(self.peek(), Some('=')) => { + self.next(); + tokens.push(Token::Neq); + } + '+' => tokens.push(Token::Add), '-' => tokens.push(Token::Sub), '*' => tokens.push(Token::Mul), @@ -151,6 +166,10 @@ impl Token { Token::Shl => BinOpType::Shl, Token::Shr => BinOpType::Shr, + + Token::Equ => BinOpType::Equ, + Token::Neq => BinOpType::Neq, + _ => return None, }) } diff --git a/src/parser.rs b/src/parser.rs index ecda899..5748da1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -34,6 +34,12 @@ pub enum BinOpType { /// Shift Right Shr, + + /// Check equality + Equ, + + /// Check unequality + Neq, } /// Types for unary operators @@ -166,9 +172,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::Equ | BinOpType::Neq => 3, + BinOpType::Shl | BinOpType::Shr => 4, + BinOpType::Add | BinOpType::Sub => 5, + BinOpType::Mul | BinOpType::Div | BinOpType::Mod => 6, } } }