about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode/compiler.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-10-02T12·55+0300
committertazjin <mail@tazj.in>2021-10-19T12·58+0000
commitad7e591c8046d6e179476192ef9928d5fae78422 (patch)
tree52347fd8cdd8f7d888746abbd6f66a3a2946de83 /users/tazjin/rlox/src/bytecode/compiler.rs
parent6a38600ce88bbf2d9fa9a3821d7df3ebd8e0d4f3 (diff)
feat(tazjin/rlox): Global variable assignment r/2977
Needed for example code compatibility.

Change-Id: Id83210eaaad7dcfef5aa238dd3a7ec159f6935e9
Reviewed-on: https://cl.tvl.fyi/c/depot/+/3684
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
Diffstat (limited to 'users/tazjin/rlox/src/bytecode/compiler.rs')
-rw-r--r--users/tazjin/rlox/src/bytecode/compiler.rs12
1 files changed, 10 insertions, 2 deletions
diff --git a/users/tazjin/rlox/src/bytecode/compiler.rs b/users/tazjin/rlox/src/bytecode/compiler.rs
index b8b91667d3fa..a6675d1c20b6 100644
--- a/users/tazjin/rlox/src/bytecode/compiler.rs
+++ b/users/tazjin/rlox/src/bytecode/compiler.rs
@@ -236,7 +236,8 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
     fn expression_statement(&mut self) -> LoxResult<()> {
         self.expression()?;
         self.expect_semicolon("expect ';' after expression")?;
-        self.emit_op(OpCode::OpPop);
+        // TODO(tazjin): Why did I add this originally?
+        // self.emit_op(OpCode::OpPop);
         Ok(())
     }
 
@@ -343,7 +344,14 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
         let ident = self.identifier_str(Self::previous)?;
         let constant_id =
             self.emit_constant(Value::String(ident.into()), false);
-        self.emit_op(OpCode::OpGetGlobal(constant_id));
+
+        if self.match_token(&TokenKind::Equal) {
+            self.expression()?;
+            self.emit_op(OpCode::OpSetGlobal(constant_id));
+        } else {
+            self.emit_op(OpCode::OpGetGlobal(constant_id));
+        }
+
         Ok(())
     }