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