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::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::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"),
|
// _ => panic!("Value types are not compatible"),
|
||||||
}
|
}
|
||||||
|
|||||||
19
src/lexer.rs
19
src/lexer.rs
@ -43,6 +43,12 @@ pub enum Token {
|
|||||||
/// Shift Right (>>)
|
/// Shift Right (>>)
|
||||||
Shr,
|
Shr,
|
||||||
|
|
||||||
|
/// Equal sign (==)
|
||||||
|
Equ,
|
||||||
|
|
||||||
|
/// Not Equal sign (!=)
|
||||||
|
Neq,
|
||||||
|
|
||||||
/// End of file
|
/// End of file
|
||||||
EoF,
|
EoF,
|
||||||
}
|
}
|
||||||
@ -97,6 +103,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::Equ);
|
||||||
|
}
|
||||||
|
'!' if matches!(self.peek(), Some('=')) => {
|
||||||
|
self.next();
|
||||||
|
tokens.push(Token::Neq);
|
||||||
|
}
|
||||||
|
|
||||||
'+' => tokens.push(Token::Add),
|
'+' => tokens.push(Token::Add),
|
||||||
'-' => tokens.push(Token::Sub),
|
'-' => tokens.push(Token::Sub),
|
||||||
'*' => tokens.push(Token::Mul),
|
'*' => tokens.push(Token::Mul),
|
||||||
@ -151,6 +166,10 @@ impl Token {
|
|||||||
|
|
||||||
Token::Shl => BinOpType::Shl,
|
Token::Shl => BinOpType::Shl,
|
||||||
Token::Shr => BinOpType::Shr,
|
Token::Shr => BinOpType::Shr,
|
||||||
|
|
||||||
|
Token::Equ => BinOpType::Equ,
|
||||||
|
Token::Neq => BinOpType::Neq,
|
||||||
|
|
||||||
_ => return None,
|
_ => return None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -34,6 +34,12 @@ pub enum BinOpType {
|
|||||||
|
|
||||||
/// Shift Right
|
/// Shift Right
|
||||||
Shr,
|
Shr,
|
||||||
|
|
||||||
|
/// Check equality
|
||||||
|
Equ,
|
||||||
|
|
||||||
|
/// Check unequality
|
||||||
|
Neq,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Types for unary operators
|
/// Types for unary operators
|
||||||
@ -166,9 +172,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::Equ | BinOpType::Neq => 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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user