Implement bitwise not
This commit is contained in:
parent
5ffa0ea2ec
commit
ea60f17647
@ -30,7 +30,7 @@
|
||||
- [x] Bitwise AND `X&Y`
|
||||
- [x] Bitwise OR `X|Y`
|
||||
- [x] Bitwise XOR `X^Y`
|
||||
- [ ] Bitwise NOT `~X`
|
||||
- [x] Bitwise NOT `~X`
|
||||
- [x] Bitwise left shift `X<<Y`
|
||||
- [x] Bitwise right shift `X>>Y`
|
||||
- [ ] Variables
|
||||
|
||||
@ -33,6 +33,7 @@ impl Interpreter {
|
||||
|
||||
match (operand, uo) {
|
||||
(Value::I64(val), UnOpType::Negate) => Value::I64(-val),
|
||||
(Value::I64(val), UnOpType::BNot) => Value::I64(!val),
|
||||
// _ => panic!("Value type is not compatible with unary operation"),
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,6 +43,9 @@ pub enum Token {
|
||||
/// Shift Right (>>)
|
||||
Shr,
|
||||
|
||||
/// Tilde (~)
|
||||
Tilde,
|
||||
|
||||
/// End of file
|
||||
EoF,
|
||||
}
|
||||
@ -107,6 +110,7 @@ impl<'a> Lexer<'a> {
|
||||
'^' => tokens.push(Token::BXor),
|
||||
'(' => tokens.push(Token::LParen),
|
||||
')' => tokens.push(Token::RParen),
|
||||
'~' => tokens.push(Token::Tilde),
|
||||
|
||||
//TODO: Don't panic, keep calm
|
||||
_ => panic!("Lexer encountered unexpected char: '{}'", ch),
|
||||
|
||||
@ -40,6 +40,9 @@ pub enum BinOpType {
|
||||
pub enum UnOpType {
|
||||
/// Unary Negate
|
||||
Negate,
|
||||
|
||||
/// Bitwise Not
|
||||
BNot,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
@ -55,7 +58,7 @@ pub enum Ast {
|
||||
/*
|
||||
## Grammar
|
||||
### Expressions
|
||||
expr_primary = LITERAL | "(" expr p | "-" expr_primary
|
||||
expr_primary = LITERAL | "(" expr p | "-" expr_primary | "~" expr_primary
|
||||
expr_mul = expr_primary (("*" | "/" | "%") expr_primary)*
|
||||
expr_add = expr_mul (("+" | "-") expr_mul)*
|
||||
expr_shift = expr_add ((">>" | "<<") expr_add)*
|
||||
@ -137,6 +140,11 @@ impl<T: Iterator<Item = Token>> Parser<T> {
|
||||
Ast::UnOp(UnOpType::Negate, operand.into())
|
||||
}
|
||||
|
||||
Token::Tilde => {
|
||||
let operand = self.parse_primary();
|
||||
Ast::UnOp(UnOpType::BNot, operand.into())
|
||||
}
|
||||
|
||||
tok => panic!("Error parsing primary expr: Unexpected Token '{:?}'", tok),
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user