diff options
author | Vincent Ambo <mail@tazj.in> | 2021-10-19T20·59+0200 |
---|---|---|
committer | tazjin <mail@tazj.in> | 2021-10-20T12·50+0000 |
commit | 050a2b473c48b87994e56ade381afbfc2bca4de3 (patch) | |
tree | 97dd9dc0c36418cf967befd9ba672c93b095c561 /users/tazjin/rlox/src/bytecode/compiler.rs | |
parent | e92a951f6cd14b5e207e5a1b04ea93cf97ba92d0 (diff) |
refactor(tazjin/rlox): Introduce newtypes for various indices r/2980
Since this code is essentially a fairly plain translation from C, it is a bit confusing to deal with the original untyped code. This is an attempt to try and clean some of it up. Change-Id: Icd21f531932e1a811c0d6dbf2e9acba61ca9c45d Reviewed-on: https://cl.tvl.fyi/c/depot/+/3734 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.rs | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/users/tazjin/rlox/src/bytecode/compiler.rs b/users/tazjin/rlox/src/bytecode/compiler.rs index 28b00cbb6ed8..02757a7a1422 100644 --- a/users/tazjin/rlox/src/bytecode/compiler.rs +++ b/users/tazjin/rlox/src/bytecode/compiler.rs @@ -1,7 +1,7 @@ use super::chunk::Chunk; use super::errors::{Error, ErrorKind, LoxResult}; use super::interner::{InternedStr, Interner}; -use super::opcode::OpCode; +use super::opcode::{ConstantIdx, OpCode, StackIdx}; use super::value::Value; use crate::scanner::{self, Token, TokenKind}; @@ -234,7 +234,7 @@ impl<T: Iterator<Item = Token>> Compiler<T> { self.define_variable(global) } - fn define_variable(&mut self, var: usize) -> LoxResult<()> { + fn define_variable(&mut self, var: ConstantIdx) -> LoxResult<()> { if self.locals.scope_depth == 0 { self.emit_op(OpCode::OpDefineGlobal(var)); } else { @@ -493,12 +493,12 @@ impl<T: Iterator<Item = Token>> Compiler<T> { Ok(self.strings.intern(ident)) } - fn identifier_constant(&mut self) -> LoxResult<usize> { + fn identifier_constant(&mut self) -> LoxResult<ConstantIdx> { let ident = self.identifier_str(Self::previous)?; Ok(self.emit_constant(Value::String(ident.into()), false)) } - fn resolve_local(&mut self) -> Option<usize> { + fn resolve_local(&mut self) -> Option<StackIdx> { dbg!(&self.locals); for (idx, local) in self.locals.locals.iter().enumerate().rev() { if self.previous().lexeme == local.name.lexeme { @@ -506,21 +506,20 @@ impl<T: Iterator<Item = Token>> Compiler<T> { // TODO(tazjin): *return* err panic!("can't read variable in its own initialiser"); } - return Some(idx); + return Some(StackIdx(idx)); } } None } - fn add_local(&mut self, name: Token) -> LoxResult<()> { + fn add_local(&mut self, name: Token) { let local = Local { name, depth: Depth::Unitialised, }; self.locals.locals.push(local); - Ok(()) // TODO(tazjin): needed? } fn declare_variable(&mut self) -> LoxResult<()> { @@ -543,10 +542,11 @@ impl<T: Iterator<Item = Token>> Compiler<T> { } } - self.add_local(name) + self.add_local(name); + Ok(()) } - fn parse_variable(&mut self) -> LoxResult<usize> { + fn parse_variable(&mut self) -> LoxResult<ConstantIdx> { consume!( self, TokenKind::Identifier(_), @@ -555,7 +555,7 @@ impl<T: Iterator<Item = Token>> Compiler<T> { self.declare_variable()?; if self.locals.scope_depth > 0 { - return Ok(0); // TODO(tazjin): grr sentinel + return Ok(ConstantIdx(0)); // TODO(tazjin): grr sentinel } let id = self.identifier_str(Self::previous)?; @@ -583,8 +583,8 @@ impl<T: Iterator<Item = Token>> Compiler<T> { self.current_chunk().add_op(op, line); } - fn emit_constant(&mut self, val: Value, with_op: bool) -> usize { - let idx = self.chunk.add_constant(val); + fn emit_constant(&mut self, val: Value, with_op: bool) -> ConstantIdx { + let idx = ConstantIdx(self.chunk.add_constant(val)); if with_op { self.emit_op(OpCode::OpConstant(idx)); |