forked from sass/libsass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.hpp
163 lines (135 loc) · 3.74 KB
/
environment.hpp
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
#ifndef SASS_ENVIRONMENT_H
#define SASS_ENVIRONMENT_H
#include <map>
#include <string>
#include <iostream>
#include "ast_fwd_decl.hpp"
#include "ast_def_macros.hpp"
#include "memory_manager.hpp"
namespace Sass {
using std::string;
using std::map;
using std::cerr;
using std::endl;
template <typename T>
class Environment {
// TODO: test with unordered_map
map<string, T> local_frame_;
ADD_PROPERTY(Environment*, parent);
public:
Memory_Manager<AST_Node> mem;
Environment() : local_frame_(map<string, T>()), parent_(0) { }
// link parent to create a stack
void link(Environment& env) { parent_ = &env; }
void link(Environment* env) { parent_ = env; }
// this is used to find the global frame
// which is the second last on the stack
bool 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
bool is_root_scope() const
{
return parent_ && ! parent_->parent_;
}
// scope operates on the current frame
map<string, T>& local_frame() {
return local_frame_;
}
bool has_local(const string& key) const
{ return local_frame_.count(key); }
T& get_local(const string& key)
{ return local_frame_[key]; }
void set_local(const string& key, T val)
{ local_frame_[key] = val; }
void del_local(const string& key)
{ local_frame_.erase(key); }
// global operates on the global frame
// which is the second last on the stack
Environment* global_env()
{
Environment* cur = this;
while (cur->is_lexical()) {
cur = cur->parent_;
}
return cur;
}
bool has_global(const string& key)
{ return global_env()->has(key); }
T& get_global(const string& key)
{ return (*global_env())[key]; }
void set_global(const string& key, T val)
{ global_env()->local_frame_[key] = val; }
void del_global(const string& key)
{ global_env()->local_frame_.erase(key); }
// see if we have a lexical variable
// move down the stack but stop before we
// reach the global frame (is not included)
bool has_lexical(const 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
void set_lexical(const string& key, T val)
{
auto cur = this;
while (cur->is_lexical()) {
if (cur->has_local(key)) {
cur->set_local(key, val);
return;
}
cur = cur->parent_;
}
set_local(key, val);
}
// look on the full stack for key
// include all scopes available
bool has(const string& key) const
{
auto cur = this;
while (cur) {
if (cur->has_local(key)) {
return true;
}
cur = cur->parent_;
}
return false;
}
// use array access for getter and setter functions
T& operator[](const 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
void print()
{
for (typename map<string, T>::iterator i = local_frame_.begin(); i != local_frame_.end(); ++i) {
cerr << i->first << endl;
}
if (parent_) {
cerr << "---" << endl;
parent_->print();
}
}
#endif
};
}
#endif