diff options
author | Vincent Ambo <mail@tazj.in> | 2021-02-28T13·32+0200 |
---|---|---|
committer | tazjin <mail@tazj.in> | 2021-02-28T14·36+0000 |
commit | 93c30b339c4904e86ac6046bd2a67c0d872a6a3b (patch) | |
tree | 08eeda0d367d928dac75cae3d8d7a616bed63082 | |
parent | 2d9456d2474116d9a42213f692b13821eb28de81 (diff) |
refactor(tazjin/rlox): Let binary_op! work on different types r/2254
This makes it possible to specify the input & output types of the binary_op macro. If only one type is specified, it is assumed that the input and output types are the same. Change-Id: Idfcc9ba462db3976b69379b6693d091e1a525a3b Reviewed-on: https://cl.tvl.fyi/c/depot/+/2573 Reviewed-by: tazjin <mail@tazj.in> Tested-by: BuildkiteCI
-rw-r--r-- | users/tazjin/rlox/src/bytecode/vm.rs | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/users/tazjin/rlox/src/bytecode/vm.rs b/users/tazjin/rlox/src/bytecode/vm.rs index b7a394afbe3e..acfb522a9c47 100644 --- a/users/tazjin/rlox/src/bytecode/vm.rs +++ b/users/tazjin/rlox/src/bytecode/vm.rs @@ -42,16 +42,20 @@ macro_rules! with_type { } macro_rules! binary_op { - ( $vm:ident, $op:tt ) => {{ + ( $vm:ident, $type:tt, $op:tt ) => { + binary_op!($vm, $type, $type, $op) + }; + + ( $vm:ident, $in_type:tt, $out_type:tt, $op:tt ) => {{ let b = $vm.pop(); let a = $vm.pop(); - 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)) + with_type!($vm, b, Value::$in_type(val_b), { + with_type!($vm, a, Value::$in_type(val_a), { + $vm.push(Value::$out_type(val_a $op val_b)) }) }) - }} + }}; } impl VM { @@ -91,10 +95,10 @@ impl VM { ); } - OpCode::OpAdd => binary_op!(self, +), - OpCode::OpSubtract => binary_op!(self, -), - OpCode::OpMultiply => binary_op!(self, *), - OpCode::OpDivide => binary_op!(self, /), + OpCode::OpAdd => binary_op!(self, Number, +), + OpCode::OpSubtract => binary_op!(self, Number, -), + OpCode::OpMultiply => binary_op!(self, Number, *), + OpCode::OpDivide => binary_op!(self, Number, /), } #[cfg(feature = "disassemble")] |