-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.h
249 lines (222 loc) · 6.14 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#ifndef AST_H
#define AST_H
#include <iostream>
#include <cstring>
#include <vector>
#include <memory>
#include <optional>
#include <map>
#include <unordered_map>
#include "sym.h"
// #include "traversal.h"
extern int yylineno, yycolumn;
extern int semantic_error(std::string, int a, int b, bool print_line);
// Abstract Node class
class Node;
// Start Class
class Start;
extern Start *root;
// Classes that are a part of the Declare Section
class Decl;
enum class GradSpecifier
{
CNS,
VAR
};
enum class TypeSpecifier
{
CHAR,
INT,
FLOAT,
BOOL,
TENSOR
};
class ConstValue;
class InitDeclarator;
class Declarator;
class Initializer;
// Operations class
class AssgnStmt;
enum class LibFuncs
{
SIN,
COS,
LOG,
EXP,
TRANSPOSE
};
enum class AssignmentOperator
{
AST_ASSIGN, // only =
AST_ADD_ASSIGN,
AST_SUB_ASSIGN,
AST_MUL_ASSIGN,
AST_DIV_ASSIGN,
AST_AT_ASSIGN
};
class Expr;
class BinaryExpr;
class UnaryExpr;
// Gradient class
class GradStmt;
enum class GradType
{
GRAD,
BACKWARD,
PRINT
};
class Node
{
public:
Node();
virtual ~Node() = default;
// virtual void print() = 0;
// int row_num, col_num;
// add codegen() function from llvm for IR gen
};
// Start Class stores the pointers to all the declarations, expressions and gradients
class Start : public Node
{
public:
std::vector<class Decl *> *DeclList;
std::vector<class AssgnStmt *> *AssgnStmtList;
std::vector<class GradStmt *> *GradStmtList;
std::unordered_map<std::string, SymTabItem> *symbolTable;
Start(std::vector<class Decl *> *DeclList, std::vector<class AssgnStmt *> *AssgnStmtList, std::vector<class GradStmt *> *GradStmtList, std::unordered_map<std::string, SymTabItem> *symbolTable);
virtual ~Start() = default;
void transpile(std::ostream &out, int tab = 0) const;
};
// Decl Class stores the declarations of a variable such as their gradient specifier, data type and the pointer to the initializer
// It also stores the initial value of the variable if it is initialized
class Decl : public Node
{
public:
GradSpecifier GradType;
TypeSpecifier DataType;
InitDeclarator *InitDeclaratorList;
Decl(GradSpecifier, TypeSpecifier, InitDeclarator *);
virtual ~Decl() = default;
void transpile(std::ostream &out, int tab = 0) const;
};
// InitDeclarator Class stores the pointer to the declarator and the pointer to the initializer
class InitDeclarator : public Node
{
public:
Declarator *declarator;
Initializer *initializer;
InitDeclarator(Declarator *, Initializer *);
virtual ~InitDeclarator() = default;
void transpile(std::ostream &out, int tab = 0) const;
};
// Declarator Class stores the name of the variable and the dimensions of the variable
class Declarator : public Node
{
public:
std::string name;
std::vector<int> Dimensions;
Declarator(std::string);
virtual ~Declarator() = default;
void transpile(std::ostream &out, int tab = 0) const;
};
class ConstValue : public Node
{
public:
union inbuilt_type
{
int int_val;
float float_val;
};
inbuilt_type value;
bool isInt;
// ConstValue(int value, bool isInt = false);
ConstValue(int value);
ConstValue(float value);
virtual ~ConstValue() = default;
};
// Initializer Class stores the value of the variable using a union structure
// It also stores the pointers to the initializers of the elements of the variable if it is an array
class Initializer : public Node
{
public:
union type_value
{
ConstValue *cvalue;
std::vector<Initializer *> *InitializerList;
constexpr type_value() : cvalue(nullptr) {}
~type_value() {}
};
type_value val;
bool isScalar;
Initializer(ConstValue *value);
Initializer(std::vector<Initializer *> *InitializerList);
// Initializer(ConstValue*, std::vector<Initializer*>);
void printInitializerList();
virtual ~Initializer() = default;
void transpile(std::ostream &out, int tab = 0) const;
};
// Operations
class AssgnStmt : public Node
{
public:
int row_num, col_num;
// Declarator *declarator;
// Initializer *initializer;
// AssgnStmt(Declarator*, Initializer*);
std::string name;
std::optional<AssignmentOperator> op;
Expr *expr;
AssgnStmt(std::string, std::optional<AssignmentOperator>, Expr *);
virtual ~AssgnStmt() = default;
void transpile(std::ostream &out, int tab = 0) const;
};
class Expr : public Node
{
public:
// below two to be initialized after ast generation and before semantic analysis of operations part
std::vector<int> dimensions;
TypeSpecifier DataType;
int row_num, col_num;
Expr();
virtual void printExpression();
virtual void initialize_expression_node_info(std::unordered_map<std::string, SymTabItem> *symbolTable);
virtual ~Expr() = default;
virtual void transpile(std::ostream &out, int tab = 0) const;
};
class BinaryExpr : public Expr
{
public:
Expr *lhs, *rhs;
char op;
BinaryExpr(Expr *lhs, Expr *rhs, char op);
virtual ~BinaryExpr() = default;
virtual void printExpression() override;
virtual void initialize_expression_node_info(std::unordered_map<std::string, SymTabItem> *symbolTable) override;
void transpile(std::ostream &out, int tab = 0) const override;
};
class UnaryExpr : public Expr
{
public:
Expr *expr; // expr for libfunc if present
std::string identifier;
ConstValue *cvalue;
std::optional<LibFuncs> libfunc;
UnaryExpr(Expr *expr, std::optional<LibFuncs> libfunc, std::string identifier, ConstValue *cvalue);
virtual ~UnaryExpr() = default;
virtual void printExpression() override;
virtual void initialize_expression_node_info(std::unordered_map<std::string, SymTabItem> *symbolTable) override;
void transpile(std::ostream &out, int tab = 0) const override;
};
// Gradient
class GradStmt : public Node
{
public:
// Declarator *declarator;
// Initializer *initializer;
// GradStmt(Declarator*, Initializer*);
GradType grad_type;
std::string name;
GradStmt(GradType, std::string);
virtual ~GradStmt() = default;
void transpile(std::ostream &out, int tab = 0) const;
};
#endif