Implement logical not
This commit is contained in:
parent
3c0e5f6b4d
commit
2946e67349
@ -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`
|
||||
|
||||
@ -69,6 +69,9 @@ pub enum UnOpType {
|
||||
|
||||
/// Bitwise Not
|
||||
BNot,
|
||||
|
||||
/// Logical Not
|
||||
LNot,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
|
||||
@ -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"),
|
||||
}
|
||||
}
|
||||
|
||||
@ -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' => {
|
||||
|
||||
@ -202,10 +202,17 @@ impl<T: Iterator<Item = Token>> Parser<T> {
|
||||
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),
|
||||
}
|
||||
|
||||
@ -77,6 +77,9 @@ pub enum Token {
|
||||
/// Tilde (~)
|
||||
Tilde,
|
||||
|
||||
/// Logical not (!)
|
||||
LNot,
|
||||
|
||||
/// Left angle bracket (<)
|
||||
LAngle,
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user