diff options
Diffstat (limited to 'users/tazjin/rlox')
-rw-r--r-- | users/tazjin/rlox/src/bytecode/compiler.rs | 28 | ||||
-rw-r--r-- | users/tazjin/rlox/src/bytecode/opcode.rs | 2 | ||||
-rw-r--r-- | users/tazjin/rlox/src/bytecode/tests.rs | 10 | ||||
-rw-r--r-- | users/tazjin/rlox/src/bytecode/vm.rs | 3 |
4 files changed, 43 insertions, 0 deletions
diff --git a/users/tazjin/rlox/src/bytecode/compiler.rs b/users/tazjin/rlox/src/bytecode/compiler.rs index 8eef40f0e669..8d3f716d1647 100644 --- a/users/tazjin/rlox/src/bytecode/compiler.rs +++ b/users/tazjin/rlox/src/bytecode/compiler.rs @@ -128,6 +128,22 @@ fn rule_for<T: Iterator<Item = Token>>(token: &TokenKind) -> ParseRule<T> { ParseRule::new(None, Some(Compiler::binary), Precedence::Equality) } + TokenKind::Greater => { + ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison) + } + + TokenKind::GreaterEqual => { + ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison) + } + + TokenKind::Less => { + ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison) + } + + TokenKind::LessEqual => { + ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison) + } + _ => ParseRule::new(None, None, Precedence::None), } } @@ -209,6 +225,18 @@ impl<T: Iterator<Item = Token>> Compiler<T> { } TokenKind::EqualEqual => self.emit_op(OpCode::OpEqual), + TokenKind::Greater => self.emit_op(OpCode::OpGreater), + + TokenKind::GreaterEqual => { + self.emit_op(OpCode::OpLess); + self.emit_op(OpCode::OpNot); + } + + TokenKind::Less => self.emit_op(OpCode::OpLess), + TokenKind::LessEqual => { + self.emit_op(OpCode::OpGreater); + self.emit_op(OpCode::OpNot); + } _ => unreachable!("only called for binary operator tokens"), } diff --git a/users/tazjin/rlox/src/bytecode/opcode.rs b/users/tazjin/rlox/src/bytecode/opcode.rs index e50fdfed9338..78c1b2d8e3a5 100644 --- a/users/tazjin/rlox/src/bytecode/opcode.rs +++ b/users/tazjin/rlox/src/bytecode/opcode.rs @@ -14,6 +14,8 @@ pub enum OpCode { // Boolean & comparison operators OpNot, OpEqual, + OpGreater, + OpLess, /// Unary negation OpNegate, diff --git a/users/tazjin/rlox/src/bytecode/tests.rs b/users/tazjin/rlox/src/bytecode/tests.rs index 500779301b04..9b31d5469be1 100644 --- a/users/tazjin/rlox/src/bytecode/tests.rs +++ b/users/tazjin/rlox/src/bytecode/tests.rs @@ -88,3 +88,13 @@ fn equality() { expect_bool("42 == true", false); expect_bool("!42 == !true", true); } + +#[test] +fn comparisons() { + expect_bool("42 > 23", true); + expect_bool("42 < 23", false); + expect_bool("42 <= 42", true); + expect_bool("42 <= 23", false); + expect_bool("42 >= 42", true); + expect_bool("42 >= 23", true); +} diff --git a/users/tazjin/rlox/src/bytecode/vm.rs b/users/tazjin/rlox/src/bytecode/vm.rs index 0b24dc46cdb9..634d4eced146 100644 --- a/users/tazjin/rlox/src/bytecode/vm.rs +++ b/users/tazjin/rlox/src/bytecode/vm.rs @@ -91,6 +91,9 @@ impl VM { self.push(Value::Bool(a == b)); } + OpCode::OpLess => binary_op!(self, Number, Bool, <), + OpCode::OpGreater => binary_op!(self, Number, Bool, >), + OpCode::OpNegate => { let v = self.pop(); with_type!( |