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.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/users/tazjin/rlox/src/bytecode/vm.rs b/users/tazjin/rlox/src/bytecode/vm.rs
index 9161bdfe53b8..53431a083785 100644
--- a/users/tazjin/rlox/src/bytecode/vm.rs
+++ b/users/tazjin/rlox/src/bytecode/vm.rs
@@ -1,3 +1,5 @@
+use std::collections::HashMap;
+
 use super::chunk;
 use super::errors::*;
 use super::interner::Interner;
@@ -14,6 +16,8 @@ pub struct VM {
     stack: Vec<Value>,
     strings: Interner,
 
+    globals: HashMap<LoxString, Value>,
+
     // Operations that consume values from the stack without pushing
     // anything leave their last value in this slot, which makes it
     // possible to return values from interpreters that ran code which
@@ -157,6 +161,15 @@ impl VM {
                 OpCode::OpPop => {
                     self.last_drop = Some(self.pop());
                 }
+
+                OpCode::OpDefineGlobal(name_idx) => {
+                    let name = self.chunk.constant(*name_idx);
+                    with_type!(self, name, Value::String(name), {
+                        let name = name.clone();
+                        let val = self.pop();
+                        self.globals.insert(name, val);
+                    });
+                }
             }
 
             #[cfg(feature = "disassemble")]
@@ -197,6 +210,7 @@ pub fn interpret(strings: Interner, chunk: chunk::Chunk) -> LoxResult<Value> {
     let mut vm = VM {
         chunk,
         strings,
+        globals: HashMap::new(),
         ip: 0,
         stack: vec![],
         last_drop: None,