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.rs34
1 files changed, 31 insertions, 3 deletions
diff --git a/users/tazjin/rlox/src/bytecode/vm.rs b/users/tazjin/rlox/src/bytecode/vm.rs
index 87c2aa440bf4..ee3abbd6cc18 100644
--- a/users/tazjin/rlox/src/bytecode/vm.rs
+++ b/users/tazjin/rlox/src/bytecode/vm.rs
@@ -23,11 +23,34 @@ impl VM {
     }
 }
 
+macro_rules! with_type {
+    ( $self:ident, $val:ident, $type:pat, $body:expr ) => {
+        match $val {
+            $type => $body,
+            _ => {
+                return Err(Error {
+                    line: $self.chunk.get_line($self.ip - 1),
+                    kind: ErrorKind::TypeError(format!(
+                        "Expected type {}, but found value: {:?}",
+                        stringify!($type),
+                        $val,
+                    )),
+                })
+            }
+        }
+    };
+}
+
 macro_rules! binary_op {
     ( $vm:ident, $op:tt ) => {{
         let b = $vm.pop();
         let a = $vm.pop();
-        $vm.push(a $op b);
+
+        with_type!($vm, b, Value::Number(num_b), {
+            with_type!($vm, a, Value::Number(num_a), {
+                $vm.push(Value::Number(num_a $op num_b))
+            })
+        })
     }}
 }
 
@@ -45,13 +68,18 @@ impl VM {
                 OpCode::OpReturn => return Ok(self.pop()),
 
                 OpCode::OpConstant(idx) => {
-                    let c = *self.chunk.constant(*idx);
+                    let c = self.chunk.constant(*idx).clone();
                     self.push(c);
                 }
 
                 OpCode::OpNegate => {
                     let v = self.pop();
-                    self.push(-v)
+                    with_type!(
+                        self,
+                        v,
+                        Value::Number(num),
+                        self.push(Value::Number(-num))
+                    );
                 }
 
                 OpCode::OpAdd => binary_op!(self, +),