dark_vm/utils/
stack.rs

1//! The Stack struct is the backbone for the VM. It maintains all of the values that are operated on.
2//! The methods on the stack allow the stack to be changed and modified. The owner of the stack is the VM.
3//! These methods should not be accessed outside of the VM struct as it could cause unexpected behavior.
4
5use crate::errors::{error::Error, error_kind::ErrorKind};
6use std::fmt::Debug;
7
8#[derive(Debug)]
9pub struct Stack<T: Debug + PartialEq>(pub Vec<T>);
10
11impl<T: Debug + PartialEq> Stack<T> {
12    /// Constructs an empty stack.
13    pub fn new() -> Stack<T> {
14        Stack(vec![])
15    }
16
17    /// This function pushes the given value on to the stack.
18    ///
19    /// # Arguments
20    /// `value` - The value to push on to the stack.
21    pub fn push(&mut self, value: T) {
22        self.0.push(value)
23    }
24
25    /// This function pop the top value on to the stack. This may result in an error if the stack is empty.
26    ///
27    /// # Arguments
28    /// `pos` - The position where the pop was called. This is used if there was error.
29    pub fn pop(&mut self, pos: usize) -> Result<T, Error> {
30        self.0
31            .pop()
32            .ok_or_else(|| Error::new(ErrorKind::EmptyStack, pos))
33    }
34
35    /// This function returns a reference to the top value on the stack, without consuming it.
36    /// If the stack is empty, None is returned.
37    pub fn peek(&self) -> Option<&T> {
38        if self.0.is_empty() {
39            None
40        } else {
41            self.0.last()
42        }
43    }
44
45    /// This function returns a reference to the top value on the stack, without consuming it.
46    /// If the stack is empty, None is returned.
47    pub fn peek_mut(&mut self) -> Option<&mut T> {
48        if self.0.is_empty() {
49            None
50        } else {
51            self.0.last_mut()
52        }
53    }
54
55    /// This function returns true if there are no elements in the stack.
56    pub fn is_empty(&self) -> bool {
57        self.0.is_empty()
58    }
59}