labast/src/instructions/stack_manage.rs

45 lines
926 B
Rust

use crate::{errors::RunError, stack::StackVec};
pub fn push(memory: &mut StackVec, data: u16) {
memory.push(data);
}
pub fn pop(memory: &mut StackVec) {
memory.pop();
}
pub fn dup(memory: &mut StackVec) {
let data = memory.last();
memory.push(data);
}
pub fn swap(memory: &mut StackVec) {
let a: u16 = memory.pop();
let b: u16 = memory.pop();
memory.push(a);
memory.push(b);
}
pub fn pick(memory: &mut StackVec, data: u16) {
if data > memory.len() as u16 {
eprintln!("{}", RunError::PickTooDeep);
std::process::exit(2);
}
let arg = memory.pop();
memory.push(
*memory
.get(memory.len() - (arg as usize) - 1)
.expect(&format!("{}", RunError::PickOutOfBounds)),
);
}
pub fn string(memory: &mut StackVec, arg: String) {
for i in arg.chars() {
memory.push(i as u16);
}
memory.push(arg.len() as u16);
}