author | laurent |
Sat, 05 Dec 2009 18:38:53 +0100 | |
changeset 226 | 29f8ffc203c1 |
parent 221 | c6aed7e5f070 |
child 228 | 43831b683764 |
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); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
143 |
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
|
144 |
s4o.print(SET_EXTERNAL); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
145 |
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
|
146 |
s4o.print(SET_LOCATED); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
147 |
else |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
148 |
s4o.print(SET_VAR); |
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 |
|
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
151 |
if (fb_symbol != NULL) { |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
152 |
print_variable_prefix(); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
153 |
fb_symbol->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
154 |
s4o.print("."); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
155 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
156 |
else |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
157 |
wanted_variablegeneration = complextype_base_vg; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
158 |
symbol->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
159 |
s4o.print(","); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
160 |
wanted_variablegeneration = expression_vg; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
161 |
print_check_function(type, value, fb_value); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
162 |
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
|
163 |
s4o.print(","); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
164 |
wanted_variablegeneration = complextype_suffix_vg; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
165 |
symbol->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
166 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
167 |
s4o.print(")"); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
168 |
wanted_variablegeneration = expression_vg; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
169 |
return NULL; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
170 |
} |
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
171 |
|
70 | 172 |
/*********************/ |
173 |
/* B 1.4 - Variables */ |
|
174 |
/*********************/ |
|
175 |
void *visit(symbolic_variable_c *symbol) { |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
176 |
unsigned int vartype; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
177 |
if (wanted_variablegeneration == complextype_base_vg) |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
178 |
generate_c_base_c::visit(symbol); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
179 |
else if (wanted_variablegeneration == complextype_suffix_vg) |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
180 |
return NULL; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
181 |
else if (this->is_variable_prefix_null()) { |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
182 |
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
|
183 |
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
|
184 |
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
|
185 |
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
|
186 |
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
|
187 |
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
|
188 |
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
|
189 |
generate_c_base_c::visit(symbol); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
190 |
s4o.print(")"); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
191 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
192 |
else { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
193 |
if (vartype == search_var_instance_decl_c::external_vt) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
194 |
s4o.print(GET_EXTERNAL); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
195 |
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
|
196 |
generate_c_base_c::visit(symbol); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
197 |
s4o.print(")"); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
198 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
199 |
else |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
200 |
generate_c_base_c::visit(symbol); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
201 |
} |
145 | 202 |
} |
70 | 203 |
else { |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
204 |
switch (wanted_variablegeneration) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
205 |
case expression_vg: |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
206 |
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
|
207 |
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
|
208 |
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
|
209 |
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
|
210 |
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
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
generate_c_base_c::visit(symbol); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
215 |
s4o.print(")"); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
216 |
break; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
217 |
case fparam_output_vg: |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
218 |
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
|
219 |
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
|
220 |
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
|
221 |
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
|
222 |
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
|
223 |
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
|
224 |
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
|
225 |
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
|
226 |
generate_c_base_c::visit(symbol); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
227 |
s4o.print(")"); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
228 |
break; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
229 |
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
|
230 |
generate_c_base_c::visit(symbol); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
231 |
break; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
232 |
} |
70 | 233 |
} |
234 |
return NULL; |
|
235 |
} |
|
236 |
||
237 |
/********************************************/ |
|
238 |
/* B.1.4.1 Directly Represented Variables */ |
|
239 |
/********************************************/ |
|
240 |
// direct_variable: direct_variable_token {$$ = new direct_variable_c($1);}; |
|
241 |
void *visit(direct_variable_c *symbol) { |
|
242 |
TRACE("direct_variable_c"); |
|
243 |
/* Do not use print_token() as it will change everything into uppercase */ |
|
244 |
if (strlen(symbol->value) == 0) ERROR; |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
245 |
if (this->is_variable_prefix_null()) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
246 |
if (wanted_variablegeneration != fparam_output_vg) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
247 |
s4o.print("*("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
248 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
249 |
else { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
250 |
switch (wanted_variablegeneration) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
251 |
case expression_vg: |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
252 |
s4o.print(GET_LOCATED); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
253 |
s4o.print("("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
254 |
break; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
255 |
case fparam_output_vg: |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
256 |
s4o.print(GET_LOCATED_BY_REF); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
257 |
s4o.print("("); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
258 |
break; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
259 |
default: |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
260 |
break; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
261 |
} |
145 | 262 |
} |
70 | 263 |
this->print_variable_prefix(); |
264 |
s4o.printlocation(symbol->value + 1); |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
265 |
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
|
266 |
wanted_variablegeneration != assignment_vg) |
145 | 267 |
s4o.print(")"); |
70 | 268 |
return NULL; |
269 |
} |
|
270 |
||
98 | 271 |
/*************************************/ |
272 |
/* B.1.4.2 Multi-element Variables */ |
|
273 |
/*************************************/ |
|
274 |
||
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
|
275 |
// 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
|
276 |
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
|
277 |
TRACE("structured_variable_c"); |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
278 |
switch (wanted_variablegeneration) { |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
279 |
case complextype_base_vg: |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
280 |
symbol->record_variable->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
281 |
break; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
282 |
case complextype_suffix_vg: |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
283 |
symbol->record_variable->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
284 |
s4o.print("."); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
285 |
symbol->field_selector->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
286 |
break; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
287 |
default: |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
288 |
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
|
289 |
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
|
290 |
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
|
291 |
symbol->field_selector->accept(*this); |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
292 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
293 |
else |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
294 |
print_getter(symbol); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
295 |
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
|
296 |
} |
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
|
297 |
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
|
298 |
} |
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
|
299 |
|
98 | 300 |
/* subscripted_variable '[' subscript_list ']' */ |
301 |
//SYM_REF2(array_variable_c, subscripted_variable, subscript_list) |
|
302 |
void *visit(array_variable_c *symbol) { |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
303 |
switch (wanted_variablegeneration) { |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
304 |
case complextype_base_vg: |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
305 |
symbol->subscripted_variable->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
306 |
break; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
307 |
case complextype_suffix_vg: |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
308 |
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
|
309 |
symbol->subscripted_variable->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
310 |
if (current_array_type != NULL) { |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
311 |
symbol->subscript_list->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
312 |
current_array_type = NULL; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
313 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
314 |
break; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
315 |
default: |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
316 |
if (this->is_variable_prefix_null()) { |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
317 |
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
|
318 |
symbol->subscripted_variable->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
319 |
if (current_array_type != NULL) { |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
320 |
symbol->subscript_list->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
321 |
current_array_type = NULL; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
322 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
323 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
324 |
else |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
325 |
print_getter(symbol); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
326 |
break; |
98 | 327 |
} |
328 |
return NULL; |
|
329 |
} |
|
330 |
||
331 |
/* subscript_list ',' subscript */ |
|
332 |
void *visit(subscript_list_c *symbol) { |
|
333 |
for (int i = 0; i < symbol->n; i++) { |
|
334 |
s4o.print("[__"); |
|
335 |
current_array_type->accept(*this); |
|
336 |
s4o.print("_TRANSIDX"); |
|
337 |
print_integer(i); |
|
338 |
s4o.print("("); |
|
339 |
symbol->elements[i]->accept(*this); |
|
340 |
s4o.print(")]"); |
|
341 |
} |
|
342 |
return NULL; |
|
343 |
} |
|
344 |
||
345 |
||
70 | 346 |
/***************************************/ |
347 |
/* B.3 - Language ST (Structured Text) */ |
|
348 |
/***************************************/ |
|
349 |
/***********************/ |
|
350 |
/* B 3.1 - Expressions */ |
|
351 |
/***********************/ |
|
352 |
void *visit(or_expression_c *symbol) { |
|
353 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
354 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
355 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
356 |
ERROR; |
|
357 |
if (search_expression_type->is_bool_type(left_type)) |
|
358 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " || "); |
|
359 |
if (search_expression_type->is_binary_type(left_type)) |
|
360 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " | "); |
|
361 |
ERROR; |
|
362 |
return NULL; |
|
363 |
} |
|
364 |
||
365 |
void *visit(xor_expression_c *symbol) { |
|
366 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
367 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
368 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
369 |
ERROR; |
|
370 |
if (search_expression_type->is_bool_type(left_type)) { |
|
371 |
s4o.print("("); |
|
372 |
symbol->l_exp->accept(*this); |
|
373 |
s4o.print(" && !"); |
|
374 |
symbol->r_exp->accept(*this); |
|
375 |
s4o.print(") || (!"); |
|
376 |
symbol->l_exp->accept(*this); |
|
377 |
s4o.print(" && "); |
|
378 |
symbol->r_exp->accept(*this); |
|
379 |
s4o.print(")"); |
|
130 | 380 |
return NULL; |
70 | 381 |
} |
382 |
if (search_expression_type->is_binary_type(left_type)) |
|
383 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " ^ "); |
|
384 |
ERROR; |
|
385 |
return NULL; |
|
386 |
} |
|
387 |
||
388 |
void *visit(and_expression_c *symbol) { |
|
389 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
390 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
391 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
392 |
ERROR; |
|
393 |
if (search_expression_type->is_bool_type(left_type)) |
|
394 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " && "); |
|
395 |
if (search_expression_type->is_binary_type(left_type)) |
|
396 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " & "); |
|
397 |
ERROR; |
|
398 |
return NULL; |
|
399 |
} |
|
400 |
||
401 |
void *visit(equ_expression_c *symbol) { |
|
402 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
403 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
404 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
405 |
ERROR; |
|
123 | 406 |
if (search_expression_type->is_time_type(left_type) || |
407 |
search_expression_type->is_string_type(left_type)) |
|
70 | 408 |
return print_compare_function("__eq_", left_type, symbol->l_exp, symbol->r_exp); |
409 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " == "); |
|
410 |
} |
|
411 |
||
412 |
void *visit(notequ_expression_c *symbol) { |
|
413 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
414 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
415 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
416 |
ERROR; |
|
123 | 417 |
if (search_expression_type->is_time_type(left_type) || |
418 |
search_expression_type->is_string_type(left_type)) |
|
70 | 419 |
return print_compare_function("__ne_", left_type, symbol->l_exp, symbol->r_exp); |
420 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " != "); |
|
421 |
} |
|
422 |
||
423 |
void *visit(lt_expression_c *symbol) { |
|
424 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
425 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
426 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
|
427 |
ERROR; |
|
123 | 428 |
if (search_expression_type->is_time_type(left_type) || |
429 |
search_expression_type->is_string_type(left_type)) |
|
70 | 430 |
return print_compare_function("__lt_", left_type, symbol->l_exp, symbol->r_exp); |
123 | 431 |
if (!search_base_type.type_is_enumerated(left_type)) |
432 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " < "); |
|
433 |
ERROR; |
|
434 |
return NULL; |
|
70 | 435 |
} |
436 |
||
437 |
void *visit(gt_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("__gt_", 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(le_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("__le_", 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(ge_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("__ge_", 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(add_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 ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
483 |
(typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
484 |
(typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c))) |
|
149 | 485 |
return print_binary_function("__TIME_ADD", symbol->l_exp, symbol->r_exp); |
70 | 486 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
487 |
ERROR; |
|
488 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
489 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " + "); |
|
490 |
ERROR; |
|
491 |
return NULL; |
|
492 |
} |
|
493 |
||
494 |
void *visit(sub_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 ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
498 |
(typeid(*left_type) == typeid(date_type_name_c) && typeid(*right_type) == typeid(date_type_name_c)) || |
|
499 |
(typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
500 |
(typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(tod_type_name_c)) || |
|
501 |
(typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
502 |
(typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(dt_type_name_c))) |
|
149 | 503 |
return print_binary_function("__TIME_SUB", symbol->l_exp, symbol->r_exp); |
70 | 504 |
if (!search_expression_type->is_same_type(left_type, right_type)) |
505 |
ERROR; |
|
506 |
if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
507 |
return print_binary_expression(symbol->l_exp, symbol->r_exp, " - "); |
|
508 |
ERROR; |
|
509 |
return NULL; |
|
510 |
} |
|
511 |
||
512 |
void *visit(mul_expression_c *symbol) { |
|
513 |
symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
514 |
symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
515 |
if ((typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_integer_type(right_type)) || |
|
516 |
(typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_real_type(right_type))) |
|
149 | 517 |
return print_binary_function("__TIME_MUL", 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(div_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); |
|
149 | 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))) |
|
531 |
return print_binary_function("__TIME_DIV", 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(mod_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); |
|
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 |
s4o.print("(("); |
|
547 |
symbol->r_exp->accept(*this); |
|
548 |
s4o.print(" == 0)?0:"); |
|
549 |
print_binary_expression(symbol->l_exp, symbol->r_exp, " % "); |
|
550 |
s4o.print(")"); |
|
551 |
return NULL; |
|
552 |
} |
|
553 |
ERROR; |
|
554 |
return NULL; |
|
555 |
} |
|
556 |
||
557 |
/* TODO: power expression... */ |
|
558 |
void *visit(power_expression_c *symbol) {ERROR; return print_binary_expression(symbol->l_exp, symbol->r_exp, " ** ");} |
|
123 | 559 |
void *visit(neg_expression_c *symbol) { |
560 |
symbol_c *exp_type = search_expression_type->get_type(symbol->exp); |
|
561 |
if (search_expression_type->is_integer_type(exp_type) || search_expression_type->is_real_type(exp_type)) |
|
562 |
return print_unary_expression(symbol->exp, " -"); |
|
563 |
ERROR; |
|
564 |
return NULL; |
|
565 |
} |
|
70 | 566 |
|
567 |
void *visit(not_expression_c *symbol) { |
|
568 |
symbol_c *exp_type = search_expression_type->get_type(symbol->exp); |
|
123 | 569 |
if (search_expression_type->is_binary_type(exp_type)) |
570 |
return print_unary_expression(symbol->exp, search_expression_type->is_bool_type(exp_type)?"!":"~"); |
|
571 |
ERROR; |
|
572 |
return NULL; |
|
70 | 573 |
} |
574 |
||
575 |
void *visit(function_invocation_c *symbol) { |
|
149 | 576 |
symbol_c* function_type_prefix = NULL; |
577 |
symbol_c* function_name = NULL; |
|
578 |
symbol_c* function_type_suffix = NULL; |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
579 |
DECLARE_PARAM_LIST() |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
580 |
|
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
581 |
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
|
582 |
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
|
583 |
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
|
584 |
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
|
585 |
|
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
586 |
function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
70 | 587 |
if (f_decl == function_symtable.end_value()) { |
588 |
/* The function called is not in the symtable, so we test if it is a |
|
589 |
* 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
|
590 |
|
70 | 591 |
function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
592 |
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
|
593 |
|
70 | 594 |
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
|
595 |
|
70 | 596 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
167
ae423926bc84
Fixed parameter count error on variadic functions with EN/ENO
etisserant
parents:
162
diff
changeset
|
597 |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
598 |
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
|
599 |
|
149 | 600 |
identifier_c en_param_name("EN"); |
601 |
/* 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
|
602 |
symbol_c *EN_param_value = function_call_param_iterator.search_f(&en_param_name); |
149 | 603 |
if (EN_param_value == NULL) |
604 |
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
|
605 |
else |
ae423926bc84
Fixed parameter count error on variadic functions with EN/ENO
etisserant
parents:
162
diff
changeset
|
606 |
nb_param --; |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
607 |
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
|
608 |
|
162
60a124678842
Fixed typo in EN/ENO parameters handling for std library functions
etisserant
parents:
160
diff
changeset
|
609 |
identifier_c eno_param_name("ENO"); |
149 | 610 |
/* 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
|
611 |
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
|
612 |
if (ENO_param_value != NULL) |
ae423926bc84
Fixed parameter count error on variadic functions with EN/ENO
etisserant
parents:
162
diff
changeset
|
613 |
nb_param --; |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
614 |
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
|
615 |
|
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
616 |
#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
|
617 |
|
70 | 618 |
} |
619 |
else { |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
620 |
function_name = symbol->function_name; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
621 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
622 |
/* loop through each function parameter, find the value we should pass |
70 | 623 |
* to it, and then output the c equivalent... |
624 |
*/ |
|
625 |
function_param_iterator_c fp_iterator(f_decl); |
|
626 |
identifier_c *param_name; |
|
627 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
628 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
149 | 629 |
|
70 | 630 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
149 | 631 |
|
70 | 632 |
/* 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
|
633 |
symbol_c *param_value = function_call_param_iterator.search_f(param_name); |
70 | 634 |
|
635 |
/* Get the value from a foo(<param_value>) style call */ |
|
636 |
if (param_value == NULL) |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
637 |
param_value = function_call_param_iterator.next_nf(); |
149 | 638 |
|
639 |
if (param_value == NULL && param_direction == function_param_iterator_c::direction_in) { |
|
640 |
/* No value given for parameter, so we must use the default... */ |
|
641 |
/* First check whether default value specified in function declaration...*/ |
|
642 |
param_value = fp_iterator.default_value(); |
|
643 |
} |
|
644 |
||
70 | 645 |
symbol_c *param_type = fp_iterator.param_type(); |
646 |
if (param_type == NULL) ERROR; |
|
149 | 647 |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
648 |
ADD_PARAM_LIST(param_name, param_value, param_type, param_direction) |
70 | 649 |
} /* for(...) */ |
650 |
// symbol->parameter_assignment->accept(*this); |
|
149 | 651 |
} |
652 |
||
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
653 |
bool has_output_params = false; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
654 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
655 |
if (!this->is_variable_prefix_null()) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
656 |
PARAM_LIST_ITERATOR() { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
657 |
if ((PARAM_DIRECTION == function_param_iterator_c::direction_out || |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
658 |
PARAM_DIRECTION == function_param_iterator_c::direction_inout) && |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
659 |
PARAM_VALUE != NULL) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
660 |
if (!has_output_params) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
661 |
has_output_params = true; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
662 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
663 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
664 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
665 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
666 |
|
149 | 667 |
if (function_type_prefix != NULL) { |
668 |
s4o.print("("); |
|
669 |
function_type_prefix->accept(*this); |
|
70 | 670 |
s4o.print(")"); |
149 | 671 |
} |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
672 |
if (has_output_params) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
673 |
fcall_number++; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
674 |
s4o.print("__"); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
675 |
fbname->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
676 |
s4o.print("_"); |
149 | 677 |
function_name->accept(*this); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
678 |
s4o.print_integer(fcall_number); |
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 |
else { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
681 |
function_name->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
682 |
if (function_type_suffix != NULL) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
683 |
function_type_suffix->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
684 |
} |
149 | 685 |
s4o.print("("); |
686 |
s4o.indent_right(); |
|
687 |
||
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
688 |
int nb_param = 0; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
689 |
PARAM_LIST_ITERATOR() { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
690 |
symbol_c *param_value = PARAM_VALUE; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
691 |
symbol_c *param_type = PARAM_TYPE; |
149 | 692 |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
693 |
switch (PARAM_DIRECTION) { |
149 | 694 |
case function_param_iterator_c::direction_in: |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
695 |
if (nb_param > 0) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
696 |
s4o.print(",\n"+s4o.indent_spaces); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
697 |
if (param_value == NULL) { |
149 | 698 |
/* If not, get the default value of this variable's type */ |
699 |
param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance()); |
|
700 |
} |
|
701 |
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
|
702 |
s4o.print("("); |
220 | 703 |
if (search_expression_type->is_literal_integer_type(param_type)) |
704 |
search_expression_type->lint_type_name.accept(*this); |
|
705 |
else if (search_expression_type->is_literal_real_type(param_type)) |
|
706 |
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
|
707 |
else |
220 | 708 |
param_type->accept(*this); |
216
136d6ae70745
Bug with using non explicit literals as standard library functions input parameters fixed
laurent
parents:
208
diff
changeset
|
709 |
s4o.print(")"); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
710 |
print_check_function(param_type, param_value); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
711 |
nb_param++; |
149 | 712 |
break; |
713 |
case function_param_iterator_c::direction_out: |
|
714 |
case function_param_iterator_c::direction_inout: |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
715 |
if (!has_output_params) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
716 |
if (nb_param > 0) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
717 |
s4o.print(",\n"+s4o.indent_spaces); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
718 |
if (param_value == NULL) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
719 |
s4o.print("NULL"); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
720 |
else { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
721 |
wanted_variablegeneration = fparam_output_vg; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
722 |
param_value->accept(*this); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
723 |
wanted_variablegeneration = expression_vg; |
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 |
nb_param++; |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
726 |
} |
149 | 727 |
break; |
728 |
case function_param_iterator_c::direction_extref: |
|
729 |
/* TODO! */ |
|
730 |
ERROR; |
|
731 |
break; |
|
732 |
} /* switch */ |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
733 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
734 |
if (has_output_params) { |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
735 |
if (nb_param > 0) |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
736 |
s4o.print(",\n"+s4o.indent_spaces); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
737 |
s4o.print(FB_FUNCTION_PARAM); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
738 |
} |
149 | 739 |
s4o.print(")"); |
740 |
s4o.indent_left(); |
|
70 | 741 |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
742 |
CLEAR_PARAM_LIST() |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
743 |
|
70 | 744 |
return NULL; |
745 |
} |
|
746 |
||
747 |
/********************/ |
|
748 |
/* B 3.2 Statements */ |
|
749 |
/********************/ |
|
750 |
void *visit(statement_list_c *symbol) { |
|
751 |
return print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); |
|
752 |
} |
|
753 |
||
754 |
/*********************************/ |
|
755 |
/* B 3.2.1 Assignment Statements */ |
|
756 |
/*********************************/ |
|
757 |
void *visit(assignment_statement_c *symbol) { |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
758 |
symbol_c *left_type = search_varfb_instance_type->get_rawtype(symbol->l_exp); |
98 | 759 |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
760 |
if (this->is_variable_prefix_null()) { |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
761 |
symbol->l_exp->accept(*this); |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
762 |
s4o.print(" = "); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
763 |
print_check_function(left_type, symbol->r_exp); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
764 |
} |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
765 |
else { |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
766 |
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
|
767 |
} |
70 | 768 |
return NULL; |
769 |
} |
|
770 |
||
771 |
/*****************************************/ |
|
772 |
/* B 3.2.2 Subprogram Control Statements */ |
|
773 |
/*****************************************/ |
|
774 |
||
775 |
/* fb_name '(' [param_assignment_list] ')' */ |
|
776 |
/* param_assignment_list -> may be NULL ! */ |
|
777 |
//SYM_REF2(fb_invocation_c, fb_name, param_assignment_list) |
|
778 |
void *visit(fb_invocation_c *symbol) { |
|
779 |
TRACE("fb_invocation_c"); |
|
780 |
/* first figure out what is the name of the function block type of the function block being called... */ |
|
781 |
symbol_c *function_block_type_name = this->search_fb_instance_decl->get_type_name(symbol->fb_name); |
|
782 |
/* should never occur. The function block instance MUST have been declared... */ |
|
783 |
if (function_block_type_name == NULL) ERROR; |
|
784 |
||
785 |
/* Now find the declaration of the function block type being called... */ |
|
786 |
function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(function_block_type_name); |
|
787 |
/* should never occur. The function block type being called MUST be in the symtable... */ |
|
788 |
if (fb_decl == function_block_type_symtable.end_value()) ERROR; |
|
789 |
||
790 |
/* loop through each function block parameter, find the value we should pass |
|
791 |
* to it, and then output the c equivalent... |
|
792 |
*/ |
|
793 |
function_param_iterator_c fp_iterator(fb_decl); |
|
794 |
identifier_c *param_name; |
|
795 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
796 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
797 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
160 | 798 |
|
162
60a124678842
Fixed typo in EN/ENO parameters handling for std library functions
etisserant
parents:
160
diff
changeset
|
799 |
/*fprintf(stderr, "param : %s\n", param_name->value);*/ |
160 | 800 |
|
70 | 801 |
/* 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
|
802 |
symbol_c *param_value = function_call_param_iterator.search_f(param_name); |
70 | 803 |
|
804 |
/* Get the value from a foo(<param_value>) style call */ |
|
805 |
if (param_value == NULL) |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
806 |
param_value = function_call_param_iterator.next_nf(); |
70 | 807 |
|
98 | 808 |
symbol_c *param_type = fp_iterator.param_type(); |
809 |
if (param_type == NULL) ERROR; |
|
810 |
||
70 | 811 |
/* now output the value assignment */ |
812 |
if (param_value != NULL) |
|
813 |
if ((param_direction == function_param_iterator_c::direction_in) || |
|
814 |
(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
|
815 |
if (this->is_variable_prefix_null()) { |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
816 |
print_variable_prefix(); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
817 |
symbol->fb_name->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
818 |
s4o.print("."); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
819 |
param_name->accept(*this); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
820 |
s4o.print(" = "); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
821 |
print_check_function(param_type, param_value); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
822 |
} |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
823 |
else { |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
824 |
print_setter(param_name, param_type, param_value, symbol->fb_name); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
825 |
} |
70 | 826 |
s4o.print(";\n" + s4o.indent_spaces); |
827 |
} |
|
828 |
} /* for(...) */ |
|
829 |
||
830 |
/* now call the function... */ |
|
831 |
function_block_type_name->accept(*this); |
|
832 |
s4o.print(FB_FUNCTION_SUFFIX); |
|
833 |
s4o.print("(&"); |
|
834 |
print_variable_prefix(); |
|
835 |
symbol->fb_name->accept(*this); |
|
836 |
s4o.print(")"); |
|
837 |
||
838 |
/* loop through each function parameter, find the variable to which |
|
839 |
* we should atribute the value of all output or inoutput parameters. |
|
840 |
*/ |
|
841 |
fp_iterator.reset(); |
|
842 |
function_call_param_iterator.reset(); |
|
843 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
844 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
845 |
||
846 |
/* 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
|
847 |
symbol_c *param_value = function_call_param_iterator.search_f(param_name); |
70 | 848 |
|
849 |
/* Get the value from a foo(<param_value>) style call */ |
|
850 |
if (param_value == NULL) |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
169
diff
changeset
|
851 |
param_value = function_call_param_iterator.next_nf(); |
70 | 852 |
|
853 |
/* now output the value assignment */ |
|
854 |
if (param_value != NULL) |
|
855 |
if ((param_direction == function_param_iterator_c::direction_out) || |
|
856 |
(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
|
857 |
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
|
858 |
if (this->is_variable_prefix_null()) { |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
859 |
param_value->accept(*this); |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
860 |
s4o.print(" = "); |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
861 |
print_check_function(param_type, param_name, symbol->fb_name); |
98 | 862 |
} |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
216
diff
changeset
|
863 |
else { |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
221
diff
changeset
|
864 |
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
|
865 |
} |
70 | 866 |
} |
867 |
} /* for(...) */ |
|
868 |
||
869 |
return NULL; |
|
870 |
} |
|
871 |
||
872 |
||
873 |
||
874 |
||
875 |
/* helper symbol for fb_invocation */ |
|
876 |
/* param_assignment_list ',' param_assignment */ |
|
877 |
void *visit(param_assignment_list_c *symbol) { |
|
878 |
TRACE("param_assignment_list_c"); |
|
879 |
/* this should never be called... */ |
|
880 |
ERROR; |
|
881 |
return NULL; |
|
882 |
// return print_list(symbol, "", ", "); |
|
883 |
} |
|
884 |
||
885 |
||
886 |
void *visit(input_variable_param_assignment_c *symbol) { |
|
887 |
TRACE("input_variable_param_assignment_c"); |
|
888 |
/* this should never be called... */ |
|
889 |
ERROR; |
|
890 |
return NULL; |
|
891 |
/* |
|
892 |
symbol->variable_name->accept(*this); |
|
893 |
s4o.print(" = "); |
|
894 |
symbol->expression->accept(*this); |
|
895 |
return NULL; |
|
896 |
*/ |
|
897 |
} |
|
898 |
||
899 |
void *visit(output_variable_param_assignment_c *symbol) { |
|
900 |
TRACE("output_variable_param_assignment_c"); |
|
901 |
/* this should never be called... */ |
|
902 |
ERROR; |
|
903 |
return NULL; |
|
904 |
/* |
|
905 |
s4o.print(s4o.indent_spaces); |
|
906 |
if (symbol->not_param != NULL) |
|
907 |
symbol->not_param->accept(*this); |
|
908 |
symbol->variable_name->accept(*this); |
|
909 |
s4o.print(" => "); |
|
910 |
symbol->variable->accept(*this); |
|
911 |
return NULL; |
|
912 |
*/ |
|
913 |
} |
|
914 |
||
915 |
// TODO: the NOT symbol in function (block) calls... |
|
916 |
void *visit(not_paramassign_c *symbol) { |
|
917 |
TRACE("not_paramassign_c"); |
|
918 |
/* this should never be called... */ |
|
919 |
ERROR; |
|
920 |
return NULL; |
|
921 |
/* |
|
922 |
s4o.print("NOT "); |
|
923 |
return NULL; |
|
924 |
*/ |
|
925 |
} |
|
926 |
||
927 |
||
928 |
/********************************/ |
|
929 |
/* B 3.2.3 Selection Statements */ |
|
930 |
/********************************/ |
|
931 |
void *visit(if_statement_c *symbol) { |
|
932 |
s4o.print("if ("); |
|
933 |
symbol->expression->accept(*this); |
|
934 |
s4o.print(") {\n"); |
|
935 |
s4o.indent_right(); |
|
936 |
symbol->statement_list->accept(*this); |
|
937 |
s4o.indent_left(); |
|
938 |
symbol->elseif_statement_list->accept(*this); |
|
939 |
||
940 |
if (symbol->else_statement_list != NULL) { |
|
941 |
s4o.print(s4o.indent_spaces); s4o.print("} else {\n"); |
|
942 |
s4o.indent_right(); |
|
943 |
symbol->else_statement_list->accept(*this); |
|
944 |
s4o.indent_left(); |
|
945 |
} |
|
946 |
s4o.print(s4o.indent_spaces); s4o.print("}"); |
|
947 |
return NULL; |
|
948 |
} |
|
949 |
||
950 |
/* helper symbol for if_statement */ |
|
951 |
void *visit(elseif_statement_list_c *symbol) {return print_list(symbol);} |
|
952 |
||
953 |
/* helper symbol for elseif_statement_list */ |
|
954 |
void *visit(elseif_statement_c *symbol) { |
|
955 |
s4o.print(s4o.indent_spaces); s4o.print("} else if ("); |
|
956 |
symbol->expression->accept(*this); |
|
957 |
s4o.print(") {\n"); |
|
958 |
s4o.indent_right(); |
|
959 |
symbol->statement_list->accept(*this); |
|
960 |
s4o.indent_left(); |
|
961 |
return NULL; |
|
962 |
} |
|
963 |
||
964 |
void *visit(case_statement_c *symbol) { |
|
965 |
s4o.print("switch("); |
|
966 |
symbol->expression->accept(*this); |
|
967 |
s4o.print(") {\n"); |
|
968 |
s4o.indent_right(); |
|
969 |
symbol->case_element_list->accept(*this); |
|
970 |
if (symbol->statement_list != NULL) { |
|
971 |
s4o.print(s4o.indent_spaces + "default:\n"); |
|
972 |
s4o.indent_right(); |
|
973 |
symbol->statement_list->accept(*this); |
|
974 |
s4o.print(s4o.indent_spaces + "break;\n"); |
|
975 |
s4o.indent_left(); |
|
976 |
} |
|
977 |
s4o.indent_left(); |
|
978 |
s4o.print(s4o.indent_spaces + "}"); |
|
979 |
return NULL; |
|
980 |
} |
|
981 |
||
982 |
/* helper symbol for case_statement */ |
|
983 |
void *visit(case_element_list_c *symbol) {return print_list(symbol);} |
|
984 |
||
985 |
void *visit(case_element_c *symbol) { |
|
986 |
s4o.print(s4o.indent_spaces + "case "); |
|
987 |
symbol->case_list->accept(*this); |
|
988 |
s4o.print(" :\n"); |
|
989 |
s4o.indent_right(); |
|
990 |
symbol->statement_list->accept(*this); |
|
991 |
s4o.print(s4o.indent_spaces + "break;\n"); |
|
992 |
s4o.indent_left(); |
|
993 |
return NULL; |
|
994 |
} |
|
995 |
||
996 |
void *visit(case_list_c *symbol) {return print_list(symbol, "", ", ");} |
|
997 |
||
998 |
/********************************/ |
|
999 |
/* B 3.2.4 Iteration Statements */ |
|
1000 |
/********************************/ |
|
1001 |
void *visit(for_statement_c *symbol) { |
|
1002 |
s4o.print("for("); |
|
1003 |
symbol->control_variable->accept(*this); |
|
1004 |
s4o.print(" = "); |
|
1005 |
symbol->beg_expression->accept(*this); |
|
1006 |
s4o.print("; "); |
|
1007 |
symbol->control_variable->accept(*this); |
|
1008 |
s4o.print(" != "); |
|
1009 |
symbol->end_expression->accept(*this); |
|
1010 |
s4o.print("; "); |
|
1011 |
symbol->control_variable->accept(*this); |
|
1012 |
if (symbol->by_expression != NULL) { |
|
1013 |
s4o.print(" += "); |
|
1014 |
symbol->by_expression->accept(*this); |
|
1015 |
} else { |
|
1016 |
s4o.print("++"); |
|
1017 |
} |
|
1018 |
s4o.print(") {\n"); |
|
1019 |
s4o.indent_right(); |
|
1020 |
symbol->statement_list->accept(*this); |
|
1021 |
s4o.indent_left(); |
|
1022 |
s4o.print(s4o.indent_spaces); s4o.print("}"); |
|
1023 |
return NULL; |
|
1024 |
} |
|
1025 |
void *visit(while_statement_c *symbol) { |
|
1026 |
s4o.print("while ("); |
|
1027 |
symbol->expression->accept(*this); |
|
1028 |
s4o.print(") {\n"); |
|
1029 |
s4o.indent_right(); |
|
1030 |
symbol->statement_list->accept(*this); |
|
1031 |
s4o.indent_left(); |
|
1032 |
s4o.print(s4o.indent_spaces); s4o.print("}"); |
|
1033 |
return NULL; |
|
1034 |
} |
|
1035 |
void *visit(repeat_statement_c *symbol) { |
|
1036 |
s4o.print("do {\n"); |
|
1037 |
s4o.indent_right(); |
|
1038 |
symbol->statement_list->accept(*this); |
|
1039 |
s4o.indent_left(); |
|
1040 |
s4o.print(s4o.indent_spaces); s4o.print("} while("); |
|
1041 |
symbol->expression->accept(*this); |
|
1042 |
s4o.print(")"); |
|
1043 |
return NULL; |
|
1044 |
} |
|
1045 |
void *visit(exit_statement_c *symbol) { |
|
1046 |
s4o.print("exit(0)"); |
|
1047 |
return NULL; |
|
1048 |
} |
|
1049 |
||
1050 |
||
1051 |
||
1052 |
}; /* generate_c_st_c */ |
|
1053 |
||
1054 |
||
1055 |
||
1056 |
||
1057 |
||
1058 |
||
1059 |
||
1060 |
||
1061 |