forked from tweag/nickel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.rs
230 lines (197 loc) · 6.07 KB
/
stack.rs
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//! Define the main evaluation stack of the Nickel abstract machine and related operations.
//!
//! See [eval](../eval/index.html).
use crate::eval::Closure;
use crate::operation::OperationCont;
use crate::position::RawSpan;
use std::cell::RefCell;
use std::rc::Weak;
/// An element of the stack.
#[derive(Debug)]
pub enum Marker {
/// An argument of an application.
Arg(Closure, Option<RawSpan>),
/// A thunk, which is pointer to a mutable memory cell to be updated.
Thunk(Weak<RefCell<Closure>>),
/// The continuation of a primitive operation.
Cont(
OperationCont,
usize, /*callStack size*/
Option<RawSpan>, /*position span of the operation*/
),
}
impl Marker {
pub fn is_arg(&self) -> bool {
match *self {
Marker::Arg(_, _) => true,
Marker::Thunk(_) => false,
Marker::Cont(_, _, _) => false,
}
}
pub fn is_thunk(&self) -> bool {
match *self {
Marker::Arg(_, _) => false,
Marker::Thunk(_) => true,
Marker::Cont(_, _, _) => false,
}
}
pub fn is_cont(&self) -> bool {
match *self {
Marker::Arg(_, _) => false,
Marker::Thunk(_) => false,
Marker::Cont(_, _, _) => true,
}
}
}
/// The evaluation stack.
#[derive(Debug)]
pub struct Stack(Vec<Marker>);
impl IntoIterator for Stack {
type Item = Marker;
type IntoIter = ::std::vec::IntoIter<Marker>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl Stack {
pub fn new() -> Stack {
Stack(Vec::new())
}
/// Count the number of consecutive elements satisfying `pred` from the top of the stack.
fn count<P>(&self, pred: P) -> usize
where
P: Fn(&Marker) -> bool,
{
let mut count = 0;
for marker in self.0.iter().rev() {
if pred(marker) {
count += 1;
} else {
break;
}
}
count
}
/// Count the number of arguments at the top of the stack.
pub fn count_args(&self) -> usize {
Stack::count(self, Marker::is_arg)
}
pub fn push_arg(&mut self, arg: Closure, pos: Option<RawSpan>) {
self.0.push(Marker::Arg(arg, pos))
}
pub fn push_thunk(&mut self, thunk: Weak<RefCell<Closure>>) {
self.0.push(Marker::Thunk(thunk))
}
pub fn push_op_cont(&mut self, cont: OperationCont, len: usize, pos: Option<RawSpan>) {
self.0.push(Marker::Cont(cont, len, pos))
}
pub fn pop_arg(&mut self) -> Option<(Closure, Option<RawSpan>)> {
match self.0.pop() {
Some(Marker::Arg(arg, pos)) => Some((arg, pos)),
Some(m) => {
self.0.push(m);
None
}
_ => None,
}
}
pub fn pop_thunk(&mut self) -> Option<Weak<RefCell<Closure>>> {
match self.0.pop() {
Some(Marker::Thunk(thunk)) => Some(thunk),
Some(m) => {
self.0.push(m);
None
}
_ => None,
}
}
pub fn pop_op_cont(&mut self) -> Option<(OperationCont, usize, Option<RawSpan>)> {
match self.0.pop() {
Some(Marker::Cont(cont, len, pos)) => Some((cont, len, pos)),
Some(m) => {
self.0.push(m);
None
}
_ => None,
}
}
/// Check if the top element is an argument.
pub fn is_top_thunk(&self) -> bool {
self.0.last().map(Marker::is_thunk).unwrap_or(false)
}
/// Check if the top element is an operation continuation.
pub fn is_top_cont(&self) -> bool {
self.0.last().map(Marker::is_cont).unwrap_or(false)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::term::{Term, UnaryOp};
use std::rc::Rc;
impl Stack {
/// Count the number of thunks at the top of the stack.
pub fn count_thunks(&self) -> usize {
Stack::count(self, Marker::is_thunk)
}
/// Count the number of operation continuation at the top of the stack.
pub fn count_conts(&self) -> usize {
Stack::count(self, Marker::is_cont)
}
}
fn some_closure() -> Closure {
Closure::atomic_closure(Term::Bool(true).into())
}
fn some_cont() -> OperationCont {
OperationCont::Op1(UnaryOp::IsZero(), None)
}
fn some_arg_marker() -> Marker {
Marker::Arg(some_closure(), None)
}
fn some_thunk_marker() -> Marker {
let rc = Rc::new(RefCell::new(some_closure()));
Marker::Thunk(Rc::downgrade(&rc))
}
fn some_cont_marker() -> Marker {
Marker::Cont(some_cont(), 42, None)
}
#[test]
fn marker_differentiates() {
assert!(some_arg_marker().is_arg());
assert!(some_thunk_marker().is_thunk());
assert!(some_cont_marker().is_cont());
}
#[test]
fn pushing_and_poping_args() {
let mut s = Stack::new();
assert_eq!(0, s.count_args());
s.push_arg(some_closure(), None);
s.push_arg(some_closure(), None);
assert_eq!(2, s.count_args());
assert_eq!(some_closure(), s.pop_arg().expect("Already checked").0);
assert_eq!(1, s.count_args());
}
#[test]
fn pushing_and_poping_thunks() {
let mut s = Stack::new();
assert_eq!(0, s.count_thunks());
s.push_thunk(Rc::downgrade(&Rc::new(RefCell::new(some_closure()))));
s.push_thunk(Rc::downgrade(&Rc::new(RefCell::new(some_closure()))));
assert_eq!(2, s.count_thunks());
s.pop_thunk().expect("Already checked");
assert_eq!(1, s.count_thunks());
}
#[test]
fn pushing_and_poping_conts() {
let mut s = Stack::new();
assert_eq!(0, s.count_conts());
s.push_op_cont(some_cont(), 3, None);
s.push_op_cont(some_cont(), 4, None);
assert_eq!(2, s.count_conts());
assert_eq!(
(some_cont(), 4, None),
s.pop_op_cont().expect("Already checked")
);
assert_eq!(1, s.count_conts());
}
}