This repository has been archived by the owner on Jan 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast.hh
241 lines (202 loc) · 6.76 KB
/
ast.hh
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
// Copyright 2015-2018 RWTH Aachen University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* AST class that represets an abstract syntax tree
*
* Author: Sascha Schmerling
* Created: 2015/06/02
*/
#pragma once
#include <iostream>
#include <iomanip>
#include <vector>
#include <boost/variant.hpp>
#include <boost/algorithm/string.hpp>
using namespace std;
class Ast;
class AstVisitor;
class AstVisitableNode;
class AstOperation;
class AstConstant;
class AstId;
class AstFunction;
//Visitor pattern
//base class for the visitor that will used to get the type of a node
class AstVisitor {
public:
virtual void visit(AstOperation &) = 0;
virtual void visit(AstConstant &) = 0;
virtual void visit(AstId &) = 0;
virtual void visit(AstFunction &) = 0;
virtual void visit(Ast &) = 0;
virtual ~AstVisitor() {};
};
enum class AstNodeType : uint8_t {
Operation,
Constant,
Id,
Function
};
//Visitor pattern
//base class that allows use to traverse through the AST
//with a generic base type
class AstVisitableNode {
public:
virtual void accept(AstVisitor &v) = 0;
virtual ~AstVisitableNode() {}
AstNodeType nodeType;
};
class Ast {
public:
AstVisitableNode *root;
Ast(AstVisitableNode *initRoot) {
root = initRoot;
}
};
//all types with that will have a binary node (two children)
enum class AstOperationType : uint8_t {
ELIMINATED_NOT, //status that eliminated NOTs will become
NOT,
AND,
OR,
EQUAL,
NEQ,
LESS,
LEQ,
GREATER,
GEQ,
IS_TRUE,
IS_FALSE,
};
//a node that represents an operation with a list of children
class AstOperation : public AstVisitableNode {
public:
AstOperationType type;
//constructor for a operation with only one child
AstOperation(AstOperationType initType, AstVisitableNode *left) {
nodeType = AstNodeType::Operation;
type = initType;
addChild(left);
}
//constructor for a binary operation (two children)
AstOperation(AstOperationType initType, AstVisitableNode *left, AstVisitableNode *right) {
nodeType = AstNodeType::Operation;
type = initType;
addChild(left);
addChild(right);
}
uint8_t getNumberOfChildren() {
return children.size();
}
vector<AstVisitableNode *> &getChildren() {
return children;
}
AstVisitableNode *getChild(uint8_t pos) {
return children.at(pos);
}
void setChild(uint8_t pos, AstVisitableNode *node) {
children.at(pos) = node;
}
void addChild(AstVisitableNode *child) {
return children.push_back(child);
}
void accept(AstVisitor &visitor) {
visitor.visit(*this);
}
//delete all children when the node gets destroyed
~AstOperation() {
for(vector<AstVisitableNode *>::iterator it = children.begin(); it != children.end(); ++it)
delete *it;
}
vector<AstVisitableNode *> children;
};
enum class AstValueType : uint8_t {
Unknown,
Boolean,
Integer,
Float,
String,
Id, //just used for the IS_TRUE, IS_FALSE compression feature
EnumValue, //position inside an enum
};
typedef boost::variant<bool, int64_t, double, string> AstValue;
//Ast leaf for a number
class AstConstant : public AstVisitableNode {
public:
//create a tagged union for the type
//we need to check every time the type
//before we access the union, since
//else we could recieve wrong values
AstValueType type;
AstValue value;
uint8_t bitsForEnumValuePosition;
AstConstant(AstValueType type, AstValue value) : type(type), value(value) {
nodeType = AstNodeType::Constant;
}
void accept(AstVisitor &visitor) {
visitor.visit(*this);
}
};
//Ast leaf for an id
class AstId : public AstVisitableNode {
public:
AstValueType type;
string name;
uint16_t position;
bool isEnum;
AstId(AstValueType type, string name) : type(type), name(name), position(0) {
nodeType = AstNodeType::Id;
}
void accept(AstVisitor &visitor) {
visitor.visit(*this);
}
};
//Ast leaf for a function
class AstFunction : public AstVisitableNode {
public:
AstValueType type;
string name;
vector<AstConstant *> parameters;
uint16_t position;
AstFunction(string callString) : position(0) {
nodeType = AstNodeType::Function;
type = AstValueType::Boolean; //currently just functions with a boolean return type are supported
//extract the function id from the callString
name = callString.substr(0, callString.find('('));
string rawParameters = callString.substr(callString.find('(') + 1); //remove the left bracket
rawParameters = rawParameters.substr(0, rawParameters.find(')')); //remove the right bracket
for(size_t curPos = 0; curPos < rawParameters.size(); ) {
//get the current raw parameter
string curRawParameter;
size_t commaPos = rawParameters.find(',', curPos);
if(commaPos != string::npos) { //get next param (comma is seperator)
curRawParameter = rawParameters.substr(curPos, commaPos - curPos);
curPos = commaPos + 1;
}
else {//last parameter, since no following comma
curRawParameter = rawParameters.substr(curPos, rawParameters.size());
curPos += curRawParameter.size();
}
//remove unwanted whitespaces
boost::algorithm::trim(curRawParameter);
//just store the param with type unknown as string
//the preprocessor will check and convert the value
AstConstant *param = new AstConstant(AstValueType::Unknown, curRawParameter);
parameters.push_back(param);
}
}
void accept(AstVisitor &visitor) {
visitor.visit(*this);
}
};