Implement logical not

This commit is contained in:
2022-02-02 19:14:11 +01:00
parent 75b99869d4
commit 39bd4400b4
6 changed files with 18 additions and 2 deletions

View File

@@ -69,6 +69,9 @@ pub enum UnOpType {
/// Bitwise Not
BNot,
/// Logical Not
LNot,
}
#[derive(Debug, PartialEq, Eq, Clone)]

View File

@@ -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"),
}
}

View File

@@ -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' => {

View File

@@ -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),
}

View File

@@ -77,6 +77,9 @@ pub enum Token {
/// Tilde (~)
Tilde,
/// Logical not (!)
LNot,
/// Left angle bracket (<)
LAngle,