Implement logical and / or

This commit is contained in:
Kai-Philipp Nosper 2022-02-02 18:56:45 +01:00
parent 92f59cbf9a
commit de0bbb8171
5 changed files with 36 additions and 8 deletions

View File

@ -43,6 +43,12 @@ pub enum BinOpType {
/// Bitwise Xor (exclusive or) /// Bitwise Xor (exclusive or)
BXor, BXor,
/// Logical And
LAnd,
/// Logical Or
LOr,
/// Shift Left /// Shift Left
Shl, Shl,

View File

@ -128,6 +128,8 @@ impl Interpreter {
BinOpType::BOr => Value::I64(lhs | rhs), BinOpType::BOr => Value::I64(lhs | rhs),
BinOpType::BAnd => Value::I64(lhs & rhs), BinOpType::BAnd => Value::I64(lhs & rhs),
BinOpType::BXor => 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::Shr => Value::I64(lhs >> rhs),
BinOpType::Shl => Value::I64(lhs << rhs), BinOpType::Shl => Value::I64(lhs << rhs),
BinOpType::EquEqu => Value::I64(if lhs == rhs { 1 } else { 0 }), BinOpType::EquEqu => Value::I64(if lhs == rhs { 1 } else { 0 }),

View File

@ -51,6 +51,15 @@ impl<'a> Lexer<'a> {
self.next(); self.next();
tokens.push(Token::LArrow); 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) // Line comment. Consume every char until linefeed (next line)
'/' if matches!(self.peek(), '/') => while self.next() != '\n' {}, '/' if matches!(self.peek(), '/') => while self.next() != '\n' {},

View File

@ -239,14 +239,16 @@ impl BinOpType {
match self { match self {
BinOpType::Declare => 0, BinOpType::Declare => 0,
BinOpType::Assign => 1, BinOpType::Assign => 1,
BinOpType::BOr => 2, BinOpType::LOr => 2,
BinOpType::BXor => 3, BinOpType::LAnd => 3,
BinOpType::BAnd => 4, BinOpType::BOr => 4,
BinOpType::EquEqu | BinOpType::NotEqu => 5, BinOpType::BXor => 5,
BinOpType::Less | BinOpType::LessEqu | BinOpType::Greater | BinOpType::GreaterEqu => 6, BinOpType::BAnd => 6,
BinOpType::Shl | BinOpType::Shr => 7, BinOpType::EquEqu | BinOpType::NotEqu => 7,
BinOpType::Add | BinOpType::Sub => 8, BinOpType::Less | BinOpType::LessEqu | BinOpType::Greater | BinOpType::GreaterEqu => 8,
BinOpType::Mul | BinOpType::Div | BinOpType::Mod => 9, BinOpType::Shl | BinOpType::Shr => 9,
BinOpType::Add | BinOpType::Sub => 10,
BinOpType::Mul | BinOpType::Div | BinOpType::Mod => 11,
} }
} }
} }

View File

@ -62,6 +62,12 @@ pub enum Token {
/// Circumflex (^) /// Circumflex (^)
BXor, BXor,
/// Logical AND (&&)
LAnd,
/// Logical OR (||)
LOr,
/// Shift Left (<<) /// Shift Left (<<)
Shl, Shl,
@ -110,6 +116,9 @@ impl Token {
Token::BOr => BinOpType::BOr, Token::BOr => BinOpType::BOr,
Token::BXor => BinOpType::BXor, Token::BXor => BinOpType::BXor,
Token::LAnd => BinOpType::LAnd,
Token::LOr => BinOpType::LOr,
Token::Shl => BinOpType::Shl, Token::Shl => BinOpType::Shl,
Token::Shr => BinOpType::Shr, Token::Shr => BinOpType::Shr,