|
2 | 2 | #include <cstdio>
|
3 | 3 | #include <iostream>
|
4 | 4 | #include "semantic_actions_utils.h"
|
| 5 | + #include <string.h> |
5 | 6 |
|
6 | 7 | using namespace semantic_actions_util;
|
7 | 8 |
|
|
18 | 19 | %token<id> IDENTIFIER
|
19 | 20 | %token INT_NUM
|
20 | 21 | %token FLOAT_NUM
|
21 |
| -%token ARITH_OP |
22 |
| -%token BOOL_OP |
23 |
| -%token REL_OP |
| 22 | +%token<smallString> ARITH_OP |
| 23 | +%token <smallString>BOOL_OP |
| 24 | +%token <smallString>REL_OP |
24 | 25 | %token IF
|
25 | 26 | %token ELSE
|
26 | 27 | %token WHILE
|
|
31 | 32 |
|
32 | 33 | %type<varType> primitive_type
|
33 | 34 | %type<expressionType> expression;
|
| 35 | +%type<booleanExpressionType> boolean_expression; |
| 36 | + |
| 37 | +%code requires{ |
| 38 | + #include <unordered_set> |
| 39 | +} |
34 | 40 |
|
35 | 41 | %union{
|
| 42 | + char smallString[20]; |
36 | 43 | char id[30];
|
37 | 44 | int varType;
|
38 | 45 | struct ExpressionType{
|
39 | 46 | int varType;
|
| 47 | + std::unordered_set<int> trueList; |
| 48 | + std::unordered_set<int> falseList; |
40 | 49 | }expressionType;
|
| 50 | + struct BooleanExpressionType{ |
| 51 | + std::unordered_set<int> trueList; |
| 52 | + std::unordered_set<int> falseList; |
| 53 | + }booleanExpressionType; |
41 | 54 | }
|
42 | 55 |
|
43 | 56 | %%
|
@@ -101,141 +114,74 @@ assignment: IDENTIFIER '=' expression ';'{
|
101 | 114 | };
|
102 | 115 |
|
103 | 116 | expression:
|
104 |
| -<<<<<<< HEAD |
105 |
| - INT_NUM {$$.varType = VarType::INT_TYPE;} |
106 |
| - |FLOAT_NUM {$$.varType = VarType::FLOAT_TYPE;} |
| 117 | + INT_NUM {$$.varType = VarType::INT_TYPE; } |
| 118 | + |FLOAT_NUM {$$.varType = VarType::FLOAT_TYPE ; } |
107 | 119 | |IDENTIFIER {
|
108 |
| - if(checkIfVariableExists($1)) { |
109 |
| - $$.varType = varToVarIndexAndType[$1].second; |
110 |
| - } else { |
111 |
| - //TODO handle the error of variable used but not declared |
112 |
| - } |
113 |
| - } |
114 |
| - |expression ARITH_OP expression |
115 |
| - |'(' expression ')'{} |
116 |
| -======= |
117 |
| - INT_NUM {$$.sType = INT_TYPE; } |
118 |
| - |FLOAT_NUM {$$.sType = FLOAT_TYPE ; } |
119 |
| - |IDENTIFIER { |
120 |
| - |
121 | 120 | // check if the identifier already exist to load or not
|
122 |
| - if(checkIfVariableExists($1)) |
123 |
| - { |
124 |
| - $$.sType = varToVarIndexAndType[$1].second; |
125 |
| - if($$.sType == INT_TYPE ) |
126 |
| - { |
127 |
| - //write iload + identifier |
128 |
| - appendToCode("iload " + to_string(varToVarIndexAndType[$1].first)); |
| 121 | + if(checkIfVariableExists($1)) { |
| 122 | + $$.varType = varToVarIndexAndType[$1].second; |
| 123 | + if($$.varType == VarType::INT_TYPE ) { |
| 124 | + //write iload + identifier |
| 125 | + appendToCode("iload_" + to_string(varToVarIndexAndType[$1].first)); |
129 | 126 | }
|
130 |
| - else |
131 |
| - //float |
132 |
| - { |
133 |
| - //write fload + identifier |
134 |
| - appendToCode("fload " + to_string(varToVarIndexAndType[$1].first)); |
| 127 | + else { |
| 128 | + //float |
| 129 | + //write fload + identifier |
| 130 | + appendToCode("fload_" + to_string(varToVarIndexAndType[$1].first)); |
135 | 131 | }
|
136 |
| - |
137 |
| - |
138 |
| - |
139 | 132 | }
|
140 |
| - else //it's not declared at all |
141 |
| - |
142 |
| - { |
143 |
| - string err = "identifier: "+$1+" isn't declared in this scope"; |
144 |
| - yyerror(err.c_str()); |
145 |
| - $$.sType = ERROR_T; |
| 133 | + else {//it's not declared at all |
| 134 | + string err = "identifier: "+string{$1}+" isn't declared in this scope"; |
| 135 | + yyerror(err.c_str()); |
146 | 136 | }
|
147 |
| - |
148 |
| - |
149 |
| - |
150 | 137 | }
|
151 | 138 | |expression ARITH_OP expression {
|
152 |
| - |
153 |
| - if ($1.sType == $3.sType ) |
154 |
| - { |
155 |
| - if ($1.sType == INT_TYPE) |
156 |
| - |
157 |
| - |
158 |
| - appendToCode("i" + getOperationCode($2) ); |
159 |
| - else //it's float |
160 |
| - |
161 |
| - appendToCode("f" + getOperationCode($2) ); |
162 |
| - } |
163 |
| - |
164 |
| - |
165 |
| - |
166 |
| - |
167 |
| - } |
168 |
| - |'(' expression ')' {$$.sType = $2.sType;} |
169 |
| ->>>>>>> feature/expression |
| 139 | + if ($1.varType == $3.varType ) { |
| 140 | + if ($1.varType == VarType::INT_TYPE) |
| 141 | + appendToCode("i" + getOperationCode($2)); |
| 142 | + else //it's float |
| 143 | + appendToCode("f" + getOperationCode($2)); |
| 144 | + } |
| 145 | + } |
| 146 | + |'(' expression ')' {$$.varType = $2.varType;} |
170 | 147 | ;
|
171 | 148 |
|
172 | 149 | boolean_expression:
|
173 |
| - TRUE |
174 |
| - { |
175 |
| - $$.trueList = new vector<int> (); |
176 |
| - $$.trueList->push_back(//code size ); |
177 |
| - $$.falseList = new vector<int>(); |
| 150 | + TRUE { |
| 151 | + // $$.trueList = new vector<int> (); |
| 152 | + $$.trueList.insert(static_cast<int>(outputCode.size())); |
| 153 | + // $$.falseList = new vector<int>(); |
178 | 154 | // write code goto line #
|
179 |
| - appendToCode("goto ") |
180 |
| - |
181 |
| - |
| 155 | + appendToCode("goto "); |
182 | 156 | }
|
183 |
| - |FALSE |
184 |
| - { |
185 |
| - $$.trueList = new vector<int> (); |
186 |
| - $$.falseList= new vector<int>(); |
187 |
| - $$.falseList->push_back(//code size); |
| 157 | + |FALSE { |
| 158 | + // $$.trueList = new vector<int> (); |
| 159 | + // $$.falseList= new vector<int>(); |
| 160 | + $$.falseList.insert(static_cast<int>(outputCode.size())); |
188 | 161 | // write code goto line #
|
189 |
| - appendToCode("goto ") |
190 |
| - |
| 162 | + appendToCode("goto "); |
191 | 163 | }
|
192 |
| - |expression BOOL_OP expression |
193 |
| - { |
194 |
| - if(!strcmp($2, "&&")) |
195 |
| - { |
196 |
| - |
197 |
| - $$.trueList = $3.trueList; |
198 |
| - $$.falseList = merge($1.falseList,$3.falseList); |
199 |
| - } |
200 |
| - else if (!strcmp($2,"||")) |
201 |
| - { |
202 |
| - |
203 |
| - $$.trueList = merge($1.trueList, $3.trueList); |
204 |
| - $$.falseList = $3.falseList; |
205 |
| - } |
206 |
| - |
207 |
| - |
| 164 | + |expression BOOL_OP expression { |
| 165 | + if(!strcmp($2, "&&")) { |
| 166 | + $$.trueList = $3.trueList; |
| 167 | + // $$.falseList = merge($1.falseList,$3.falseList); |
| 168 | + } |
| 169 | + else if (!strcmp($2,"||")) { |
| 170 | + // $$.trueList = merge($1.trueList, $3.trueList); |
| 171 | + $$.falseList = $3.falseList; |
| 172 | + } |
208 | 173 | }
|
209 |
| - |expression REL_OP expression |
210 |
| - { |
211 |
| - $$.trueList = new vector<int>(); |
212 |
| - $$.trueList ->push_back (//code size); |
213 |
| - |
214 |
| - $$.falseList = new vector<int>(); |
215 |
| - $$.falseList->push_back(//code size+1); |
216 |
| - |
217 |
| - |
218 |
| - appendToCode(getOperationCode($2)+ " "); |
219 |
| - appendToCode("goto "); |
220 |
| - |
221 |
| - |
| 174 | + |expression REL_OP expression { |
| 175 | + // $$.trueList = new vector<int>(); |
| 176 | + $$.trueList.insert(static_cast<int>(outputCode.size())); |
| 177 | + // $$.falseList = new vector<int>(); |
| 178 | + $$.falseList.insert(static_cast<int>(outputCode.size()+1)); |
| 179 | + appendToCode(getOperationCode($2)+ " "); |
| 180 | + appendToCode("goto "); |
222 | 181 | }
|
223 |
| - |
224 |
| - |
| 182 | + ; |
225 | 183 | %%
|
226 | 184 |
|
227 |
| - |
228 |
| -string getOperationCode(string operational) |
229 |
| -{ |
230 |
| - map<string, string>::iterator it ; |
231 |
| - it = mp.find(operational); |
232 |
| - |
233 |
| - if(it == opList.end()) |
234 |
| - return ""; |
235 |
| - else |
236 |
| - return it->second; |
237 |
| - |
238 |
| -} |
239 | 185 | string genLabel()
|
240 | 186 | {
|
241 | 187 | return "L_"+to_string(labelsCount++);
|
|
0 commit comments