author | Mario de Sousa <msousa@fe.up.pt> |
Wed, 30 Mar 2011 19:53:32 +0100 | |
changeset 257 | 90782e241346 |
parent 202 | da1a8186f86f |
child 258 | d7d92b2f87e9 |
permissions | -rwxr-xr-x |
70 | 1 |
/* |
2 |
* (c) 2003 Mario de Sousa |
|
3 |
* |
|
4 |
* Offered to the public under the terms of the GNU General Public License |
|
5 |
* as published by the Free Software Foundation; either version 2 of the |
|
6 |
* License, or (at your option) any later version. |
|
7 |
* |
|
8 |
* This program is distributed in the hope that it will be useful, but |
|
9 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
11 |
* Public License for more details. |
|
12 |
* |
|
13 |
* This code is made available on the understanding that it will not be |
|
14 |
* used in safety-critical situations without a full and competent review. |
|
15 |
*/ |
|
16 |
||
17 |
/* |
|
18 |
* An IEC 61131-3 IL and ST compiler. |
|
19 |
* |
|
20 |
* Based on the |
|
21 |
* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
22 |
* |
|
23 |
*/ |
|
24 |
||
25 |
||
26 |
/* |
|
27 |
* Conversion of st statements (i.e. ST code). |
|
28 |
* |
|
29 |
* This is part of the 4th stage that generates |
|
30 |
* a c++ source program equivalent to the IL and ST |
|
31 |
* code. |
|
32 |
*/ |
|
33 |
||
34 |
||
35 |
/***********************************************************************/ |
|
36 |
/***********************************************************************/ |
|
37 |
/***********************************************************************/ |
|
38 |
/***********************************************************************/ |
|
39 |
||
40 |
||
41 |
class generate_c_st_c: public generate_c_typedecl_c { |
|
42 |
||
43 |
private: |
|
44 |
/* When calling a function block, we must first find it's type, |
|
45 |
* by searching through the declarations of the variables currently |
|
46 |
* in scope. |
|
47 |
* This class does just that... |
|
48 |
* A new class is instantiated whenever we begin generating the code |
|
49 |
* for a function block type declaration, or a program declaration. |
|
50 |
* This object instance will then later be called while the |
|
51 |
* function block's or the program's body is being handled. |
|
52 |
* |
|
53 |
* Note that functions cannot contain calls to function blocks, |
|
54 |
* so we do not create an object instance when handling |
|
55 |
* a function declaration. |
|
56 |
*/ |
|
57 |
search_fb_instance_decl_c *search_fb_instance_decl; |
|
58 |
||
59 |
/* When compiling st code, it becomes necessary to determine the |
|
60 |
* data type of st expressions. To do this, we must first find the |
|
61 |
* st operand's declaration, within the scope of the function block |
|
62 |
* or function currently being processed. |
|
63 |
* The following object does just that... |
|
64 |
* This object instance will then later be called while the |
|
65 |
* remaining st code is being handled. |
|
66 |
*/ |
|
67 |
search_expression_type_c *search_expression_type; |
|
68 |
||
69 |
search_varfb_instance_type_c *search_varfb_instance_type; |
|
70 |
||
98 | 71 |
search_base_type_c search_base_type; |
72 |
||
73 |
symbol_c* current_array_type; |
|
74 |
||
145 | 75 |
bool current_param_is_pointer; |
76 |
||
70 | 77 |
public: |
78 |
generate_c_st_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL) |
|
79 |
: generate_c_typedecl_c(s4o_ptr) { |
|
80 |
search_fb_instance_decl = new search_fb_instance_decl_c(scope); |
|
81 |
search_expression_type = new search_expression_type_c(scope); |
|
82 |
search_varfb_instance_type = new search_varfb_instance_type_c(scope); |
|
83 |
this->set_variable_prefix(variable_prefix); |
|
98 | 84 |
current_array_type = NULL; |
145 | 85 |
current_param_is_pointer = false; |
70 | 86 |
} |
87 |
||
88 |
virtual ~generate_c_st_c(void) { |
|
89 |
delete search_fb_instance_decl; |
|
90 |
delete search_expression_type; |
|
91 |
delete search_varfb_instance_type; |
|
92 |
} |
|
93 |
||
94 |
||
95 |
public: |
|
96 |
void generate(statement_list_c *stl) { |
|
97 |
stl->accept(*this); |
|
98 |
} |
|
99 |
||
100 |
private: |
|
101 |
||
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
102 |
|
70 | 103 |
/*********************/ |
104 |
/* B 1.4 - Variables */ |
|
105 |
/*********************/ |
|
106 |
void *visit(symbolic_variable_c *symbol) { |
|
107 |
unsigned int vartype = search_varfb_instance_type->get_vartype(symbol); |
|
145 | 108 |
if (!current_param_is_pointer && (vartype == search_var_instance_decl_c::external_vt || vartype == search_var_instance_decl_c::located_vt)) { |
70 | 109 |
s4o.print("*("); |
110 |
generate_c_base_c::visit(symbol); |
|
111 |
s4o.print(")"); |
|
112 |
} |
|
145 | 113 |
else if (current_param_is_pointer && vartype != search_var_instance_decl_c::external_vt && vartype != search_var_instance_decl_c::located_vt) { |
114 |
s4o.print("&("); |
|
115 |
generate_c_base_c::visit(symbol); |
|
116 |
s4o.print(")"); |
|
117 |
} |
|
70 | 118 |
else { |
119 |
generate_c_base_c::visit(symbol); |
|
120 |
} |
|
121 |
return NULL; |
|
122 |
} |
|
123 |
||
124 |
/********************************************/ |
|
125 |
/* B.1.4.1 Directly Represented Variables */ |
|
126 |
/********************************************/ |
|
127 |
// direct_variable: direct_variable_token {$$ = new direct_variable_c($1);}; |
|
128 |
void *visit(direct_variable_c *symbol) { |
|
129 |
TRACE("direct_variable_c"); |
|
130 |
/* Do not use print_token() as it will change everything into uppercase */ |
|
131 |
if (strlen(symbol->value) == 0) ERROR; |
|
145 | 132 |
if (!current_param_is_pointer) { |
133 |
s4o.print("*("); |
|
134 |
} |
|
70 | 135 |
this->print_variable_prefix(); |
136 |
s4o.printlocation(symbol->value + 1); |
|
145 | 137 |
if (!current_param_is_pointer) { |
138 |
s4o.print(")"); |
|
139 |
} |
|
70 | 140 |
return NULL; |
141 |
} |
|
142 |
||
98 | 143 |
/*************************************/ |
144 |
/* B.1.4.2 Multi-element Variables */ |
|
145 |
/*************************************/ |
|
146 |
||
147 |
/* subscripted_variable '[' subscript_list ']' */ |
|
148 |
//SYM_REF2(array_variable_c, subscripted_variable, subscript_list) |
|
149 |
void *visit(array_variable_c *symbol) { |
|
150 |
current_array_type = search_varfb_instance_type->get_type(symbol->subscripted_variable, false); |
|
151 |
symbol->subscripted_variable->accept(*this); |
|
152 |
if (current_array_type != NULL) { |
|
153 |
symbol->subscript_list->accept(*this); |
|
154 |
current_array_type = NULL; |
|
155 |
} |
|
156 |
return NULL; |
|
157 |
} |
|
158 |
||
159 |
/* subscript_list ',' subscript */ |
|
160 |
void *visit(subscript_list_c *symbol) { |
|
161 |
for (int i = 0; i < symbol->n; i++) { |
|
162 |
s4o.print("[__"); |
|
163 |
current_array_type->accept(*this); |
|
164 |
s4o.print("_TRANSIDX"); |
|
165 |
print_integer(i); |
|
166 |
s4o.print("("); |
|
167 |
symbol->elements[i]->accept(*this); |
|
168 |
s4o.print(")]"); |
|
169 |
} |
|
170 |
return NULL; |
|
171 |
} |
|
172 |
||
173 |
||
70 | 174 |
/***************************************/ |
175 |
/* B.3 - Language ST (Structured Text) */ |
|
176 |
/***************************************/ |
|
177 |
/***********************/ |
|
178 |
/* B 3.1 - Expressions */ |
|
179 |
/***********************/ |
|
180 |
void *visit(or_expression_c *symbol) { |
|
181 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
182 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
183 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
184 |
ERROR; |
|
185 |
if (search_expression_type->is_bool_type(left_type)) |
|
186 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " || "); |
|
187 |
if (search_expression_type->is_binary_type(left_type)) |
|
188 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " | "); |
|
189 |
ERROR; |
|
190 |
return NULL; |
|
191 |
} |
|
192 |
||
193 |
void *visit(xor_expression_c *symbol) { |
|
194 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
195 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
196 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
197 |
ERROR; |
|
198 |
if (search_expression_type->is_bool_type(left_type)) { |
|
199 |
s4o.print("("); |
|
200 |
symbol->l_exp->accept(*this); |
|
201 |
s4o.print(" && !"); |
|
202 |
symbol->r_exp->accept(*this); |
|
203 |
s4o.print(") || (!"); |
|
204 |
symbol->l_exp->accept(*this); |
|
205 |
s4o.print(" && "); |
|
206 |
symbol->r_exp->accept(*this); |
|
207 |
s4o.print(")"); |
|
130 | 208 |
return NULL; |
70 | 209 |
} |
210 |
if (search_expression_type->is_binary_type(left_type)) |
|
211 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " ^ "); |
|
212 |
ERROR; |
|
213 |
return NULL; |
|
214 |
} |
|
215 |
||
216 |
void *visit(and_expression_c *symbol) { |
|
217 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
218 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
219 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
220 |
ERROR; |
|
221 |
if (search_expression_type->is_bool_type(left_type)) |
|
222 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " && "); |
|
223 |
if (search_expression_type->is_binary_type(left_type)) |
|
224 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " & "); |
|
225 |
ERROR; |
|
226 |
return NULL; |
|
227 |
} |
|
228 |
||
229 |
void *visit(equ_expression_c *symbol) { |
|
230 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
231 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
232 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
233 |
ERROR; |
|
123 | 234 |
if (search_expression_type->is_time_type(left_type) || |
235 |
search_expression_type->is_string_type(left_type)) |
|
70 | 236 |
return print_compare_function("__eq_", left_type, symbol->l_exp, symbol->r_exp); |
237 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " == "); |
|
238 |
} |
|
239 |
||
240 |
void *visit(notequ_expression_c *symbol) { |
|
241 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
242 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
243 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
244 |
ERROR; |
|
123 | 245 |
if (search_expression_type->is_time_type(left_type) || |
246 |
search_expression_type->is_string_type(left_type)) |
|
70 | 247 |
return print_compare_function("__ne_", left_type, symbol->l_exp, symbol->r_exp); |
248 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " != "); |
|
249 |
} |
|
250 |
||
251 |
void *visit(lt_expression_c *symbol) { |
|
252 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
253 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
254 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
255 |
ERROR; |
|
123 | 256 |
if (search_expression_type->is_time_type(left_type) || |
257 |
search_expression_type->is_string_type(left_type)) |
|
70 | 258 |
return print_compare_function("__lt_", left_type, symbol->l_exp, symbol->r_exp); |
123 | 259 |
if (!search_base_type.type_is_enumerated(left_type)) |
260 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " < "); |
|
261 |
ERROR; |
|
262 |
return NULL; |
|
70 | 263 |
} |
264 |
||
265 |
void *visit(gt_expression_c *symbol) { |
|
266 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
267 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
268 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
269 |
ERROR; |
|
123 | 270 |
if (search_expression_type->is_time_type(left_type) || |
271 |
search_expression_type->is_string_type(left_type)) |
|
70 | 272 |
return print_compare_function("__gt_", left_type, symbol->l_exp, symbol->r_exp); |
123 | 273 |
if (!search_base_type.type_is_enumerated(left_type)) |
274 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " > "); |
|
275 |
ERROR; |
|
276 |
return NULL; |
|
70 | 277 |
} |
278 |
||
279 |
void *visit(le_expression_c *symbol) { |
|
280 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
281 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
282 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
283 |
ERROR; |
|
123 | 284 |
if (search_expression_type->is_time_type(left_type) || |
285 |
search_expression_type->is_string_type(left_type)) |
|
70 | 286 |
return print_compare_function("__le_", left_type, symbol->l_exp, symbol->r_exp); |
123 | 287 |
if (!search_base_type.type_is_enumerated(left_type)) |
288 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= "); |
|
289 |
ERROR; |
|
290 |
return NULL; |
|
70 | 291 |
} |
292 |
||
293 |
void *visit(ge_expression_c *symbol) { |
|
294 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
295 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
296 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
297 |
ERROR; |
|
123 | 298 |
if (search_expression_type->is_time_type(left_type) || |
299 |
search_expression_type->is_string_type(left_type)) |
|
70 | 300 |
return print_compare_function("__ge_", left_type, symbol->l_exp, symbol->r_exp); |
123 | 301 |
if (!search_base_type.type_is_enumerated(left_type)) |
302 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= "); |
|
303 |
ERROR; |
|
304 |
return NULL; |
|
70 | 305 |
} |
306 |
||
307 |
void *visit(add_expression_c *symbol) { |
|
308 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
309 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
310 |
if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
311 |
(typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
312 |
(typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c))) |
|
149 | 313 |
return print_binary_function("__TIME_ADD", symbol->l_exp, symbol->r_exp); |
70 | 314 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
315 |
ERROR; |
|
316 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
317 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " + "); |
|
318 |
ERROR; |
|
319 |
return NULL; |
|
320 |
} |
|
321 |
||
322 |
void *visit(sub_expression_c *symbol) { |
|
323 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
324 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
325 |
if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
326 |
(typeid(*left_type) == typeid(date_type_name_c) && typeid(*right_type) == typeid(date_type_name_c)) || |
|
327 |
(typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
328 |
(typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(tod_type_name_c)) || |
|
329 |
(typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
330 |
(typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(dt_type_name_c))) |
|
149 | 331 |
return print_binary_function("__TIME_SUB", symbol->l_exp, symbol->r_exp); |
70 | 332 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
333 |
ERROR; |
|
334 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
335 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " - "); |
|
336 |
ERROR; |
|
337 |
return NULL; |
|
338 |
} |
|
339 |
||
340 |
void *visit(mul_expression_c *symbol) { |
|
341 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
342 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
343 |
if ((typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_integer_type(right_type)) || |
|
344 |
(typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_real_type(right_type))) |
|
149 | 345 |
return print_binary_function("__TIME_MUL", symbol->l_exp, symbol->r_exp); |
70 | 346 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
347 |
ERROR; |
|
348 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
349 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " * "); |
|
350 |
ERROR; |
|
351 |
return NULL; |
|
352 |
} |
|
353 |
||
354 |
void *visit(div_expression_c *symbol) { |
|
355 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
356 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
149 | 357 |
if ((typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_integer_type(right_type)) || |
358 |
(typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_real_type(right_type))) |
|
359 |
return print_binary_function("__TIME_DIV", symbol->l_exp, symbol->r_exp); |
|
70 | 360 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
361 |
ERROR; |
|
362 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
363 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " / "); |
|
364 |
ERROR; |
|
365 |
return NULL; |
|
366 |
} |
|
367 |
||
368 |
void *visit(mod_expression_c *symbol) { |
|
369 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
370 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
371 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
372 |
ERROR; |
|
373 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) { |
|
374 |
s4o.print("(("); |
|
375 |
symbol->r_exp->accept(*this); |
|
376 |
s4o.print(" == 0)?0:"); |
|
377 |
print_binary_expression(symbol->l_exp, symbol->r_exp, " % "); |
|
378 |
s4o.print(")"); |
|
379 |
return NULL; |
|
380 |
} |
|
381 |
ERROR; |
|
382 |
return NULL; |
|
383 |
} |
|
384 |
||
385 |
/* TODO: power expression... */ |
|
257 | 386 |
void *visit(power_expression_c *symbol) { |
387 |
ERROR; |
|
388 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " ** "); |
|
389 |
} |
|
390 |
||
123 | 391 |
void *visit(neg_expression_c *symbol) { |
257 | 392 |
return print_unary_expression(symbol->exp, " -"); |
123 | 393 |
} |
70 | 394 |
|
395 |
void *visit(not_expression_c *symbol) { |
|
396 |
symbol_c *exp_type = search_expression_type->get_type(symbol->exp); |
|
123 | 397 |
if (search_expression_type->is_binary_type(exp_type)) |
398 |
return print_unary_expression(symbol->exp, search_expression_type->is_bool_type(exp_type)?"!":"~"); |
|
399 |
ERROR; |
|
400 |
return NULL; |
|
70 | 401 |
} |
402 |
||
403 |
void *visit(function_invocation_c *symbol) { |
|
149 | 404 |
symbol_c* function_type_prefix = NULL; |
405 |
symbol_c* function_name = NULL; |
|
406 |
symbol_c* function_type_suffix = NULL; |
|
407 |
std::list<FUNCTION_PARAM> param_list; |
|
408 |
FUNCTION_PARAM *param; |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
409 |
|
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
410 |
symbol_c *parameter_assignment_list = NULL; |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
411 |
if (NULL != symbol-> formal_param_list) parameter_assignment_list = symbol-> formal_param_list; |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
412 |
if (NULL != symbol->nonformal_param_list) parameter_assignment_list = symbol->nonformal_param_list; |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
413 |
if (NULL == parameter_assignment_list) ERROR; |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
414 |
|
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
415 |
function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
70 | 416 |
if (f_decl == function_symtable.end_value()) { |
417 |
/* The function called is not in the symtable, so we test if it is a |
|
418 |
* standard function defined in standard */ |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
419 |
|
70 | 420 |
function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
421 |
if (current_function_type == function_none) ERROR; |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
422 |
|
70 | 423 |
symbol_c *function_return_type = search_expression_type->get_type(symbol); |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
424 |
|
70 | 425 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
167
ae423926bc84
Fixed parameter count error on variadic functions with EN/ENO
etisserant
parents:
162
diff
changeset
|
426 |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
427 |
int nb_param = ((list_c *)parameter_assignment_list)->n; |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
428 |
|
149 | 429 |
identifier_c en_param_name("EN"); |
430 |
/* Get the value from EN param */ |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
431 |
symbol_c *EN_param_value = function_call_param_iterator.search_f(&en_param_name); |
149 | 432 |
if (EN_param_value == NULL) |
433 |
EN_param_value = (symbol_c*)(new boolean_literal_c((symbol_c*)(new bool_type_name_c()), new boolean_true_c())); |
|
167
ae423926bc84
Fixed parameter count error on variadic functions with EN/ENO
etisserant
parents:
162
diff
changeset
|
434 |
else |
ae423926bc84
Fixed parameter count error on variadic functions with EN/ENO
etisserant
parents:
162
diff
changeset
|
435 |
nb_param --; |
149 | 436 |
ADD_PARAM_LIST(EN_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_in) |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
437 |
|
162
60a124678842
Fixed typo in EN/ENO parameters handling for std library functions
etisserant
parents:
160
diff
changeset
|
438 |
identifier_c eno_param_name("ENO"); |
149 | 439 |
/* Get the value from ENO param */ |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
440 |
symbol_c *ENO_param_value = function_call_param_iterator.search_f(&eno_param_name); |
167
ae423926bc84
Fixed parameter count error on variadic functions with EN/ENO
etisserant
parents:
162
diff
changeset
|
441 |
if (ENO_param_value != NULL) |
ae423926bc84
Fixed parameter count error on variadic functions with EN/ENO
etisserant
parents:
162
diff
changeset
|
442 |
nb_param --; |
149 | 443 |
ADD_PARAM_LIST(ENO_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_out) |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
444 |
|
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
445 |
#define search(x) search_f(x) |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
446 |
#define next() next_nf() |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
447 |
// #define search_constant_type_c::constant_int_type_name search_expression_type_c::integer |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
448 |
#define constant_int_type_name integer |
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
449 |
#include "st_code_gen.c" |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
450 |
#undef constant_int_type_name |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
451 |
// #undef search_constant_type_c::constant_int_type_name |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
452 |
#undef next |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
453 |
#undef search |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
454 |
|
70 | 455 |
} |
456 |
else { |
|
457 |
/* loop through each function parameter, find the value we should pass |
|
458 |
* to it, and then output the c equivalent... |
|
459 |
*/ |
|
460 |
function_param_iterator_c fp_iterator(f_decl); |
|
461 |
||
149 | 462 |
function_name = symbol->function_name; |
70 | 463 |
|
464 |
identifier_c *param_name; |
|
465 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
466 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
149 | 467 |
|
70 | 468 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
149 | 469 |
|
70 | 470 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
471 |
symbol_c *param_value = function_call_param_iterator.search_f(param_name); |
70 | 472 |
|
473 |
/* Get the value from a foo(<param_value>) style call */ |
|
474 |
if (param_value == NULL) |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
475 |
param_value = function_call_param_iterator.next_nf(); |
149 | 476 |
|
477 |
if (param_value == NULL && param_direction == function_param_iterator_c::direction_in) { |
|
478 |
/* No value given for parameter, so we must use the default... */ |
|
479 |
/* First check whether default value specified in function declaration...*/ |
|
480 |
param_value = fp_iterator.default_value(); |
|
481 |
} |
|
482 |
||
70 | 483 |
symbol_c *param_type = fp_iterator.param_type(); |
484 |
if (param_type == NULL) ERROR; |
|
149 | 485 |
|
486 |
ADD_PARAM_LIST(param_value, param_type, param_direction) |
|
70 | 487 |
} /* for(...) */ |
488 |
// symbol->parameter_assignment->accept(*this); |
|
149 | 489 |
} |
490 |
||
491 |
if (function_type_prefix != NULL) { |
|
492 |
s4o.print("("); |
|
493 |
function_type_prefix->accept(*this); |
|
70 | 494 |
s4o.print(")"); |
149 | 495 |
} |
496 |
if (function_name != NULL) |
|
497 |
function_name->accept(*this); |
|
498 |
if (function_type_suffix != NULL) |
|
499 |
function_type_suffix->accept(*this); |
|
500 |
s4o.print("("); |
|
501 |
s4o.indent_right(); |
|
502 |
||
503 |
std::list<FUNCTION_PARAM>::iterator pt; |
|
504 |
for(pt = param_list.begin(); pt != param_list.end(); pt++) { |
|
505 |
if (pt != param_list.begin()) |
|
506 |
s4o.print(",\n"+s4o.indent_spaces); |
|
507 |
symbol_c *param_value = pt->param_value; |
|
508 |
symbol_c *param_type = pt->param_type; |
|
509 |
||
510 |
switch (pt->param_direction) { |
|
511 |
case function_param_iterator_c::direction_in: |
|
512 |
if (param_value == NULL) { |
|
513 |
/* If not, get the default value of this variable's type */ |
|
514 |
param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance()); |
|
515 |
} |
|
516 |
if (param_value == NULL) ERROR; |
|
517 |
if (search_base_type.type_is_subrange(param_type)) { |
|
518 |
s4o.print("__CHECK_"); |
|
519 |
param_type->accept(*this); |
|
520 |
s4o.print("("); |
|
521 |
} |
|
522 |
param_value->accept(*this); |
|
523 |
if (search_base_type.type_is_subrange(param_type)) |
|
524 |
s4o.print(")"); |
|
525 |
break; |
|
526 |
case function_param_iterator_c::direction_out: |
|
527 |
case function_param_iterator_c::direction_inout: |
|
528 |
current_param_is_pointer = true; |
|
529 |
if (param_value == NULL) { |
|
530 |
s4o.print("NULL"); |
|
531 |
} else { |
|
532 |
param_value->accept(*this); |
|
533 |
} |
|
534 |
current_param_is_pointer = false; |
|
535 |
break; |
|
536 |
case function_param_iterator_c::direction_extref: |
|
537 |
/* TODO! */ |
|
538 |
ERROR; |
|
539 |
break; |
|
540 |
} /* switch */ |
|
541 |
} |
|
169 | 542 |
|
149 | 543 |
s4o.print(")"); |
544 |
s4o.indent_left(); |
|
70 | 545 |
|
546 |
return NULL; |
|
547 |
} |
|
548 |
||
549 |
/********************/ |
|
550 |
/* B 3.2 Statements */ |
|
551 |
/********************/ |
|
552 |
void *visit(statement_list_c *symbol) { |
|
553 |
return print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); |
|
554 |
} |
|
555 |
||
556 |
/*********************************/ |
|
557 |
/* B 3.2.1 Assignment Statements */ |
|
558 |
/*********************************/ |
|
559 |
void *visit(assignment_statement_c *symbol) { |
|
98 | 560 |
symbol_c *left_type = search_varfb_instance_type->get_type(symbol->l_exp, false); |
561 |
||
70 | 562 |
symbol->l_exp->accept(*this); |
563 |
s4o.print(" = "); |
|
123 | 564 |
if (search_base_type.type_is_subrange(left_type)) { |
98 | 565 |
s4o.print("__CHECK_"); |
566 |
left_type->accept(*this); |
|
567 |
s4o.print("("); |
|
568 |
} |
|
70 | 569 |
symbol->r_exp->accept(*this); |
123 | 570 |
if (search_base_type.type_is_subrange(left_type)) |
98 | 571 |
s4o.print(")"); |
70 | 572 |
return NULL; |
573 |
} |
|
574 |
||
575 |
/*****************************************/ |
|
576 |
/* B 3.2.2 Subprogram Control Statements */ |
|
577 |
/*****************************************/ |
|
578 |
||
579 |
/* fb_name '(' [param_assignment_list] ')' */ |
|
580 |
/* param_assignment_list -> may be NULL ! */ |
|
581 |
//SYM_REF2(fb_invocation_c, fb_name, param_assignment_list) |
|
582 |
void *visit(fb_invocation_c *symbol) { |
|
583 |
TRACE("fb_invocation_c"); |
|
584 |
/* first figure out what is the name of the function block type of the function block being called... */ |
|
585 |
symbol_c *function_block_type_name = this->search_fb_instance_decl->get_type_name(symbol->fb_name); |
|
586 |
/* should never occur. The function block instance MUST have been declared... */ |
|
587 |
if (function_block_type_name == NULL) ERROR; |
|
588 |
||
589 |
/* Now find the declaration of the function block type being called... */ |
|
590 |
function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(function_block_type_name); |
|
591 |
/* should never occur. The function block type being called MUST be in the symtable... */ |
|
592 |
if (fb_decl == function_block_type_symtable.end_value()) ERROR; |
|
593 |
||
594 |
/* loop through each function block parameter, find the value we should pass |
|
595 |
* to it, and then output the c equivalent... |
|
596 |
*/ |
|
597 |
function_param_iterator_c fp_iterator(fb_decl); |
|
598 |
identifier_c *param_name; |
|
599 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
600 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
601 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
160 | 602 |
|
162
60a124678842
Fixed typo in EN/ENO parameters handling for std library functions
etisserant
parents:
160
diff
changeset
|
603 |
/*fprintf(stderr, "param : %s\n", param_name->value);*/ |
160 | 604 |
|
70 | 605 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
606 |
symbol_c *param_value = function_call_param_iterator.search_f(param_name); |
70 | 607 |
|
608 |
/* Get the value from a foo(<param_value>) style call */ |
|
609 |
if (param_value == NULL) |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
610 |
param_value = function_call_param_iterator.next_nf(); |
70 | 611 |
|
98 | 612 |
symbol_c *param_type = fp_iterator.param_type(); |
613 |
if (param_type == NULL) ERROR; |
|
614 |
||
70 | 615 |
/* now output the value assignment */ |
616 |
if (param_value != NULL) |
|
617 |
if ((param_direction == function_param_iterator_c::direction_in) || |
|
618 |
(param_direction == function_param_iterator_c::direction_inout)) { |
|
619 |
print_variable_prefix(); |
|
620 |
symbol->fb_name->accept(*this); |
|
621 |
s4o.print("."); |
|
622 |
param_name->accept(*this); |
|
623 |
s4o.print(" = "); |
|
123 | 624 |
if (search_base_type.type_is_subrange(param_type)) { |
98 | 625 |
s4o.print("__CHECK_"); |
626 |
param_type->accept(*this); |
|
627 |
s4o.print("("); |
|
628 |
} |
|
70 | 629 |
param_value->accept(*this); |
123 | 630 |
if (search_base_type.type_is_subrange(param_type)) |
98 | 631 |
s4o.print(")"); |
70 | 632 |
s4o.print(";\n" + s4o.indent_spaces); |
633 |
} |
|
634 |
} /* for(...) */ |
|
635 |
||
636 |
/* now call the function... */ |
|
637 |
function_block_type_name->accept(*this); |
|
638 |
s4o.print(FB_FUNCTION_SUFFIX); |
|
639 |
s4o.print("(&"); |
|
640 |
print_variable_prefix(); |
|
641 |
symbol->fb_name->accept(*this); |
|
642 |
s4o.print(")"); |
|
643 |
||
644 |
/* loop through each function parameter, find the variable to which |
|
645 |
* we should atribute the value of all output or inoutput parameters. |
|
646 |
*/ |
|
647 |
fp_iterator.reset(); |
|
648 |
function_call_param_iterator.reset(); |
|
649 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
650 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
651 |
||
652 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
653 |
symbol_c *param_value = function_call_param_iterator.search_f(param_name); |
70 | 654 |
|
655 |
/* Get the value from a foo(<param_value>) style call */ |
|
656 |
if (param_value == NULL) |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
657 |
param_value = function_call_param_iterator.next_nf(); |
70 | 658 |
|
659 |
/* now output the value assignment */ |
|
660 |
if (param_value != NULL) |
|
661 |
if ((param_direction == function_param_iterator_c::direction_out) || |
|
662 |
(param_direction == function_param_iterator_c::direction_inout)) { |
|
98 | 663 |
symbol_c *param_type = search_varfb_instance_type->get_type(param_value, false); |
664 |
||
70 | 665 |
s4o.print(";\n"+ s4o.indent_spaces); |
666 |
param_value->accept(*this); |
|
667 |
s4o.print(" = "); |
|
123 | 668 |
if (search_base_type.type_is_subrange(param_type)) { |
98 | 669 |
s4o.print("__CHECK_"); |
670 |
param_type->accept(*this); |
|
671 |
s4o.print("("); |
|
672 |
} |
|
70 | 673 |
print_variable_prefix(); |
674 |
symbol->fb_name->accept(*this); |
|
675 |
s4o.print("."); |
|
676 |
param_name->accept(*this); |
|
123 | 677 |
if (search_base_type.type_is_subrange(param_type)) |
98 | 678 |
s4o.print(")"); |
70 | 679 |
} |
680 |
} /* for(...) */ |
|
681 |
||
682 |
return NULL; |
|
683 |
} |
|
684 |
||
685 |
||
686 |
||
687 |
||
688 |
/* helper symbol for fb_invocation */ |
|
689 |
/* param_assignment_list ',' param_assignment */ |
|
690 |
void *visit(param_assignment_list_c *symbol) { |
|
691 |
TRACE("param_assignment_list_c"); |
|
692 |
/* this should never be called... */ |
|
693 |
ERROR; |
|
694 |
return NULL; |
|
695 |
// return print_list(symbol, "", ", "); |
|
696 |
} |
|
697 |
||
698 |
||
699 |
void *visit(input_variable_param_assignment_c *symbol) { |
|
700 |
TRACE("input_variable_param_assignment_c"); |
|
701 |
/* this should never be called... */ |
|
702 |
ERROR; |
|
703 |
return NULL; |
|
704 |
/* |
|
705 |
symbol->variable_name->accept(*this); |
|
706 |
s4o.print(" = "); |
|
707 |
symbol->expression->accept(*this); |
|
708 |
return NULL; |
|
709 |
*/ |
|
710 |
} |
|
711 |
||
712 |
void *visit(output_variable_param_assignment_c *symbol) { |
|
713 |
TRACE("output_variable_param_assignment_c"); |
|
714 |
/* this should never be called... */ |
|
715 |
ERROR; |
|
716 |
return NULL; |
|
717 |
/* |
|
718 |
s4o.print(s4o.indent_spaces); |
|
719 |
if (symbol->not_param != NULL) |
|
720 |
symbol->not_param->accept(*this); |
|
721 |
symbol->variable_name->accept(*this); |
|
722 |
s4o.print(" => "); |
|
723 |
symbol->variable->accept(*this); |
|
724 |
return NULL; |
|
725 |
*/ |
|
726 |
} |
|
727 |
||
728 |
// TODO: the NOT symbol in function (block) calls... |
|
729 |
void *visit(not_paramassign_c *symbol) { |
|
730 |
TRACE("not_paramassign_c"); |
|
731 |
/* this should never be called... */ |
|
732 |
ERROR; |
|
733 |
return NULL; |
|
734 |
/* |
|
735 |
s4o.print("NOT "); |
|
736 |
return NULL; |
|
737 |
*/ |
|
738 |
} |
|
739 |
||
740 |
||
741 |
/********************************/ |
|
742 |
/* B 3.2.3 Selection Statements */ |
|
743 |
/********************************/ |
|
744 |
void *visit(if_statement_c *symbol) { |
|
745 |
s4o.print("if ("); |
|
746 |
symbol->expression->accept(*this); |
|
747 |
s4o.print(") {\n"); |
|
748 |
s4o.indent_right(); |
|
749 |
symbol->statement_list->accept(*this); |
|
750 |
s4o.indent_left(); |
|
751 |
symbol->elseif_statement_list->accept(*this); |
|
752 |
||
753 |
if (symbol->else_statement_list != NULL) { |
|
754 |
s4o.print(s4o.indent_spaces); s4o.print("} else {\n"); |
|
755 |
s4o.indent_right(); |
|
756 |
symbol->else_statement_list->accept(*this); |
|
757 |
s4o.indent_left(); |
|
758 |
} |
|
759 |
s4o.print(s4o.indent_spaces); s4o.print("}"); |
|
760 |
return NULL; |
|
761 |
} |
|
762 |
||
763 |
/* helper symbol for if_statement */ |
|
764 |
void *visit(elseif_statement_list_c *symbol) {return print_list(symbol);} |
|
765 |
||
766 |
/* helper symbol for elseif_statement_list */ |
|
767 |
void *visit(elseif_statement_c *symbol) { |
|
768 |
s4o.print(s4o.indent_spaces); s4o.print("} else if ("); |
|
769 |
symbol->expression->accept(*this); |
|
770 |
s4o.print(") {\n"); |
|
771 |
s4o.indent_right(); |
|
772 |
symbol->statement_list->accept(*this); |
|
773 |
s4o.indent_left(); |
|
774 |
return NULL; |
|
775 |
} |
|
776 |
||
777 |
void *visit(case_statement_c *symbol) { |
|
778 |
s4o.print("switch("); |
|
779 |
symbol->expression->accept(*this); |
|
780 |
s4o.print(") {\n"); |
|
781 |
s4o.indent_right(); |
|
782 |
symbol->case_element_list->accept(*this); |
|
783 |
if (symbol->statement_list != NULL) { |
|
784 |
s4o.print(s4o.indent_spaces + "default:\n"); |
|
785 |
s4o.indent_right(); |
|
786 |
symbol->statement_list->accept(*this); |
|
787 |
s4o.print(s4o.indent_spaces + "break;\n"); |
|
788 |
s4o.indent_left(); |
|
789 |
} |
|
790 |
s4o.indent_left(); |
|
791 |
s4o.print(s4o.indent_spaces + "}"); |
|
792 |
return NULL; |
|
793 |
} |
|
794 |
||
795 |
/* helper symbol for case_statement */ |
|
796 |
void *visit(case_element_list_c *symbol) {return print_list(symbol);} |
|
797 |
||
798 |
void *visit(case_element_c *symbol) { |
|
799 |
s4o.print(s4o.indent_spaces + "case "); |
|
800 |
symbol->case_list->accept(*this); |
|
801 |
s4o.print(" :\n"); |
|
802 |
s4o.indent_right(); |
|
803 |
symbol->statement_list->accept(*this); |
|
804 |
s4o.print(s4o.indent_spaces + "break;\n"); |
|
805 |
s4o.indent_left(); |
|
806 |
return NULL; |
|
807 |
} |
|
808 |
||
809 |
void *visit(case_list_c *symbol) {return print_list(symbol, "", ", ");} |
|
810 |
||
811 |
/********************************/ |
|
812 |
/* B 3.2.4 Iteration Statements */ |
|
813 |
/********************************/ |
|
814 |
void *visit(for_statement_c *symbol) { |
|
815 |
s4o.print("for("); |
|
816 |
symbol->control_variable->accept(*this); |
|
817 |
s4o.print(" = "); |
|
818 |
symbol->beg_expression->accept(*this); |
|
819 |
s4o.print("; "); |
|
820 |
symbol->control_variable->accept(*this); |
|
821 |
s4o.print(" != "); |
|
822 |
symbol->end_expression->accept(*this); |
|
823 |
s4o.print("; "); |
|
824 |
symbol->control_variable->accept(*this); |
|
825 |
if (symbol->by_expression != NULL) { |
|
826 |
s4o.print(" += "); |
|
827 |
symbol->by_expression->accept(*this); |
|
828 |
} else { |
|
829 |
s4o.print("++"); |
|
830 |
} |
|
831 |
s4o.print(") {\n"); |
|
832 |
s4o.indent_right(); |
|
833 |
symbol->statement_list->accept(*this); |
|
834 |
s4o.indent_left(); |
|
835 |
s4o.print(s4o.indent_spaces); s4o.print("}"); |
|
836 |
return NULL; |
|
837 |
} |
|
838 |
void *visit(while_statement_c *symbol) { |
|
839 |
s4o.print("while ("); |
|
840 |
symbol->expression->accept(*this); |
|
841 |
s4o.print(") {\n"); |
|
842 |
s4o.indent_right(); |
|
843 |
symbol->statement_list->accept(*this); |
|
844 |
s4o.indent_left(); |
|
845 |
s4o.print(s4o.indent_spaces); s4o.print("}"); |
|
846 |
return NULL; |
|
847 |
} |
|
848 |
void *visit(repeat_statement_c *symbol) { |
|
849 |
s4o.print("do {\n"); |
|
850 |
s4o.indent_right(); |
|
851 |
symbol->statement_list->accept(*this); |
|
852 |
s4o.indent_left(); |
|
853 |
s4o.print(s4o.indent_spaces); s4o.print("} while("); |
|
854 |
symbol->expression->accept(*this); |
|
855 |
s4o.print(")"); |
|
856 |
return NULL; |
|
857 |
} |
|
858 |
void *visit(exit_statement_c *symbol) { |
|
859 |
s4o.print("exit(0)"); |
|
860 |
return NULL; |
|
861 |
} |
|
862 |
||
863 |
||
864 |
||
865 |
}; /* generate_c_st_c */ |
|
866 |
||
867 |
||
868 |
||
869 |
||
870 |
||
871 |
||
872 |
||
873 |
||
874 |