Implement equ, neq comparison

This commit is contained in:
Daniel M 2022-01-28 14:46:55 +01:00
parent 3c6fb5466e
commit 6816392173
3 changed files with 31 additions and 3 deletions

View File

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

View File

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

View File

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