-
Notifications
You must be signed in to change notification settings - Fork 464
/
environment.cpp
260 lines (235 loc) · 6.73 KB
/
environment.cpp
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "sass.hpp"
#include "ast.hpp"
#include "environment.hpp"
namespace Sass {
template <typename T>
Environment<T>::Environment(bool is_shadow)
: local_frame_(environment_map<std::string, T>()),
parent_(0), is_shadow_(false)
{ }
template <typename T>
Environment<T>::Environment(Environment<T>* env, bool is_shadow)
: local_frame_(environment_map<std::string, T>()),
parent_(env), is_shadow_(is_shadow)
{ }
template <typename T>
Environment<T>::Environment(Environment<T>& env, bool is_shadow)
: local_frame_(environment_map<std::string, T>()),
parent_(&env), is_shadow_(is_shadow)
{ }
// link parent to create a stack
template <typename T>
void Environment<T>::link(Environment& env) { parent_ = &env; }
template <typename T>
void Environment<T>::link(Environment* env) { parent_ = env; }
// this is used to find the global frame
// which is the second last on the stack
template <typename T>
bool Environment<T>::is_lexical() const
{
return !! parent_ && parent_->parent_;
}
// only match the real root scope
// there is still a parent around
// not sure what it is actually use for
// I guess we store functions etc. there
template <typename T>
bool Environment<T>::is_global() const
{
return parent_ && ! parent_->parent_;
}
template <typename T>
environment_map<std::string, T>& Environment<T>::local_frame() {
return local_frame_;
}
template <typename T>
bool Environment<T>::has_local(const std::string& key) const
{ return local_frame_.find(key) != local_frame_.end(); }
template <typename T> EnvResult
Environment<T>::find_local(const std::string& key)
{
auto end = local_frame_.end();
auto it = local_frame_.find(key);
return EnvResult(it, it != end);
}
template <typename T>
T& Environment<T>::get_local(const std::string& key)
{ return local_frame_[key]; }
template <typename T>
void Environment<T>::set_local(const std::string& key, const T& val)
{
local_frame_[key] = val;
}
template <typename T>
void Environment<T>::set_local(const std::string& key, T&& val)
{
local_frame_[key] = val;
}
template <typename T>
void Environment<T>::del_local(const std::string& key)
{ local_frame_.erase(key); }
template <typename T>
Environment<T>* Environment<T>::global_env()
{
Environment* cur = this;
while (cur->is_lexical()) {
cur = cur->parent_;
}
return cur;
}
template <typename T>
bool Environment<T>::has_global(const std::string& key)
{ return global_env()->has(key); }
template <typename T>
T& Environment<T>::get_global(const std::string& key)
{ return (*global_env())[key]; }
template <typename T>
void Environment<T>::set_global(const std::string& key, const T& val)
{
global_env()->local_frame_[key] = val;
}
template <typename T>
void Environment<T>::set_global(const std::string& key, T&& val)
{
global_env()->local_frame_[key] = val;
}
template <typename T>
void Environment<T>::del_global(const std::string& key)
{ global_env()->local_frame_.erase(key); }
template <typename T>
Environment<T>* Environment<T>::lexical_env(const std::string& key)
{
Environment* cur = this;
while (cur) {
if (cur->has_local(key)) {
return cur;
}
cur = cur->parent_;
}
return this;
}
// see if we have a lexical variable
// move down the stack but stop before we
// reach the global frame (is not included)
template <typename T>
bool Environment<T>::has_lexical(const std::string& key) const
{
auto cur = this;
while (cur->is_lexical()) {
if (cur->has_local(key)) return true;
cur = cur->parent_;
}
return false;
}
// see if we have a lexical we could update
// either update already existing lexical value
// or if flag is set, we create one if no lexical found
template <typename T>
void Environment<T>::set_lexical(const std::string& key, const T& val)
{
Environment<T>* cur = this;
bool shadow = false;
while ((cur && cur->is_lexical()) || shadow) {
EnvResult rv(cur->find_local(key));
if (rv.found) {
rv.it->second = val;
return;
}
shadow = cur->is_shadow();
cur = cur->parent_;
}
set_local(key, val);
}
// this one moves the value
template <typename T>
void Environment<T>::set_lexical(const std::string& key, T&& val)
{
Environment<T>* cur = this;
bool shadow = false;
while ((cur && cur->is_lexical()) || shadow) {
EnvResult rv(cur->find_local(key));
if (rv.found) {
rv.it->second = val;
return;
}
shadow = cur->is_shadow();
cur = cur->parent_;
}
set_local(key, val);
}
// look on the full stack for key
// include all scopes available
template <typename T>
bool Environment<T>::has(const std::string& key) const
{
auto cur = this;
while (cur) {
if (cur->has_local(key)) {
return true;
}
cur = cur->parent_;
}
return false;
}
// look on the full stack for key
// include all scopes available
template <typename T> EnvResult
Environment<T>::find(const std::string& key)
{
auto cur = this;
while (true) {
EnvResult rv(cur->find_local(key));
if (rv.found) return rv;
cur = cur->parent_;
if (!cur) return rv;
}
};
// use array access for getter and setter functions
template <typename T>
T& Environment<T>::get(const std::string& key)
{
auto cur = this;
while (cur) {
if (cur->has_local(key)) {
return cur->get_local(key);
}
cur = cur->parent_;
}
return get_local(key);
}
// use array access for getter and setter functions
template <typename T>
T& Environment<T>::operator[](const std::string& key)
{
auto cur = this;
while (cur) {
if (cur->has_local(key)) {
return cur->get_local(key);
}
cur = cur->parent_;
}
return get_local(key);
}
/*
#ifdef DEBUG
template <typename T>
size_t Environment<T>::print(std::string prefix)
{
size_t indent = 0;
if (parent_) indent = parent_->print(prefix) + 1;
std::cerr << prefix << std::string(indent, ' ') << "== " << this << std::endl;
for (typename environment_map<std::string, T>::iterator i = local_frame_.begin(); i != local_frame_.end(); ++i) {
if (!ends_with(i->first, "[f]") && !ends_with(i->first, "[f]4") && !ends_with(i->first, "[f]2")) {
std::cerr << prefix << std::string(indent, ' ') << i->first << " " << i->second;
if (Value* val = Cast<Value>(i->second))
{ std::cerr << " : " << val->to_string(); }
std::cerr << std::endl;
}
}
return indent ;
}
#endif
*/
// compile implementation for AST_Node
template class Environment<AST_Node_Obj>;
}