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.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/users/tazjin/rlox/src/bytecode/vm.rs b/users/tazjin/rlox/src/bytecode/vm.rs
index 02db30de58a9..c43b197279be 100644
--- a/users/tazjin/rlox/src/bytecode/vm.rs
+++ b/users/tazjin/rlox/src/bytecode/vm.rs
@@ -72,6 +72,10 @@ impl VM {
 
             match op {
                 OpCode::OpReturn => {
+                    if self.stack.is_empty() {
+                        return Ok(Value::Nil);
+                    }
+
                     let val = self.pop();
                     return Ok(self.return_value(val));
                 }
@@ -135,6 +139,11 @@ impl VM {
                         })
                     }
                 }
+
+                OpCode::OpPrint => {
+                    let val = self.pop();
+                    println!("{}", self.print_value(val));
+                }
             }
 
             #[cfg(feature = "disassemble")]
@@ -159,6 +168,16 @@ impl VM {
             LoxString::Interned(id) => self.strings.lookup(*id),
         }
     }
+
+    fn print_value(&self, val: Value) -> String {
+        match val {
+            Value::String(LoxString::Heap(s)) => s,
+            Value::String(LoxString::Interned(id)) => {
+                self.strings.lookup(id).into()
+            }
+            _ => format!("{:?}", val),
+        }
+    }
 }
 
 pub fn interpret(strings: Interner, chunk: chunk::Chunk) -> LoxResult<Value> {