From e0414009ff737bc5f489950e0dd86eb7e9347ca1 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 7 Apr 2025 17:01:18 -0500 Subject: [PATCH] I'm still wondering what I want to do --- src/instructions/common.rs | 28 +++++++++++++++++++++++++++- src/instructions/mod.rs | 26 ++++++++++++++++++++++++++ src/push/state.rs | 2 +- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/instructions/common.rs b/src/instructions/common.rs index f759542..8e04a3b 100644 --- a/src/instructions/common.rs +++ b/src/instructions/common.rs @@ -1,3 +1,29 @@ use crate::push::state::{Gene, PushState}; -/// Swaps the top two values +/// Acts as a NoOp, does nothing with the vals list. +fn _noop(_: Vec) -> Option { + None +} +make_instruction_clone!(code, code, _noop, Gene, 0); +make_instruction_clone!(exec, exec, _noop, Gene, 0); + +#[cfg(test)] +mod tests { + use super::*; + use crate::push::state::EMPTY_STATE; + + #[test] + fn noop_test() { + let mut test_state = EMPTY_STATE; + + test_state.int = vec![1, 2]; + let test_state_copy = test_state.clone(); + code_noop(&mut test_state); + assert_eq!(test_state, test_state_copy); + + test_state.int = vec![1, 2]; + let test_state_copy = test_state.clone(); + exec_noop(&mut test_state); + assert_eq!(test_state, test_state_copy); + } +} diff --git a/src/instructions/mod.rs b/src/instructions/mod.rs index 2b93ae6..3405beb 100644 --- a/src/instructions/mod.rs +++ b/src/instructions/mod.rs @@ -100,6 +100,32 @@ pub mod macros { } }; } + + #[macro_export] + macro_rules! make_instruction_mult { + ($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.extend(result.iter()); + } + } + } + }; + } } pub mod code; diff --git a/src/push/state.rs b/src/push/state.rs index 8c8d65e..78b0596 100644 --- a/src/push/state.rs +++ b/src/push/state.rs @@ -4,7 +4,7 @@ use rust_decimal::prelude::*; /// /// I chose to use `rust_decimal` crate here because /// there are round off errors with the build in `f64`. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct PushState { pub int: Vec, pub float: Vec,