diff --git a/README.md b/README.md index bbe81a4..3440dab 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Supported mathematical operations: The logical operators evaluate the operands as `false` if they are equal to `0` and `true` if they are not equal to `0` - And `a && b` - Or `a || b` +- Not `!a` ### Equality & Relational Operators The equality and relational operations result in `1` if the condition is evaluated as `true` and in `0` if the condition is evaluated as `false`. @@ -148,8 +149,7 @@ Line comments can be initiated by using `//` - [x] Subtraction `a - b` - [x] Multiplication `a * b` - [x] Division `a / b` - - [x] Modulo `a % b` - - [x] Unary operators + - [x] Modulo `a % b - [x] Negate `-a` - [x] Parentheses `(a + b) * c` - [x] Logical boolean operators @@ -162,6 +162,7 @@ Line comments can be initiated by using `//` - [x] Logical operators - [x] And `a && b` - [x] Or `a || b` + - [x] Not `!a` - [x] Bitwise operators - [x] Bitwise AND `a & b` - [x] Bitwise OR `a | b` diff --git a/src/ast.rs b/src/ast.rs index 25d3fd0..73c1ae0 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -69,6 +69,9 @@ pub enum UnOpType { /// Bitwise Not BNot, + + /// Logical Not + LNot, } #[derive(Debug, PartialEq, Eq, Clone)] diff --git a/src/interpreter.rs b/src/interpreter.rs index 69c367e..2c859aa 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -94,6 +94,7 @@ impl Interpreter { match (operand, uo) { (Value::I64(val), UnOpType::Negate) => Value::I64(-val), (Value::I64(val), UnOpType::BNot) => Value::I64(!val), + (Value::I64(val), UnOpType::LNot) => Value::I64(if val == 0 { 1 } else { 0 }), // _ => panic!("Value type is not compatible with unary operation"), } } diff --git a/src/lexer.rs b/src/lexer.rs index e87cbbf..25187c9 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -80,6 +80,7 @@ impl<'a> Lexer<'a> { '=' => tokens.push(Token::Equ), '{' => tokens.push(Token::LBraces), '}' => tokens.push(Token::RBraces), + '!' => tokens.push(Token::LNot), // Lex numbers ch @ '0'..='9' => { diff --git a/src/parser.rs b/src/parser.rs index 3a8efcc..503e924 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -202,10 +202,17 @@ impl> Parser { Expression::UnOp(UnOpType::Negate, operand.into()) } + // Unary bitwise not (bitflip) Token::Tilde => { let operand = self.parse_primary(); Expression::UnOp(UnOpType::BNot, operand.into()) } + + // Unary logical not + Token::LNot => { + let operand = self.parse_primary(); + Expression::UnOp(UnOpType::LNot, operand.into()) + } tok => panic!("Error parsing primary expr: Unexpected Token '{:?}'", tok), } diff --git a/src/token.rs b/src/token.rs index 443ce93..39746f9 100644 --- a/src/token.rs +++ b/src/token.rs @@ -77,6 +77,9 @@ pub enum Token { /// Tilde (~) Tilde, + /// Logical not (!) + LNot, + /// Left angle bracket (<) LAngle,