about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode/vm.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-01-17T21·08+0300
committertazjin <mail@tazj.in>2021-01-17T21·17+0000
commitd6d3c12efbcec61b3d868bc7d3f861fdb91835a5 (patch)
treefeabb720b4412f15ee798d095d3705ea8b3dd193 /users/tazjin/rlox/src/bytecode/vm.rs
parent7fb93fb49008491184a7d55ccd43db846452dce0 (diff)
feat(tazjin/rlox): Implement simple arithmetic operators r/2128
Change-Id: I9873bcd281053f4e9820a5119f5992a0b8cb8cfc
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2417
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
Diffstat (limited to 'users/tazjin/rlox/src/bytecode/vm.rs')
-rw-r--r--users/tazjin/rlox/src/bytecode/vm.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/users/tazjin/rlox/src/bytecode/vm.rs b/users/tazjin/rlox/src/bytecode/vm.rs
index 1b9c4a235940..fd91cd7be627 100644
--- a/users/tazjin/rlox/src/bytecode/vm.rs
+++ b/users/tazjin/rlox/src/bytecode/vm.rs
@@ -23,6 +23,14 @@ impl VM {
     }
 }
 
+macro_rules! binary_op {
+    ( $vm:ident, $op:tt ) => {{
+        let a = $vm.pop();
+        let b = $vm.pop();
+        $vm.push(a $op b);
+    }}
+}
+
 impl VM {
     fn run(&mut self) -> LoxResult<()> {
         loop {
@@ -43,6 +51,16 @@ impl VM {
                     let c = *self.chunk.constant(*idx);
                     self.push(c);
                 }
+
+                OpCode::OpNegate => {
+                    let v = self.pop();
+                    self.push(-v)
+                }
+
+                OpCode::OpAdd => binary_op!(self, +),
+                OpCode::OpSubtract => binary_op!(self, -),
+                OpCode::OpMultiply => binary_op!(self, *),
+                OpCode::OpDivide => binary_op!(self, /),
             }
         }
     }