author | laurent |
Fri, 02 Sep 2011 18:15:55 +0200 | |
changeset 347 | 44ff2a6fcadc |
parent 344 | 8f71c46a0a55 |
child 349 | b826f13c260e |
permissions | -rwxr-xr-x |
70 | 1 |
/* |
279
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
258
diff
changeset
|
2 |
* matiec - a compiler for the programming languages defined in IEC 61131-3 |
70 | 3 |
* |
279
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
258
diff
changeset
|
4 |
* Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) |
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
258
diff
changeset
|
5 |
* Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant |
70 | 6 |
* |
279
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
258
diff
changeset
|
7 |
* This program is free software: you can redistribute it and/or modify |
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
258
diff
changeset
|
8 |
* it under the terms of the GNU General Public License as published by |
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
258
diff
changeset
|
9 |
* the Free Software Foundation, either version 3 of the License, or |
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
258
diff
changeset
|
10 |
* (at your option) any later version. |
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
258
diff
changeset
|
11 |
* |
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
258
diff
changeset
|
12 |
* This program is distributed in the hope that it will be useful, |
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
258
diff
changeset
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
258
diff
changeset
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
258
diff
changeset
|
15 |
* GNU General Public License for more details. |
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
258
diff
changeset
|
16 |
* |
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
258
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License |
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
258
diff
changeset
|
18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
70 | 19 |
* |
20 |
* This code is made available on the understanding that it will not be |
|
21 |
* used in safety-critical situations without a full and competent review. |
|
22 |
* |
|
23 |
* Based on the |
|
24 |
* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
25 |
* |
|
26 |
*/ |
|
27 |
||
28 |
||
29 |
/* |
|
30 |
* Conversion of st statements (i.e. ST code). |
|
31 |
* |
|
32 |
* This is part of the 4th stage that generates |
|
279
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
258
diff
changeset
|
33 |
* a C source program equivalent to the IL and ST, or SFC |
70 | 34 |
* code. |
35 |
*/ |
|
36 |
||
37 |
||
38 |
/***********************************************************************/ |
|
39 |
/***********************************************************************/ |
|
40 |
/***********************************************************************/ |
|
41 |
/***********************************************************************/ |
|
42 |
||
43 |
||
44 |
class generate_c_st_c: public generate_c_typedecl_c { |
|
45 |
||
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
46 |
public: |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
47 |
typedef enum { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
48 |
expression_vg, |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
49 |
assignment_vg, |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
50 |
complextype_base_vg, |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
51 |
complextype_suffix_vg, |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
52 |
fparam_output_vg |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
53 |
} variablegeneration_t; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
54 |
|
347
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
55 |
typedef enum { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
56 |
single_cg, |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
57 |
subrange_cg, |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
58 |
none_cg |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
59 |
} casegeneration_t; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
60 |
|
70 | 61 |
private: |
62 |
/* When calling a function block, we must first find it's type, |
|
63 |
* by searching through the declarations of the variables currently |
|
64 |
* in scope. |
|
65 |
* This class does just that... |
|
66 |
* A new class is instantiated whenever we begin generating the code |
|
67 |
* for a function block type declaration, or a program declaration. |
|
68 |
* This object instance will then later be called while the |
|
69 |
* function block's or the program's body is being handled. |
|
70 |
* |
|
71 |
* Note that functions cannot contain calls to function blocks, |
|
72 |
* so we do not create an object instance when handling |
|
73 |
* a function declaration. |
|
74 |
*/ |
|
75 |
search_fb_instance_decl_c *search_fb_instance_decl; |
|
76 |
||
77 |
/* When compiling st code, it becomes necessary to determine the |
|
78 |
* data type of st expressions. To do this, we must first find the |
|
79 |
* st operand's declaration, within the scope of the function block |
|
80 |
* or function currently being processed. |
|
81 |
* The following object does just that... |
|
82 |
* This object instance will then later be called while the |
|
83 |
* remaining st code is being handled. |
|
84 |
*/ |
|
85 |
search_expression_type_c *search_expression_type; |
|
86 |
||
87 |
search_varfb_instance_type_c *search_varfb_instance_type; |
|
88 |
||
98 | 89 |
search_base_type_c search_base_type; |
90 |
||
91 |
symbol_c* current_array_type; |
|
235 | 92 |
symbol_c* current_param_type; |
98 | 93 |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
94 |
int fcall_number; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
95 |
symbol_c *fbname; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
96 |
|
347
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
97 |
bool first_subrange_case_list; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
98 |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
99 |
variablegeneration_t wanted_variablegeneration; |
347
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
100 |
casegeneration_t wanted_casegeneration; |
145 | 101 |
|
70 | 102 |
public: |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
103 |
generate_c_st_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL) |
70 | 104 |
: generate_c_typedecl_c(s4o_ptr) { |
105 |
search_fb_instance_decl = new search_fb_instance_decl_c(scope); |
|
106 |
search_expression_type = new search_expression_type_c(scope); |
|
107 |
search_varfb_instance_type = new search_varfb_instance_type_c(scope); |
|
108 |
this->set_variable_prefix(variable_prefix); |
|
98 | 109 |
current_array_type = NULL; |
235 | 110 |
current_param_type = NULL; |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
111 |
fcall_number = 0; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
112 |
fbname = name; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
113 |
wanted_variablegeneration = expression_vg; |
347
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
114 |
wanted_casegeneration = none_cg; |
70 | 115 |
} |
116 |
||
117 |
virtual ~generate_c_st_c(void) { |
|
118 |
delete search_fb_instance_decl; |
|
119 |
delete search_expression_type; |
|
120 |
delete search_varfb_instance_type; |
|
121 |
} |
|
122 |
||
123 |
||
124 |
public: |
|
125 |
void generate(statement_list_c *stl) { |
|
126 |
stl->accept(*this); |
|
127 |
} |
|
128 |
||
129 |
private: |
|
130 |
||
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
131 |
void *print_getter(symbol_c *symbol) { |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
132 |
unsigned int vartype = search_varfb_instance_type->get_vartype(symbol); |
235 | 133 |
if (wanted_variablegeneration == fparam_output_vg) { |
134 |
if (vartype == search_var_instance_decl_c::external_vt) |
|
135 |
s4o.print(GET_EXTERNAL_BY_REF); |
|
136 |
else if (vartype == search_var_instance_decl_c::located_vt) |
|
137 |
s4o.print(GET_LOCATED_BY_REF); |
|
138 |
else |
|
139 |
s4o.print(GET_VAR_BY_REF); |
|
140 |
} |
|
141 |
else { |
|
142 |
if (vartype == search_var_instance_decl_c::external_vt) |
|
143 |
s4o.print(GET_EXTERNAL); |
|
144 |
else if (vartype == search_var_instance_decl_c::located_vt) |
|
145 |
s4o.print(GET_LOCATED); |
|
146 |
else |
|
147 |
s4o.print(GET_VAR); |
|
148 |
} |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
149 |
s4o.print("("); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
150 |
|
235 | 151 |
variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration; |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
152 |
wanted_variablegeneration = complextype_base_vg; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
153 |
symbol->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
154 |
if (search_varfb_instance_type->type_is_complex()) |
235 | 155 |
s4o.print(","); |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
156 |
wanted_variablegeneration = complextype_suffix_vg; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
157 |
symbol->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
158 |
s4o.print(")"); |
235 | 159 |
wanted_variablegeneration = old_wanted_variablegeneration; |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
160 |
return NULL; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
161 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
162 |
|
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
163 |
void *print_setter(symbol_c* symbol, |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
164 |
symbol_c* type, |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
165 |
symbol_c* value, |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
166 |
symbol_c* fb_symbol = NULL, |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
167 |
symbol_c* fb_value = NULL) { |
302
d120740a6b58
Fixing bug related to setting array variables with values coming from other variables
Mario de Sousa <msousa@fe.up.pt>
parents:
298
diff
changeset
|
168 |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
169 |
unsigned int vartype = search_varfb_instance_type->get_vartype(symbol); |
231
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
170 |
if (vartype == search_var_instance_decl_c::external_vt) { |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
171 |
symbolic_variable_c *variable = dynamic_cast<symbolic_variable_c *>(symbol); |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
172 |
/* TODO Find a solution for forcing global complex variables */ |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
173 |
if (variable != NULL) { |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
174 |
s4o.print(SET_EXTERNAL); |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
175 |
s4o.print("("); |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
176 |
variable->var_name->accept(*this); |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
177 |
s4o.print(","); |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
178 |
} |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
179 |
else { |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
180 |
s4o.print(SET_COMPLEX_EXTERNAL); |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
181 |
s4o.print("("); |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
182 |
} |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
183 |
} |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
184 |
else { |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
185 |
if (vartype == search_var_instance_decl_c::located_vt) |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
186 |
s4o.print(SET_LOCATED); |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
187 |
else |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
188 |
s4o.print(SET_VAR); |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
189 |
s4o.print("("); |
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
228
diff
changeset
|
190 |
} |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
191 |
|
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
192 |
if (fb_symbol != NULL) { |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
193 |
print_variable_prefix(); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
194 |
fb_symbol->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
195 |
s4o.print("."); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
196 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
197 |
else |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
198 |
wanted_variablegeneration = complextype_base_vg; |
302
d120740a6b58
Fixing bug related to setting array variables with values coming from other variables
Mario de Sousa <msousa@fe.up.pt>
parents:
298
diff
changeset
|
199 |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
200 |
symbol->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
201 |
s4o.print(","); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
202 |
wanted_variablegeneration = expression_vg; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
203 |
print_check_function(type, value, fb_value); |
302
d120740a6b58
Fixing bug related to setting array variables with values coming from other variables
Mario de Sousa <msousa@fe.up.pt>
parents:
298
diff
changeset
|
204 |
/* We need to call search_varfb_instance_type->get_vartype() again, as it may have been called |
d120740a6b58
Fixing bug related to setting array variables with values coming from other variables
Mario de Sousa <msousa@fe.up.pt>
parents:
298
diff
changeset
|
205 |
* again since we called it in the beginning of this print_setter() function. |
d120740a6b58
Fixing bug related to setting array variables with values coming from other variables
Mario de Sousa <msousa@fe.up.pt>
parents:
298
diff
changeset
|
206 |
* This make sure the call to search_varfb_instance_type->type_is_complex() will return |
d120740a6b58
Fixing bug related to setting array variables with values coming from other variables
Mario de Sousa <msousa@fe.up.pt>
parents:
298
diff
changeset
|
207 |
* the correct value regarding our 'symbol'. |
d120740a6b58
Fixing bug related to setting array variables with values coming from other variables
Mario de Sousa <msousa@fe.up.pt>
parents:
298
diff
changeset
|
208 |
*/ |
d120740a6b58
Fixing bug related to setting array variables with values coming from other variables
Mario de Sousa <msousa@fe.up.pt>
parents:
298
diff
changeset
|
209 |
search_varfb_instance_type->get_vartype(symbol); |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
210 |
if (search_varfb_instance_type->type_is_complex()) { |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
211 |
s4o.print(","); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
212 |
wanted_variablegeneration = complextype_suffix_vg; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
213 |
symbol->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
214 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
215 |
s4o.print(")"); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
216 |
wanted_variablegeneration = expression_vg; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
217 |
return NULL; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
218 |
} |
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
219 |
|
347
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
220 |
/********************************/ |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
221 |
/* B 1.3.3 - Derived data types */ |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
222 |
/********************************/ |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
223 |
/* signed_integer DOTDOT signed_integer */ |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
224 |
void *visit(subrange_c *symbol) { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
225 |
switch (wanted_casegeneration) { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
226 |
case subrange_cg: |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
227 |
s4o.print("case_expression >= "); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
228 |
symbol->lower_limit->accept(*this); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
229 |
s4o.print(" && case_expression <= "); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
230 |
symbol->upper_limit->accept(*this); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
231 |
break; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
232 |
default: |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
233 |
break; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
234 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
235 |
return NULL; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
236 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
237 |
|
70 | 238 |
/*********************/ |
239 |
/* B 1.4 - Variables */ |
|
240 |
/*********************/ |
|
241 |
void *visit(symbolic_variable_c *symbol) { |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
242 |
unsigned int vartype; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
243 |
if (wanted_variablegeneration == complextype_base_vg) |
298
022a874149ce
Fixing code generation of array indexes in Function Blocks.
Mario de Sousa <msousa@fe.up.pt>
parents:
283
diff
changeset
|
244 |
generate_c_base_c::visit(symbol); |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
245 |
else if (wanted_variablegeneration == complextype_suffix_vg) |
298
022a874149ce
Fixing code generation of array indexes in Function Blocks.
Mario de Sousa <msousa@fe.up.pt>
parents:
283
diff
changeset
|
246 |
return NULL; |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
247 |
else if (this->is_variable_prefix_null()) { |
298
022a874149ce
Fixing code generation of array indexes in Function Blocks.
Mario de Sousa <msousa@fe.up.pt>
parents:
283
diff
changeset
|
248 |
vartype = search_varfb_instance_type->get_vartype(symbol); |
022a874149ce
Fixing code generation of array indexes in Function Blocks.
Mario de Sousa <msousa@fe.up.pt>
parents:
283
diff
changeset
|
249 |
if (wanted_variablegeneration == fparam_output_vg) { |
022a874149ce
Fixing code generation of array indexes in Function Blocks.
Mario de Sousa <msousa@fe.up.pt>
parents:
283
diff
changeset
|
250 |
s4o.print("&("); |
022a874149ce
Fixing code generation of array indexes in Function Blocks.
Mario de Sousa <msousa@fe.up.pt>
parents:
283
diff
changeset
|
251 |
generate_c_base_c::visit(symbol); |
022a874149ce
Fixing code generation of array indexes in Function Blocks.
Mario de Sousa <msousa@fe.up.pt>
parents:
283
diff
changeset
|
252 |
s4o.print(")"); |
022a874149ce
Fixing code generation of array indexes in Function Blocks.
Mario de Sousa <msousa@fe.up.pt>
parents:
283
diff
changeset
|
253 |
} |
022a874149ce
Fixing code generation of array indexes in Function Blocks.
Mario de Sousa <msousa@fe.up.pt>
parents:
283
diff
changeset
|
254 |
else { |
022a874149ce
Fixing code generation of array indexes in Function Blocks.
Mario de Sousa <msousa@fe.up.pt>
parents:
283
diff
changeset
|
255 |
generate_c_base_c::visit(symbol); |
022a874149ce
Fixing code generation of array indexes in Function Blocks.
Mario de Sousa <msousa@fe.up.pt>
parents:
283
diff
changeset
|
256 |
} |
145 | 257 |
} |
235 | 258 |
else |
298
022a874149ce
Fixing code generation of array indexes in Function Blocks.
Mario de Sousa <msousa@fe.up.pt>
parents:
283
diff
changeset
|
259 |
print_getter(symbol); |
70 | 260 |
return NULL; |
261 |
} |
|
262 |
||
263 |
/********************************************/ |
|
264 |
/* B.1.4.1 Directly Represented Variables */ |
|
265 |
/********************************************/ |
|
266 |
// direct_variable: direct_variable_token {$$ = new direct_variable_c($1);}; |
|
267 |
void *visit(direct_variable_c *symbol) { |
|
268 |
TRACE("direct_variable_c"); |
|
269 |
/* Do not use print_token() as it will change everything into uppercase */ |
|
270 |
if (strlen(symbol->value) == 0) ERROR; |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
271 |
if (this->is_variable_prefix_null()) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
272 |
if (wanted_variablegeneration != fparam_output_vg) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
273 |
s4o.print("*("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
274 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
275 |
else { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
276 |
switch (wanted_variablegeneration) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
277 |
case expression_vg: |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
278 |
s4o.print(GET_LOCATED); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
279 |
s4o.print("("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
280 |
break; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
281 |
case fparam_output_vg: |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
282 |
s4o.print(GET_LOCATED_BY_REF); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
283 |
s4o.print("("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
284 |
break; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
285 |
default: |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
286 |
break; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
287 |
} |
145 | 288 |
} |
70 | 289 |
this->print_variable_prefix(); |
290 |
s4o.printlocation(symbol->value + 1); |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
291 |
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
|
292 |
wanted_variablegeneration != assignment_vg) |
145 | 293 |
s4o.print(")"); |
70 | 294 |
return NULL; |
295 |
} |
|
296 |
||
98 | 297 |
/*************************************/ |
298 |
/* B.1.4.2 Multi-element Variables */ |
|
299 |
/*************************************/ |
|
300 |
||
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
220
diff
changeset
|
301 |
// SYM_REF2(structured_variable_c, record_variable, field_selector) |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
220
diff
changeset
|
302 |
void *visit(structured_variable_c *symbol) { |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
220
diff
changeset
|
303 |
TRACE("structured_variable_c"); |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
304 |
switch (wanted_variablegeneration) { |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
305 |
case complextype_base_vg: |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
306 |
symbol->record_variable->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
307 |
break; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
308 |
case complextype_suffix_vg: |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
309 |
symbol->record_variable->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
310 |
s4o.print("."); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
311 |
symbol->field_selector->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
312 |
break; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
313 |
default: |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
314 |
if (this->is_variable_prefix_null()) { |
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
220
diff
changeset
|
315 |
symbol->record_variable->accept(*this); |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
220
diff
changeset
|
316 |
s4o.print("."); |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
220
diff
changeset
|
317 |
symbol->field_selector->accept(*this); |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
318 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
319 |
else |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
320 |
print_getter(symbol); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
321 |
break; |
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
220
diff
changeset
|
322 |
} |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
220
diff
changeset
|
323 |
return NULL; |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
220
diff
changeset
|
324 |
} |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
220
diff
changeset
|
325 |
|
98 | 326 |
/* subscripted_variable '[' subscript_list ']' */ |
327 |
//SYM_REF2(array_variable_c, subscripted_variable, subscript_list) |
|
328 |
void *visit(array_variable_c *symbol) { |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
329 |
switch (wanted_variablegeneration) { |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
330 |
case complextype_base_vg: |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
331 |
symbol->subscripted_variable->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
332 |
break; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
333 |
case complextype_suffix_vg: |
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
237
diff
changeset
|
334 |
symbol->subscripted_variable->accept(*this); |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
237
diff
changeset
|
335 |
|
321
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
309
diff
changeset
|
336 |
current_array_type = search_varfb_instance_type->get_type_id(symbol->subscripted_variable); |
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
237
diff
changeset
|
337 |
if (current_array_type == NULL) ERROR; |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
237
diff
changeset
|
338 |
|
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
237
diff
changeset
|
339 |
s4o.print(".table"); |
298
022a874149ce
Fixing code generation of array indexes in Function Blocks.
Mario de Sousa <msousa@fe.up.pt>
parents:
283
diff
changeset
|
340 |
wanted_variablegeneration = expression_vg; |
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
237
diff
changeset
|
341 |
symbol->subscript_list->accept(*this); |
298
022a874149ce
Fixing code generation of array indexes in Function Blocks.
Mario de Sousa <msousa@fe.up.pt>
parents:
283
diff
changeset
|
342 |
wanted_variablegeneration = complextype_suffix_vg; |
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
237
diff
changeset
|
343 |
|
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
237
diff
changeset
|
344 |
current_array_type = NULL; |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
345 |
break; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
346 |
default: |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
347 |
if (this->is_variable_prefix_null()) { |
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
237
diff
changeset
|
348 |
symbol->subscripted_variable->accept(*this); |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
237
diff
changeset
|
349 |
|
321
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
309
diff
changeset
|
350 |
current_array_type = search_varfb_instance_type->get_type_id(symbol->subscripted_variable); |
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
237
diff
changeset
|
351 |
if (current_array_type == NULL) ERROR; |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
237
diff
changeset
|
352 |
|
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
237
diff
changeset
|
353 |
s4o.print(".table"); |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
237
diff
changeset
|
354 |
symbol->subscript_list->accept(*this); |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
237
diff
changeset
|
355 |
|
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
237
diff
changeset
|
356 |
current_array_type = NULL; |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
357 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
358 |
else |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
359 |
print_getter(symbol); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
360 |
break; |
98 | 361 |
} |
362 |
return NULL; |
|
363 |
} |
|
364 |
||
365 |
/* subscript_list ',' subscript */ |
|
366 |
void *visit(subscript_list_c *symbol) { |
|
367 |
for (int i = 0; i < symbol->n; i++) { |
|
368 |
s4o.print("[__"); |
|
369 |
current_array_type->accept(*this); |
|
309
f93bcada0f51
Fix bug when declaring data type directly derived from array type
laurent
parents:
302
diff
changeset
|
370 |
s4o.print("_TRANSIDX("); |
98 | 371 |
print_integer(i); |
309
f93bcada0f51
Fix bug when declaring data type directly derived from array type
laurent
parents:
302
diff
changeset
|
372 |
s4o.print(","); |
98 | 373 |
symbol->elements[i]->accept(*this); |
374 |
s4o.print(")]"); |
|
375 |
} |
|
376 |
return NULL; |
|
377 |
} |
|
378 |
||
235 | 379 |
/******************************************/ |
380 |
/* B 1.4.3 - Declaration & Initialisation */ |
|
381 |
/******************************************/ |
|
237 | 382 |
|
383 |
/* helper symbol for structure_initialization */ |
|
384 |
/* structure_element_initialization_list ',' structure_element_initialization */ |
|
235 | 385 |
void *visit(structure_element_initialization_list_c *symbol) { |
386 |
generate_c_structure_initialization_c *structure_initialization = new generate_c_structure_initialization_c(&s4o); |
|
387 |
structure_initialization->init_structure_default(this->current_param_type); |
|
237 | 388 |
structure_initialization->init_structure_values(symbol); |
235 | 389 |
delete structure_initialization; |
390 |
return NULL; |
|
391 |
} |
|
98 | 392 |
|
237 | 393 |
/* helper symbol for array_initialization */ |
394 |
/* array_initial_elements_list ',' array_initial_elements */ |
|
395 |
void *visit(array_initial_elements_list_c *symbol) { |
|
396 |
generate_c_array_initialization_c *array_initialization = new generate_c_array_initialization_c(&s4o); |
|
397 |
array_initialization->init_array_size(this->current_param_type); |
|
398 |
array_initialization->init_array_values(symbol); |
|
399 |
delete array_initialization; |
|
400 |
return NULL; |
|
401 |
} |
|
402 |
||
70 | 403 |
/***************************************/ |
404 |
/* B.3 - Language ST (Structured Text) */ |
|
405 |
/***************************************/ |
|
406 |
/***********************/ |
|
407 |
/* B 3.1 - Expressions */ |
|
408 |
/***********************/ |
|
409 |
void *visit(or_expression_c *symbol) { |
|
410 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
411 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
412 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
413 |
ERROR; |
|
414 |
if (search_expression_type->is_bool_type(left_type)) |
|
415 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " || "); |
|
416 |
if (search_expression_type->is_binary_type(left_type)) |
|
417 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " | "); |
|
418 |
ERROR; |
|
419 |
return NULL; |
|
420 |
} |
|
421 |
||
422 |
void *visit(xor_expression_c *symbol) { |
|
423 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
424 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
425 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
426 |
ERROR; |
|
427 |
if (search_expression_type->is_bool_type(left_type)) { |
|
428 |
s4o.print("("); |
|
429 |
symbol->l_exp->accept(*this); |
|
430 |
s4o.print(" && !"); |
|
431 |
symbol->r_exp->accept(*this); |
|
432 |
s4o.print(") || (!"); |
|
433 |
symbol->l_exp->accept(*this); |
|
434 |
s4o.print(" && "); |
|
435 |
symbol->r_exp->accept(*this); |
|
436 |
s4o.print(")"); |
|
130 | 437 |
return NULL; |
70 | 438 |
} |
439 |
if (search_expression_type->is_binary_type(left_type)) |
|
440 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " ^ "); |
|
441 |
ERROR; |
|
442 |
return NULL; |
|
443 |
} |
|
444 |
||
445 |
void *visit(and_expression_c *symbol) { |
|
446 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
447 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
448 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
449 |
ERROR; |
|
450 |
if (search_expression_type->is_bool_type(left_type)) |
|
451 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " && "); |
|
452 |
if (search_expression_type->is_binary_type(left_type)) |
|
453 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " & "); |
|
454 |
ERROR; |
|
455 |
return NULL; |
|
456 |
} |
|
457 |
||
458 |
void *visit(equ_expression_c *symbol) { |
|
459 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
460 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
461 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
462 |
ERROR; |
|
123 | 463 |
if (search_expression_type->is_time_type(left_type) || |
464 |
search_expression_type->is_string_type(left_type)) |
|
70 | 465 |
return print_compare_function("__eq_", left_type, symbol->l_exp, symbol->r_exp); |
466 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " == "); |
|
467 |
} |
|
468 |
||
469 |
void *visit(notequ_expression_c *symbol) { |
|
470 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
471 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
472 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
473 |
ERROR; |
|
123 | 474 |
if (search_expression_type->is_time_type(left_type) || |
475 |
search_expression_type->is_string_type(left_type)) |
|
70 | 476 |
return print_compare_function("__ne_", left_type, symbol->l_exp, symbol->r_exp); |
477 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " != "); |
|
478 |
} |
|
479 |
||
480 |
void *visit(lt_expression_c *symbol) { |
|
481 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
482 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
483 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
484 |
ERROR; |
|
123 | 485 |
if (search_expression_type->is_time_type(left_type) || |
486 |
search_expression_type->is_string_type(left_type)) |
|
70 | 487 |
return print_compare_function("__lt_", left_type, symbol->l_exp, symbol->r_exp); |
123 | 488 |
if (!search_base_type.type_is_enumerated(left_type)) |
489 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " < "); |
|
490 |
ERROR; |
|
491 |
return NULL; |
|
70 | 492 |
} |
493 |
||
494 |
void *visit(gt_expression_c *symbol) { |
|
495 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
496 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
497 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
498 |
ERROR; |
|
123 | 499 |
if (search_expression_type->is_time_type(left_type) || |
500 |
search_expression_type->is_string_type(left_type)) |
|
70 | 501 |
return print_compare_function("__gt_", left_type, symbol->l_exp, symbol->r_exp); |
123 | 502 |
if (!search_base_type.type_is_enumerated(left_type)) |
503 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " > "); |
|
504 |
ERROR; |
|
505 |
return NULL; |
|
70 | 506 |
} |
507 |
||
508 |
void *visit(le_expression_c *symbol) { |
|
509 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
510 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
511 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
512 |
ERROR; |
|
123 | 513 |
if (search_expression_type->is_time_type(left_type) || |
514 |
search_expression_type->is_string_type(left_type)) |
|
70 | 515 |
return print_compare_function("__le_", left_type, symbol->l_exp, symbol->r_exp); |
123 | 516 |
if (!search_base_type.type_is_enumerated(left_type)) |
517 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= "); |
|
518 |
ERROR; |
|
519 |
return NULL; |
|
70 | 520 |
} |
521 |
||
522 |
void *visit(ge_expression_c *symbol) { |
|
523 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
524 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
525 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
526 |
ERROR; |
|
123 | 527 |
if (search_expression_type->is_time_type(left_type) || |
528 |
search_expression_type->is_string_type(left_type)) |
|
70 | 529 |
return print_compare_function("__ge_", left_type, symbol->l_exp, symbol->r_exp); |
123 | 530 |
if (!search_base_type.type_is_enumerated(left_type)) |
531 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= "); |
|
532 |
ERROR; |
|
533 |
return NULL; |
|
70 | 534 |
} |
535 |
||
536 |
void *visit(add_expression_c *symbol) { |
|
537 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
538 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
539 |
if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
540 |
(typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
541 |
(typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c))) |
|
149 | 542 |
return print_binary_function("__TIME_ADD", symbol->l_exp, symbol->r_exp); |
70 | 543 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
544 |
ERROR; |
|
545 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
546 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " + "); |
|
547 |
ERROR; |
|
548 |
return NULL; |
|
549 |
} |
|
550 |
||
551 |
void *visit(sub_expression_c *symbol) { |
|
552 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
553 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
554 |
if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
555 |
(typeid(*left_type) == typeid(date_type_name_c) && typeid(*right_type) == typeid(date_type_name_c)) || |
|
556 |
(typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
557 |
(typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(tod_type_name_c)) || |
|
558 |
(typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
559 |
(typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(dt_type_name_c))) |
|
149 | 560 |
return print_binary_function("__TIME_SUB", symbol->l_exp, symbol->r_exp); |
70 | 561 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
562 |
ERROR; |
|
563 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
564 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " - "); |
|
565 |
ERROR; |
|
566 |
return NULL; |
|
567 |
} |
|
568 |
||
569 |
void *visit(mul_expression_c *symbol) { |
|
570 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
571 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
572 |
if ((typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_integer_type(right_type)) || |
|
573 |
(typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_real_type(right_type))) |
|
149 | 574 |
return print_binary_function("__TIME_MUL", symbol->l_exp, symbol->r_exp); |
70 | 575 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
576 |
ERROR; |
|
577 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
578 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " * "); |
|
579 |
ERROR; |
|
580 |
return NULL; |
|
581 |
} |
|
582 |
||
583 |
void *visit(div_expression_c *symbol) { |
|
584 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
585 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
149 | 586 |
if ((typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_integer_type(right_type)) || |
587 |
(typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_real_type(right_type))) |
|
588 |
return print_binary_function("__TIME_DIV", symbol->l_exp, symbol->r_exp); |
|
70 | 589 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
590 |
ERROR; |
|
591 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
592 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " / "); |
|
593 |
ERROR; |
|
594 |
return NULL; |
|
595 |
} |
|
596 |
||
597 |
void *visit(mod_expression_c *symbol) { |
|
598 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
599 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
600 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
601 |
ERROR; |
|
602 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) { |
|
603 |
s4o.print("(("); |
|
604 |
symbol->r_exp->accept(*this); |
|
605 |
s4o.print(" == 0)?0:"); |
|
606 |
print_binary_expression(symbol->l_exp, symbol->r_exp, " % "); |
|
607 |
s4o.print(")"); |
|
608 |
return NULL; |
|
609 |
} |
|
610 |
ERROR; |
|
611 |
return NULL; |
|
612 |
} |
|
613 |
||
614 |
/* TODO: power expression... */ |
|
257 | 615 |
void *visit(power_expression_c *symbol) { |
616 |
ERROR; |
|
617 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " ** "); |
|
618 |
} |
|
619 |
||
123 | 620 |
void *visit(neg_expression_c *symbol) { |
257 | 621 |
return print_unary_expression(symbol->exp, " -"); |
123 | 622 |
} |
70 | 623 |
|
624 |
void *visit(not_expression_c *symbol) { |
|
625 |
symbol_c *exp_type = search_expression_type->get_type(symbol->exp); |
|
123 | 626 |
if (search_expression_type->is_binary_type(exp_type)) |
627 |
return print_unary_expression(symbol->exp, search_expression_type->is_bool_type(exp_type)?"!":"~"); |
|
628 |
ERROR; |
|
629 |
return NULL; |
|
70 | 630 |
} |
631 |
||
632 |
void *visit(function_invocation_c *symbol) { |
|
149 | 633 |
symbol_c* function_type_prefix = NULL; |
634 |
symbol_c* function_name = NULL; |
|
635 |
symbol_c* function_type_suffix = NULL; |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
636 |
DECLARE_PARAM_LIST() |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
637 |
|
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
638 |
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
|
639 |
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
|
640 |
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
|
641 |
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
|
642 |
|
233
3d23a68183d3
Bug on standard functions called with literals fixed
laurent
parents:
231
diff
changeset
|
643 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
3d23a68183d3
Bug on standard functions called with literals fixed
laurent
parents:
231
diff
changeset
|
644 |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
645 |
function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
70 | 646 |
if (f_decl == function_symtable.end_value()) { |
647 |
/* The function called is not in the symtable, so we test if it is a |
|
648 |
* 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
|
649 |
|
70 | 650 |
function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
651 |
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
|
652 |
|
70 | 653 |
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
|
654 |
|
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
655 |
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
|
656 |
|
235 | 657 |
symbol_c *en_param_name = (symbol_c *)(new identifier_c("EN")); |
149 | 658 |
/* Get the value from EN param */ |
235 | 659 |
symbol_c *EN_param_value = function_call_param_iterator.search_f(en_param_name); |
149 | 660 |
if (EN_param_value == NULL) |
661 |
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
|
662 |
else |
ae423926bc84
Fixed parameter count error on variadic functions with EN/ENO
etisserant
parents:
162
diff
changeset
|
663 |
nb_param --; |
235 | 664 |
ADD_PARAM_LIST(en_param_name, EN_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_in) |
665 |
||
666 |
symbol_c *eno_param_name = (symbol_c *)(new identifier_c("ENO")); |
|
149 | 667 |
/* Get the value from ENO param */ |
235 | 668 |
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
|
669 |
if (ENO_param_value != NULL) |
ae423926bc84
Fixed parameter count error on variadic functions with EN/ENO
etisserant
parents:
162
diff
changeset
|
670 |
nb_param --; |
235 | 671 |
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
|
672 |
|
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
673 |
#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
|
674 |
|
70 | 675 |
} |
676 |
else { |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
677 |
function_name = symbol->function_name; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
678 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
679 |
/* loop through each function parameter, find the value we should pass |
70 | 680 |
* to it, and then output the c equivalent... |
681 |
*/ |
|
682 |
function_param_iterator_c fp_iterator(f_decl); |
|
683 |
identifier_c *param_name; |
|
684 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
149 | 685 |
|
233
3d23a68183d3
Bug on standard functions called with literals fixed
laurent
parents:
231
diff
changeset
|
686 |
symbol_c *param_type = fp_iterator.param_type(); |
3d23a68183d3
Bug on standard functions called with literals fixed
laurent
parents:
231
diff
changeset
|
687 |
if (param_type == NULL) ERROR; |
3d23a68183d3
Bug on standard functions called with literals fixed
laurent
parents:
231
diff
changeset
|
688 |
|
70 | 689 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
149 | 690 |
|
70 | 691 |
/* 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
|
692 |
symbol_c *param_value = function_call_param_iterator.search_f(param_name); |
70 | 693 |
|
694 |
/* Get the value from a foo(<param_value>) style call */ |
|
233
3d23a68183d3
Bug on standard functions called with literals fixed
laurent
parents:
231
diff
changeset
|
695 |
if (param_value == NULL) { |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
696 |
param_value = function_call_param_iterator.next_nf(); |
233
3d23a68183d3
Bug on standard functions called with literals fixed
laurent
parents:
231
diff
changeset
|
697 |
if (param_value != NULL && fp_iterator.is_en_eno_param_implicit()) ERROR; |
3d23a68183d3
Bug on standard functions called with literals fixed
laurent
parents:
231
diff
changeset
|
698 |
} |
149 | 699 |
|
700 |
if (param_value == NULL && param_direction == function_param_iterator_c::direction_in) { |
|
701 |
/* No value given for parameter, so we must use the default... */ |
|
702 |
/* First check whether default value specified in function declaration...*/ |
|
703 |
param_value = fp_iterator.default_value(); |
|
704 |
} |
|
705 |
||
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
706 |
ADD_PARAM_LIST(param_name, param_value, param_type, param_direction) |
70 | 707 |
} /* for(...) */ |
708 |
// symbol->parameter_assignment->accept(*this); |
|
149 | 709 |
} |
710 |
||
233
3d23a68183d3
Bug on standard functions called with literals fixed
laurent
parents:
231
diff
changeset
|
711 |
if (function_call_param_iterator.next_nf() != NULL) ERROR; |
3d23a68183d3
Bug on standard functions called with literals fixed
laurent
parents:
231
diff
changeset
|
712 |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
713 |
bool has_output_params = false; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
714 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
715 |
if (!this->is_variable_prefix_null()) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
716 |
PARAM_LIST_ITERATOR() { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
717 |
if ((PARAM_DIRECTION == function_param_iterator_c::direction_out || |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
718 |
PARAM_DIRECTION == function_param_iterator_c::direction_inout) && |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
719 |
PARAM_VALUE != NULL) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
720 |
if (!has_output_params) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
721 |
has_output_params = true; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
722 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
723 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
724 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
725 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
726 |
|
149 | 727 |
if (function_type_prefix != NULL) { |
728 |
s4o.print("("); |
|
336 | 729 |
search_expression_type->default_literal_type(function_type_prefix)->accept(*this); |
70 | 730 |
s4o.print(")"); |
149 | 731 |
} |
336 | 732 |
if (function_type_suffix != NULL) { |
344 | 733 |
function_type_suffix = search_expression_type->default_literal_type(function_type_suffix); |
336 | 734 |
} |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
735 |
if (has_output_params) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
736 |
fcall_number++; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
737 |
s4o.print("__"); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
738 |
fbname->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
739 |
s4o.print("_"); |
149 | 740 |
function_name->accept(*this); |
235 | 741 |
if (function_type_suffix != NULL) |
742 |
function_type_suffix->accept(*this); |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
743 |
s4o.print_integer(fcall_number); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
744 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
745 |
else { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
746 |
function_name->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
747 |
if (function_type_suffix != NULL) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
748 |
function_type_suffix->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
749 |
} |
149 | 750 |
s4o.print("("); |
751 |
s4o.indent_right(); |
|
752 |
||
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
753 |
int nb_param = 0; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
754 |
PARAM_LIST_ITERATOR() { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
755 |
symbol_c *param_value = PARAM_VALUE; |
235 | 756 |
current_param_type = PARAM_TYPE; |
149 | 757 |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
758 |
switch (PARAM_DIRECTION) { |
149 | 759 |
case function_param_iterator_c::direction_in: |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
760 |
if (nb_param > 0) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
761 |
s4o.print(",\n"+s4o.indent_spaces); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
762 |
if (param_value == NULL) { |
149 | 763 |
/* If not, get the default value of this variable's type */ |
235 | 764 |
param_value = (symbol_c *)current_param_type->accept(*type_initial_value_c::instance()); |
149 | 765 |
} |
766 |
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
|
767 |
s4o.print("("); |
235 | 768 |
if (search_expression_type->is_literal_integer_type(current_param_type)) |
220 | 769 |
search_expression_type->lint_type_name.accept(*this); |
235 | 770 |
else if (search_expression_type->is_literal_real_type(current_param_type)) |
220 | 771 |
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
|
772 |
else |
235 | 773 |
current_param_type->accept(*this); |
216
136d6ae70745
Bug with using non explicit literals as standard library functions input parameters fixed
laurent
parents:
208
diff
changeset
|
774 |
s4o.print(")"); |
235 | 775 |
print_check_function(current_param_type, param_value); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
776 |
nb_param++; |
149 | 777 |
break; |
778 |
case function_param_iterator_c::direction_out: |
|
779 |
case function_param_iterator_c::direction_inout: |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
780 |
if (!has_output_params) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
781 |
if (nb_param > 0) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
782 |
s4o.print(",\n"+s4o.indent_spaces); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
783 |
if (param_value == NULL) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
784 |
s4o.print("NULL"); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
785 |
else { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
786 |
wanted_variablegeneration = fparam_output_vg; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
787 |
param_value->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
788 |
wanted_variablegeneration = expression_vg; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
789 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
790 |
nb_param++; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
791 |
} |
149 | 792 |
break; |
793 |
case function_param_iterator_c::direction_extref: |
|
794 |
/* TODO! */ |
|
795 |
ERROR; |
|
796 |
break; |
|
797 |
} /* switch */ |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
798 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
799 |
if (has_output_params) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
800 |
if (nb_param > 0) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
801 |
s4o.print(",\n"+s4o.indent_spaces); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
802 |
s4o.print(FB_FUNCTION_PARAM); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
803 |
} |
149 | 804 |
s4o.print(")"); |
805 |
s4o.indent_left(); |
|
70 | 806 |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
807 |
CLEAR_PARAM_LIST() |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
808 |
|
70 | 809 |
return NULL; |
810 |
} |
|
811 |
||
812 |
/********************/ |
|
813 |
/* B 3.2 Statements */ |
|
814 |
/********************/ |
|
815 |
void *visit(statement_list_c *symbol) { |
|
816 |
return print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); |
|
817 |
} |
|
818 |
||
819 |
/*********************************/ |
|
820 |
/* B 3.2.1 Assignment Statements */ |
|
821 |
/*********************************/ |
|
822 |
void *visit(assignment_statement_c *symbol) { |
|
321
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
309
diff
changeset
|
823 |
symbol_c *left_type = search_varfb_instance_type->get_type_id(symbol->l_exp); |
98 | 824 |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
825 |
if (this->is_variable_prefix_null()) { |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
826 |
symbol->l_exp->accept(*this); |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
827 |
s4o.print(" = "); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
828 |
print_check_function(left_type, symbol->r_exp); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
829 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
830 |
else { |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
831 |
print_setter(symbol->l_exp, left_type, symbol->r_exp); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
832 |
} |
70 | 833 |
return NULL; |
834 |
} |
|
835 |
||
836 |
/*****************************************/ |
|
837 |
/* B 3.2.2 Subprogram Control Statements */ |
|
838 |
/*****************************************/ |
|
839 |
||
283
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
282
diff
changeset
|
840 |
void *visit(return_statement_c *symbol) { |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
282
diff
changeset
|
841 |
s4o.print("goto "); s4o.print(END_LABEL); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
282
diff
changeset
|
842 |
return NULL; |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
282
diff
changeset
|
843 |
} |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
282
diff
changeset
|
844 |
|
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
282
diff
changeset
|
845 |
|
70 | 846 |
/* fb_name '(' [param_assignment_list] ')' */ |
847 |
/* param_assignment_list -> may be NULL ! */ |
|
848 |
//SYM_REF2(fb_invocation_c, fb_name, param_assignment_list) |
|
849 |
void *visit(fb_invocation_c *symbol) { |
|
850 |
TRACE("fb_invocation_c"); |
|
851 |
/* first figure out what is the name of the function block type of the function block being called... */ |
|
852 |
symbol_c *function_block_type_name = this->search_fb_instance_decl->get_type_name(symbol->fb_name); |
|
853 |
/* should never occur. The function block instance MUST have been declared... */ |
|
854 |
if (function_block_type_name == NULL) ERROR; |
|
855 |
||
856 |
/* Now find the declaration of the function block type being called... */ |
|
857 |
function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(function_block_type_name); |
|
858 |
/* should never occur. The function block type being called MUST be in the symtable... */ |
|
859 |
if (fb_decl == function_block_type_symtable.end_value()) ERROR; |
|
860 |
||
861 |
/* loop through each function block parameter, find the value we should pass |
|
862 |
* to it, and then output the c equivalent... |
|
863 |
*/ |
|
864 |
function_param_iterator_c fp_iterator(fb_decl); |
|
865 |
identifier_c *param_name; |
|
866 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
867 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
868 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
160 | 869 |
|
162
60a124678842
Fixed typo in EN/ENO parameters handling for std library functions
etisserant
parents:
160
diff
changeset
|
870 |
/*fprintf(stderr, "param : %s\n", param_name->value);*/ |
160 | 871 |
|
70 | 872 |
/* 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
|
873 |
symbol_c *param_value = function_call_param_iterator.search_f(param_name); |
70 | 874 |
|
875 |
/* Get the value from a foo(<param_value>) style call */ |
|
876 |
if (param_value == NULL) |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
877 |
param_value = function_call_param_iterator.next_nf(); |
70 | 878 |
|
98 | 879 |
symbol_c *param_type = fp_iterator.param_type(); |
880 |
if (param_type == NULL) ERROR; |
|
881 |
||
70 | 882 |
/* now output the value assignment */ |
883 |
if (param_value != NULL) |
|
884 |
if ((param_direction == function_param_iterator_c::direction_in) || |
|
885 |
(param_direction == function_param_iterator_c::direction_inout)) { |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
886 |
if (this->is_variable_prefix_null()) { |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
887 |
symbol->fb_name->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
888 |
s4o.print("."); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
889 |
param_name->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
890 |
s4o.print(" = "); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
891 |
print_check_function(param_type, param_value); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
892 |
} |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
893 |
else { |
228
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
226
diff
changeset
|
894 |
print_setter(param_name, param_type, param_value, symbol->fb_name); |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
895 |
} |
70 | 896 |
s4o.print(";\n" + s4o.indent_spaces); |
897 |
} |
|
898 |
} /* for(...) */ |
|
899 |
||
900 |
/* now call the function... */ |
|
901 |
function_block_type_name->accept(*this); |
|
902 |
s4o.print(FB_FUNCTION_SUFFIX); |
|
903 |
s4o.print("(&"); |
|
904 |
print_variable_prefix(); |
|
905 |
symbol->fb_name->accept(*this); |
|
906 |
s4o.print(")"); |
|
907 |
||
908 |
/* loop through each function parameter, find the variable to which |
|
909 |
* we should atribute the value of all output or inoutput parameters. |
|
910 |
*/ |
|
911 |
fp_iterator.reset(); |
|
912 |
function_call_param_iterator.reset(); |
|
913 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
914 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
915 |
||
916 |
/* 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
|
917 |
symbol_c *param_value = function_call_param_iterator.search_f(param_name); |
70 | 918 |
|
919 |
/* Get the value from a foo(<param_value>) style call */ |
|
920 |
if (param_value == NULL) |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
921 |
param_value = function_call_param_iterator.next_nf(); |
70 | 922 |
|
923 |
/* now output the value assignment */ |
|
924 |
if (param_value != NULL) |
|
925 |
if ((param_direction == function_param_iterator_c::direction_out) || |
|
926 |
(param_direction == function_param_iterator_c::direction_inout)) { |
|
321
a96399ab57c2
Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents:
309
diff
changeset
|
927 |
symbol_c *param_type = search_varfb_instance_type->get_type_id(param_value); |
240 | 928 |
s4o.print(";\n" + s4o.indent_spaces); |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
929 |
if (this->is_variable_prefix_null()) { |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
930 |
param_value->accept(*this); |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
931 |
s4o.print(" = "); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
932 |
print_check_function(param_type, param_name, symbol->fb_name); |
98 | 933 |
} |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
934 |
else { |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
935 |
print_setter(param_value, param_type, param_name, NULL, symbol->fb_name); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
936 |
} |
70 | 937 |
} |
938 |
} /* for(...) */ |
|
939 |
||
940 |
return NULL; |
|
941 |
} |
|
942 |
||
943 |
||
944 |
||
945 |
||
946 |
/* helper symbol for fb_invocation */ |
|
947 |
/* param_assignment_list ',' param_assignment */ |
|
948 |
void *visit(param_assignment_list_c *symbol) { |
|
949 |
TRACE("param_assignment_list_c"); |
|
950 |
/* this should never be called... */ |
|
951 |
ERROR; |
|
952 |
return NULL; |
|
953 |
// return print_list(symbol, "", ", "); |
|
954 |
} |
|
955 |
||
956 |
||
957 |
void *visit(input_variable_param_assignment_c *symbol) { |
|
958 |
TRACE("input_variable_param_assignment_c"); |
|
959 |
/* this should never be called... */ |
|
960 |
ERROR; |
|
961 |
return NULL; |
|
962 |
/* |
|
963 |
symbol->variable_name->accept(*this); |
|
964 |
s4o.print(" = "); |
|
965 |
symbol->expression->accept(*this); |
|
966 |
return NULL; |
|
967 |
*/ |
|
968 |
} |
|
969 |
||
970 |
void *visit(output_variable_param_assignment_c *symbol) { |
|
971 |
TRACE("output_variable_param_assignment_c"); |
|
972 |
/* this should never be called... */ |
|
973 |
ERROR; |
|
974 |
return NULL; |
|
975 |
/* |
|
976 |
s4o.print(s4o.indent_spaces); |
|
977 |
if (symbol->not_param != NULL) |
|
978 |
symbol->not_param->accept(*this); |
|
979 |
symbol->variable_name->accept(*this); |
|
980 |
s4o.print(" => "); |
|
981 |
symbol->variable->accept(*this); |
|
982 |
return NULL; |
|
983 |
*/ |
|
984 |
} |
|
985 |
||
986 |
// TODO: the NOT symbol in function (block) calls... |
|
987 |
void *visit(not_paramassign_c *symbol) { |
|
988 |
TRACE("not_paramassign_c"); |
|
989 |
/* this should never be called... */ |
|
990 |
ERROR; |
|
991 |
return NULL; |
|
992 |
/* |
|
993 |
s4o.print("NOT "); |
|
994 |
return NULL; |
|
995 |
*/ |
|
996 |
} |
|
997 |
||
998 |
||
999 |
/********************************/ |
|
1000 |
/* B 3.2.3 Selection Statements */ |
|
1001 |
/********************************/ |
|
1002 |
void *visit(if_statement_c *symbol) { |
|
1003 |
s4o.print("if ("); |
|
1004 |
symbol->expression->accept(*this); |
|
1005 |
s4o.print(") {\n"); |
|
1006 |
s4o.indent_right(); |
|
1007 |
symbol->statement_list->accept(*this); |
|
1008 |
s4o.indent_left(); |
|
1009 |
symbol->elseif_statement_list->accept(*this); |
|
1010 |
||
1011 |
if (symbol->else_statement_list != NULL) { |
|
1012 |
s4o.print(s4o.indent_spaces); s4o.print("} else {\n"); |
|
1013 |
s4o.indent_right(); |
|
1014 |
symbol->else_statement_list->accept(*this); |
|
1015 |
s4o.indent_left(); |
|
1016 |
} |
|
1017 |
s4o.print(s4o.indent_spaces); s4o.print("}"); |
|
1018 |
return NULL; |
|
1019 |
} |
|
1020 |
||
1021 |
/* helper symbol for if_statement */ |
|
1022 |
void *visit(elseif_statement_list_c *symbol) {return print_list(symbol);} |
|
1023 |
||
1024 |
/* helper symbol for elseif_statement_list */ |
|
1025 |
void *visit(elseif_statement_c *symbol) { |
|
1026 |
s4o.print(s4o.indent_spaces); s4o.print("} else if ("); |
|
1027 |
symbol->expression->accept(*this); |
|
1028 |
s4o.print(") {\n"); |
|
1029 |
s4o.indent_right(); |
|
1030 |
symbol->statement_list->accept(*this); |
|
1031 |
s4o.indent_left(); |
|
1032 |
return NULL; |
|
1033 |
} |
|
1034 |
||
1035 |
void *visit(case_statement_c *symbol) { |
|
347
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1036 |
symbol_c *expression_type = search_expression_type->get_type(symbol->expression); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1037 |
s4o.print("{\n"); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1038 |
s4o.indent_right(); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1039 |
if (search_base_type.type_is_enumerated(expression_type)) { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1040 |
s4o.print(s4o.indent_spaces); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1041 |
expression_type->accept(*this); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1042 |
s4o.print(" case_expression = "); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1043 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1044 |
else { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1045 |
s4o.print(s4o.indent_spaces + "IEC_LINT case_expression = (IEC_LINT)"); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1046 |
} |
70 | 1047 |
symbol->expression->accept(*this); |
347
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1048 |
s4o.print(";\n" + s4o.indent_spaces + "switch (case_expression) {\n"); |
70 | 1049 |
s4o.indent_right(); |
347
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1050 |
wanted_casegeneration = single_cg; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1051 |
symbol->case_element_list->accept(*this); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1052 |
wanted_casegeneration = subrange_cg; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1053 |
s4o.print(s4o.indent_spaces + "default:\n"); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1054 |
s4o.indent_right(); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1055 |
first_subrange_case_list = true; |
70 | 1056 |
symbol->case_element_list->accept(*this); |
1057 |
if (symbol->statement_list != NULL) { |
|
347
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1058 |
if (!first_subrange_case_list) { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1059 |
s4o.print(s4o.indent_spaces + "else {\n"); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1060 |
s4o.indent_right(); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1061 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1062 |
symbol->statement_list->accept(*this); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1063 |
if (!first_subrange_case_list) { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1064 |
s4o.indent_left(); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1065 |
s4o.print(s4o.indent_spaces + "}\n"); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1066 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1067 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1068 |
s4o.print(s4o.indent_spaces + "break;\n"); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1069 |
s4o.indent_left(); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1070 |
wanted_casegeneration = none_cg; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1071 |
s4o.indent_left(); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1072 |
s4o.print(s4o.indent_spaces + "}\n"); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1073 |
s4o.indent_left(); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1074 |
s4o.print(s4o.indent_spaces + "}"); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1075 |
return NULL; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1076 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1077 |
|
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1078 |
/* helper symbol for case_statement */ |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1079 |
void *visit(case_element_list_c *symbol) {return print_list(symbol);} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1080 |
|
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1081 |
void *visit(case_element_c *symbol) { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1082 |
case_element_iterator_c *case_element_iterator; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1083 |
symbol_c* element = NULL; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1084 |
bool first_element = true; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1085 |
|
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1086 |
switch (wanted_casegeneration) { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1087 |
case single_cg: |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1088 |
case_element_iterator = new case_element_iterator_c(symbol->case_list, case_element_iterator_c::element_single); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1089 |
for (element = case_element_iterator->next(); element != NULL; element = case_element_iterator->next()) { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1090 |
if (first_element) first_element = false; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1091 |
s4o.print(s4o.indent_spaces + "case "); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1092 |
element->accept(*this); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1093 |
s4o.print(":\n"); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1094 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1095 |
delete case_element_iterator; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1096 |
break; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1097 |
|
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1098 |
case subrange_cg: |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1099 |
case_element_iterator = new case_element_iterator_c(symbol->case_list, case_element_iterator_c::element_subrange); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1100 |
for (element = case_element_iterator->next(); element != NULL; element = case_element_iterator->next()) { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1101 |
if (first_element) { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1102 |
if (first_subrange_case_list) { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1103 |
s4o.print(s4o.indent_spaces + "if ("); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1104 |
first_subrange_case_list = false; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1105 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1106 |
else { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1107 |
s4o.print(s4o.indent_spaces + "else if ("); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1108 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1109 |
first_element = false; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1110 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1111 |
else { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1112 |
s4o.print(" && "); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1113 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1114 |
element->accept(*this); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1115 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1116 |
delete case_element_iterator; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1117 |
if (!first_element) { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1118 |
s4o.print(") {\n"); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1119 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1120 |
break; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1121 |
|
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1122 |
default: |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1123 |
break; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1124 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1125 |
|
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1126 |
if (!first_element) { |
70 | 1127 |
s4o.indent_right(); |
1128 |
symbol->statement_list->accept(*this); |
|
347
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1129 |
switch (wanted_casegeneration) { |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1130 |
case single_cg: |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1131 |
s4o.print(s4o.indent_spaces + "break;\n"); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1132 |
s4o.indent_left(); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1133 |
break; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1134 |
case subrange_cg: |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1135 |
s4o.indent_left(); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1136 |
s4o.print(s4o.indent_spaces + "}\n"); |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1137 |
break; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1138 |
default: |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1139 |
break; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1140 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1141 |
} |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1142 |
return NULL; |
44ff2a6fcadc
Fix case statement in ST, adding support for enumerated types
laurent
parents:
344
diff
changeset
|
1143 |
} |
70 | 1144 |
|
1145 |
/********************************/ |
|
1146 |
/* B 3.2.4 Iteration Statements */ |
|
1147 |
/********************************/ |
|
1148 |
void *visit(for_statement_c *symbol) { |
|
1149 |
s4o.print("for("); |
|
1150 |
symbol->control_variable->accept(*this); |
|
1151 |
s4o.print(" = "); |
|
1152 |
symbol->beg_expression->accept(*this); |
|
1153 |
s4o.print("; "); |
|
1154 |
symbol->control_variable->accept(*this); |
|
1155 |
s4o.print(" != "); |
|
1156 |
symbol->end_expression->accept(*this); |
|
1157 |
s4o.print("; "); |
|
1158 |
symbol->control_variable->accept(*this); |
|
1159 |
if (symbol->by_expression != NULL) { |
|
1160 |
s4o.print(" += "); |
|
1161 |
symbol->by_expression->accept(*this); |
|
1162 |
} else { |
|
1163 |
s4o.print("++"); |
|
1164 |
} |
|
1165 |
s4o.print(") {\n"); |
|
1166 |
s4o.indent_right(); |
|
1167 |
symbol->statement_list->accept(*this); |
|
1168 |
s4o.indent_left(); |
|
1169 |
s4o.print(s4o.indent_spaces); s4o.print("}"); |
|
1170 |
return NULL; |
|
1171 |
} |
|
1172 |
void *visit(while_statement_c *symbol) { |
|
1173 |
s4o.print("while ("); |
|
1174 |
symbol->expression->accept(*this); |
|
1175 |
s4o.print(") {\n"); |
|
1176 |
s4o.indent_right(); |
|
1177 |
symbol->statement_list->accept(*this); |
|
1178 |
s4o.indent_left(); |
|
1179 |
s4o.print(s4o.indent_spaces); s4o.print("}"); |
|
1180 |
return NULL; |
|
1181 |
} |
|
1182 |
void *visit(repeat_statement_c *symbol) { |
|
1183 |
s4o.print("do {\n"); |
|
1184 |
s4o.indent_right(); |
|
1185 |
symbol->statement_list->accept(*this); |
|
1186 |
s4o.indent_left(); |
|
1187 |
s4o.print(s4o.indent_spaces); s4o.print("} while("); |
|
1188 |
symbol->expression->accept(*this); |
|
1189 |
s4o.print(")"); |
|
1190 |
return NULL; |
|
1191 |
} |
|
1192 |
void *visit(exit_statement_c *symbol) { |
|
281
686fa44e3832
fixing exit_statement_c: produce 'break' instead of 'exit(0); (Thanks to Manuele)
Mario de Sousa <msousa@fe.up.pt>
parents:
258
diff
changeset
|
1193 |
s4o.print("break"); |
70 | 1194 |
return NULL; |
1195 |
} |
|
1196 |
||
1197 |
||
1198 |
||
1199 |
}; /* generate_c_st_c */ |
|
1200 |
||
1201 |
||
1202 |
||
1203 |
||
1204 |
||
1205 |
||
1206 |
||
1207 |
||
1208 |