From 29b2a547055ba1adaf3f0d79055b7d7657eb3a5e Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 5 Mar 2021 17:48:07 +0200 Subject: feat(tazjin/rlox): Implement global variable definition identifier_str might look a bit overengineered, but we want to reuse this bit of code and it needs a reference to the token from which to pick the identifier. The problem with this is that the token would be owned by self, but the function needs to mutate (the interner), so this implementation is the most straightforward way of acquiring and working with an immutable reference to the token before interning the identifier. Change-Id: I618ce8f789cb59b3a9c5b79a13111ea6d00b2424 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2592 Reviewed-by: tazjin Tested-by: BuildkiteCI --- users/tazjin/rlox/src/bytecode/vm.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'users/tazjin/rlox/src/bytecode/vm.rs') 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, strings: Interner, + globals: HashMap, + // 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 { let mut vm = VM { chunk, strings, + globals: HashMap::new(), ip: 0, stack: vec![], last_drop: None, -- cgit 1.4.1