diff --git a/src/instructions/common.rs b/src/instructions/common.rs index 8e04a3b..1e9c348 100644 --- a/src/instructions/common.rs +++ b/src/instructions/common.rs @@ -1,3 +1,5 @@ +use rust_decimal::Decimal; + use crate::push::state::{Gene, PushState}; /// Acts as a NoOp, does nothing with the vals list. @@ -7,6 +9,28 @@ fn _noop(_: Vec) -> Option { make_instruction_clone!(code, code, _noop, Gene, 0); make_instruction_clone!(exec, exec, _noop, Gene, 0); +/// Pops the top value from the stack +fn _pop(vals: Vec) -> Option +where + T: Clone +{ + // This is suboptimal, how to re-write? + // Calls for a complete overhaul later down the line. + Some(vals[0].clone()) +} +make_instruction_no_out!(int, _pop, i128, 1); +make_instruction_no_out!(float, _pop, Decimal, 1); +make_instruction_no_out!(string, _pop, Vec, 1); +make_instruction_no_out!(boolean, _pop, bool, 1); +make_instruction_no_out!(char, _pop, char, 1); +make_instruction_no_out!(vector_int, _pop, Vec, 1); +make_instruction_no_out!(vector_float, _pop, Vec, 1); +make_instruction_no_out!(vector_string, _pop, Vec>, 1); +make_instruction_no_out!(vector_boolean, _pop, Vec, 1); +make_instruction_no_out!(vector_char, _pop, Vec, 1); +make_instruction_no_out!(code, _pop, Gene, 1); +make_instruction_no_out!(exec, _pop, Gene, 1); + #[cfg(test)] mod tests { use super::*; @@ -26,4 +50,19 @@ mod tests { exec_noop(&mut test_state); assert_eq!(test_state, test_state_copy); } + + #[test] + fn pop_test() { + let mut test_state = EMPTY_STATE; + + test_state.int = vec![1, 2]; + int_pop(&mut test_state); + assert_eq!(vec![1], test_state.int); + test_state.int.clear(); + + test_state.code = vec![Gene::GeneInt(4)]; + code_pop(&mut test_state); + let empty_vec: Vec = vec![]; + assert_eq!(empty_vec, test_state.code); + } }