feat(tazjin/rlox): Global variable assignment

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>
This commit is contained in:
Vincent Ambo 2021-10-02 15:55:58 +03:00 committed by tazjin
parent 6a38600ce8
commit ad7e591c80
4 changed files with 38 additions and 3 deletions

View file

@ -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(())
}