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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use crate::Value;
use std::iter;
use std::mem;
use thiserror::Error;
#[derive(Debug, Error)]
#[error("tried to access out-of-bounds stack entry")]
pub struct StackError(());
#[derive(Debug, Clone)]
pub struct Stack {
stack: Vec<Value>,
stack_top: usize,
}
impl Stack {
pub const fn new() -> Self {
Self {
stack: Vec::new(),
stack_top: 0,
}
}
pub fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = Value>,
{
self.stack.extend(iter);
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
stack: Vec::with_capacity(capacity),
stack_top: 0,
}
}
pub fn clear(&mut self) {
self.stack.clear();
self.stack_top = 0;
}
#[inline]
pub fn peek(&mut self) -> Option<&Value> {
self.stack.last()
}
#[inline]
pub fn last(&self) -> Result<&Value, StackError> {
self.stack.last().ok_or_else(|| StackError(()))
}
pub fn at_offset(&self, offset: usize) -> Result<&Value, StackError> {
self.stack_top
.checked_add(offset)
.and_then(|n| self.stack.get(n))
.ok_or_else(|| StackError(()))
}
pub fn at_offset_mut(&mut self, offset: usize) -> Result<&mut Value, StackError> {
let n = match self.stack_top.checked_add(offset) {
Some(n) => n,
None => return Err(StackError(())),
};
match self.stack.get_mut(n) {
Some(value) => Ok(value),
None => Err(StackError(())),
}
}
pub fn push(&mut self, value: Value) {
self.stack.push(value);
}
pub fn pop(&mut self) -> Result<Value, StackError> {
if self.stack.len() == self.stack_top {
return Err(StackError(()));
}
self.stack.pop().ok_or_else(|| StackError(()))
}
pub fn popn(&mut self, count: usize) -> Result<(), StackError> {
drop(self.drain_stack_top(count)?);
Ok(())
}
pub fn is_empty(&self) -> bool {
self.stack.is_empty()
}
pub fn len(&self) -> usize {
self.stack.len()
}
pub fn iter(&self) -> impl Iterator<Item = &Value> + '_ {
self.stack.iter()
}
pub fn pop_sequence(&mut self, count: usize) -> Result<Vec<Value>, StackError> {
Ok(self.drain_stack_top(count)?.rev().collect::<Vec<_>>())
}
pub(crate) fn drain_stack_top(
&mut self,
count: usize,
) -> Result<impl DoubleEndedIterator<Item = Value> + '_, StackError> {
match self.stack.len().checked_sub(count) {
Some(start) if start >= self.stack_top => Ok(self.stack.drain(start..)),
_ => Err(StackError(())),
}
}
pub(crate) fn swap_stack_top(&mut self, count: usize) -> Result<usize, StackError> {
match self.stack.len().checked_sub(count) {
Some(new_top) => Ok(mem::replace(&mut self.stack_top, new_top)),
None => Err(StackError(())),
}
}
pub(crate) fn check_stack_top(&self) -> Result<(), StackError> {
if self.stack.len() == self.stack_top {
return Ok(());
}
Err(StackError(()))
}
pub(crate) fn pop_stack_top(&mut self, stack_top: usize) -> Result<(), StackError> {
self.check_stack_top()?;
self.stack_top = stack_top;
Ok(())
}
}
impl iter::FromIterator<Value> for Stack {
fn from_iter<T: IntoIterator<Item = Value>>(iter: T) -> Self {
Self {
stack: iter.into_iter().collect(),
stack_top: 0,
}
}
}
impl From<Vec<Value>> for Stack {
fn from(stack: Vec<Value>) -> Self {
Self {
stack,
stack_top: 0,
}
}
}