Implement comparison binops

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

View File

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

View File

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

View File

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

View File

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