Implement relational binops

This commit is contained in:
Daniel M 2022-01-29 21:48:55 +01:00
parent 88ceacd500
commit e80cae11c9
4 changed files with 54 additions and 9 deletions

View File

@ -19,13 +19,13 @@
- [x] Unary operators - [x] Unary operators
- [x] Negate `-X` - [x] Negate `-X`
- [x] Parentheses `(X+Y)*Z` - [x] Parentheses `(X+Y)*Z`
- [ ] Logical boolean operators - [x] Logical boolean operators
- [x] Equal `==` - [x] Equal `==`
- [x] Not equal `!=` - [x] Not equal `!=`
- [ ] Greater than `>` - [x] Greater than `>`
- [ ] Less than `<` - [x] Less than `<`
- [ ] Greater than or equal `>=` - [x] Greater than or equal `>=`
- [ ] Less than or equal `<=` - [x] Less than or equal `<=`
- [x] Bitwise operators - [x] Bitwise operators
- [x] Bitwise AND `X&Y` - [x] Bitwise AND `X&Y`
- [x] Bitwise OR `X|Y` - [x] Bitwise OR `X|Y`

View File

@ -56,6 +56,10 @@ impl Interpreter {
BinOpType::Shl => Value::I64(lhs << rhs), BinOpType::Shl => Value::I64(lhs << rhs),
BinOpType::EquEqu => Value::I64(if lhs == rhs { 1 } else { 0 }), BinOpType::EquEqu => Value::I64(if lhs == rhs { 1 } else { 0 }),
BinOpType::NotEqu => Value::I64(if lhs != rhs { 1 } else { 0 }), BinOpType::NotEqu => Value::I64(if lhs != rhs { 1 } else { 0 }),
BinOpType::Less => Value::I64(if lhs < rhs { 1 } else { 0 }),
BinOpType::LessEqu => Value::I64(if lhs <= rhs { 1 } else { 0 }),
BinOpType::Greater => Value::I64(if lhs > rhs { 1 } else { 0 }),
BinOpType::GreaterEqu => Value::I64(if lhs >= rhs { 1 } else { 0 }),
}, },
// _ => panic!("Value types are not compatible"), // _ => panic!("Value types are not compatible"),
} }

View File

@ -52,6 +52,18 @@ pub enum Token {
/// Tilde (~) /// Tilde (~)
Tilde, Tilde,
/// Left angle bracket (<)
LAngle,
/// Right angle bracket (>)
RAngle,
/// Left angle bracket Equal (<=)
LAngleEqu,
/// Left angle bracket Equal (>=)
RAngleEqu,
/// End of file /// End of file
EoF, EoF,
} }
@ -114,7 +126,15 @@ impl<'a> Lexer<'a> {
self.next(); self.next();
tokens.push(Token::NotEqu); tokens.push(Token::NotEqu);
} }
'<' if matches!(self.peek(), Some('=')) => {
self.next();
tokens.push(Token::LAngleEqu);
}
'>' if matches!(self.peek(), Some('=')) => {
self.next();
tokens.push(Token::RAngleEqu);
}
'+' => tokens.push(Token::Add), '+' => tokens.push(Token::Add),
'-' => tokens.push(Token::Sub), '-' => tokens.push(Token::Sub),
'*' => tokens.push(Token::Mul), '*' => tokens.push(Token::Mul),
@ -126,6 +146,8 @@ impl<'a> Lexer<'a> {
'(' => tokens.push(Token::LParen), '(' => tokens.push(Token::LParen),
')' => tokens.push(Token::RParen), ')' => tokens.push(Token::RParen),
'~' => tokens.push(Token::Tilde), '~' => tokens.push(Token::Tilde),
'<' => tokens.push(Token::LAngle),
'>' => tokens.push(Token::RAngle),
//TODO: Don't panic, keep calm //TODO: Don't panic, keep calm
_ => panic!("Lexer encountered unexpected char: '{}'", ch), _ => panic!("Lexer encountered unexpected char: '{}'", ch),
@ -174,6 +196,12 @@ impl Token {
Token::EquEqu => BinOpType::EquEqu, Token::EquEqu => BinOpType::EquEqu,
Token::NotEqu => BinOpType::NotEqu, Token::NotEqu => BinOpType::NotEqu,
Token::LAngle => BinOpType::Less,
Token::LAngleEqu => BinOpType::LessEqu,
Token::RAngle => BinOpType::Greater,
Token::RAngleEqu => BinOpType::GreaterEqu,
_ => return None, _ => return None,
}) })
} }

View File

@ -26,6 +26,18 @@ pub enum BinOpType {
/// Compare Not Equal /// Compare Not Equal
NotEqu, NotEqu,
/// Less than
Less,
/// Less than or Equal
LessEqu,
/// Greater than
Greater,
/// Greater than or Equal
GreaterEqu,
/// Bitwise OR (inclusive or) /// Bitwise OR (inclusive or)
BOr, BOr,
@ -184,9 +196,10 @@ impl BinOpType {
BinOpType::BXor => 1, BinOpType::BXor => 1,
BinOpType::BAnd => 2, BinOpType::BAnd => 2,
BinOpType::EquEqu | BinOpType::NotEqu => 3, BinOpType::EquEqu | BinOpType::NotEqu => 3,
BinOpType::Shl | BinOpType::Shr => 4, BinOpType::Less | BinOpType::LessEqu | BinOpType::Greater | BinOpType::GreaterEqu => 4,
BinOpType::Add | BinOpType::Sub => 5, BinOpType::Shl | BinOpType::Shr => 5,
BinOpType::Mul | BinOpType::Div | BinOpType::Mod => 6, BinOpType::Add | BinOpType::Sub => 6,
BinOpType::Mul | BinOpType::Div | BinOpType::Mod => 7,
} }
} }
} }