reference macros

This commit is contained in:
Rowan Torbitzky-Lane 2025-04-07 15:42:08 -05:00
parent 2e6369fb0c
commit 0cbe372b98

View File

@ -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;