Implement equ, neq comparison
This commit is contained in:
parent
3c6fb5466e
commit
6816392173
@ -44,6 +44,8 @@ impl Interpreter {
|
||||
BinOpType::BXor => Value::I64(lhs ^ rhs),
|
||||
BinOpType::Shr => Value::I64(lhs >> rhs),
|
||||
BinOpType::Shl => Value::I64(lhs << rhs),
|
||||
BinOpType::Equ => Value::I64(if lhs == rhs { 1 } else { 0 }),
|
||||
BinOpType::Neq => Value::I64(if lhs != rhs { 1 } else { 0 }),
|
||||
},
|
||||
// _ => panic!("Value types are not compatible"),
|
||||
}
|
||||
|
||||
19
src/lexer.rs
19
src/lexer.rs
@ -43,6 +43,12 @@ pub enum Token {
|
||||
/// Shift Right (>>)
|
||||
Shr,
|
||||
|
||||
/// Equal sign (==)
|
||||
Equ,
|
||||
|
||||
/// Not Equal sign (!=)
|
||||
Neq,
|
||||
|
||||
/// End of file
|
||||
EoF,
|
||||
}
|
||||
@ -97,6 +103,15 @@ impl<'a> Lexer<'a> {
|
||||
self.next();
|
||||
tokens.push(Token::Shl);
|
||||
}
|
||||
'=' if matches!(self.peek(), Some('=')) => {
|
||||
self.next();
|
||||
tokens.push(Token::Equ);
|
||||
}
|
||||
'!' if matches!(self.peek(), Some('=')) => {
|
||||
self.next();
|
||||
tokens.push(Token::Neq);
|
||||
}
|
||||
|
||||
'+' => tokens.push(Token::Add),
|
||||
'-' => tokens.push(Token::Sub),
|
||||
'*' => tokens.push(Token::Mul),
|
||||
@ -151,6 +166,10 @@ impl Token {
|
||||
|
||||
Token::Shl => BinOpType::Shl,
|
||||
Token::Shr => BinOpType::Shr,
|
||||
|
||||
Token::Equ => BinOpType::Equ,
|
||||
Token::Neq => BinOpType::Neq,
|
||||
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
@ -34,6 +34,12 @@ pub enum BinOpType {
|
||||
|
||||
/// Shift Right
|
||||
Shr,
|
||||
|
||||
/// Check equality
|
||||
Equ,
|
||||
|
||||
/// Check unequality
|
||||
Neq,
|
||||
}
|
||||
|
||||
/// Types for unary operators
|
||||
@ -166,9 +172,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::Equ | BinOpType::Neq => 3,
|
||||
BinOpType::Shl | BinOpType::Shr => 4,
|
||||
BinOpType::Add | BinOpType::Sub => 5,
|
||||
BinOpType::Mul | BinOpType::Div | BinOpType::Mod => 6,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user