Skip to main content

runmat_vm/interpreter/
stack.rs

1use crate::interpreter::errors::mex;
2use runmat_builtins::Value;
3use runmat_runtime::RuntimeError;
4
5#[inline]
6pub fn pop_value(stack: &mut Vec<Value>) -> Result<Value, RuntimeError> {
7    stack
8        .pop()
9        .ok_or_else(|| mex("StackUnderflow", "stack underflow"))
10}
11
12#[inline]
13pub fn pop2(stack: &mut Vec<Value>) -> Result<(Value, Value), RuntimeError> {
14    let b = pop_value(stack)?;
15    let a = pop_value(stack)?;
16    Ok((a, b))
17}
18
19#[inline]
20pub fn pop_args(stack: &mut Vec<Value>, argc: usize) -> Result<Vec<Value>, RuntimeError> {
21    let mut args = Vec::with_capacity(argc);
22    for _ in 0..argc {
23        args.push(pop_value(stack)?);
24    }
25    args.reverse();
26    Ok(args)
27}