I'm still wondering what I want to do
This commit is contained in:
parent
a6094bbed4
commit
e0414009ff
@ -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<T>(_: Vec<T>) -> Option<T> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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<i128>,
|
||||
pub float: Vec<Decimal>,
|
||||
|
Loading…
x
Reference in New Issue
Block a user