about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode/compiler.rs
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/tazjin/rlox/src/bytecode/compiler.rs
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/tazjin/rlox/src/bytecode/compiler.rs')
-rw-r--r--users/tazjin/rlox/src/bytecode/compiler.rs28
1 files changed, 28 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"),
         }