-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheeyoreAST.hpp
258 lines (230 loc) · 8.67 KB
/
eeyoreAST.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
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
250
251
252
253
254
255
256
257
258
#include <bits/stdc++.h>
#ifndef tiggerAST_hpp
#include "tiggerAST.hpp"
#define tiggerAST_hpp
#endif
using namespace std;
extern void print(string x) ;
extern void printTab(string x) ;
class eeyoreAST {
public:
eeyoreAST() {}
~eeyoreAST() {}
virtual void Dump() { assert(false); } // printCode
virtual string getName() { assert(false); }
virtual int getInt() { assert(false); }
virtual void translate() { assert(false); } // translate AST of eeyoreAST into AST of tigger
virtual bool isnum() { assert(false); } // for _ATOM, return true if it is an integer number
virtual bool isdef() { return false; } // for convenience, "return false" ..
virtual void localDecl() { assert(false); }
virtual void globalDecl() { assert(false); }
/* here below, are functions for optimization */
virtual void optimize() { assert(false); }
virtual void _analyse_def_use(int line) { assert(false); }
virtual void _analyse_cf(int line) { assert(false); }
virtual void _analyse_direct_pass(int line) { assert(false); }
};
/*
Definition of nodes in Abstraction Syntax Tree of eeyore
all specific nodes in eeyore AST has prefix _e,
so as to avoid multiple global classes with same names.
The whole structure is interpreted as
sequence of statements, sequence of functions.
and each function is also composed of sequential statements
statements have several forms.
*/
class _eATOM: public eeyoreAST {
public:
_eATOM() {}
};
class _eNUM: public _eATOM {
public:
int num;
_eNUM(int _num): num(_num) {}
virtual string getName() { return to_string(num); }
virtual int getInt() { return num; }
virtual bool isnum() { return true; }
};
class _eVAR: public _eATOM {
public:
string var;
_eVAR(string _var): var(_var) {}
virtual string getName() { return var; }
virtual bool isnum() { return false; }
};
class _eDEFVAR: public eeyoreAST {
public:
string var; bool isconst;
_eDEFVAR(string _var, bool _isconst): var(_var), isconst(_isconst) {}
virtual string getName() { return var; }
virtual void Dump() { printTab("var " + var); }
virtual bool isdef() { return true; }
virtual void localDecl() ;
virtual void globalDecl() ;
virtual void _analyse_def_use(int line) ;
virtual void _analyse_cf(int line) ;
virtual void _analyse_direct_pass(int line) ;
};
class _eDEFARR: public eeyoreAST {
public:
string var; int size; bool isconst; // without * 4
_eDEFARR(string _var, int _size, bool _isconst): var(_var), size(_size), isconst(_isconst) {}
virtual string getName() { return var; }
virtual void Dump() { printTab("var " + to_string(size * 4) + " " + var); }
virtual bool isdef() { return true; }
virtual void localDecl() ;
virtual void globalDecl() ;
virtual void _analyse_def_use(int line) ;
virtual void _analyse_cf(int line) ;
virtual void _analyse_direct_pass(int line) ;
};
class _eDIRECT: public eeyoreAST {
public:
eeyoreAST *a, *t;
_eDIRECT(eeyoreAST *_a, eeyoreAST *_t): a(_a), t(_t) {}
virtual void Dump() { printTab(a->getName() + " = " + t->getName()); }
virtual void translate() ;
virtual void _analyse_def_use(int line) ;
virtual void _analyse_cf(int line) ;
virtual void _analyse_direct_pass(int line) ;
};
class _eUNARY: public eeyoreAST {
public:
eeyoreAST *a, *t; string op;
_eUNARY(eeyoreAST *_a, string _op, eeyoreAST *_t): a(_a), op(_op), t(_t) {}
virtual void Dump() { printTab(a->getName() + " = " + op + t->getName()); }
virtual void translate() ;
virtual void _analyse_def_use(int line) ;
virtual void _analyse_cf(int line) ;
virtual void _analyse_direct_pass(int line) ;
};
class _eBINARY: public eeyoreAST {
public:
eeyoreAST *a, *t1, *t2; string op;
_eBINARY(eeyoreAST *_a, eeyoreAST *_t1, string _op, eeyoreAST *_t2): a(_a), t1(_t1), op(_op), t2(_t2) {}
virtual void Dump() { printTab(a->getName() + " = " + t1->getName() + " " + op + " " + t2->getName()); }
virtual void translate() ;
virtual void _analyse_def_use(int line) ;
virtual void _analyse_cf(int line) ;
virtual void _analyse_direct_pass(int line) ;
};
class _eSEEK: public eeyoreAST {
public:
eeyoreAST *a, *x; string t;
_eSEEK(eeyoreAST *_a, string _t, eeyoreAST *_x): a(_a), t(_t), x(_x) {}
virtual void Dump() { printTab(a->getName() + " = " + t + "[" + x->getName() + "]"); }
virtual void translate() ;
virtual void _analyse_def_use(int line) ;
virtual void _analyse_cf(int line) ;
virtual void _analyse_direct_pass(int line) ;
};
class _eSAVE: public eeyoreAST {
public:
eeyoreAST *t, *x; string a;
_eSAVE(string _a, eeyoreAST *_x, eeyoreAST *_t): a(_a), x(_x), t(_t) {}
virtual void Dump() { printTab(a + "[" + x->getName() + "] = " + t->getName()); }
virtual void translate() ;
virtual void _analyse_def_use(int line) ;
virtual void _analyse_cf(int line) ;
virtual void _analyse_direct_pass(int line) ;
};
class _eFUNCRET: public eeyoreAST {
public:
eeyoreAST *a; string func;
_eFUNCRET(eeyoreAST *_a, string _func): a(_a), func(_func) {}
virtual void Dump() { printTab(a->getName() + " = call f_" + func); }
virtual void translate() ;
virtual void _analyse_cf(int line) ;
virtual void _analyse_def_use(int line) ;
virtual void _analyse_direct_pass(int line) ;
};
class _eIFGOTO: public eeyoreAST {
public:
eeyoreAST *t1, *t2; string l; string op;
_eIFGOTO(eeyoreAST *_t1, string _op, eeyoreAST *_t2, string _l): t1(_t1), op(_op), t2(_t2), l(_l) {}
virtual void Dump() { printTab("if " + t1->getName() + " " + op + " " + t2->getName() + " goto " + l); }
virtual void translate() ;
virtual void _analyse_def_use(int line) ;
virtual void _analyse_cf(int line) ;
virtual void _analyse_direct_pass(int line) ;
};
class _eGOTO: public eeyoreAST {
public:
string l;
_eGOTO(string _l): l(_l) {}
virtual void Dump() { printTab("goto " + l); }
virtual void translate() ;
virtual void _analyse_def_use(int line) ;
virtual void _analyse_cf(int line) ;
virtual void _analyse_direct_pass(int line) ;
};
class _ePARAM: public eeyoreAST {
public:
eeyoreAST *t;
_ePARAM(eeyoreAST *_t): t(_t) {}
virtual void Dump() { printTab("param " + t->getName()); }
virtual void translate() ;
virtual void _analyse_def_use(int line) ;
virtual void _analyse_cf(int line) ;
virtual void _analyse_direct_pass(int line) ;
};
class _eLABEL: public eeyoreAST {
public:
string l;
_eLABEL(string _l): l(_l) {}
virtual void Dump() { print(l + ":"); }
virtual void translate() ;
virtual void _analyse_def_use(int line) ;
virtual void _analyse_cf(int line) ;
virtual void _analyse_direct_pass(int line) ;
};
class _eRETVOID: public eeyoreAST {
public:
_eRETVOID() {}
virtual void Dump() { printTab("return"); }
virtual void translate() ;
virtual void _analyse_def_use(int line) ;
virtual void _analyse_cf(int line) ;
virtual void _analyse_direct_pass(int line) ;
};
class _eRET: public eeyoreAST {
public:
eeyoreAST *t;
_eRET(eeyoreAST *_t): t(_t) {}
virtual void Dump() { printTab("return " + t->getName()); }
virtual void translate() ;
virtual void _analyse_def_use(int line) ;
virtual void _analyse_cf(int line) ;
virtual void _analyse_direct_pass(int line) ;
};
class _eFUNC: public eeyoreAST {
public:
eeyoreAST *body; string func; int arity;
_eFUNC(string _func, int _arity, eeyoreAST *_body): func(_func), arity(_arity), body(_body) {}
virtual void Dump() { print("f_" + func + " [" + to_string(arity) + "]"); body->Dump(); print("end f_" + func); }
virtual void translate() ;
virtual void optimize() ;
};
class _eCALL: public eeyoreAST {
public:
string func;
_eCALL(string _func): func(_func) {}
virtual void Dump() { printTab("call f_" + func); }
virtual void translate() ;
virtual void _analyse_def_use(int line) ;
virtual void _analyse_cf(int line) ;
virtual void _analyse_direct_pass(int line) ;
};
class _eSEQ: public eeyoreAST {
public:
vector<eeyoreAST *> seq;
_eSEQ(vector<eeyoreAST *> _seq): seq(_seq) {}
virtual void Dump() { for (auto stmt: seq) stmt->Dump(); }
virtual void translate() ;
virtual void optimize() ;
};
typedef enum {
DEFVAR, DEFARR, DIRECT, UNARY, BINARY, SEEK, SAVE,
FUNCRET, CALL, PARAM, IFGOTO, GOTO, LABEL, RET, RETVOID,
DEAD
}typeAST;