-
Notifications
You must be signed in to change notification settings - Fork 2
/
E--_parser.y++
753 lines (616 loc) · 38.7 KB
/
E--_parser.y++
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
%{
#include "Ast.h"
#include "ParserUtil.h"
#include "Error.h"
using namespace std;
extern int verbosity;
extern int yylex();
extern void yyerror(const char *s);
extern const OpNode::OpInfo opInfo[];
#define prt(x) //cout << x << endl;
static Type *curType = NULL;
static vector<ExprNode*> *curExprList = NULL;
static vector<ExprNode*> *funcParamList = NULL;
static list<StmtNode*> *curStmtList1 = NULL;
static list<StmtNode*> *curStmtList3 = NULL;
static vector<Type*> *curTypeList = NULL;
static vector<VariableEntry*> *curVarList = NULL;
static FunctionEntry *curFuncEnt = NULL;
static EventEntry *curEventEnt = NULL;
static RuleBlockEntry *curRBlkNode = NULL;
static ReturnStmtNode *curReturnNode = NULL;
static int curClassSize = 0;
static EventEntry* getEventEntry(char *eventName) {
EventEntry *sym = (EventEntry*) stm.lookUp(string(eventName));
if (sym && sym->type()->tag() == Type::EVENT) {
return sym;
}
errMsg((string)"Expected an event name, but got \"" + (string)eventName + (string)"\"");
sym = new EventEntry(string(eventName), yylinenum, yycolumnno, string(yyfilename));
sym->kind(SymTabEntry::Kind::ERROR_KIND);
return sym;
}
static Type* getUserType(char *typeName) {
SymTabEntry *sym = stm.lookUp(string(typeName));
if (sym && sym->type()->tag() == Type::CLASS) {
return sym->type();
} else {
errMsg((string)"Undeclared type \"" + (string)typeName + (string)"\"");
return new Type(Type::VOID);
}
}
static void checkEventParamType(Type *ptype) {
switch(ptype->tag()) {
// Valid types
case Type::INT:
case Type::UINT:
case Type::DOUBLE:
break;
// Invalid types
default:
errMsg((string)"Invalid parameter type '" + ptype->fullName() + (string)"' for event declaration");
break;
}
}
static void checkReturnStmt(FunctionEntry *func) {
if (!curReturnNode && func->type()->retType()->tag() != Type::VOID)
errMsg((string)"Return Statement expected for function '" + func->name() + "'");
curReturnNode = NULL;
}
static bool IsFuncSymEntry(SymTabEntry *sym) {
if (sym && sym->kind() == SymTabEntry::Kind::FUNCTION_KIND)
return true;
return false;
}
static bool IsFuncBodyDefined(SymTabEntry *sym) {
if (IsFuncSymEntry(sym) && ((FunctionEntry*)sym)->body() != NULL)
return true;
return false;
}
static bool IsEventSymEntry(SymTabEntry *sym) {
if (sym && sym->kind() == SymTabEntry::Kind::EVENT_KIND)
return true;
return false;
}
static bool IsCurRuleBlockEntry() {
return curRBlkNode ? true : false;
}
static VariableEntry* allocVarEntry(string name, VariableEntry::VarKind v, Type* type, ExprNode* init) {
VariableEntry *retVarEnt = (VariableEntry*)stm.lookUpInScope(name, stm.currentScope()->kind());
if (retVarEnt) {
errMsg((string)"Redefinition of name " + name);
} else {
retVarEnt = new VariableEntry(name, v, type, init, yylinenum, yycolumnno, string(yyfilename));
stm.insert(retVarEnt);
}
return retVarEnt;
}
static ClassEntry* allocClassEntry(char* name) {
assert(name != NULL);
ClassEntry *retClassEnt = (ClassEntry*)stm.lookUpInScope(name, stm.currentScope()->kind());
if (retClassEnt) {
errMsg((string)"Redefinition of name " + name);
} else {
errMsg("Unsupported 3 address code generation for class structures");
retClassEnt = new ClassEntry(string(name), yylinenum, yycolumnno, string(yyfilename));
Type *te = new Type(retClassEnt, Type::CLASS);
retClassEnt->type(te);
stm.insert(retClassEnt);
stm.enterScope(retClassEnt);
}
return retClassEnt;
}
static ClassFuncInvocationNode* allocClassFuncInvNode(char *objName, char* funcName, vector<ExprNode*>* param_l) {
assert(objName != NULL);
assert(funcName != NULL);
SymTabEntry *objSym = stm.lookUpInScope(objName, stm.currentScope()->kind());
SymTabEntry *classSym = NULL, *funcSym = NULL;
if (objSym && objSym->type()->tag() == Type::CLASS) {
classSym = stm.lookUp(objSym->type()->typeDesc()->name());
funcSym = classSym->symTab()->lookUp(string(funcName));
}
if (!IsFuncSymEntry(funcSym)) {
errMsg((string)"\"" + (string)funcName + (string)"\" is not a function name");
}
return new ClassFuncInvocationNode(objSym, funcSym, param_l, yylinenum, yycolumnno, string(yyfilename));
}
static ClassRefExprNode* allocClassRefExprNode(char* objName, char* varName) {
assert(objName != NULL);
assert(varName != NULL);
SymTabEntry *objSym = stm.lookUpInScope(string(objName), stm.currentScope()->kind());
SymTabEntry *classSym = NULL, *varSym = NULL;
if (objSym == NULL) {
errMsg((string)"Undeclared identifier \"" + (string)objName + (string)"\"");
}
if (objSym && objSym->type()->tag() == Type::CLASS) {
classSym = stm.lookUp(objSym->type()->typeDesc()->name());
varSym = classSym->symTab()->lookUp(string(varName));
}
if (varSym == NULL) {
errMsg((string)"Undeclared identifier \"" + (string)varName + (string)"\"");
}
return new ClassRefExprNode(string(varName), objSym, varSym, yylinenum, yycolumnno, string(yyfilename));
}
static EventEntry* allocEventEntry(char* name = NULL, vector<Type*>* param_l = NULL) {
EventEntry* retEventEnt = curEventEnt;
if (retEventEnt) {
assert(name != NULL);
if (!(strlen(name) == 1 && ((name[0] >= 'a' && name[0] <= 'z') || (name[0] >= 'A' && name[0] <= 'Z')))) {
errMsg((string)"Invalid event name '" + name + "', should be one of {a-z, A-Z}");
} else if (IsEventSymEntry(stm.lookUp(string(name)))) {
errMsg((string)"Redefinition of name " + name);
}
Type *te = new Type(param_l, Type::EVENT);
retEventEnt->name(name);
retEventEnt->type(te);
curEventEnt = NULL;
} else {
retEventEnt = new EventEntry(string("dummy_entry"), yylinenum, yycolumnno, string(yyfilename));
stm.enterScope(retEventEnt);
}
return retEventEnt;
}
static FunctionEntry* allocFuncEntry(char* name = NULL, Type* rettype = NULL, vector<Type*>* param_l = NULL) {
FunctionEntry* retFuncEnt = curFuncEnt;
if (retFuncEnt) {
assert(name != NULL);
if (IsFuncSymEntry(stm.lookUp(string(name)))) {
errMsg((string)"Redefinition of name " + name);
}
Type *te = new Type(param_l, rettype);
retFuncEnt->name(name);
retFuncEnt->type(te);
curFuncEnt = NULL;
} else {
retFuncEnt = new FunctionEntry(string("dummy_func"), NULL, yylinenum, yycolumnno, string(yyfilename));
stm.enterScope(retFuncEnt);
}
return retFuncEnt;
}
static RefExprNode* allocRefExprNode(char* str) {
assert(str != NULL);
SymTabEntry *sym = stm.lookUp(string(str));
if (sym == NULL) {
errMsg((string)"Undeclared identifier \"" + (string)str + (string)"\"");
sym = new SymTabEntry(string(str), SymTabEntry::Kind::ERROR_KIND,
yylinenum, yycolumnno, string(yyfilename), new Type(Type::VOID));
}
return new RefExprNode(string(str), sym, yylinenum, yycolumnno, string(yyfilename));
}
static InvocationNode* allocInvNode(char* str, vector<ExprNode*>* param_l) {
assert(str != NULL);
SymTabEntry *sym = stm.lookUp(string(str));
bool IsRecursiveCall = false;
// Look for recursive function call to current function scope
if (!sym) {
sym = stm.currentScope(SymTabEntry::Kind::FUNCTION_KIND);
if (sym == NULL || sym->name().compare(str)) sym = NULL;
else IsRecursiveCall = true;
}
if (!IsFuncSymEntry(sym)) {
errMsg((string)"\"" + (string)str + (string)"\" is not a function name");
sym = new SymTabEntry(string(str), SymTabEntry::Kind::ERROR_KIND,
yylinenum, yycolumnno, string(yyfilename), new Type(Type::VOID));
} else if (!IsRecursiveCall && !IsFuncBodyDefined(sym)) {
errMsg((string)"Missing function body for \"" + (string)str + (string)"\"");
}
return new InvocationNode(sym, param_l, yylinenum, yycolumnno, string(yyfilename));
}
static PrintFunctionNode* allocPrintNode(vector<ExprNode*>* param_l) {
return new PrintFunctionNode(param_l, yylinenum, yycolumnno, string(yyfilename));
}
static ValueNode* allocValueNode(Value* val) {
return new ValueNode(val, yylinenum, yycolumnno, string(yyfilename));
}
static OpNode* allocOpNode(OpNode::OpCode op, ExprNode *l, ExprNode *r) {
assert(l);
return new OpNode(op, l, r, yylinenum, yycolumnno, string(yyfilename));
}
static ExprStmtNode* allocExprStmtNode(ExprNode* e) {
return new ExprStmtNode(e, yylinenum, yycolumnno, string(yyfilename));
}
static ReturnStmtNode* allocReturnNode(ExprNode* e) {
if (IsCurRuleBlockEntry())
errMsg((string)"Return statement is valid only within a function");
curReturnNode = new ReturnStmtNode(e, (FunctionEntry*)stm.currentScope(SymTabEntry::Kind::FUNCTION_KIND),
yylinenum, yycolumnno, string(yyfilename));
return curReturnNode;
}
static BreakStmtNode* allocBreakNode(ExprNode* e) {
return new BreakStmtNode(e, yylinenum, yycolumnno, string(yyfilename));
}
static ContinueStmtNode* allocContinueNode(ExprNode* e) {
return new ContinueStmtNode(e, yylinenum, yycolumnno, string(yyfilename));
}
static IfNode* allocIfNode(ExprNode* cond, StmtNode* thenStmt, StmtNode* elseStmt) {
return new IfNode(cond, thenStmt, elseStmt, yylinenum, yycolumnno, string(yyfilename));
}
static WhileNode* allocWhileNode(ExprNode* cond, StmtNode* bodyStmt) {
return new WhileNode(cond, bodyStmt, yylinenum, yycolumnno, string(yyfilename));
}
static CompoundStmtNode* allocCompStmtNode(list<StmtNode*> *Slist) {
if (!Slist) Slist = new list<StmtNode*>;
return new CompoundStmtNode(Slist, yylinenum, yycolumnno, string(yyfilename));
}
static CompoundStmtNode* allocScopeNode(CompoundStmtNode* node) {
CompoundStmtNode *retCompNode = allocCompStmtNode(new list<StmtNode*>);
retCompNode->add(node);
return retCompNode;
}
static PrimitivePatNode* allocPrimPatNode(EventEntry* ee, vector<VariableEntry*>* params, ExprNode* c, bool IsSupported) {
if (ee) {
if (!IsSupported) warnMsg("Unsupported Primitive PatNode '" + ee->name() + "'");
return new PrimitivePatNode(ee, params, c, yylinenum, yycolumnno, string(yyfilename));
}
return NULL;
}
static PatNode* allocPatNode(BasePatNode::PatNodeKind pk, BasePatNode *p1, BasePatNode *p2) {
if (p1 &&
((pk == BasePatNode::PatNodeKind::NEG || pk == BasePatNode::PatNodeKind::STAR) ||
((pk == BasePatNode::PatNodeKind::OR || pk == BasePatNode::PatNodeKind::SEQ) && p2)))
{
warnMsg("Unsupported PatNode Operation");
return new PatNode(pk, p1, p2, yylinenum, yycolumnno, string(yyfilename));
}
return NULL;
}
static RuleNode* allocRuleNode(BasePatNode* pat, StmtNode* reaction) {
RuleNode *retRuleNode = NULL;
if (pat) {
retRuleNode = new RuleNode(curRBlkNode, pat, reaction, yylinenum, yycolumnno, string(yyfilename));
GlobalEntry *globalSymEnt = (GlobalEntry*) stm.currentScope(SymTabEntry::Kind::GLOBAL_KIND);
globalSymEnt->addRule(retRuleNode);
}
stm.leaveScope();
curRBlkNode = NULL;
return retRuleNode;
}
static RuleBlockEntry* allocRuleBlockNode() {
if (!curRBlkNode) {
curRBlkNode = new RuleBlockEntry(yylinenum, yycolumnno, string(yyfilename));
stm.enterScope(curRBlkNode);
}
return curRBlkNode;
}
%}
%union {
unsigned int uVal;
double dVal;
char* cVal;
vector<string>* strVector;
Value* valVal;
ExprNode* exprVal;
vector<ExprNode*>* exprList;
RefExprNode* refexpVal;
ClassEntry* classEntry;
ClassFuncInvocationNode* classFuncVal;
ClassRefExprNode* classRefExpVal;
Type* typeVal;
vector<Type*>* typeList;
FunctionEntry* functionEntry;
EventEntry* eventEntry;
VariableEntry* variableEntry;
vector<VariableEntry*>* varList;
StmtNode* stmtVal;
list<StmtNode*>* stmtList;
IfNode* ifVal;
WhileNode* whileVal;
BreakStmtNode* breakVal;
ContinueStmtNode* continueVal;
ReturnStmtNode* returnVal;
CompoundStmtNode* stmt_plusVal;
RuleNode* ruleNode;
BasePatNode* patVal;
PrimitivePatNode* primPatVal;
vector<RuleNode*>* transList;
const OpNode::OpInfo* funIval;
};
/* Expect only 1 Shift-Reduce Conflict for "if-if-else" */
%expect 1
/* Operators */
%token TOK_LEX_ERROR TOK_PAT_STAR TOK_PAT_OR TOK_PAT_NOT
%token TOK_PLUS TOK_MINUS TOK_UMINUS TOK_MULT TOK_DIV TOK_MOD
%token TOK_BITAND TOK_BITOR TOK_BITNOT TOK_BITXOR TOK_SHL TOK_SHR
%token TOK_GT TOK_LT TOK_GE TOK_LE TOK_EQ TOK_NE TOK_AND TOK_OR TOK_NOT
%token TOK_COMMA TOK_ASSIGN TOK_SEMICOLON TOK_COLON TOK_ARROW TOK_QMARK
%token TOK_LBRACE TOK_RBRACE TOK_LPAREN TOK_RPAREN TOK_LBRACK TOK_RBRACK
/* Key words */
%token TOK_IF TOK_ELSE TOK_CLASS TOK_RETURN TOK_EVENT TOK_ANY TOK_PRINT TOK_WHILE TOK_BREAK TOK_CONTINUE
%token TOK_UINTNUM TOK_DOUBLENUM TOK_VOID TOK_STRING TOK_STRCONST
%token TOK_INT TOK_UNSIGNED TOK_BOOL TOK_BYTE TOK_DOUBLE TOK_SHORT TOK_LONG TOK_BIT TOK_ENUM
%token TOK_TRUE TOK_FALSE TOK_IDENT
/******************************** Type Info ********************************/
%type <functionEntry> function_decl function_proto
%type <variableEntry> global_var local_var class_var
%type <classEntry> class_decl class_proto
%type <classFuncVal> obj_func
%type <eventEntry> event_decl any_event normal_event
%type <valVal> literal
%type <exprVal> expression func_invocation assignment binary_operation unary_operation condition ref_expr obj_var
%type <typeVal> type intType baseType func_param event_param
%type <typeList> func_param_l event_param_l
%type <exprList> caller_param_l
%type <ifVal> if_then_else_stmt
%type <whileVal> while_stmt
%type <returnVal> return_stmt
%type <stmtVal> simple_stmt basic_block rule_stmts
%type <stmt_plusVal> compound_stmt1 compound_stmt2 empty_block function_defn
%type <stmtList> stmt_plus2
%type <breakVal> break_stmt
%type <continueVal> continue_stmt
%type <ruleNode> rule
%type <patVal> primitive_all binary_event_oprs
%type <cVal> class_func_invocation
%type <cVal> TOK_IDENT TOK_PRINT
%type <uVal> TOK_UINTNUM
%type <dVal> TOK_DOUBLENUM
%type <cVal> TOK_STRCONST
%type <stmtVal> TOK_SEMICOLON
/********************* Precedence and Associativity ********************/
%nonassoc TOK_ASSIGN
%left TOK_PAT_OR
%left TOK_COLON
%left TOK_PAT_STAR
%right TOK_PAT_NOT
%left TOK_OR
%left TOK_AND
%right TOK_NOT
%nonassoc TOK_EQ TOK_NE TOK_GT TOK_LT TOK_GE TOK_LE
%left TOK_BITOR TOK_BITXOR
%left TOK_BITAND
%right TOK_BITNOT
%left TOK_SHL TOK_SHR
%right TOK_DOUBLE_COLON
%left TOK_PLUS TOK_MINUS
%left TOK_MULT TOK_DIV TOK_MOD
%right TOK_UMINUS
%left TOK_LBRACK
%left TOK_DOT
/**************************************************************************/
%start s_specification
%%
s_specification: specification { prt("Spec"); }
;
specification: specification declaration
| specification rule
|
;
declaration: event_decl { stm.leaveScope(); stm.insert($1); }
| function_proto { stm.leaveScope(); stm.insert($1); }
| class_proto { stm.leaveScope(); }
| global_decl
;
rule: primitive_all TOK_ARROW rule_stmts { $$ = allocRuleNode($1, $3); prt("Rule"); }
;
rule_stmts: compound_stmt2 semi_colon { $$ = $1; }
| compound_stmt2 { $$ = $1; }
| expression semi_colon { $$ = allocExprStmtNode($1); prt("AssignStmt"); }
| semi_colon { $$ = allocExprStmtNode(NULL); }
;
compound_stmt2: TOK_LBRACE compound_stmt2 TOK_RBRACE { $$ = allocScopeNode($2); prt("CompoundStmt"); }
| TOK_LBRACE compound_stmt2 semi_colon TOK_RBRACE { $$ = allocScopeNode($2); prt("CompoundStmt"); }
| compound_stmt1 { $$ = $1; }
;
compound_stmt1: TOK_LBRACE stmt_plus1 TOK_RBRACE { $$ = allocCompStmtNode(curStmtList1); curStmtList1 = NULL; prt("CompoundStmt"); }
| TOK_LBRACE semi_colon TOK_RBRACE { $$ = allocCompStmtNode(new list<StmtNode*>); prt("CompoundStmt"); }
;
stmt_plus1: stmt_plus1 simple_stmt { if (!curStmtList1) curStmtList1 = new list<StmtNode*>; curStmtList1->push_back($2);}
|
;
primitive_all: normal_event { $$ = allocPrimPatNode($1, curVarList, NULL, true); curVarList = NULL; prt("PrimitivePat"); }
| any_event { $$ = allocPrimPatNode($1, curVarList, NULL, false); curVarList = NULL; prt("PrimitivePat"); }
| normal_event TOK_BITOR expression { $$ = allocPrimPatNode($1, curVarList, $3, false); curVarList = NULL; prt("PrimitivePatWithCond"); }
| binary_event_oprs { $$ = $1; }
| TOK_LPAREN primitive_all TOK_RPAREN { $$ = $2; prt("(EventPat)"); }
;
binary_event_oprs: primitive_all TOK_PAT_OR primitive_all { $$ = allocPatNode(BasePatNode::PatNodeKind::OR , $1, $3); prt("EventPat \\/ EventPat"); }
| primitive_all TOK_COLON primitive_all { $$ = allocPatNode(BasePatNode::PatNodeKind::SEQ , $1, $3); prt("EventPat : EventPat"); }
| primitive_all TOK_PAT_STAR { $$ = allocPatNode(BasePatNode::PatNodeKind::STAR, $1, NULL); prt("EventPat **"); }
| TOK_NOT primitive_all { $$ = allocPatNode(BasePatNode::PatNodeKind::NEG , $2, NULL); prt("!EventPat"); }
;
any_event: TOK_ANY { allocRuleBlockNode(); $$ = any; prt("anyEvent"); }
;
normal_event: TOK_IDENT TOK_LPAREN event_aparam_l TOK_RPAREN { $$ = getEventEntry($1); prt("NormalEvent"); }
;
event_aparam_l:
event_aparam_l TOK_COMMA TOK_IDENT { curVarList->push_back(allocVarEntry($3, VariableEntry::PARAM_VAR, new Type(Type::UNKNOWN), NULL)); prt("EventFormalParam");}
| TOK_IDENT { allocRuleBlockNode(); curVarList = new vector<VariableEntry*>();
curVarList->push_back(allocVarEntry($1, VariableEntry::PARAM_VAR, new Type(Type::UNKNOWN), NULL)); prt("EventFormalParam");}
|
;
while_stmt: TOK_WHILE condition basic_block { $$ = allocWhileNode($2, $3); prt("WhileStatement"); }
;
if_then_else_stmt: TOK_IF condition basic_block { $$ = allocIfNode($2, $3, NULL); prt("IfThenStmt"); }
| TOK_IF condition basic_block TOK_ELSE basic_block { $$ = allocIfNode($2, $3, $5); prt("IfThenElseStmt"); }
;
basic_block: TOK_LBRACE stmt_plus2 TOK_RBRACE semi_colon { $$ = allocCompStmtNode($2); prt("CompoundStmt"); }
| TOK_LBRACE stmt_plus2 TOK_RBRACE { $$ = allocCompStmtNode($2); prt("CompoundStmt"); }
| empty_block { $$ = $1; }
| simple_stmt { $$ = $1; }
| semi_colon { $$ = allocExprStmtNode(NULL); }
;
stmt_plus2: stmt_plus2 simple_stmt { if($2) $$->push_back($2); }
| simple_stmt { $$ = new list<StmtNode*>; if($1) $$->push_back($1); }
;
simple_stmt: func_invocation semi_colon { $$ = allocExprStmtNode($1); prt("FunctionInvocationStmt"); }
| assignment semi_colon { $$ = allocExprStmtNode($1); prt("AssignStmt"); }
| if_then_else_stmt { $$ = $1; }
| return_stmt { $$ = $1; }
| while_stmt { $$ = $1; }
| break_stmt { $$ = $1; }
| continue_stmt { $$ = $1; }
| obj_func { $$ = allocExprStmtNode($1); prt("ObjectFunctionCall"); }
;
return_stmt: TOK_RETURN expression semi_colon { $$ = allocReturnNode($2); prt("ReturnStmt"); }
| TOK_RETURN semi_colon { $$ = allocReturnNode(NULL); prt("ReturnStmt"); }
;
break_stmt: TOK_BREAK semi_colon { $$ = allocBreakNode(allocValueNode(new Value(1, Type::UINT))); prt("BreakStmt"); }
| TOK_BREAK TOK_UINTNUM semi_colon { $$ = allocBreakNode(allocValueNode(new Value($2, Type::UINT))); prt("BreakStmt"); }
;
continue_stmt: TOK_CONTINUE semi_colon { $$ = allocContinueNode(allocValueNode(new Value(1, Type::UINT))); prt("ContinueStmt");}
| TOK_CONTINUE TOK_UINTNUM semi_colon { $$ = allocContinueNode(allocValueNode(new Value($2, Type::UINT))); prt("ContinueStmt");}
;
empty_block: TOK_LBRACE semi_colon TOK_RBRACE semi_colon { $$ = allocCompStmtNode(new list<StmtNode*>); prt("CompoundStmt"); }
| TOK_LBRACE semi_colon TOK_RBRACE { $$ = allocCompStmtNode(new list<StmtNode*>); prt("CompoundStmt"); }
| TOK_LBRACE TOK_RBRACE semi_colon { $$ = allocCompStmtNode(new list<StmtNode*>); prt("CompoundStmt"); }
| TOK_LBRACE TOK_RBRACE { $$ = allocCompStmtNode(new list<StmtNode*>); prt("CompoundStmt"); }
;
condition: TOK_LPAREN expression TOK_RPAREN { $$ = $2; prt("(Expr)"); }
;
func_invocation: TOK_IDENT TOK_LPAREN caller_param_l TOK_RPAREN { $$ = allocInvNode ($1, curExprList); curExprList = NULL; prt("FunctionInvocation"); }
| TOK_IDENT TOK_LPAREN TOK_RPAREN { $$ = allocInvNode ($1, NULL); prt("FunctionInvocation"); }
| TOK_PRINT TOK_LPAREN caller_param_l TOK_RPAREN { $$ = allocPrintNode(curExprList); curExprList = NULL; prt("PrintFunctionNode"); }
| TOK_PRINT TOK_LPAREN TOK_RPAREN { $$ = allocPrintNode ( NULL); prt("FunctionInvocation"); }
;
caller_param_l: caller_param_l TOK_COMMA expression { curExprList->push_back($3); }
| expression { curExprList = new vector<ExprNode*>(); curExprList->push_back($1); }
;
assignment: ref_expr TOK_ASSIGN expression { $$ = allocOpNode(OpNode::OpCode::ASSIGN, $1, $3); prt("Assignment"); }
;
expression: literal { $$ = allocValueNode($1); }
| ref_expr { $$ = $1; }
| func_invocation { $$ = $1; }
| assignment { $$ = $1; }
| unary_operation { $$ = $1; }
| binary_operation { $$ = $1; }
| TOK_LPAREN expression TOK_RPAREN { $$ = $2; prt("(Expr)"); }
;
unary_operation: TOK_NOT expression { $$ = allocOpNode(OpNode::OpCode::NOT , $2, NULL); prt("!Expr"); }
| TOK_BITNOT expression { $$ = allocOpNode(OpNode::OpCode::BITNOT, $2, NULL); prt("~Expr"); }
| TOK_MINUS expression %prec TOK_UMINUS { $$ = allocOpNode(OpNode::OpCode::UMINUS, $2, NULL); prt("-Expr"); }
;
binary_operation: expression TOK_OR expression { $$ = allocOpNode(OpNode::OpCode::OR , $1, $3); prt("Expr || Expr"); }
| expression TOK_AND expression { $$ = allocOpNode(OpNode::OpCode::AND , $1, $3); prt("Expr && Expr"); }
| expression TOK_EQ expression { $$ = allocOpNode(OpNode::OpCode::EQ , $1, $3); prt("Expr == Expr"); }
| expression TOK_NE expression { $$ = allocOpNode(OpNode::OpCode::NE , $1, $3); prt("Expr != Expr"); }
| expression TOK_GT expression { $$ = allocOpNode(OpNode::OpCode::GT , $1, $3); prt("Expr > Expr"); }
| expression TOK_LT expression { $$ = allocOpNode(OpNode::OpCode::LT , $1, $3); prt("Expr < Expr"); }
| expression TOK_GE expression { $$ = allocOpNode(OpNode::OpCode::GE , $1, $3); prt("Expr >= Expr"); }
| expression TOK_LE expression { $$ = allocOpNode(OpNode::OpCode::LE , $1, $3); prt("Expr <= Expr"); }
| expression TOK_BITOR expression { $$ = allocOpNode(OpNode::OpCode::BITOR , $1, $3); prt("Expr | Expr"); }
| expression TOK_BITXOR expression { $$ = allocOpNode(OpNode::OpCode::BITXOR, $1, $3); prt("Expr ^ Expr"); }
| expression TOK_BITAND expression { $$ = allocOpNode(OpNode::OpCode::BITAND, $1, $3); prt("Expr & Expr"); }
| expression TOK_SHL expression { $$ = allocOpNode(OpNode::OpCode::SHL , $1, $3); prt("Expr << Expr"); }
| expression TOK_SHR expression { $$ = allocOpNode(OpNode::OpCode::SHR , $1, $3); prt("Expr >> Expr"); }
| expression TOK_PLUS expression { $$ = allocOpNode(OpNode::OpCode::PLUS , $1, $3); prt("Expr + Expr"); }
| expression TOK_MINUS expression { $$ = allocOpNode(OpNode::OpCode::MINUS , $1, $3); prt("Expr - Expr"); }
| expression TOK_MULT expression { $$ = allocOpNode(OpNode::OpCode::MULT , $1, $3); prt("Expr * Expr"); }
| expression TOK_DIV expression { $$ = allocOpNode(OpNode::OpCode::DIV , $1, $3); prt("Expr / Expr"); }
| expression TOK_MOD expression { $$ = allocOpNode(OpNode::OpCode::MOD , $1, $3); prt("Expr % Expr"); }
;
ref_expr: TOK_IDENT { $$ = allocRefExprNode($1); prt("RefExpr"); }
| obj_var { $$ = $1; prt("Object Function Call"); }
;
global_decl: type global_l semi_colon
;
global_l: global_l TOK_COMMA global_var
| global_var
;
global_var: TOK_IDENT { $$ = allocVarEntry($1, VariableEntry::GLOBAL_VAR, curType, NULL); prt("VariableDecl"); }
| TOK_IDENT TOK_ASSIGN expression { $$ = allocVarEntry($1, VariableEntry::GLOBAL_VAR, curType, $3); prt("VariableDeclWithInit");}
;
class_proto: class_decl semi_colon { $$ = $1; }
| class_decl class_defn semi_colon { $$ = $1; $$->type()->size(curClassSize); curClassSize = 0; }
;
class_decl: TOK_CLASS TOK_IDENT { $$ = allocClassEntry($2); prt("ClassDecl"); }
;
class_defn: TOK_LBRACE class_body TOK_RBRACE
;
class_body: class_body class_var_decl
| class_body function_proto { stm.leaveScope(); stm.insert($2); }
|
;
class_var_decl: type class_var_l semi_colon
;
class_var_l: class_var_l TOK_COMMA class_var
| class_var
;
class_var: TOK_IDENT { $$ = allocVarEntry($1, VariableEntry::CLASS_VAR, curType, NULL); prt("ClassVarDecl");
; curClassSize += $$->type()->size(); }
obj_var: TOK_IDENT TOK_DOT TOK_IDENT { $$ = allocClassRefExprNode($1, $3); prt("classVariableCall"); }
;
obj_func: TOK_IDENT TOK_DOT class_func_invocation semi_colon { $$ = allocClassFuncInvNode($1, $3, funcParamList); prt("classFuntionInvocation"); }
;
class_func_invocation: TOK_IDENT TOK_LPAREN caller_param_l TOK_RPAREN { funcParamList = curExprList; curExprList = NULL; prt("ClassFunctionInvocation"); }
| TOK_IDENT TOK_LPAREN TOK_RPAREN { funcParamList = NULL; prt("ClassFunctionInvocation"); }
;
function_proto: function_decl semi_colon { $$ = $1; }
| function_decl function_defn { $$ = $1; $1->body($2); checkReturnStmt($1); }
;
function_defn: TOK_LBRACE function_body TOK_RBRACE semi_colon { $$ = allocCompStmtNode(curStmtList3); curStmtList3 = NULL; prt("FunctionBody"); }
| TOK_LBRACE function_body TOK_RBRACE { $$ = allocCompStmtNode(curStmtList3); curStmtList3 = NULL; prt("FunctionBody"); }
;
function_body: local_decl_l function_stmts
| local_decl_l
| function_stmts
|
;
function_stmts: function_stmts simple_stmt { curStmtList3->push_back($2); }
| simple_stmt { curStmtList3 = new list<StmtNode*>; curStmtList3->push_back($1); }
;
local_decl_l: local_decl_l local_decl
| local_decl
;
local_decl: type local_l semi_colon
;
local_l: local_l TOK_COMMA local_var
| local_var
;
local_var: TOK_IDENT { $$ = allocVarEntry($1, VariableEntry::LOCAL_VAR, curType, NULL); prt("VariableDecl"); }
| TOK_IDENT TOK_ASSIGN expression { $$ = allocVarEntry($1, VariableEntry::LOCAL_VAR, curType, $3); prt("VariableDeclWithInit"); }
;
function_decl: type TOK_IDENT func_param_l { $$ = allocFuncEntry($2, $1, curTypeList); curTypeList = NULL; prt("FunctionDecl"); }
;
func_param_l: TOK_LPAREN func_param_l TOK_RPAREN { prt("FormalParamList"); }
| func_param_l TOK_COMMA func_param { curTypeList->push_back($3); }
| func_param { curTypeList = new vector<Type*>(); curTypeList->push_back($1); }
| TOK_LPAREN TOK_RPAREN { curFuncEnt = allocFuncEntry(); }
;
func_param: type TOK_IDENT { if (!curFuncEnt) curFuncEnt = allocFuncEntry();
$$ = $1; allocVarEntry($2, VariableEntry::PARAM_VAR, $1, NULL); prt("FormalParam"); }
;
event_decl: TOK_EVENT TOK_IDENT event_param_l semi_colon { $$ = allocEventEntry($2, curTypeList); curTypeList = NULL; prt("EventDecl"); }
;
event_param_l: TOK_LPAREN event_param_l TOK_RPAREN { prt("FormalParamList"); }
| event_param_l TOK_COMMA event_param { curTypeList->push_back($3); }
| event_param { curTypeList = new vector<Type*>(); curTypeList->push_back($1);}
| TOK_LPAREN TOK_RPAREN { curEventEnt = allocEventEntry();}
;
event_param: type TOK_IDENT { if (!curEventEnt) curEventEnt = allocEventEntry(); checkEventParamType($1);
$$ = $1; allocVarEntry($2, VariableEntry::PARAM_VAR, $1, NULL); prt("FormalParam");
}
;
semi_colon: semi_colon TOK_SEMICOLON
| TOK_SEMICOLON
;
literal:
TOK_UINTNUM { $$ = new Value($1, Type::UINT); prt("Literal"); }
| TOK_DOUBLENUM { $$ = new Value($1); prt("Literal"); }
| TOK_STRCONST { $$ = new Value($1); prt("Literal"); }
| TOK_TRUE { $$ = new Value(true); prt("Literal"); }
| TOK_FALSE { $$ = new Value(false); prt("Literal"); }
;
type: baseType { $$ = curType = $1; prt("Type"); }
| intType { $$ = curType = $1; prt("Type"); }
| TOK_IDENT { $$ = curType = getUserType($1); prt("Type"); }
| TOK_UNSIGNED baseType { $$ = curType = new Type(Type::VOID);prt("Type");
errMsg("Invalid use of keyword \"unsigned\""); }
;
baseType: TOK_VOID { $$ = new Type(Type::VOID); }
| TOK_BOOL { $$ = new Type(Type::BOOL); }
| TOK_BYTE { $$ = new Type(Type::BYTE); }
| TOK_STRING { $$ = new Type(Type::STRING); }
| TOK_DOUBLE { $$ = new Type(Type::DOUBLE); }
| TOK_SHORT { $$ = new Type(Type::INT);
warnMsg("Unsupported datatype, converting 'short' to 'int'"); }
| TOK_LONG { $$ = new Type(Type::INT);
warnMsg("Unsupported datatype, converting 'long' to 'int'"); }
| TOK_BIT { $$ = new Type(Type::INT);
warnMsg("Unsupported datatype, Converting 'bit' to 'int'"); }
| TOK_ENUM { $$ = new Type(Type::VOID);
errMsg("Unsupported datatype 'enum'"); }
;
intType: TOK_INT { $$ = new Type(Type::INT); }
| TOK_UNSIGNED TOK_INT { $$ = new Type(Type::UINT); }
;
%%