1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! The Stack struct is the backbone for the VM. It maintains all of the values that are operated on.
//! The methods on the stack allow the stack to be changed and modified. The owner of the stack is the VM.
//! These methods should not be accessed outside of the VM struct as it could cause unexpected behavior.

use crate::errors::{error::Error, error_kind::ErrorKind};
use std::fmt::Debug;

#[derive(Debug)]
pub struct Stack<T: Debug + PartialEq>(pub Vec<T>);

impl<T: Debug + PartialEq> Stack<T> {
    /// Constructs an empty stack.
    pub fn new() -> Stack<T> {
        Stack(vec![])
    }

    /// This function pushes the given value on to the stack.
    ///
    /// # Arguments
    /// `value` - The value to push on to the stack.
    pub fn push(&mut self, value: T) {
        self.0.push(value)
    }

    /// This function pop the top value on to the stack. This may result in an error if the stack is empty.
    ///
    /// # Arguments
    /// `pos` - The position where the pop was called. This is used if there was error.
    pub fn pop(&mut self, pos: usize) -> Result<T, Error> {
        self.0
            .pop()
            .ok_or_else(|| Error::new(ErrorKind::EmptyStack, pos))
    }

    /// This function returns a reference to the top value on the stack, without consuming it.
    /// If the stack is empty, None is returned.
    pub fn peek(&self) -> Option<&T> {
        if self.0.is_empty() {
            None
        } else {
            self.0.last()
        }
    }

    /// This function returns a reference to the top value on the stack, without consuming it.
    /// If the stack is empty, None is returned.
    pub fn peek_mut(&mut self) -> Option<&mut T> {
        if self.0.is_empty() {
            None
        } else {
            self.0.last_mut()
        }
    }

    /// This function returns true if there are no elements in the stack.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}