refactor(tazjin/rlox): Represent VM values as enums

Introduces a new enum which represents the different types of possible
values, and modifies the rest of the existing code to wrap/unwrap
these enum variants correctly.

Notably in the vm module, a new macro has been introduced that makes
it possible to encode a type expectation and return a runtime error in
case of a type mismatch.

Change-Id: I325b5e31e395c62d8819ab2af6d398e1277333c0
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2570
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2021-02-28 14:37:35 +02:00 committed by tazjin
parent 6b990a7571
commit 127ef98486
6 changed files with 59 additions and 25 deletions

View file

@ -23,11 +23,34 @@ impl VM {
}
}
macro_rules! with_type {
( $self:ident, $val:ident, $type:pat, $body:expr ) => {
match $val {
$type => $body,
_ => {
return Err(Error {
line: $self.chunk.get_line($self.ip - 1),
kind: ErrorKind::TypeError(format!(
"Expected type {}, but found value: {:?}",
stringify!($type),
$val,
)),
})
}
}
};
}
macro_rules! binary_op {
( $vm:ident, $op:tt ) => {{
let b = $vm.pop();
let a = $vm.pop();
$vm.push(a $op b);
with_type!($vm, b, Value::Number(num_b), {
with_type!($vm, a, Value::Number(num_a), {
$vm.push(Value::Number(num_a $op num_b))
})
})
}}
}
@ -45,13 +68,18 @@ impl VM {
OpCode::OpReturn => return Ok(self.pop()),
OpCode::OpConstant(idx) => {
let c = *self.chunk.constant(*idx);
let c = self.chunk.constant(*idx).clone();
self.push(c);
}
OpCode::OpNegate => {
let v = self.pop();
self.push(-v)
with_type!(
self,
v,
Value::Number(num),
self.push(Value::Number(-num))
);
}
OpCode::OpAdd => binary_op!(self, +),