From 93c30b339c4904e86ac6046bd2a67c0d872a6a3b Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 28 Feb 2021 15:32:41 +0200 Subject: refactor(tazjin/rlox): Let binary_op! work on different types 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 Tested-by: BuildkiteCI --- users/tazjin/rlox/src/bytecode/vm.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'users') diff --git a/users/tazjin/rlox/src/bytecode/vm.rs b/users/tazjin/rlox/src/bytecode/vm.rs index b7a394afbe..acfb522a9c 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")] -- cgit 1.4.1