-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAST.h
239 lines (186 loc) · 4.76 KB
/
AST.h
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
#pragma once
#include <string>
#include <vector>
#include <iostream>
#include <functional>
struct ASTNode {
std::string val;
std::vector<ASTNode*> children;
};
inline void printAstNode (std::ostream& os, ASTNode const& node, int tabDepth){
for(int i = 0; i < tabDepth; ++i)
os << "|\t";
os << "| " << node.val << "\n";
for(auto* n : node.children) {
printAstNode(os, *n, tabDepth + 1);
}
}
inline std::ostream& operator<<(std::ostream& os, ASTNode const& node) {
printAstNode(os, node, 0);
return os;
}
// Macros to generate the names of the different types of nodes quickly
#define NODE_TYPES()\
X(NODE_SCOPE)\
X(NODE_START)\
X(NODE_TYPE)\
X(NODE_DECL)\
X(NODE_FDECL)\
X(NODE_ASSN)\
X(NODE_STMT)\
X(NODE_LITR)\
X(NODE_MAIN)\
X(NODE_EMPTY)\
X(NODE_BREAK)\
X(NODE_RETURN)\
X(NODE_IF)\
X(NODE_IFELSE)\
X(NODE_BCOND)\
X(NODE_WHILE)\
X(NODE_WHILE_END)\
X(NODE_ACTUALS)\
X(NODE_FCALL)\
X(NODE_UNARY)\
X(NODE_NOTUNARY)\
X(NODE_STAR)\
X(NODE_SLASH)\
X(NODE_PERCT)\
X(NODE_ADD)\
X(NODE_SUB)\
X(NODE_GT)\
X(NODE_LT)\
X(NODE_LE)\
X(NODE_GE)\
X(NODE_QUANT)\
X(NODE_QUANTU)\
X(NODE_QUANTB)\
X(NODE_EQUALITY)\
X(NODE_AND)\
X(NODE_OR)\
X(NODE_LOGIC)\
X(NODE_BLOCK)\
X(NODE_PARAM)\
X(NODE_PARAM_LIST)\
X(NODE_FHEADER)\
X(NODE_ID)
enum ast_node_types {
#define X(NAME) NAME,
NODE_TYPES()
#undef X
TOTAL_NODES
};
inline char const* getName(int val) {
switch(val) {
#define X(NAME) case NAME: return #NAME;
NODE_TYPES()
#undef X
default: return "";
}
}
#undef NODE_TYPES
enum prog_value_types {
INT,
BOOL,
VOID
};
// A type to store all the information about a declaration in a node
// Covers three cases of a declaration
// 0: no declaration
// 1: an id in isolation
// 2: a declaration with a type
struct Decl {
Decl() : valueType(0), name(), type() {}
Decl(std::string name) : valueType(1), name(name), type() {}
Decl(std::string name, std::vector<std::string> type) : valueType(2), name(name), type(type) {}
std::string name;
int valueType;
std::vector<std::string> type;
bool isGlobal;
};
// Function to print declarations, used in the generic function printAst
inline std::ostream& operator<<(std::ostream& os, Decl const& decl) {
if(decl.valueType == 0)
return os;
os << "name=" << decl.name;
if(decl.valueType == 2){
os << ", type=";
for(int i = 0;i<decl.type.size(); i++){
os<< decl.type[i];
}
}
return os;
}
// Templated node type with some data as it's child type
template<class Data>
struct node_impl {
int type;
Data data;
std::vector<node_impl<Data>*> children;
unsigned int lineNumber;
};
// As node is being used in the parser, it requires them to be in the namespace yy
namespace yy {
// alias node_impl<Decl> as node
using node = node_impl<Decl>;
}
// Generic function to print ast information
template<class T>
inline void printAst(node_impl<T>* n, int depth = 0) {
for(int i = 0; i < depth; ++i)
std::cout << " ";
std::cout << getName(n->type) << "{ ";
std::cout << n->data;
std::cout << " }\n";
for(auto* c : n->children) {
printAst(c, depth + 1);
}
}
// return value is only used during the pre-traversal to break out early!
// when pre returns false, stop traversal
// when pre returns true, continue traversal
template<class Data>
bool pre_post_traversal (node_impl<Data>* node
, std::function<bool(node_impl<Data>*)> pre
, std::function<void(node_impl<Data>*)> post) {
if(!pre(node))
return false;
for(auto* c : node->children) {
if(!pre_post_traversal(c, pre, post))
return false;
}
post(node);
return true;
}
// implement other traversals in terms of more general traversal
template<class Data>
bool pre_traversal(node_impl<Data>* node, std::function<bool(node_impl<Data>*)> const& pre) {
return pre_post_traversal(node, pre, [](auto*) {});
}
template<class Data>
void post_traversal(node_impl<Data>* node, std::function<void(node_impl<Data>*)> const& post) {
pre_post_traversal(node, [](auto*) { return true; }, post);
}
// return value is only used during the pre-traversal to break out early!
// when pre returns false, stop traversal
// when pre returns true, continue traversal
template <class Data, class PreFunc, class PostFunc>
bool pre_post_traversal(node_impl<Data>* node, PreFunc pre, PostFunc post) {
if (!pre(node))
return false;
for (auto* c : node->children) {
if (!pre_post_traversal(c, pre, post))
return false;
}
post(node);
return true;
}
// implement other traversals in terms of more general traversal
template <class Data, class Func>
bool pre_traversal(node_impl<Data>* node, Func pre) {
return pre_post_traversal(node, pre, [](auto*) {});
}
template <class Data, class Func>
void post_traversal(node_impl<Data>* node, Func post) {
pre_post_traversal(
node, [](auto*) { return true; }, post);
}