From 32e4f1ea4f1cc0b91579da9ad150cc5a35ddd13a Mon Sep 17 00:00:00 2001 From: Daniel M Date: Sat, 29 Jan 2022 21:48:55 +0100 Subject: [PATCH] Implement relational binops --- README.md | 10 +++++----- src/interpreter.rs | 4 ++++ src/lexer.rs | 30 +++++++++++++++++++++++++++++- src/parser.rs | 19 ++++++++++++++++--- 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 86cd0d2..fc205b2 100644 --- a/README.md +++ b/README.md @@ -19,13 +19,13 @@ - [x] Unary operators - [x] Negate `-X` - [x] Parentheses `(X+Y)*Z` - - [ ] Logical boolean operators + - [x] Logical boolean operators - [x] Equal `==` - [x] Not equal `!=` - - [ ] Greater than `>` - - [ ] Less than `<` - - [ ] Greater than or equal `>=` - - [ ] Less than or equal `<=` + - [x] Greater than `>` + - [x] Less than `<` + - [x] Greater than or equal `>=` + - [x] Less than or equal `<=` - [x] Bitwise operators - [x] Bitwise AND `X&Y` - [x] Bitwise OR `X|Y` diff --git a/src/interpreter.rs b/src/interpreter.rs index fe9e274..0c45d8d 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -56,6 +56,10 @@ impl Interpreter { 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 }), + BinOpType::Less => Value::I64(if lhs < rhs { 1 } else { 0 }), + BinOpType::LessEqu => Value::I64(if lhs <= rhs { 1 } else { 0 }), + BinOpType::Greater => Value::I64(if lhs > rhs { 1 } else { 0 }), + BinOpType::GreaterEqu => 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 9fb5116..7132572 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -52,6 +52,18 @@ pub enum Token { /// Tilde (~) Tilde, + /// Left angle bracket (<) + LAngle, + + /// Right angle bracket (>) + RAngle, + + /// Left angle bracket Equal (<=) + LAngleEqu, + + /// Left angle bracket Equal (>=) + RAngleEqu, + /// End of file EoF, } @@ -114,7 +126,15 @@ impl<'a> Lexer<'a> { self.next(); tokens.push(Token::NotEqu); } - + '<' if matches!(self.peek(), Some('=')) => { + self.next(); + tokens.push(Token::LAngleEqu); + } + '>' if matches!(self.peek(), Some('=')) => { + self.next(); + tokens.push(Token::RAngleEqu); + } + '+' => tokens.push(Token::Add), '-' => tokens.push(Token::Sub), '*' => tokens.push(Token::Mul), @@ -126,6 +146,8 @@ impl<'a> Lexer<'a> { '(' => tokens.push(Token::LParen), ')' => tokens.push(Token::RParen), '~' => tokens.push(Token::Tilde), + '<' => tokens.push(Token::LAngle), + '>' => tokens.push(Token::RAngle), //TODO: Don't panic, keep calm _ => panic!("Lexer encountered unexpected char: '{}'", ch), @@ -174,6 +196,12 @@ impl Token { Token::EquEqu => BinOpType::EquEqu, Token::NotEqu => BinOpType::NotEqu, + Token::LAngle => BinOpType::Less, + Token::LAngleEqu => BinOpType::LessEqu, + + Token::RAngle => BinOpType::Greater, + Token::RAngleEqu => BinOpType::GreaterEqu, + _ => return None, }) } diff --git a/src/parser.rs b/src/parser.rs index c4b9980..e7847af 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -26,6 +26,18 @@ pub enum BinOpType { /// Compare Not Equal NotEqu, + /// Less than + Less, + + /// Less than or Equal + LessEqu, + + /// Greater than + Greater, + + /// Greater than or Equal + GreaterEqu, + /// Bitwise OR (inclusive or) BOr, @@ -184,9 +196,10 @@ impl BinOpType { BinOpType::BXor => 1, BinOpType::BAnd => 2, BinOpType::EquEqu | BinOpType::NotEqu => 3, - BinOpType::Shl | BinOpType::Shr => 4, - BinOpType::Add | BinOpType::Sub => 5, - BinOpType::Mul | BinOpType::Div | BinOpType::Mod => 6, + BinOpType::Less | BinOpType::LessEqu | BinOpType::Greater | BinOpType::GreaterEqu => 4, + BinOpType::Shl | BinOpType::Shr => 5, + BinOpType::Add | BinOpType::Sub => 6, + BinOpType::Mul | BinOpType::Div | BinOpType::Mod => 7, } } }