-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAST.h
567 lines (497 loc) · 12 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <utility>
#include <boost/lexical_cast.hpp>
using namespace std;
class AST_node ;
class AST_prog ;
class AST_stmts ;
class AST_stmt;
class AST_semicolon;
class AST_keyword;
class AST_function_decl;
class AST_variable_decl;
class AST_assignStmt;
class AST_assignStmt_old;
class AST_assignStmt_new;
class AST_functionCall;
class AST_functionCall_noargs;
class AST_functionCall_args;
class AST_ifStmt;
class AST_ifWEStmt;
class AST_ifElseStmt;
class AST_whileStmt;
class AST_forStmt;
class AST_returnStmt;
class AST_inputStmt;
class AST_outputStmt;
class AST_expr;
class AST_expr_unary;
class AST_expr_binary;
class AST_expr_ternary;
class AST_paramD_list;
class AST_param_list;
class AST_paramD;
class AST_param ;
class AST_int;
class AST_bool;
class AST_float;
class AST_variable;
class AST_variable_0D;
class AST_variable_1D_i;
class AST_variable_1D_v;
class AST_variable_2D_ii;
class AST_variable_2D_iv;
class AST_variable_2D_vi;
class AST_variable_2D_vv;
union _NODE_
{
char name[20];
int number;
float fnumber;
int bvalue;
char boperator[3];
char uoperator[3];
char roperator[3];
char aoperator[3];
char aboperator[3];
char dtype[20];
AST_node* node;
AST_prog* prog;
AST_stmts* stmts;
AST_stmt* stmt;
AST_semicolon* semicolon;
AST_keyword* keyword;
AST_function_decl* function_decl;
AST_variable_decl* variable_decl;
AST_assignStmt* assignStmt;
AST_assignStmt_old* assignStmt_old;
AST_assignStmt_new* assignStmt_new;
AST_functionCall* functionCall;
AST_functionCall_noargs* functionCall_noargs;
AST_functionCall_args* functionCall_args;
AST_ifStmt* ifStmt;
AST_ifWEStmt* ifWEStmt;
AST_ifElseStmt* ifElseStmt;
AST_whileStmt* whileStmt;
AST_forStmt* forStmt;
AST_returnStmt* returnStmt;
AST_inputStmt* inputStmt;
AST_outputStmt* outputStmt;
AST_expr* expr;
AST_expr_unary* expr_unary;
AST_expr_binary* expr_binary;
AST_expr_ternary* expr_ternary;
AST_paramD_list* paramD_list;
AST_param_list* param_list;
AST_paramD* paramD;
AST_param * param ;
AST_int* intVal;
AST_bool* boolVal;
AST_float* floatVal;
AST_variable* variable;
AST_variable_0D* variable_0D;
AST_variable_1D_i* variable_1D_i;
AST_variable_1D_v* variable_1D_v;
AST_variable_2D_ii* variable_2D_ii;
AST_variable_2D_iv* variable_2D_is;
AST_variable_2D_vi* variable_2D_vi;
AST_variable_2D_vv* variable_2D_vv;
};
typedef union _NODE_ YYSTYPE;
#define YYSTYPE_IS_DECLARED 1
struct Variable{
AST_variable* var;
string dtype;
bool operator<(const Variable&) const;
};
struct Literal{
string value;
string dtype;
};
struct Function{
string dtype;
string functionName;
vector<string> parameters;
bool operator<(const Function&) const;
};
extern map<Variable, Literal> VarStore;
extern map<Function, vector<AST_variable*>> ParamStore;
extern map<Function, AST_stmts*> StmtStore;
class Visitor
{
public:
virtual Literal visit(AST_stmts *) = 0;
virtual Literal visit(AST_function_decl *) = 0;
virtual Literal visit(AST_variable_decl *) = 0;
virtual Literal visit(AST_semicolon* ) = 0;
virtual Literal visit(AST_keyword* ) = 0;
virtual Literal visit(AST_assignStmt_old *) = 0;
virtual Literal visit(AST_assignStmt_new *) = 0;
virtual Literal visit(AST_ifWEStmt *) = 0;
virtual Literal visit(AST_ifElseStmt *) = 0;
virtual Literal visit(AST_whileStmt *) = 0;
virtual Literal visit(AST_forStmt *) = 0;
virtual Literal visit(AST_returnStmt *) = 0;
virtual Literal visit(AST_inputStmt *) = 0;
virtual Literal visit(AST_outputStmt *) = 0;
virtual Literal visit(AST_expr_unary *) = 0;
virtual Literal visit(AST_expr_binary *) = 0;
virtual Literal visit(AST_expr_ternary *) = 0;
virtual Literal visit(AST_functionCall_noargs *) = 0;
virtual Literal visit(AST_functionCall_args *) = 0;
virtual Literal visit(AST_paramD_list *) = 0;
virtual Literal visit(AST_param_list *) = 0;
virtual Literal visit(AST_paramD *) = 0;
virtual Literal visit(AST_int *) = 0;
virtual Literal visit(AST_float *) = 0;
virtual Literal visit(AST_bool *) = 0;
virtual Literal visit(AST_variable_0D *) = 0;
virtual Literal visit(AST_variable_1D_v *) = 0;
virtual Literal visit(AST_variable_1D_i*) = 0;
virtual Literal visit(AST_variable_2D_ii *) = 0;
virtual Literal visit(AST_variable_2D_iv *) = 0;
virtual Literal visit(AST_variable_2D_vi *) = 0;
virtual Literal visit(AST_variable_2D_vv *) = 0;
};
class AST_node
{
public:
virtual Literal accept(Visitor &) = 0;
};
class AST_prog : public AST_node
{
public:
virtual Literal accept(Visitor &) = 0;
};
class AST_stmts : public AST_prog
{
private:
friend class Traverse;
vector<AST_stmt*> stmt_list;
public:
void push_back(AST_stmt * stmt);
Literal accept(Visitor &);
};
class AST_stmt : public AST_node
{
};
class AST_semicolon : public AST_stmt{
private:
friend class Traverse;
private:
Literal accept(Visitor &);
};
class AST_keyword : public AST_stmt
{
private:
friend class Traverse;
string keyword_type;
public:
AST_keyword(string keyword_type);
Literal accept(Visitor &);
};
class AST_function_decl: public AST_stmt
{
private:
friend class Traverse;
string dtype;
string functionName;
AST_paramD_list* paramD_list;
AST_stmts* stmts;
public:
AST_function_decl(string dtype, string functionName, AST_paramD_list* paramD_list, AST_stmts* stmts);
Literal accept(Visitor &);
};
class AST_variable_decl: public AST_stmt
{
private:
friend class Traverse;
string dtype;
AST_variable* variable;
public:
AST_variable_decl(string dtype, AST_variable* variable);
Literal accept(Visitor &);
};
class AST_assignStmt : public AST_stmt
{
};
class AST_assignStmt_old: public AST_assignStmt
{
private:
friend class Traverse;
AST_variable* varName;
AST_expr* expr;
public:
AST_assignStmt_old(AST_variable* varName, AST_expr* expr);
Literal accept(Visitor &);
};
class AST_assignStmt_new: public AST_assignStmt
{
private:
friend class Traverse;
string dtype;
AST_variable* varName;
AST_expr* expr;
public:
AST_assignStmt_new(string dtype, AST_variable* varName, AST_expr* expr);
Literal accept(Visitor &);
};
class AST_ifStmt : public AST_stmt
{
};
class AST_ifWEStmt : public AST_ifStmt
{
private:
friend class Traverse;
AST_expr* expr;
AST_stmts* ifStmts;
public:
AST_ifWEStmt(AST_expr* expr, AST_stmts* ifStmts);
Literal accept(Visitor &);
};
class AST_ifElseStmt : public AST_ifStmt
{
private:
friend class Traverse;
AST_expr* expr;
AST_stmts* ifStmts;
AST_stmts* ifEStmts;
public:
AST_ifElseStmt(AST_expr * expr, AST_stmts* ifStmts, AST_stmts* ifEStmts);
Literal accept(Visitor &);
};
class AST_whileStmt : public AST_stmt
{
private:
friend class Traverse;
AST_expr* expr;
AST_stmts* whileStmts;
public:
AST_whileStmt(AST_expr* expr, AST_stmts* whileStmts);
Literal accept(Visitor &);
};
class AST_forStmt : public AST_stmt
{
private:
friend class Traverse;
AST_variable* intialize_var;
AST_variable* start;
AST_variable* step;
AST_variable* end;
AST_stmts* forStmts;
public:
AST_forStmt(AST_variable* intialize_var, AST_variable* start, AST_variable* step, AST_variable* end, AST_stmts* forStmts);
Literal accept(Visitor &);
};
class AST_returnStmt : public AST_stmt
{
private:
friend class Traverse;
AST_expr* expr;
public:
AST_returnStmt(AST_expr* expr);
Literal accept(Visitor &);
};
class AST_inputStmt : public AST_stmt
{
private:
friend class Traverse;
AST_variable* var;
public:
AST_inputStmt(AST_variable* var);
Literal accept(Visitor &);
};
class AST_outputStmt : public AST_stmt
{
private:
friend class Traverse;
AST_variable* var;
public:
AST_outputStmt(AST_variable* var);
Literal accept(Visitor &);
};
class AST_expr: public AST_node
{
};
class AST_expr_unary: public AST_expr
{
private:
friend class Traverse;
string opType;
AST_expr* expr;
public:
AST_expr_unary(string opType, AST_expr* expr);
Literal accept(Visitor &);
};
class AST_expr_binary: public AST_expr
{
private:
friend class Traverse;
string opType;
AST_expr* expr1, *expr2;
public:
AST_expr_binary(AST_expr* expr1, string opType, AST_expr* expr2);
Literal accept(Visitor &);
};
class AST_expr_ternary: public AST_expr
{
private:
friend class Traverse;
AST_expr* expr1, *expr2, *expr3;
public:
AST_expr_ternary(AST_expr* expr1, AST_expr* expr2, AST_expr* expr3);
Literal accept(Visitor &);
};
class AST_functionCall : public AST_expr
{
};
class AST_functionCall_noargs : public AST_functionCall
{
private:
friend class Traverse;
string functionName;
public:
AST_functionCall_noargs(string functionName);
Literal accept(Visitor &);
};
class AST_functionCall_args : public AST_functionCall
{
private:
friend class Traverse;
string functionName;
AST_param_list * param_list;
public:
AST_functionCall_args(string functionName, AST_param_list * param_list);
Literal accept(Visitor &);
};
class AST_paramD_list : public AST_node
{
private:
friend class Traverse;
vector<AST_paramD*> paramDs;
public:
void push_back(AST_paramD* paramD);
Literal accept(Visitor &);
};
class AST_param_list : public AST_node
{
private:
friend class Traverse;
vector<AST_param*> params;
public:
void push_back(AST_param* param);
Literal accept(Visitor &);
};
class AST_paramD : public AST_node
{
private:
friend class Traverse;
string dtype;
AST_variable* var;
public:
AST_paramD(string dtype, AST_variable* var);
Literal accept(Visitor &);
};
class AST_param : public AST_expr
{
};
class AST_int : public AST_param
{
private:
friend class Traverse;
int value;
public:
AST_int(int value);
Literal accept(Visitor &);
};
class AST_bool : public AST_param
{
private:
friend class Traverse;
int value;
public:
AST_bool(int value);
Literal accept(Visitor &);
};
class AST_float : public AST_param
{
private:
friend class Traverse;
float value;
public:
AST_float(float value);
Literal accept(Visitor &);
};
class AST_variable : public AST_param
{
public:
string variableName;
};
class AST_variable_0D : public AST_variable
{
private:
friend class Traverse;
public:
AST_variable_0D(string variableName);
Literal accept(Visitor &);
};
class AST_variable_1D_v : public AST_variable
{
private:
friend class Traverse;
AST_variable_0D* index;
public:
AST_variable_1D_v(string variableName, AST_variable_0D* index);
Literal accept(Visitor &);
};
class AST_variable_1D_i : public AST_variable
{
private:
friend class Traverse;
int index;
public:
AST_variable_1D_i(string variableName, int index);
Literal accept(Visitor &);
};
class AST_variable_2D_ii : public AST_variable
{
private:
friend class Traverse;
int index1,index2;
public:
AST_variable_2D_ii(string variableName, int index1,int index2);
Literal accept(Visitor &);
};
class AST_variable_2D_iv : public AST_variable
{
private:
friend class Traverse;
int index1;
AST_variable_0D* index2;
public:
AST_variable_2D_iv(string variableName, int index1,AST_variable_0D* index2);
Literal accept(Visitor &);
};
class AST_variable_2D_vi : public AST_variable
{
private:
friend class Traverse;
AST_variable_0D* index1;
int index2;
public:
AST_variable_2D_vi(string variableName, AST_variable_0D* index1, int index2);
Literal accept(Visitor &);
};
class AST_variable_2D_vv : public AST_variable
{
private:
friend class Traverse;
AST_variable_0D* index1,*index2;
public:
AST_variable_2D_vv(string variableName, AST_variable_0D* index1,AST_variable_0D* index2);
Literal accept(Visitor &);
};
#include "traverse.h"