Implement comparison binops

This commit is contained in:
Daniel M 2022-01-29 21:37:44 +01:00
parent 1079eb1671
commit 88ceacd500
4 changed files with 33 additions and 5 deletions

View File

@ -20,8 +20,8 @@
- [x] Negate `-X`
- [x] Parentheses `(X+Y)*Z`
- [ ] Logical boolean operators
- [ ] Equal `==`
- [ ] Not equal `!=`
- [x] Equal `==`
- [x] Not equal `!=`
- [ ] Greater than `>`
- [ ] Less than `<`
- [ ] Greater than or equal `>=`

View File

@ -54,6 +54,8 @@ impl Interpreter {
BinOpType::BXor => Value::I64(lhs ^ rhs),
BinOpType::Shr => Value::I64(lhs >> rhs),
BinOpType::Shl => Value::I64(lhs << rhs),
BinOpType::EquEqu => Value::I64(if lhs == rhs { 1 } else { 0 }),
BinOpType::NotEqu => Value::I64(if lhs != rhs { 1 } else { 0 }),
},
// _ => panic!("Value types are not compatible"),
}

View File

@ -28,6 +28,12 @@ pub enum Token {
/// Percent (%)
Mod,
/// Equal Equal (==)
EquEqu,
/// Exclamationmark Equal (!=)
NotEqu,
/// Pipe (|)
BOr,
@ -100,6 +106,15 @@ impl<'a> Lexer<'a> {
self.next();
tokens.push(Token::Shl);
}
'=' if matches!(self.peek(), Some('=')) => {
self.next();
tokens.push(Token::EquEqu);
}
'!' if matches!(self.peek(), Some('=')) => {
self.next();
tokens.push(Token::NotEqu);
}
'+' => tokens.push(Token::Add),
'-' => tokens.push(Token::Sub),
'*' => tokens.push(Token::Mul),
@ -155,6 +170,10 @@ impl Token {
Token::Shl => BinOpType::Shl,
Token::Shr => BinOpType::Shr,
Token::EquEqu => BinOpType::EquEqu,
Token::NotEqu => BinOpType::NotEqu,
_ => return None,
})
}

View File

@ -20,6 +20,12 @@ pub enum BinOpType {
/// Modulo
Mod,
/// Compare Equal
EquEqu,
/// Compare Not Equal
NotEqu,
/// Bitwise OR (inclusive or)
BOr,
@ -177,9 +183,10 @@ impl BinOpType {
BinOpType::BOr => 0,
BinOpType::BXor => 1,
BinOpType::BAnd => 2,
BinOpType::Shl | BinOpType::Shr => 3,
BinOpType::Add | BinOpType::Sub => 4,
BinOpType::Mul | BinOpType::Div | BinOpType::Mod => 5,
BinOpType::EquEqu | BinOpType::NotEqu => 3,
BinOpType::Shl | BinOpType::Shr => 4,
BinOpType::Add | BinOpType::Sub => 5,
BinOpType::Mul | BinOpType::Div | BinOpType::Mod => 6,
}
}
}