about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode/vm.rs
diff options
context:
space:
mode:
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, /),
             }
         }
     }