author | laurent |
Tue, 01 Dec 2009 09:03:33 +0100 | |
changeset 220 | f332b62cd2c1 |
parent 217 | f5dfadf5de54 |
child 221 | c6aed7e5f070 |
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 |
||
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
43 |
public: |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
44 |
typedef enum { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
45 |
expression_vg, |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
46 |
assignment_vg, |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
47 |
fparam_output_vg |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
48 |
} variablegeneration_t; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
49 |
|
70 | 50 |
private: |
51 |
/* When calling a function block, we must first find it's type, |
|
52 |
* by searching through the declarations of the variables currently |
|
53 |
* in scope. |
|
54 |
* This class does just that... |
|
55 |
* A new class is instantiated whenever we begin generating the code |
|
56 |
* for a function block type declaration, or a program declaration. |
|
57 |
* This object instance will then later be called while the |
|
58 |
* function block's or the program's body is being handled. |
|
59 |
* |
|
60 |
* Note that functions cannot contain calls to function blocks, |
|
61 |
* so we do not create an object instance when handling |
|
62 |
* a function declaration. |
|
63 |
*/ |
|
64 |
search_fb_instance_decl_c *search_fb_instance_decl; |
|
65 |
||
66 |
/* When compiling st code, it becomes necessary to determine the |
|
67 |
* data type of st expressions. To do this, we must first find the |
|
68 |
* st operand's declaration, within the scope of the function block |
|
69 |
* or function currently being processed. |
|
70 |
* The following object does just that... |
|
71 |
* This object instance will then later be called while the |
|
72 |
* remaining st code is being handled. |
|
73 |
*/ |
|
74 |
search_expression_type_c *search_expression_type; |
|
75 |
||
76 |
search_varfb_instance_type_c *search_varfb_instance_type; |
|
77 |
||
98 | 78 |
search_base_type_c search_base_type; |
79 |
||
80 |
symbol_c* current_array_type; |
|
81 |
||
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
82 |
int fcall_number; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
83 |
symbol_c *fbname; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
84 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
85 |
variablegeneration_t wanted_variablegeneration; |
145 | 86 |
|
70 | 87 |
public: |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
88 |
generate_c_st_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL) |
70 | 89 |
: generate_c_typedecl_c(s4o_ptr) { |
90 |
search_fb_instance_decl = new search_fb_instance_decl_c(scope); |
|
91 |
search_expression_type = new search_expression_type_c(scope); |
|
92 |
search_varfb_instance_type = new search_varfb_instance_type_c(scope); |
|
93 |
this->set_variable_prefix(variable_prefix); |
|
98 | 94 |
current_array_type = NULL; |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
95 |
fcall_number = 0; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
96 |
fbname = name; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
97 |
wanted_variablegeneration = expression_vg; |
70 | 98 |
} |
99 |
||
100 |
virtual ~generate_c_st_c(void) { |
|
101 |
delete search_fb_instance_decl; |
|
102 |
delete search_expression_type; |
|
103 |
delete search_varfb_instance_type; |
|
104 |
} |
|
105 |
||
106 |
||
107 |
public: |
|
108 |
void generate(statement_list_c *stl) { |
|
109 |
stl->accept(*this); |
|
110 |
} |
|
111 |
||
112 |
private: |
|
113 |
||
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
114 |
|
70 | 115 |
/*********************/ |
116 |
/* B 1.4 - Variables */ |
|
117 |
/*********************/ |
|
118 |
void *visit(symbolic_variable_c *symbol) { |
|
119 |
unsigned int vartype = search_varfb_instance_type->get_vartype(symbol); |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
120 |
if (this->is_variable_prefix_null()) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
121 |
if (wanted_variablegeneration == fparam_output_vg) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
122 |
if (vartype == search_var_instance_decl_c::external_vt) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
123 |
s4o.print(GET_EXTERNAL); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
124 |
s4o.print("("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
125 |
symbol->var_name->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
126 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
127 |
else { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
128 |
s4o.print("&("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
129 |
generate_c_base_c::visit(symbol); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
130 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
131 |
s4o.print(")"); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
132 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
133 |
else { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
134 |
if (vartype == search_var_instance_decl_c::external_vt) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
135 |
s4o.print(GET_EXTERNAL); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
136 |
s4o.print("("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
137 |
symbol->var_name->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
138 |
s4o.print(")"); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
139 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
140 |
else |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
141 |
generate_c_base_c::visit(symbol); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
142 |
} |
145 | 143 |
} |
70 | 144 |
else { |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
145 |
switch (wanted_variablegeneration) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
146 |
case expression_vg: |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
147 |
if (vartype == search_var_instance_decl_c::external_vt) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
148 |
s4o.print(GET_EXTERNAL); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
149 |
s4o.print("("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
150 |
symbol->var_name->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
151 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
152 |
else { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
153 |
if (vartype == search_var_instance_decl_c::located_vt) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
154 |
s4o.print(GET_LOCATED); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
155 |
else |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
156 |
s4o.print(GET_VAR); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
157 |
s4o.print("("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
158 |
generate_c_base_c::visit(symbol); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
159 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
160 |
s4o.print(")"); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
161 |
break; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
162 |
case fparam_output_vg: |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
163 |
if (vartype == search_var_instance_decl_c::external_vt) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
164 |
s4o.print(GET_EXTERNAL_BY_REF); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
165 |
s4o.print("("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
166 |
symbol->var_name->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
167 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
168 |
else { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
169 |
if (vartype == search_var_instance_decl_c::located_vt) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
170 |
s4o.print(GET_LOCATED_BY_REF); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
171 |
else |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
172 |
s4o.print(GET_VAR_BY_REF); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
173 |
s4o.print("("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
174 |
generate_c_base_c::visit(symbol); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
175 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
176 |
s4o.print(")"); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
177 |
break; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
178 |
default: |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
179 |
if (vartype == search_var_instance_decl_c::external_vt) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
180 |
symbol->var_name->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
181 |
else |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
182 |
generate_c_base_c::visit(symbol); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
183 |
break; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
184 |
} |
70 | 185 |
} |
186 |
return NULL; |
|
187 |
} |
|
188 |
||
189 |
/********************************************/ |
|
190 |
/* B.1.4.1 Directly Represented Variables */ |
|
191 |
/********************************************/ |
|
192 |
// direct_variable: direct_variable_token {$$ = new direct_variable_c($1);}; |
|
193 |
void *visit(direct_variable_c *symbol) { |
|
194 |
TRACE("direct_variable_c"); |
|
195 |
/* Do not use print_token() as it will change everything into uppercase */ |
|
196 |
if (strlen(symbol->value) == 0) ERROR; |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
197 |
if (this->is_variable_prefix_null()) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
198 |
if (wanted_variablegeneration != fparam_output_vg) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
199 |
s4o.print("*("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
200 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
201 |
else { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
202 |
switch (wanted_variablegeneration) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
203 |
case expression_vg: |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
204 |
s4o.print(GET_LOCATED); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
205 |
s4o.print("("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
206 |
break; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
207 |
case fparam_output_vg: |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
208 |
s4o.print(GET_LOCATED_BY_REF); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
209 |
s4o.print("("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
210 |
break; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
211 |
default: |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
212 |
break; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
213 |
} |
145 | 214 |
} |
70 | 215 |
this->print_variable_prefix(); |
216 |
s4o.printlocation(symbol->value + 1); |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
217 |
if ((this->is_variable_prefix_null() && wanted_variablegeneration != fparam_output_vg) || |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
218 |
wanted_variablegeneration != assignment_vg) |
145 | 219 |
s4o.print(")"); |
70 | 220 |
return NULL; |
221 |
} |
|
222 |
||
98 | 223 |
/*************************************/ |
224 |
/* B.1.4.2 Multi-element Variables */ |
|
225 |
/*************************************/ |
|
226 |
||
227 |
/* subscripted_variable '[' subscript_list ']' */ |
|
228 |
//SYM_REF2(array_variable_c, subscripted_variable, subscript_list) |
|
229 |
void *visit(array_variable_c *symbol) { |
|
230 |
current_array_type = search_varfb_instance_type->get_type(symbol->subscripted_variable, false); |
|
231 |
symbol->subscripted_variable->accept(*this); |
|
232 |
if (current_array_type != NULL) { |
|
233 |
symbol->subscript_list->accept(*this); |
|
234 |
current_array_type = NULL; |
|
235 |
} |
|
236 |
return NULL; |
|
237 |
} |
|
238 |
||
239 |
/* subscript_list ',' subscript */ |
|
240 |
void *visit(subscript_list_c *symbol) { |
|
241 |
for (int i = 0; i < symbol->n; i++) { |
|
242 |
s4o.print("[__"); |
|
243 |
current_array_type->accept(*this); |
|
244 |
s4o.print("_TRANSIDX"); |
|
245 |
print_integer(i); |
|
246 |
s4o.print("("); |
|
247 |
symbol->elements[i]->accept(*this); |
|
248 |
s4o.print(")]"); |
|
249 |
} |
|
250 |
return NULL; |
|
251 |
} |
|
252 |
||
253 |
||
70 | 254 |
/***************************************/ |
255 |
/* B.3 - Language ST (Structured Text) */ |
|
256 |
/***************************************/ |
|
257 |
/***********************/ |
|
258 |
/* B 3.1 - Expressions */ |
|
259 |
/***********************/ |
|
260 |
void *visit(or_expression_c *symbol) { |
|
261 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
262 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
263 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
264 |
ERROR; |
|
265 |
if (search_expression_type->is_bool_type(left_type)) |
|
266 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " || "); |
|
267 |
if (search_expression_type->is_binary_type(left_type)) |
|
268 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " | "); |
|
269 |
ERROR; |
|
270 |
return NULL; |
|
271 |
} |
|
272 |
||
273 |
void *visit(xor_expression_c *symbol) { |
|
274 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
275 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
276 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
277 |
ERROR; |
|
278 |
if (search_expression_type->is_bool_type(left_type)) { |
|
279 |
s4o.print("("); |
|
280 |
symbol->l_exp->accept(*this); |
|
281 |
s4o.print(" && !"); |
|
282 |
symbol->r_exp->accept(*this); |
|
283 |
s4o.print(") || (!"); |
|
284 |
symbol->l_exp->accept(*this); |
|
285 |
s4o.print(" && "); |
|
286 |
symbol->r_exp->accept(*this); |
|
287 |
s4o.print(")"); |
|
130 | 288 |
return NULL; |
70 | 289 |
} |
290 |
if (search_expression_type->is_binary_type(left_type)) |
|
291 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " ^ "); |
|
292 |
ERROR; |
|
293 |
return NULL; |
|
294 |
} |
|
295 |
||
296 |
void *visit(and_expression_c *symbol) { |
|
297 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
298 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
299 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
300 |
ERROR; |
|
301 |
if (search_expression_type->is_bool_type(left_type)) |
|
302 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " && "); |
|
303 |
if (search_expression_type->is_binary_type(left_type)) |
|
304 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " & "); |
|
305 |
ERROR; |
|
306 |
return NULL; |
|
307 |
} |
|
308 |
||
309 |
void *visit(equ_expression_c *symbol) { |
|
310 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
311 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
312 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
313 |
ERROR; |
|
123 | 314 |
if (search_expression_type->is_time_type(left_type) || |
315 |
search_expression_type->is_string_type(left_type)) |
|
70 | 316 |
return print_compare_function("__eq_", left_type, symbol->l_exp, symbol->r_exp); |
317 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " == "); |
|
318 |
} |
|
319 |
||
320 |
void *visit(notequ_expression_c *symbol) { |
|
321 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
322 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
323 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
324 |
ERROR; |
|
123 | 325 |
if (search_expression_type->is_time_type(left_type) || |
326 |
search_expression_type->is_string_type(left_type)) |
|
70 | 327 |
return print_compare_function("__ne_", left_type, symbol->l_exp, symbol->r_exp); |
328 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " != "); |
|
329 |
} |
|
330 |
||
331 |
void *visit(lt_expression_c *symbol) { |
|
332 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
333 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
334 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
335 |
ERROR; |
|
123 | 336 |
if (search_expression_type->is_time_type(left_type) || |
337 |
search_expression_type->is_string_type(left_type)) |
|
70 | 338 |
return print_compare_function("__lt_", left_type, symbol->l_exp, symbol->r_exp); |
123 | 339 |
if (!search_base_type.type_is_enumerated(left_type)) |
340 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " < "); |
|
341 |
ERROR; |
|
342 |
return NULL; |
|
70 | 343 |
} |
344 |
||
345 |
void *visit(gt_expression_c *symbol) { |
|
346 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
347 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
348 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
349 |
ERROR; |
|
123 | 350 |
if (search_expression_type->is_time_type(left_type) || |
351 |
search_expression_type->is_string_type(left_type)) |
|
70 | 352 |
return print_compare_function("__gt_", left_type, symbol->l_exp, symbol->r_exp); |
123 | 353 |
if (!search_base_type.type_is_enumerated(left_type)) |
354 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " > "); |
|
355 |
ERROR; |
|
356 |
return NULL; |
|
70 | 357 |
} |
358 |
||
359 |
void *visit(le_expression_c *symbol) { |
|
360 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
361 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
362 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
363 |
ERROR; |
|
123 | 364 |
if (search_expression_type->is_time_type(left_type) || |
365 |
search_expression_type->is_string_type(left_type)) |
|
70 | 366 |
return print_compare_function("__le_", left_type, symbol->l_exp, symbol->r_exp); |
123 | 367 |
if (!search_base_type.type_is_enumerated(left_type)) |
368 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= "); |
|
369 |
ERROR; |
|
370 |
return NULL; |
|
70 | 371 |
} |
372 |
||
373 |
void *visit(ge_expression_c *symbol) { |
|
374 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
375 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
376 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
377 |
ERROR; |
|
123 | 378 |
if (search_expression_type->is_time_type(left_type) || |
379 |
search_expression_type->is_string_type(left_type)) |
|
70 | 380 |
return print_compare_function("__ge_", left_type, symbol->l_exp, symbol->r_exp); |
123 | 381 |
if (!search_base_type.type_is_enumerated(left_type)) |
382 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= "); |
|
383 |
ERROR; |
|
384 |
return NULL; |
|
70 | 385 |
} |
386 |
||
387 |
void *visit(add_expression_c *symbol) { |
|
388 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
389 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
390 |
if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
391 |
(typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
392 |
(typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c))) |
|
149 | 393 |
return print_binary_function("__TIME_ADD", symbol->l_exp, symbol->r_exp); |
70 | 394 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
395 |
ERROR; |
|
396 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
397 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " + "); |
|
398 |
ERROR; |
|
399 |
return NULL; |
|
400 |
} |
|
401 |
||
402 |
void *visit(sub_expression_c *symbol) { |
|
403 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
404 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
405 |
if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
406 |
(typeid(*left_type) == typeid(date_type_name_c) && typeid(*right_type) == typeid(date_type_name_c)) || |
|
407 |
(typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
408 |
(typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(tod_type_name_c)) || |
|
409 |
(typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
410 |
(typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(dt_type_name_c))) |
|
149 | 411 |
return print_binary_function("__TIME_SUB", symbol->l_exp, symbol->r_exp); |
70 | 412 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
413 |
ERROR; |
|
414 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
415 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " - "); |
|
416 |
ERROR; |
|
417 |
return NULL; |
|
418 |
} |
|
419 |
||
420 |
void *visit(mul_expression_c *symbol) { |
|
421 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
422 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
423 |
if ((typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_integer_type(right_type)) || |
|
424 |
(typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_real_type(right_type))) |
|
149 | 425 |
return print_binary_function("__TIME_MUL", symbol->l_exp, symbol->r_exp); |
70 | 426 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
427 |
ERROR; |
|
428 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
429 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " * "); |
|
430 |
ERROR; |
|
431 |
return NULL; |
|
432 |
} |
|
433 |
||
434 |
void *visit(div_expression_c *symbol) { |
|
435 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
436 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
149 | 437 |
if ((typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_integer_type(right_type)) || |
438 |
(typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_real_type(right_type))) |
|
439 |
return print_binary_function("__TIME_DIV", symbol->l_exp, symbol->r_exp); |
|
70 | 440 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
441 |
ERROR; |
|
442 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
443 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " / "); |
|
444 |
ERROR; |
|
445 |
return NULL; |
|
446 |
} |
|
447 |
||
448 |
void *visit(mod_expression_c *symbol) { |
|
449 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
450 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
451 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
452 |
ERROR; |
|
453 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) { |
|
454 |
s4o.print("(("); |
|
455 |
symbol->r_exp->accept(*this); |
|
456 |
s4o.print(" == 0)?0:"); |
|
457 |
print_binary_expression(symbol->l_exp, symbol->r_exp, " % "); |
|
458 |
s4o.print(")"); |
|
459 |
return NULL; |
|
460 |
} |
|
461 |
ERROR; |
|
462 |
return NULL; |
|
463 |
} |
|
464 |
||
465 |
/* TODO: power expression... */ |
|
466 |
void *visit(power_expression_c *symbol) {ERROR; return print_binary_expression(symbol->l_exp, symbol->r_exp, " ** ");} |
|
123 | 467 |
void *visit(neg_expression_c *symbol) { |
468 |
symbol_c *exp_type = search_expression_type->get_type(symbol->exp); |
|
469 |
if (search_expression_type->is_integer_type(exp_type) || search_expression_type->is_real_type(exp_type)) |
|
470 |
return print_unary_expression(symbol->exp, " -"); |
|
471 |
ERROR; |
|
472 |
return NULL; |
|
473 |
} |
|
70 | 474 |
|
475 |
void *visit(not_expression_c *symbol) { |
|
476 |
symbol_c *exp_type = search_expression_type->get_type(symbol->exp); |
|
123 | 477 |
if (search_expression_type->is_binary_type(exp_type)) |
478 |
return print_unary_expression(symbol->exp, search_expression_type->is_bool_type(exp_type)?"!":"~"); |
|
479 |
ERROR; |
|
480 |
return NULL; |
|
70 | 481 |
} |
482 |
||
483 |
void *visit(function_invocation_c *symbol) { |
|
149 | 484 |
symbol_c* function_type_prefix = NULL; |
485 |
symbol_c* function_name = NULL; |
|
486 |
symbol_c* function_type_suffix = NULL; |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
487 |
DECLARE_PARAM_LIST() |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
488 |
|
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
489 |
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
|
490 |
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
|
491 |
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
|
492 |
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
|
493 |
|
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
494 |
function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
70 | 495 |
if (f_decl == function_symtable.end_value()) { |
496 |
/* The function called is not in the symtable, so we test if it is a |
|
497 |
* 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
|
498 |
|
70 | 499 |
function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
500 |
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
|
501 |
|
70 | 502 |
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
|
503 |
|
70 | 504 |
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
|
505 |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
506 |
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
|
507 |
|
149 | 508 |
identifier_c en_param_name("EN"); |
509 |
/* 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
|
510 |
symbol_c *EN_param_value = function_call_param_iterator.search_f(&en_param_name); |
149 | 511 |
if (EN_param_value == NULL) |
512 |
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
|
513 |
else |
ae423926bc84
Fixed parameter count error on variadic functions with EN/ENO
etisserant
parents:
162
diff
changeset
|
514 |
nb_param --; |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
515 |
ADD_PARAM_LIST(&en_param_name, 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
|
516 |
|
162
60a124678842
Fixed typo in EN/ENO parameters handling for std library functions
etisserant
parents:
160
diff
changeset
|
517 |
identifier_c eno_param_name("ENO"); |
149 | 518 |
/* 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
|
519 |
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
|
520 |
if (ENO_param_value != NULL) |
ae423926bc84
Fixed parameter count error on variadic functions with EN/ENO
etisserant
parents:
162
diff
changeset
|
521 |
nb_param --; |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
522 |
ADD_PARAM_LIST(&eno_param_name, 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
|
523 |
|
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
524 |
#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
|
525 |
|
70 | 526 |
} |
527 |
else { |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
528 |
function_name = symbol->function_name; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
529 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
530 |
/* loop through each function parameter, find the value we should pass |
70 | 531 |
* to it, and then output the c equivalent... |
532 |
*/ |
|
533 |
function_param_iterator_c fp_iterator(f_decl); |
|
534 |
identifier_c *param_name; |
|
535 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
536 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
149 | 537 |
|
70 | 538 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
149 | 539 |
|
70 | 540 |
/* 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
|
541 |
symbol_c *param_value = function_call_param_iterator.search_f(param_name); |
70 | 542 |
|
543 |
/* Get the value from a foo(<param_value>) style call */ |
|
544 |
if (param_value == NULL) |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
545 |
param_value = function_call_param_iterator.next_nf(); |
149 | 546 |
|
547 |
if (param_value == NULL && param_direction == function_param_iterator_c::direction_in) { |
|
548 |
/* No value given for parameter, so we must use the default... */ |
|
549 |
/* First check whether default value specified in function declaration...*/ |
|
550 |
param_value = fp_iterator.default_value(); |
|
551 |
} |
|
552 |
||
70 | 553 |
symbol_c *param_type = fp_iterator.param_type(); |
554 |
if (param_type == NULL) ERROR; |
|
149 | 555 |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
556 |
ADD_PARAM_LIST(param_name, param_value, param_type, param_direction) |
70 | 557 |
} /* for(...) */ |
558 |
// symbol->parameter_assignment->accept(*this); |
|
149 | 559 |
} |
560 |
||
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
561 |
bool has_output_params = false; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
562 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
563 |
if (!this->is_variable_prefix_null()) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
564 |
PARAM_LIST_ITERATOR() { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
565 |
if ((PARAM_DIRECTION == function_param_iterator_c::direction_out || |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
566 |
PARAM_DIRECTION == function_param_iterator_c::direction_inout) && |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
567 |
PARAM_VALUE != NULL) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
568 |
if (!has_output_params) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
569 |
has_output_params = true; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
570 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
571 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
572 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
573 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
574 |
|
149 | 575 |
if (function_type_prefix != NULL) { |
576 |
s4o.print("("); |
|
577 |
function_type_prefix->accept(*this); |
|
70 | 578 |
s4o.print(")"); |
149 | 579 |
} |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
580 |
if (has_output_params) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
581 |
fcall_number++; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
582 |
s4o.print("__"); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
583 |
fbname->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
584 |
s4o.print("_"); |
149 | 585 |
function_name->accept(*this); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
586 |
s4o.print_integer(fcall_number); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
587 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
588 |
else { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
589 |
function_name->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
590 |
if (function_type_suffix != NULL) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
591 |
function_type_suffix->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
592 |
} |
149 | 593 |
s4o.print("("); |
594 |
s4o.indent_right(); |
|
595 |
||
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
596 |
int nb_param = 0; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
597 |
PARAM_LIST_ITERATOR() { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
598 |
symbol_c *param_value = PARAM_VALUE; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
599 |
symbol_c *param_type = PARAM_TYPE; |
149 | 600 |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
601 |
switch (PARAM_DIRECTION) { |
149 | 602 |
case function_param_iterator_c::direction_in: |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
603 |
if (nb_param > 0) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
604 |
s4o.print(",\n"+s4o.indent_spaces); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
605 |
if (param_value == NULL) { |
149 | 606 |
/* If not, get the default value of this variable's type */ |
607 |
param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance()); |
|
608 |
} |
|
609 |
if (param_value == NULL) ERROR; |
|
216
136d6ae70745
Bug with using non explicit literals as standard library functions input parameters fixed
laurent
parents:
208
diff
changeset
|
610 |
s4o.print("("); |
220 | 611 |
if (search_expression_type->is_literal_integer_type(param_type)) |
612 |
search_expression_type->lint_type_name.accept(*this); |
|
613 |
else if (search_expression_type->is_literal_real_type(param_type)) |
|
614 |
search_expression_type->lreal_type_name.accept(*this); |
|
216
136d6ae70745
Bug with using non explicit literals as standard library functions input parameters fixed
laurent
parents:
208
diff
changeset
|
615 |
else |
220 | 616 |
param_type->accept(*this); |
216
136d6ae70745
Bug with using non explicit literals as standard library functions input parameters fixed
laurent
parents:
208
diff
changeset
|
617 |
s4o.print(")"); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
618 |
print_check_function(param_type, param_value); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
619 |
nb_param++; |
149 | 620 |
break; |
621 |
case function_param_iterator_c::direction_out: |
|
622 |
case function_param_iterator_c::direction_inout: |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
623 |
if (!has_output_params) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
624 |
if (nb_param > 0) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
625 |
s4o.print(",\n"+s4o.indent_spaces); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
626 |
if (param_value == NULL) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
627 |
s4o.print("NULL"); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
628 |
else { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
629 |
wanted_variablegeneration = fparam_output_vg; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
630 |
param_value->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
631 |
wanted_variablegeneration = expression_vg; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
632 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
633 |
nb_param++; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
634 |
} |
149 | 635 |
break; |
636 |
case function_param_iterator_c::direction_extref: |
|
637 |
/* TODO! */ |
|
638 |
ERROR; |
|
639 |
break; |
|
640 |
} /* switch */ |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
641 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
642 |
if (has_output_params) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
643 |
if (nb_param > 0) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
644 |
s4o.print(",\n"+s4o.indent_spaces); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
645 |
s4o.print(FB_FUNCTION_PARAM); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
646 |
} |
149 | 647 |
s4o.print(")"); |
648 |
s4o.indent_left(); |
|
70 | 649 |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
650 |
CLEAR_PARAM_LIST() |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
651 |
|
70 | 652 |
return NULL; |
653 |
} |
|
654 |
||
655 |
/********************/ |
|
656 |
/* B 3.2 Statements */ |
|
657 |
/********************/ |
|
658 |
void *visit(statement_list_c *symbol) { |
|
659 |
return print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); |
|
660 |
} |
|
661 |
||
662 |
/*********************************/ |
|
663 |
/* B 3.2.1 Assignment Statements */ |
|
664 |
/*********************************/ |
|
665 |
void *visit(assignment_statement_c *symbol) { |
|
98 | 666 |
symbol_c *left_type = search_varfb_instance_type->get_type(symbol->l_exp, false); |
667 |
||
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
668 |
if (!this->is_variable_prefix_null()) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
669 |
unsigned int vartype = search_varfb_instance_type->get_vartype(symbol->l_exp); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
670 |
if (vartype == search_var_instance_decl_c::external_vt) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
671 |
s4o.print(SET_EXTERNAL); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
672 |
else if (vartype == search_var_instance_decl_c::located_vt) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
673 |
s4o.print(SET_LOCATED); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
674 |
else |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
675 |
s4o.print(SET_VAR); |
98 | 676 |
s4o.print("("); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
677 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
678 |
wanted_variablegeneration = assignment_vg; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
679 |
symbol->l_exp->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
680 |
wanted_variablegeneration = expression_vg; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
681 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
682 |
s4o.print(","); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
683 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
684 |
else { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
685 |
symbol->l_exp->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
686 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
687 |
s4o.print(" = "); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
688 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
689 |
print_check_function(left_type, symbol->r_exp); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
690 |
if (!this->is_variable_prefix_null()) |
98 | 691 |
s4o.print(")"); |
70 | 692 |
return NULL; |
693 |
} |
|
694 |
||
695 |
/*****************************************/ |
|
696 |
/* B 3.2.2 Subprogram Control Statements */ |
|
697 |
/*****************************************/ |
|
698 |
||
699 |
/* fb_name '(' [param_assignment_list] ')' */ |
|
700 |
/* param_assignment_list -> may be NULL ! */ |
|
701 |
//SYM_REF2(fb_invocation_c, fb_name, param_assignment_list) |
|
702 |
void *visit(fb_invocation_c *symbol) { |
|
703 |
TRACE("fb_invocation_c"); |
|
704 |
/* first figure out what is the name of the function block type of the function block being called... */ |
|
705 |
symbol_c *function_block_type_name = this->search_fb_instance_decl->get_type_name(symbol->fb_name); |
|
706 |
/* should never occur. The function block instance MUST have been declared... */ |
|
707 |
if (function_block_type_name == NULL) ERROR; |
|
708 |
||
709 |
/* Now find the declaration of the function block type being called... */ |
|
710 |
function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(function_block_type_name); |
|
711 |
/* should never occur. The function block type being called MUST be in the symtable... */ |
|
712 |
if (fb_decl == function_block_type_symtable.end_value()) ERROR; |
|
713 |
||
714 |
/* loop through each function block parameter, find the value we should pass |
|
715 |
* to it, and then output the c equivalent... |
|
716 |
*/ |
|
717 |
function_param_iterator_c fp_iterator(fb_decl); |
|
718 |
identifier_c *param_name; |
|
719 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
720 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
721 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
160 | 722 |
|
162
60a124678842
Fixed typo in EN/ENO parameters handling for std library functions
etisserant
parents:
160
diff
changeset
|
723 |
/*fprintf(stderr, "param : %s\n", param_name->value);*/ |
160 | 724 |
|
70 | 725 |
/* 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
|
726 |
symbol_c *param_value = function_call_param_iterator.search_f(param_name); |
70 | 727 |
|
728 |
/* Get the value from a foo(<param_value>) style call */ |
|
729 |
if (param_value == NULL) |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
730 |
param_value = function_call_param_iterator.next_nf(); |
70 | 731 |
|
98 | 732 |
symbol_c *param_type = fp_iterator.param_type(); |
733 |
if (param_type == NULL) ERROR; |
|
734 |
||
70 | 735 |
/* now output the value assignment */ |
736 |
if (param_value != NULL) |
|
737 |
if ((param_direction == function_param_iterator_c::direction_in) || |
|
738 |
(param_direction == function_param_iterator_c::direction_inout)) { |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
739 |
if (!this->is_variable_prefix_null()) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
740 |
s4o.print(SET_VAR); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
741 |
s4o.print("("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
742 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
743 |
print_variable_prefix(); |
70 | 744 |
symbol->fb_name->accept(*this); |
745 |
s4o.print("."); |
|
746 |
param_name->accept(*this); |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
747 |
if (this->is_variable_prefix_null()) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
748 |
s4o.print(" = "); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
749 |
else |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
750 |
s4o.print(","); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
751 |
print_check_function(param_type, param_value); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
752 |
if (!this->is_variable_prefix_null()) |
98 | 753 |
s4o.print(")"); |
70 | 754 |
s4o.print(";\n" + s4o.indent_spaces); |
755 |
} |
|
756 |
} /* for(...) */ |
|
757 |
||
758 |
/* now call the function... */ |
|
759 |
function_block_type_name->accept(*this); |
|
760 |
s4o.print(FB_FUNCTION_SUFFIX); |
|
761 |
s4o.print("(&"); |
|
762 |
print_variable_prefix(); |
|
763 |
symbol->fb_name->accept(*this); |
|
764 |
s4o.print(")"); |
|
765 |
||
766 |
/* loop through each function parameter, find the variable to which |
|
767 |
* we should atribute the value of all output or inoutput parameters. |
|
768 |
*/ |
|
769 |
fp_iterator.reset(); |
|
770 |
function_call_param_iterator.reset(); |
|
771 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
772 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
773 |
||
774 |
/* 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
|
775 |
symbol_c *param_value = function_call_param_iterator.search_f(param_name); |
70 | 776 |
|
777 |
/* Get the value from a foo(<param_value>) style call */ |
|
778 |
if (param_value == NULL) |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
779 |
param_value = function_call_param_iterator.next_nf(); |
70 | 780 |
|
781 |
/* now output the value assignment */ |
|
782 |
if (param_value != NULL) |
|
783 |
if ((param_direction == function_param_iterator_c::direction_out) || |
|
784 |
(param_direction == function_param_iterator_c::direction_inout)) { |
|
98 | 785 |
symbol_c *param_type = search_varfb_instance_type->get_type(param_value, false); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
786 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
787 |
if (!this->is_variable_prefix_null()) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
788 |
unsigned int vartype = search_varfb_instance_type->get_vartype(param_value); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
789 |
s4o.print(";\n"+ s4o.indent_spaces); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
790 |
if (vartype == search_var_instance_decl_c::external_vt) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
791 |
s4o.print(SET_EXTERNAL); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
792 |
else if (vartype == search_var_instance_decl_c::located_vt) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
793 |
s4o.print(SET_LOCATED); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
794 |
else |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
795 |
s4o.print(SET_VAR); |
98 | 796 |
s4o.print("("); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
797 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
798 |
wanted_variablegeneration = assignment_vg; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
799 |
param_value->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
800 |
wanted_variablegeneration = expression_vg; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
801 |
s4o.print(","); |
98 | 802 |
} |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
803 |
else { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
804 |
param_value->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
805 |
s4o.print(" = "); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
806 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
807 |
print_check_function(param_type, param_name, symbol->fb_name); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
808 |
if (!this->is_variable_prefix_null()) |
98 | 809 |
s4o.print(")"); |
70 | 810 |
} |
811 |
} /* for(...) */ |
|
812 |
||
813 |
return NULL; |
|
814 |
} |
|
815 |
||
816 |
||
817 |
||
818 |
||
819 |
/* helper symbol for fb_invocation */ |
|
820 |
/* param_assignment_list ',' param_assignment */ |
|
821 |
void *visit(param_assignment_list_c *symbol) { |
|
822 |
TRACE("param_assignment_list_c"); |
|
823 |
/* this should never be called... */ |
|
824 |
ERROR; |
|
825 |
return NULL; |
|
826 |
// return print_list(symbol, "", ", "); |
|
827 |
} |
|
828 |
||
829 |
||
830 |
void *visit(input_variable_param_assignment_c *symbol) { |
|
831 |
TRACE("input_variable_param_assignment_c"); |
|
832 |
/* this should never be called... */ |
|
833 |
ERROR; |
|
834 |
return NULL; |
|
835 |
/* |
|
836 |
symbol->variable_name->accept(*this); |
|
837 |
s4o.print(" = "); |
|
838 |
symbol->expression->accept(*this); |
|
839 |
return NULL; |
|
840 |
*/ |
|
841 |
} |
|
842 |
||
843 |
void *visit(output_variable_param_assignment_c *symbol) { |
|
844 |
TRACE("output_variable_param_assignment_c"); |
|
845 |
/* this should never be called... */ |
|
846 |
ERROR; |
|
847 |
return NULL; |
|
848 |
/* |
|
849 |
s4o.print(s4o.indent_spaces); |
|
850 |
if (symbol->not_param != NULL) |
|
851 |
symbol->not_param->accept(*this); |
|
852 |
symbol->variable_name->accept(*this); |
|
853 |
s4o.print(" => "); |
|
854 |
symbol->variable->accept(*this); |
|
855 |
return NULL; |
|
856 |
*/ |
|
857 |
} |
|
858 |
||
859 |
// TODO: the NOT symbol in function (block) calls... |
|
860 |
void *visit(not_paramassign_c *symbol) { |
|
861 |
TRACE("not_paramassign_c"); |
|
862 |
/* this should never be called... */ |
|
863 |
ERROR; |
|
864 |
return NULL; |
|
865 |
/* |
|
866 |
s4o.print("NOT "); |
|
867 |
return NULL; |
|
868 |
*/ |
|
869 |
} |
|
870 |
||
871 |
||
872 |
/********************************/ |
|
873 |
/* B 3.2.3 Selection Statements */ |
|
874 |
/********************************/ |
|
875 |
void *visit(if_statement_c *symbol) { |
|
876 |
s4o.print("if ("); |
|
877 |
symbol->expression->accept(*this); |
|
878 |
s4o.print(") {\n"); |
|
879 |
s4o.indent_right(); |
|
880 |
symbol->statement_list->accept(*this); |
|
881 |
s4o.indent_left(); |
|
882 |
symbol->elseif_statement_list->accept(*this); |
|
883 |
||
884 |
if (symbol->else_statement_list != NULL) { |
|
885 |
s4o.print(s4o.indent_spaces); s4o.print("} else {\n"); |
|
886 |
s4o.indent_right(); |
|
887 |
symbol->else_statement_list->accept(*this); |
|
888 |
s4o.indent_left(); |
|
889 |
} |
|
890 |
s4o.print(s4o.indent_spaces); s4o.print("}"); |
|
891 |
return NULL; |
|
892 |
} |
|
893 |
||
894 |
/* helper symbol for if_statement */ |
|
895 |
void *visit(elseif_statement_list_c *symbol) {return print_list(symbol);} |
|
896 |
||
897 |
/* helper symbol for elseif_statement_list */ |
|
898 |
void *visit(elseif_statement_c *symbol) { |
|
899 |
s4o.print(s4o.indent_spaces); s4o.print("} else if ("); |
|
900 |
symbol->expression->accept(*this); |
|
901 |
s4o.print(") {\n"); |
|
902 |
s4o.indent_right(); |
|
903 |
symbol->statement_list->accept(*this); |
|
904 |
s4o.indent_left(); |
|
905 |
return NULL; |
|
906 |
} |
|
907 |
||
908 |
void *visit(case_statement_c *symbol) { |
|
909 |
s4o.print("switch("); |
|
910 |
symbol->expression->accept(*this); |
|
911 |
s4o.print(") {\n"); |
|
912 |
s4o.indent_right(); |
|
913 |
symbol->case_element_list->accept(*this); |
|
914 |
if (symbol->statement_list != NULL) { |
|
915 |
s4o.print(s4o.indent_spaces + "default:\n"); |
|
916 |
s4o.indent_right(); |
|
917 |
symbol->statement_list->accept(*this); |
|
918 |
s4o.print(s4o.indent_spaces + "break;\n"); |
|
919 |
s4o.indent_left(); |
|
920 |
} |
|
921 |
s4o.indent_left(); |
|
922 |
s4o.print(s4o.indent_spaces + "}"); |
|
923 |
return NULL; |
|
924 |
} |
|
925 |
||
926 |
/* helper symbol for case_statement */ |
|
927 |
void *visit(case_element_list_c *symbol) {return print_list(symbol);} |
|
928 |
||
929 |
void *visit(case_element_c *symbol) { |
|
930 |
s4o.print(s4o.indent_spaces + "case "); |
|
931 |
symbol->case_list->accept(*this); |
|
932 |
s4o.print(" :\n"); |
|
933 |
s4o.indent_right(); |
|
934 |
symbol->statement_list->accept(*this); |
|
935 |
s4o.print(s4o.indent_spaces + "break;\n"); |
|
936 |
s4o.indent_left(); |
|
937 |
return NULL; |
|
938 |
} |
|
939 |
||
940 |
void *visit(case_list_c *symbol) {return print_list(symbol, "", ", ");} |
|
941 |
||
942 |
/********************************/ |
|
943 |
/* B 3.2.4 Iteration Statements */ |
|
944 |
/********************************/ |
|
945 |
void *visit(for_statement_c *symbol) { |
|
946 |
s4o.print("for("); |
|
947 |
symbol->control_variable->accept(*this); |
|
948 |
s4o.print(" = "); |
|
949 |
symbol->beg_expression->accept(*this); |
|
950 |
s4o.print("; "); |
|
951 |
symbol->control_variable->accept(*this); |
|
952 |
s4o.print(" != "); |
|
953 |
symbol->end_expression->accept(*this); |
|
954 |
s4o.print("; "); |
|
955 |
symbol->control_variable->accept(*this); |
|
956 |
if (symbol->by_expression != NULL) { |
|
957 |
s4o.print(" += "); |
|
958 |
symbol->by_expression->accept(*this); |
|
959 |
} else { |
|
960 |
s4o.print("++"); |
|
961 |
} |
|
962 |
s4o.print(") {\n"); |
|
963 |
s4o.indent_right(); |
|
964 |
symbol->statement_list->accept(*this); |
|
965 |
s4o.indent_left(); |
|
966 |
s4o.print(s4o.indent_spaces); s4o.print("}"); |
|
967 |
return NULL; |
|
968 |
} |
|
969 |
void *visit(while_statement_c *symbol) { |
|
970 |
s4o.print("while ("); |
|
971 |
symbol->expression->accept(*this); |
|
972 |
s4o.print(") {\n"); |
|
973 |
s4o.indent_right(); |
|
974 |
symbol->statement_list->accept(*this); |
|
975 |
s4o.indent_left(); |
|
976 |
s4o.print(s4o.indent_spaces); s4o.print("}"); |
|
977 |
return NULL; |
|
978 |
} |
|
979 |
void *visit(repeat_statement_c *symbol) { |
|
980 |
s4o.print("do {\n"); |
|
981 |
s4o.indent_right(); |
|
982 |
symbol->statement_list->accept(*this); |
|
983 |
s4o.indent_left(); |
|
984 |
s4o.print(s4o.indent_spaces); s4o.print("} while("); |
|
985 |
symbol->expression->accept(*this); |
|
986 |
s4o.print(")"); |
|
987 |
return NULL; |
|
988 |
} |
|
989 |
void *visit(exit_statement_c *symbol) { |
|
990 |
s4o.print("exit(0)"); |
|
991 |
return NULL; |
|
992 |
} |
|
993 |
||
994 |
||
995 |
||
996 |
}; /* generate_c_st_c */ |
|
997 |
||
998 |
||
999 |
||
1000 |
||
1001 |
||
1002 |
||
1003 |
||
1004 |
||
1005 |