diff --git a/src/instructions/mod.rs b/src/instructions/mod.rs index 2b93ae6..b28db2e 100644 --- a/src/instructions/mod.rs +++ b/src/instructions/mod.rs @@ -100,6 +100,63 @@ pub mod macros { } }; } + + /// Attempt to make this function using a reference in the internal + /// function call rather than the inputs being moved into the function. + #[macro_export] + macro_rules! make_instruction_ref { + ($in_stack:ident, $out_stack:ident, $fn_name:ident, $fn_type:ty, $fn_arity:stmt) => { + paste::item! { + /// Runs the $fn_name function on the top $fn_arity items from + /// the $in_stack and places the calculated value on the $out_stack. + pub fn [< $in_stack $fn_name >] (state: &mut PushState) { + let in_stack_len = state.$in_stack.len(); + if in_stack_len < $fn_arity { + return; + } + let mut inputs: Vec<$fn_type> = Vec::with_capacity($fn_arity); + for n in 1..=$fn_arity { + inputs.push(state.$in_stack[in_stack_len - n]); + } + if let Some(result) = $fn_name(&inputs) { + for _ in 0..$fn_arity { + state.$in_stack.pop(); + } + state.$out_stack.push(result); + } + } + } + }; + } + + /// Attempt to make this function using a reference in the internal + /// function call rather than the inputs being moved into the function. + /// Names the function with the out_stack rather than the in_stack. + #[macro_export] + macro_rules! make_instruction_ref_out { + ($in_stack:ident, $out_stack:ident, $fn_name:ident, $fn_type:ty, $fn_arity:stmt) => { + paste::item! { + /// Runs the $fn_name function on the top $fn_arity items from + /// the $in_stack and places the calculated value on the $out_stack. + pub fn [< $out_stack $fn_name >] (state: &mut PushState) { + let in_stack_len = state.$in_stack.len(); + if in_stack_len < $fn_arity { + return; + } + let mut inputs: Vec<$fn_type> = Vec::with_capacity($fn_arity); + for n in 1..=$fn_arity { + inputs.push(state.$in_stack[in_stack_len - n]); + } + if let Some(result) = $fn_name(&inputs) { + for _ in 0..$fn_arity { + state.$in_stack.pop(); + } + state.$out_stack.push(result); + } + } + } + }; + } } pub mod code;