refactor(tazjin/rlox): Introduce newtypes for various indices

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>
This commit is contained in:
Vincent Ambo 2021-10-19 22:59:48 +02:00 committed by tazjin
parent e92a951f6c
commit 050a2b473c
4 changed files with 31 additions and 25 deletions

View file

@ -1,6 +1,6 @@
use std::ops::Index;
use super::opcode::OpCode;
use super::opcode::{ConstantIdx, OpCode};
use super::value;
// In the book, this type is a hand-rolled dynamic array
@ -38,8 +38,8 @@ impl Chunk {
idx
}
pub fn constant(&self, idx: usize) -> &value::Value {
self.constants.index(idx)
pub fn constant(&self, idx: ConstantIdx) -> &value::Value {
self.constants.index(idx.0)
}
fn add_line(&mut self, line: usize) {
@ -79,7 +79,7 @@ pub fn disassemble_instruction(chunk: &Chunk, offset: usize) {
match chunk.code.index(offset) {
OpCode::OpConstant(idx) => {
println!("OpConstant({}) '{:?}'", *idx, chunk.constant(*idx))
println!("OpConstant({:?}) '{:?}'", idx, chunk.constant(*idx))
}
op => println!("{:?}", op),
}