pop instructions

This commit is contained in:
Rowan Torbitzky-Lane 2025-04-10 15:02:12 -05:00
parent c7482cd0ba
commit 23bb693fa3

View File

@ -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<T>(_: Vec<T>) -> Option<T> {
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<T>(vals: Vec<T>) -> Option<T>
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<char>, 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<i128>, 1);
make_instruction_no_out!(vector_float, _pop, Vec<Decimal>, 1);
make_instruction_no_out!(vector_string, _pop, Vec<Vec<char>>, 1);
make_instruction_no_out!(vector_boolean, _pop, Vec<bool>, 1);
make_instruction_no_out!(vector_char, _pop, Vec<char>, 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<Gene> = vec![];
assert_eq!(empty_vec, test_state.code);
}
}