75
75
%%
76
76
method_body :
77
77
%empty
78
- | {generateHeader();}statement_list {generateFooter();}
78
+ | {generateHeader();}statement_list {writeBytecode(); generateFooter();}
79
79
;
80
80
81
81
statement_list :
82
82
statement {*($$ .nextList) = *($1 .nextList);}
83
83
| statement_list marker_m statement
84
84
{
85
+ cout<<" Backpacth statement List" <<endl;
85
86
backpatch (*($1 .nextList) ,$2.nextInstructionIndex);
87
+ cout<<" finish backpatch" <<endl;
86
88
*($$ .nextList) = *($3 .nextList);
87
89
}
88
90
;
@@ -105,6 +107,7 @@ marker_m:
105
107
106
108
marker_n :
107
109
%empty {
110
+ cout<<" In marker n ==================== " << nextInstructionIndex<<" ======" <<endl;
108
111
// Save the index of the next instruction index in the marker
109
112
$$ .nextList = new unordered_set<int >();
110
113
*($$ .nextList) = makeList(nextInstructionIndex);
131
134
' {' statement_list ' }'
132
135
marker_n
133
136
ELSE ' {' marker_m statement_list ' }' {
137
+ cout<<" backpatching in if else" <<endl;
134
138
backpatch (*($3 .trueList), $5.nextInstructionIndex);
139
+ cout<<" backpatching in if else" <<endl;
135
140
backpatch (*($3 .falseList), $12.nextInstructionIndex);
141
+ cout<<" Finish backpatch if else" <<endl;
136
142
std::unordered_set<int > temp = mergeLists( *($7 .nextList), *($9 .nextList));
143
+
144
+ cout<<" printing nextlist 9 =====================" <<endl;
145
+ for (auto &&item:*($9 .nextList)) cout<<outputCode[item]<<endl;
146
+
137
147
$$ .nextList = new unordered_set<int >();
138
148
*($$ .nextList) = mergeLists(temp, *($13 .nextList));
139
149
}
142
152
while :
143
153
WHILE marker_m ' (' boolean_expression ' )'
144
154
' {' marker_m statement_list ' }' {
155
+ cout<<" backpatching in while" <<endl;
145
156
backpatch (*($8 .nextList), $2.nextInstructionIndex);
146
157
backpatch (*($4 .trueList), $7.nextInstructionIndex);
158
+ cout<<" finish backpatch" <<endl;
159
+ cout<<" next inst index ==== " <<nextInstructionIndex<<" " <<outputCode.size()<<endl;
160
+ for (auto &&item:outputCode) cout<<item<<endl;
147
161
$$ .nextList = new unordered_set<int >();
148
162
*($$ .nextList) = *($4 .falseList);
149
163
appendToCode (" goto Label_" + to_string($2 .nextInstructionIndex));
@@ -156,9 +170,9 @@ assignment: IDENTIFIER '=' expression ';'{
156
170
// Check if the two sides have the same type
157
171
if (varToVarIndexAndType[$1 ].second == $3 .varType) {
158
172
if (varToVarIndexAndType[$1 ].second == VarType::INT_TYPE) {
159
- appendToCode (" istore_ " +to_string(varToVarIndexAndType[$1 ].first));
173
+ appendToCode (" istore " +to_string(varToVarIndexAndType[$1 ].first));
160
174
} else {// Only int and float are supported
161
- appendToCode (" fstore_ " +to_string(varToVarIndexAndType[$1 ].first));
175
+ appendToCode (" fstore " +to_string(varToVarIndexAndType[$1 ].first));
162
176
}
163
177
} else { // case when the two types aren't the same
164
178
// TODO Cast the two variables
@@ -182,12 +196,12 @@ expression:
182
196
$$ .varType = varToVarIndexAndType[$1 ].second;
183
197
if ($$ .varType == VarType::INT_TYPE ) {
184
198
// write iload + identifier
185
- appendToCode (" iload_ " + to_string(varToVarIndexAndType[$1 ].first));
199
+ appendToCode (" iload " + to_string(varToVarIndexAndType[$1 ].first));
186
200
}
187
201
else {
188
202
// float
189
203
// write fload + identifier
190
- appendToCode (" fload_ " + to_string(varToVarIndexAndType[$1 ].first));
204
+ appendToCode (" fload " + to_string(varToVarIndexAndType[$1 ].first));
191
205
}
192
206
}
193
207
else {// it's not declared at all
@@ -225,11 +239,13 @@ boolean_expression:
225
239
}
226
240
| boolean_expression BOOL_OP marker_m boolean_expression {
227
241
if (!strcmp($2 , " &&" )) {
242
+ cout<<" backpatching bool" <<endl;
228
243
backpatch (*($1 .trueList), $3.nextInstructionIndex);
229
244
*($$ .trueList) = *($4 .trueList);
230
245
*($$ .falseList) = mergeLists(*($1 .falseList), *($4 .falseList));
231
246
}
232
247
else if (!strcmp($2 ," ||" )) {
248
+ cout<<" backpatching in bool" <<endl;
233
249
backpatch (*($1 .falseList), $3.nextInstructionIndex);
234
250
*($$ .trueList) = mergeLists(*($1 .trueList), *($4 .trueList));
235
251
*($$ .falseList) = *($4 .falseList);
@@ -271,7 +287,6 @@ int main(int argc, char** argv) {
271
287
yyparse ();
272
288
std::cout<<" Yay ! Parser completed working successfully." <<std::endl;
273
289
274
- writeBytecode ();
275
290
}
276
291
277
292
void yyerror (const char *s) {
0 commit comments