forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_ast.cpp
More file actions
202 lines (169 loc) · 4.72 KB
/
Copy pathsimple_ast.cpp
File metadata and controls
202 lines (169 loc) · 4.72 KB
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
#include "simple_ast.h"
// Ref methods
Ref& Ref::operator[](unsigned x) {
return (*get())[x];
}
Ref& Ref::operator[](IString x) {
return (*get())[x];
}
bool Ref::operator==(const char *str) {
return get()->isString() && !strcmp(get()->str.str, str);
}
bool Ref::operator!=(const char *str) {
return get()->isString() ? !!strcmp(get()->str.str, str) : true;
}
bool Ref::operator==(const IString &str) {
return get()->isString() && get()->str == str;
}
bool Ref::operator!=(const IString &str) {
return get()->isString() && get()->str != str;
}
bool Ref::operator==(Ref other) {
return **this == *other;
}
bool Ref::operator!() {
return !get() || get()->isNull();
}
// Arena
Arena arena;
Ref Arena::alloc() {
if (chunks.size() == 0 || index == CHUNK_SIZE) {
chunks.push_back(new Value[CHUNK_SIZE]);
index = 0;
}
return &chunks.back()[index++];
}
// dump
void dump(const char *str, Ref node, bool pretty) {
std::cerr << str << ": ";
if (!!node) node->stringify(std::cerr, pretty);
else std::cerr << "(nullptr)";
std::cerr << std::endl;
}
// AST traversals
// Traversals
struct TraverseInfo {
TraverseInfo() {}
TraverseInfo(Ref node, int index) : node(node), index(index) {}
Ref node;
int index;
};
template <class T, int init>
struct StackedStack { // a stack, on the stack
T stackStorage[init];
T* storage;
int used, available; // used amount, available amount
bool alloced;
StackedStack() : used(0), available(init), alloced(false) {
storage = stackStorage;
}
~StackedStack() {
if (alloced) free(storage);
}
int size() { return used; }
void push_back(const T& t) {
assert(used <= available);
if (used == available) {
available *= 2;
if (!alloced) {
T* old = storage;
storage = (T*)malloc(sizeof(T)*available);
memcpy(storage, old, sizeof(T)*used);
alloced = true;
} else {
storage = (T*)realloc(storage, sizeof(T)*available);
}
}
assert(used < available);
assert(storage);
storage[used++] = t;
}
T& back() {
assert(used > 0);
return storage[used-1];
}
void pop_back() {
assert(used > 0);
used--;
}
};
#define visitable(node) (node->isArray() && node->size() > 0)
#define TRAV_STACK 40
// Traverse, calling visit before the children
void traversePre(Ref node, std::function<void (Ref)> visit) {
if (!visitable(node)) return;
visit(node);
StackedStack<TraverseInfo, TRAV_STACK> stack;
stack.push_back(TraverseInfo(node, 0));
while (stack.size() > 0) {
TraverseInfo& top = stack.back();
if (top.index < (int)top.node->size()) {
Ref sub = top.node[top.index];
top.index++;
if (visitable(sub)) {
visit(sub);
stack.push_back(TraverseInfo(sub, 0));
}
} else {
stack.pop_back();
}
}
}
// Traverse, calling visitPre before the children and visitPost after
void traversePrePost(Ref node, std::function<void (Ref)> visitPre, std::function<void (Ref)> visitPost) {
if (!visitable(node)) return;
visitPre(node);
StackedStack<TraverseInfo, TRAV_STACK> stack;
stack.push_back(TraverseInfo(node, 0));
while (stack.size() > 0) {
TraverseInfo& top = stack.back();
if (top.index < (int)top.node->size()) {
Ref sub = top.node[top.index];
top.index++;
if (visitable(sub)) {
visitPre(sub);
stack.push_back(TraverseInfo(sub, 0));
}
} else {
visitPost(top.node);
stack.pop_back();
}
}
}
// Traverse, calling visitPre before the children and visitPost after. If pre returns false, do not traverse children
void traversePrePostConditional(Ref node, std::function<bool (Ref)> visitPre, std::function<void (Ref)> visitPost) {
if (!visitable(node)) return;
if (!visitPre(node)) return;
StackedStack<TraverseInfo, TRAV_STACK> stack;
stack.push_back(TraverseInfo(node, 0));
while (stack.size() > 0) {
TraverseInfo& top = stack.back();
if (top.index < (int)top.node->size()) {
Ref sub = top.node[top.index];
top.index++;
if (visitable(sub)) {
if (visitPre(sub)) {
stack.push_back(TraverseInfo(sub, 0));
}
}
} else {
visitPost(top.node);
stack.pop_back();
}
}
}
// Traverses all the top-level functions in the document
void traverseFunctions(Ref ast, std::function<void (Ref)> visit) {
if (!ast || ast->size() == 0) return;
if (ast[0] == TOPLEVEL) {
Ref stats = ast[1];
for (size_t i = 0; i < stats->size(); i++) {
Ref curr = stats[i];
if (curr[0] == DEFUN) visit(curr);
}
} else if (ast[0] == DEFUN) {
visit(ast);
}
}
// ValueBuilder
IStringSet ValueBuilder::statable("assign call binary unary-prefix if label name num conditional dot new sub seq string object array");