From de0bbb8171cc3d52c431b3738d041ea374ee1165 Mon Sep 17 00:00:00 2001 From: Kai-Philipp Nosper Date: Wed, 2 Feb 2022 18:56:45 +0100 Subject: [PATCH] Implement logical and / or --- src/ast.rs | 6 ++++++ src/interpreter.rs | 2 ++ src/lexer.rs | 9 +++++++++ src/parser.rs | 18 ++++++++++-------- src/token.rs | 9 +++++++++ 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 6390135..25d3fd0 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -43,6 +43,12 @@ pub enum BinOpType { /// Bitwise Xor (exclusive or) BXor, + /// Logical And + LAnd, + + /// Logical Or + LOr, + /// Shift Left Shl, diff --git a/src/interpreter.rs b/src/interpreter.rs index 1c31f98..69c367e 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -128,6 +128,8 @@ impl Interpreter { BinOpType::BOr => Value::I64(lhs | rhs), BinOpType::BAnd => Value::I64(lhs & rhs), BinOpType::BXor => Value::I64(lhs ^ rhs), + BinOpType::LAnd => Value::I64(if (lhs != 0) && (rhs != 0) { 1 } else { 0 }), + BinOpType::LOr => Value::I64(if (lhs != 0) || (rhs != 0) { 1 } else { 0 }), BinOpType::Shr => Value::I64(lhs >> rhs), BinOpType::Shl => Value::I64(lhs << rhs), BinOpType::EquEqu => Value::I64(if lhs == rhs { 1 } else { 0 }), diff --git a/src/lexer.rs b/src/lexer.rs index 447defc..e87cbbf 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -51,6 +51,15 @@ impl<'a> Lexer<'a> { self.next(); tokens.push(Token::LArrow); } + '&' if matches!(self.peek(), '&') => { + self.next(); + tokens.push(Token::LAnd); + } + '|' if matches!(self.peek(), '|') => { + self.next(); + tokens.push(Token::LOr); + } + // Line comment. Consume every char until linefeed (next line) '/' if matches!(self.peek(), '/') => while self.next() != '\n' {}, diff --git a/src/parser.rs b/src/parser.rs index 7f99bc2..3a8efcc 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -239,14 +239,16 @@ impl BinOpType { match self { BinOpType::Declare => 0, BinOpType::Assign => 1, - BinOpType::BOr => 2, - BinOpType::BXor => 3, - BinOpType::BAnd => 4, - BinOpType::EquEqu | BinOpType::NotEqu => 5, - BinOpType::Less | BinOpType::LessEqu | BinOpType::Greater | BinOpType::GreaterEqu => 6, - BinOpType::Shl | BinOpType::Shr => 7, - BinOpType::Add | BinOpType::Sub => 8, - BinOpType::Mul | BinOpType::Div | BinOpType::Mod => 9, + BinOpType::LOr => 2, + BinOpType::LAnd => 3, + BinOpType::BOr => 4, + BinOpType::BXor => 5, + BinOpType::BAnd => 6, + BinOpType::EquEqu | BinOpType::NotEqu => 7, + BinOpType::Less | BinOpType::LessEqu | BinOpType::Greater | BinOpType::GreaterEqu => 8, + BinOpType::Shl | BinOpType::Shr => 9, + BinOpType::Add | BinOpType::Sub => 10, + BinOpType::Mul | BinOpType::Div | BinOpType::Mod => 11, } } } diff --git a/src/token.rs b/src/token.rs index 5142ce3..443ce93 100644 --- a/src/token.rs +++ b/src/token.rs @@ -62,6 +62,12 @@ pub enum Token { /// Circumflex (^) BXor, + /// Logical AND (&&) + LAnd, + + /// Logical OR (||) + LOr, + /// Shift Left (<<) Shl, @@ -110,6 +116,9 @@ impl Token { Token::BOr => BinOpType::BOr, Token::BXor => BinOpType::BXor, + Token::LAnd => BinOpType::LAnd, + Token::LOr => BinOpType::LOr, + Token::Shl => BinOpType::Shl, Token::Shr => BinOpType::Shr,