about summary refs log tree commit diff
path: root/users
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-02-28T14·00+0200
committertazjin <mail@tazj.in>2021-02-28T14·36+0000
commit369f504250fe6508d3b927c6c8320a2525edfad1 (patch)
treed9df01a16a3a96a95f9089ccff96a8d5fae6b129 /users
parentc58fe2093e2a2870fc76ea55aa2de8e5aa6dab7d (diff)
feat(tazjin/rlox): Implement comparison operators r/2256
Change-Id: I03b751db52a3bd502fb4fbda6e89cad087ccad74
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2575
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Diffstat (limited to 'users')
-rw-r--r--users/tazjin/rlox/src/bytecode/compiler.rs28
-rw-r--r--users/tazjin/rlox/src/bytecode/opcode.rs2
-rw-r--r--users/tazjin/rlox/src/bytecode/tests.rs10
-rw-r--r--users/tazjin/rlox/src/bytecode/vm.rs3
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!(