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
use std::cell::RefCell;
use std::collections::BTreeSet;
use std::rc::Rc;
#[derive(Debug, Clone, Default)]
pub struct StaticEnvironment {
variables: Rc<RefCell<BTreeSet<String>>>,
stack: Rc<RefCell<Vec<BTreeSet<String>>>>,
}
const FORWARD_ARGS: &str = "FORWARD_ARGS";
impl StaticEnvironment {
pub fn new() -> Self {
Self {
variables: Rc::new(RefCell::new(BTreeSet::new())),
stack: Rc::new(RefCell::new(vec![])),
}
}
pub(crate) fn is_empty(&self) -> bool {
self.stack.borrow().is_empty()
}
#[allow(dead_code)]
pub(crate) fn reset(&self) {
self.variables.borrow_mut().clear();
self.stack.borrow_mut().clear();
}
pub fn extend_static(&self) {
let variables = std::mem::take(&mut *self.variables.borrow_mut());
self.stack.borrow_mut().push(variables);
}
pub fn extend_dynamic(&self) {
self.stack
.borrow_mut()
.push(self.variables.borrow().clone());
}
pub fn unextend(&self) {
*self.variables.borrow_mut() = self
.stack
.borrow_mut()
.pop()
.expect("expected static_env to have at least one frame");
}
pub fn declare(&self, name: &str) {
self.variables.borrow_mut().insert(name.to_string());
}
pub fn is_declared(&self, name: &str) -> bool {
self.variables.borrow().get(name).is_some()
}
pub(crate) fn declare_forward_args(&self) {
self.declare(FORWARD_ARGS);
}
pub(crate) fn is_forward_args_declared(&self) -> bool {
self.is_declared(FORWARD_ARGS)
}
}