about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode
diff options
context:
space:
mode:
Diffstat (limited to 'users/tazjin/rlox/src/bytecode')
-rw-r--r--users/tazjin/rlox/src/bytecode/chunk.rs1
-rw-r--r--users/tazjin/rlox/src/bytecode/mod.rs7
-rw-r--r--users/tazjin/rlox/src/bytecode/opcode.rs9
-rw-r--r--users/tazjin/rlox/src/bytecode/vm.rs18
4 files changed, 34 insertions, 1 deletions
diff --git a/users/tazjin/rlox/src/bytecode/chunk.rs b/users/tazjin/rlox/src/bytecode/chunk.rs
index 4a671c838363..5d71df2b05a4 100644
--- a/users/tazjin/rlox/src/bytecode/chunk.rs
+++ b/users/tazjin/rlox/src/bytecode/chunk.rs
@@ -66,6 +66,7 @@ impl Chunk {
 
 /// Print a single disassembled instruction at the specified offset.
 /// Some instructions are printed "raw", others have special handling.
+#[cfg(feature = "disassemble")]
 pub fn disassemble_instruction(chunk: &Chunk, offset: usize) {
     print!("{:04} ", offset);
 
diff --git a/users/tazjin/rlox/src/bytecode/mod.rs b/users/tazjin/rlox/src/bytecode/mod.rs
index de04d7b06f4d..2d83c4272239 100644
--- a/users/tazjin/rlox/src/bytecode/mod.rs
+++ b/users/tazjin/rlox/src/bytecode/mod.rs
@@ -16,7 +16,12 @@ pub fn main() {
 
     let constant = chunk.add_constant(1.2);
     chunk.add_op(OpCode::OpConstant(constant), 1);
-    chunk.add_op(OpCode::OpReturn, 1);
+
+    let constant = chunk.add_constant(2.0);
+    chunk.add_op(OpCode::OpConstant(constant), 2);
+
+    chunk.add_op(OpCode::OpAdd, 3);
+    chunk.add_op(OpCode::OpReturn, 4);
 
     vm::interpret(chunk).expect("it should work");
 }
diff --git a/users/tazjin/rlox/src/bytecode/opcode.rs b/users/tazjin/rlox/src/bytecode/opcode.rs
index f2b6a9f1baf3..0f070ce9fffc 100644
--- a/users/tazjin/rlox/src/bytecode/opcode.rs
+++ b/users/tazjin/rlox/src/bytecode/opcode.rs
@@ -5,4 +5,13 @@ pub enum OpCode {
 
     /// Return from the current function.
     OpReturn,
+
+    /// Unary negation
+    OpNegate,
+
+    // Arithmetic operators
+    OpAdd,
+    OpSubtract,
+    OpMultiply,
+    OpDivide,
 }
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, /),
             }
         }
     }