author | lbessard |
Tue, 20 Jan 2009 15:06:13 +0100 | |
changeset 170 | e10fbd3707df |
parent 164 | a0679c78143a |
child 174 | f648065fb66e |
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 type declaration constructs. |
|
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 |
//#include <stdio.h> /* required for NULL */ |
|
40 |
//#include <string> |
|
41 |
//#include <iostream> |
|
42 |
||
43 |
//#include "../../util/symtable.hh" |
|
44 |
||
45 |
||
139
668a54686827
added missing includes on some platform (gentoo/gcc-4.3.1)
etisserant
parents:
123
diff
changeset
|
46 |
#include <stdlib.h> |
70 | 47 |
|
48 |
||
49 |
class generate_c_typedecl_c: public generate_c_base_c { |
|
50 |
||
121 | 51 |
protected: |
52 |
stage4out_c &s4o_incl; |
|
53 |
||
98 | 54 |
private: |
55 |
symbol_c* current_type_name; |
|
56 |
search_base_type_c search_base_type; |
|
57 |
||
121 | 58 |
generate_c_base_c *basedecl; |
59 |
||
70 | 60 |
public: |
121 | 61 |
generate_c_typedecl_c(stage4out_c *s4o_ptr, stage4out_c *s4o_incl_ptr): generate_c_base_c(s4o_ptr), s4o_incl(*s4o_incl_ptr) { |
98 | 62 |
current_typedefinition = none_td; |
63 |
current_basetypedeclaration = none_bd; |
|
121 | 64 |
basedecl = new generate_c_base_c(&s4o_incl); |
98 | 65 |
} |
121 | 66 |
generate_c_typedecl_c(stage4out_c *s4o_ptr): generate_c_base_c(s4o_ptr), s4o_incl(*s4o_ptr) { |
67 |
current_typedefinition = none_td; |
|
68 |
current_basetypedeclaration = none_bd; |
|
69 |
basedecl = new generate_c_base_c(&s4o_incl); |
|
70 |
} |
|
71 |
~generate_c_typedecl_c(void) { |
|
72 |
delete basedecl; |
|
73 |
} |
|
70 | 74 |
|
98 | 75 |
typedef enum { |
76 |
none_td, |
|
77 |
subrange_td, |
|
78 |
array_td |
|
79 |
} typedefinition_t; |
|
80 |
||
81 |
typedefinition_t current_typedefinition; |
|
82 |
||
83 |
typedef enum { |
|
84 |
none_bd, |
|
85 |
subrangebasetype_bd, |
|
86 |
subrangetest_bd, |
|
87 |
arraybasetype_bd, |
|
121 | 88 |
arraybasetypeincl_bd, |
98 | 89 |
arraysubrange_bd, |
90 |
arraytranslateindex_bd |
|
91 |
} basetypedeclaration_t; |
|
92 |
||
93 |
basetypedeclaration_t current_basetypedeclaration; |
|
94 |
||
95 |
int extract_integer(symbol_c *integer) { |
|
96 |
return atoi(((integer_c *)integer)->value); |
|
97 |
} |
|
98 |
||
99 |
void print_integer(unsigned int integer) { |
|
100 |
char str[10]; |
|
101 |
sprintf(str, "%d", integer); |
|
102 |
s4o.print(str); |
|
103 |
} |
|
70 | 104 |
|
121 | 105 |
void print_integer_incl(unsigned int integer) { |
106 |
char str[10]; |
|
107 |
sprintf(str, "%d", integer); |
|
108 |
s4o_incl.print(str); |
|
109 |
} |
|
110 |
||
111 |
void *print_list_incl(list_c *list, |
|
112 |
std::string pre_elem_str = "", |
|
113 |
std::string inter_elem_str = "", |
|
114 |
std::string post_elem_str = "", |
|
115 |
visitor_c *visitor = NULL) { |
|
116 |
if (visitor == NULL) visitor = this; |
|
117 |
||
118 |
if (list->n > 0) { |
|
119 |
//std::cout << "generate_c_base_c::print_list(n = " << list->n << ") 000\n"; |
|
120 |
s4o_incl.print(pre_elem_str); |
|
121 |
list->elements[0]->accept(*visitor); |
|
122 |
} |
|
123 |
||
124 |
for(int i = 1; i < list->n; i++) { |
|
125 |
//std::cout << "generate_c_base_c::print_list " << i << "\n"; |
|
126 |
s4o_incl.print(inter_elem_str); |
|
127 |
list->elements[i]->accept(*visitor); |
|
128 |
} |
|
129 |
||
130 |
if (list->n > 0) |
|
131 |
s4o_incl.print(post_elem_str); |
|
132 |
||
133 |
return NULL; |
|
134 |
} |
|
135 |
||
136 |
||
70 | 137 |
/***************************/ |
138 |
/* B 0 - Programming Model */ |
|
139 |
/***************************/ |
|
140 |
/* leave for derived classes... */ |
|
141 |
||
142 |
/*************************/ |
|
143 |
/* B.1 - Common elements */ |
|
144 |
/*************************/ |
|
145 |
/*******************************************/ |
|
146 |
/* B 1.1 - Letters, digits and identifiers */ |
|
147 |
/*******************************************/ |
|
148 |
/* done in base class(es) */ |
|
149 |
||
150 |
/*********************/ |
|
151 |
/* B 1.2 - Constants */ |
|
152 |
/*********************/ |
|
153 |
/* originally empty... */ |
|
154 |
||
155 |
/******************************/ |
|
156 |
/* B 1.2.1 - Numeric Literals */ |
|
157 |
/******************************/ |
|
158 |
/* done in base class(es) */ |
|
159 |
||
160 |
/*******************************/ |
|
161 |
/* B.1.2.2 Character Strings */ |
|
162 |
/*******************************/ |
|
163 |
/* done in base class(es) */ |
|
164 |
||
165 |
/***************************/ |
|
166 |
/* B 1.2.3 - Time Literals */ |
|
167 |
/***************************/ |
|
168 |
/************************/ |
|
169 |
/* B 1.2.3.1 - Duration */ |
|
170 |
/************************/ |
|
171 |
/* done in base class(es) */ |
|
172 |
||
173 |
/************************************/ |
|
174 |
/* B 1.2.3.2 - Time of day and Date */ |
|
175 |
/************************************/ |
|
176 |
/* done in base class(es) */ |
|
177 |
||
178 |
/**********************/ |
|
179 |
/* B.1.3 - Data types */ |
|
180 |
/**********************/ |
|
181 |
/***********************************/ |
|
182 |
/* B 1.3.1 - Elementary Data Types */ |
|
183 |
/***********************************/ |
|
184 |
/* done in base class(es) */ |
|
185 |
||
186 |
/********************************/ |
|
187 |
/* B.1.3.2 - Generic data types */ |
|
188 |
/********************************/ |
|
189 |
/* originally empty... */ |
|
190 |
||
191 |
/********************************/ |
|
192 |
/* B 1.3.3 - Derived data types */ |
|
193 |
/********************************/ |
|
98 | 194 |
/* subrange_type_name ':' subrange_spec_init */ |
195 |
void *visit(subrange_type_declaration_c *symbol) { |
|
196 |
TRACE("subrange_type_declaration_c"); |
|
197 |
/* add this type declaration to the type symbol table... */ |
|
198 |
type_symtable.insert(symbol->subrange_type_name, symbol->subrange_spec_init); |
|
199 |
||
121 | 200 |
s4o_incl.print("typedef "); |
98 | 201 |
current_basetypedeclaration = subrangebasetype_bd; |
202 |
symbol->subrange_spec_init->accept(*this); |
|
203 |
current_basetypedeclaration = none_bd; |
|
121 | 204 |
s4o_incl.print(" "); |
205 |
symbol->subrange_type_name->accept(*basedecl); |
|
160 | 206 |
s4o_incl.print(";\n"); |
98 | 207 |
|
208 |
current_type_name = symbol->subrange_type_name; |
|
209 |
||
210 |
current_basetypedeclaration = subrangetest_bd; |
|
211 |
symbol->subrange_spec_init->accept(*this); |
|
212 |
current_basetypedeclaration = none_bd; |
|
213 |
||
214 |
return NULL; |
|
215 |
} |
|
216 |
||
217 |
/* subrange_specification ASSIGN signed_integer */ |
|
70 | 218 |
void *visit(subrange_spec_init_c *symbol) { |
219 |
TRACE("subrange_spec_init_c"); |
|
98 | 220 |
current_typedefinition = subrange_td; |
221 |
symbol->subrange_specification->accept(*this); |
|
222 |
current_typedefinition = none_td; |
|
223 |
return NULL; |
|
224 |
} |
|
225 |
||
226 |
/* integer_type_name '(' subrange')' */ |
|
227 |
void *visit(subrange_specification_c *symbol) { |
|
228 |
switch (current_basetypedeclaration) { |
|
229 |
case subrangebasetype_bd: |
|
121 | 230 |
symbol->integer_type_name->accept(*basedecl); |
98 | 231 |
break; |
232 |
case subrangetest_bd: |
|
233 |
if (symbol->subrange != NULL) { |
|
234 |
current_type_name->accept(*this); |
|
235 |
s4o.print(" __CHECK_"); |
|
236 |
current_type_name->accept(*this); |
|
237 |
s4o.print("("); |
|
238 |
current_type_name->accept(*this); |
|
239 |
s4o.print(" value) {\n"); |
|
240 |
s4o.indent_right(); |
|
241 |
||
123 | 242 |
if (search_base_type.type_is_subrange(symbol->integer_type_name)) { |
98 | 243 |
s4o.print(s4o.indent_spaces + "value = __CHECK_"); |
244 |
symbol->integer_type_name->accept(*this); |
|
245 |
s4o.print("(value);\n"); |
|
246 |
} |
|
247 |
||
248 |
symbol->subrange->accept(*this); |
|
249 |
||
250 |
s4o.indent_left(); |
|
251 |
s4o.print("}\n"); |
|
252 |
} |
|
253 |
else { |
|
254 |
s4o.print("#define __CHECK_"); |
|
255 |
current_type_name->accept(*this); |
|
256 |
s4o.print(" __CHECK_"); |
|
257 |
symbol->integer_type_name->accept(*this); |
|
258 |
s4o.print("\n"); |
|
259 |
} |
|
260 |
break; |
|
261 |
default: |
|
262 |
break; |
|
263 |
} |
|
264 |
return NULL; |
|
265 |
} |
|
266 |
||
267 |
/* signed_integer DOTDOT signed_integer */ |
|
268 |
void *visit(subrange_c *symbol) { |
|
269 |
int dimension; |
|
270 |
switch (current_typedefinition) { |
|
271 |
case array_td: |
|
272 |
if (current_basetypedeclaration == arraysubrange_bd) { |
|
121 | 273 |
s4o_incl.print("["); |
98 | 274 |
dimension = extract_integer(symbol->upper_limit) - extract_integer(symbol->lower_limit) + 1; |
121 | 275 |
print_integer_incl(dimension); |
276 |
s4o_incl.print("]"); |
|
98 | 277 |
} |
278 |
else |
|
279 |
symbol->lower_limit->accept(*this); |
|
280 |
break; |
|
281 |
case subrange_td: |
|
282 |
s4o.print(s4o.indent_spaces + "if (value < "); |
|
283 |
symbol->lower_limit->accept(*this); |
|
284 |
s4o.print(")\n"); |
|
285 |
s4o.indent_right(); |
|
286 |
s4o.print(s4o.indent_spaces + "return "); |
|
287 |
symbol->lower_limit->accept(*this); |
|
288 |
s4o.print(";\n"); |
|
289 |
s4o.indent_left(); |
|
290 |
s4o.print(s4o.indent_spaces + "else if (value > "); |
|
291 |
symbol->upper_limit->accept(*this); |
|
292 |
s4o.print(")\n"); |
|
293 |
s4o.indent_right(); |
|
294 |
s4o.print(s4o.indent_spaces + "return "); |
|
295 |
symbol->upper_limit->accept(*this); |
|
296 |
s4o.print(";\n"); |
|
297 |
s4o.indent_left(); |
|
298 |
s4o.print(s4o.indent_spaces + "else\n"); |
|
299 |
s4o.indent_right(); |
|
300 |
s4o.print(s4o.indent_spaces + "return value;\n"); |
|
301 |
s4o.indent_left(); |
|
302 |
default: |
|
303 |
break; |
|
304 |
} |
|
305 |
return NULL; |
|
306 |
} |
|
307 |
||
308 |
/* enumerated_type_name ':' enumerated_spec_init */ |
|
309 |
void *visit(enumerated_type_declaration_c *symbol) { |
|
310 |
TRACE("enumerated_type_declaration_c"); |
|
311 |
/* add this type declaration to the type symbol table... */ |
|
312 |
type_symtable.insert(symbol->enumerated_type_name, symbol->enumerated_spec_init); |
|
313 |
||
121 | 314 |
s4o_incl.print("typedef enum {\n"); |
315 |
s4o_incl.indent_right(); |
|
98 | 316 |
symbol->enumerated_spec_init->accept(*this); |
121 | 317 |
s4o_incl.indent_left(); |
318 |
s4o_incl.print("} "); |
|
319 |
symbol->enumerated_type_name->accept(*basedecl); |
|
320 |
s4o_incl.print(";\n"); |
|
70 | 321 |
return NULL; |
322 |
} |
|
323 |
||
324 |
void *visit(enumerated_spec_init_c *symbol) { |
|
325 |
TRACE("enumerated_spec_init_c"); |
|
98 | 326 |
symbol->enumerated_specification->accept(*this); |
327 |
return NULL; |
|
328 |
} |
|
329 |
||
330 |
/* helper symbol for enumerated_specification->enumerated_spec_init */ |
|
331 |
/* enumerated_value_list ',' enumerated_value */ |
|
332 |
void *visit(enumerated_value_list_c *symbol) { |
|
121 | 333 |
print_list_incl(symbol, s4o_incl.indent_spaces, ",\n"+s4o_incl.indent_spaces, "\n"); |
98 | 334 |
return NULL; |
335 |
} |
|
336 |
||
337 |
/* enumerated_type_name '#' identifier */ |
|
338 |
void *visit(enumerated_value_c *symbol) { |
|
121 | 339 |
symbol->value->accept(*basedecl); |
98 | 340 |
return NULL; |
341 |
} |
|
342 |
||
343 |
/* identifier ':' array_spec_init */ |
|
344 |
void *visit(array_type_declaration_c *symbol) { |
|
345 |
TRACE("array_type_declaration_c"); |
|
346 |
/* add this type declaration to the type symbol table... */ |
|
347 |
type_symtable.insert(symbol->identifier, symbol->array_spec_init); |
|
348 |
||
121 | 349 |
s4o_incl.print("typedef "); |
350 |
current_basetypedeclaration = arraybasetypeincl_bd; |
|
98 | 351 |
symbol->array_spec_init->accept(*this); |
352 |
current_basetypedeclaration = none_bd; |
|
121 | 353 |
s4o_incl.print(" "); |
354 |
symbol->identifier->accept(*basedecl); |
|
98 | 355 |
current_basetypedeclaration = arraysubrange_bd; |
356 |
symbol->array_spec_init->accept(*this); |
|
357 |
current_basetypedeclaration = none_bd; |
|
121 | 358 |
s4o_incl.print(";\n"); |
98 | 359 |
|
123 | 360 |
if (search_base_type.type_is_subrange(current_type_name)) { |
98 | 361 |
s4o.print("#define __CHECK_"); |
362 |
symbol->identifier->accept(*this); |
|
363 |
s4o.print(" __CHECK_"); |
|
364 |
current_basetypedeclaration = arraybasetype_bd; |
|
365 |
symbol->array_spec_init->accept(*this); |
|
366 |
current_basetypedeclaration = none_bd; |
|
367 |
s4o.print("\n"); |
|
368 |
} |
|
369 |
||
370 |
current_type_name = symbol->identifier; |
|
371 |
current_basetypedeclaration = arraytranslateindex_bd; |
|
372 |
symbol->array_spec_init->accept(*this); |
|
373 |
current_basetypedeclaration = none_bd; |
|
374 |
s4o.print("\n"); |
|
375 |
||
376 |
return NULL; |
|
377 |
} |
|
378 |
||
160 | 379 |
/* array_specification [ASSIGN array_initialization] */ |
98 | 380 |
/* array_initialization may be NULL ! */ |
381 |
void *visit(array_spec_init_c *symbol) { |
|
382 |
TRACE("array_spec_init_c"); |
|
160 | 383 |
|
384 |
identifier_c *array_type_name = dynamic_cast<identifier_c *>(symbol->array_specification); |
|
385 |
||
386 |
if (array_type_name == NULL) { |
|
387 |
current_typedefinition = array_td; |
|
388 |
symbol->array_specification->accept(*this); |
|
389 |
current_typedefinition = none_td; |
|
390 |
} |
|
391 |
else |
|
392 |
symbol->array_specification->accept(*basedecl); |
|
98 | 393 |
return NULL; |
394 |
} |
|
395 |
||
396 |
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
|
397 |
void *visit(array_specification_c *symbol) { |
|
398 |
switch (current_basetypedeclaration) { |
|
399 |
case arraybasetype_bd: |
|
400 |
symbol->non_generic_type_name->accept(*this); |
|
401 |
break; |
|
121 | 402 |
case arraybasetypeincl_bd: |
403 |
symbol->non_generic_type_name->accept(*basedecl); |
|
404 |
break; |
|
98 | 405 |
case arraysubrange_bd: |
406 |
case arraytranslateindex_bd: |
|
407 |
symbol->array_subrange_list->accept(*this); |
|
408 |
break; |
|
409 |
default: |
|
410 |
break; |
|
411 |
} |
|
412 |
return NULL; |
|
413 |
} |
|
414 |
||
415 |
/* helper symbol for array_specification */ |
|
416 |
/* array_subrange_list ',' subrange */ |
|
417 |
void *visit(array_subrange_list_c *symbol) { |
|
418 |
if (current_basetypedeclaration == arraytranslateindex_bd) { |
|
419 |
for (int i = 0; i < symbol->n; i++) { |
|
420 |
s4o.print("#define __"); |
|
421 |
current_type_name->accept(*this); |
|
422 |
s4o.print("_TRANSIDX"); |
|
423 |
print_integer(i); |
|
424 |
s4o.print("(index) (index) - "); |
|
425 |
symbol->elements[i]->accept(*this); |
|
426 |
s4o.print("\n"); |
|
427 |
} |
|
428 |
} |
|
429 |
else |
|
430 |
print_list(symbol); |
|
70 | 431 |
return NULL; |
432 |
} |
|
433 |
||
434 |
/* TYPE type_declaration_list END_TYPE */ |
|
435 |
void *visit(data_type_declaration_c *symbol) { |
|
436 |
TRACE("data_type_declaration_c"); |
|
437 |
symbol->type_declaration_list->accept(*this); |
|
438 |
s4o.print("\n\n"); |
|
439 |
return NULL; |
|
440 |
} |
|
441 |
||
442 |
/* helper symbol for data_type_declaration */ |
|
443 |
void *visit(type_declaration_list_c *symbol) { |
|
444 |
TRACE("type_declaration_list_c"); |
|
160 | 445 |
return print_list_incl(symbol, "", "\n", "\n"); |
70 | 446 |
} |
447 |
||
448 |
/* simple_type_name ':' simple_spec_init */ |
|
449 |
void *visit(simple_type_declaration_c *symbol) { |
|
450 |
TRACE("simple_type_declaration_c"); |
|
451 |
/* add this type declaration to the type symbol table... */ |
|
452 |
type_symtable.insert(symbol->simple_type_name, symbol->simple_spec_init); |
|
453 |
||
121 | 454 |
s4o_incl.print("typedef "); |
70 | 455 |
symbol->simple_spec_init->accept(*this); |
121 | 456 |
s4o_incl.print(" "); |
457 |
symbol->simple_type_name->accept(*basedecl); |
|
458 |
s4o_incl.print(";\n"); |
|
70 | 459 |
return NULL; |
460 |
} |
|
461 |
||
462 |
/* simple_specification [ASSIGN constant] */ |
|
463 |
//SYM_REF2(simple_spec_init_c, simple_specification, constant) |
|
464 |
// <constant> may be NULL |
|
465 |
void *visit(simple_spec_init_c *symbol) { |
|
466 |
TRACE("simple_spec_init_c"); |
|
121 | 467 |
symbol->simple_specification->accept(*basedecl); |
70 | 468 |
return NULL; |
469 |
} |
|
470 |
||
471 |
#if 0 |
|
472 |
/* subrange_type_name ':' subrange_spec_init */ |
|
473 |
SYM_REF2(subrange_type_declaration_c, subrange_type_name, subrange_spec_init) |
|
474 |
||
475 |
/* subrange_specification ASSIGN signed_integer */ |
|
476 |
SYM_REF2(subrange_spec_init_c, subrange_specification, signed_integer) |
|
477 |
||
478 |
/* integer_type_name '(' subrange')' */ |
|
479 |
SYM_REF2(subrange_specification_c, integer_type_name, subrange) |
|
480 |
||
481 |
/* signed_integer DOTDOT signed_integer */ |
|
482 |
SYM_REF2(subrange_c, lower_limit, upper_limit) |
|
483 |
||
484 |
/* enumerated_type_name ':' enumerated_spec_init */ |
|
485 |
SYM_REF2(enumerated_type_declaration_c, enumerated_type_name, enumerated_spec_init) |
|
486 |
||
487 |
/* enumerated_specification ASSIGN enumerated_value */ |
|
488 |
SYM_REF2(enumerated_spec_init_c, enumerated_specification, enumerated_value) |
|
489 |
||
490 |
/* helper symbol for enumerated_specification->enumerated_spec_init */ |
|
491 |
/* enumerated_value_list ',' enumerated_value */ |
|
492 |
SYM_LIST(enumerated_value_list_c) |
|
493 |
||
494 |
/* enumerated_type_name '#' identifier */ |
|
495 |
SYM_REF2(enumerated_value_c, type, value) |
|
496 |
||
497 |
/* identifier ':' array_spec_init */ |
|
498 |
SYM_REF2(array_type_declaration_c, identifier, array_spec_init) |
|
499 |
||
500 |
/* array_specification [ASSIGN array_initialization} */ |
|
501 |
/* array_initialization may be NULL ! */ |
|
502 |
SYM_REF2(array_spec_init_c, array_specification, array_initialization) |
|
503 |
||
504 |
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
|
505 |
SYM_REF2(array_specification_c, array_subrange_list, non_generic_type_name) |
|
506 |
||
507 |
/* helper symbol for array_specification */ |
|
508 |
/* array_subrange_list ',' subrange */ |
|
509 |
SYM_LIST(array_subrange_list_c) |
|
510 |
||
511 |
/* array_initialization: '[' array_initial_elements_list ']' */ |
|
512 |
/* helper symbol for array_initialization */ |
|
513 |
/* array_initial_elements_list ',' array_initial_elements */ |
|
514 |
SYM_LIST(array_initial_elements_list_c) |
|
515 |
||
516 |
/* integer '(' [array_initial_element] ')' */ |
|
517 |
/* array_initial_element may be NULL ! */ |
|
518 |
SYM_REF2(array_initial_elements_c, integer, array_initial_element) |
|
519 |
#endif |
|
520 |
||
521 |
/* structure_type_name ':' structure_specification */ |
|
522 |
//SYM_REF2(structure_type_declaration_c, structure_type_name, structure_specification) |
|
523 |
void *visit(structure_type_declaration_c *symbol) { |
|
524 |
TRACE("structure_type_declaration_c"); |
|
525 |
/* add this type declaration to the type symbol table... */ |
|
526 |
type_symtable.insert(symbol->structure_type_name, symbol->structure_specification); |
|
527 |
||
121 | 528 |
s4o_incl.print("typedef "); |
70 | 529 |
symbol->structure_specification->accept(*this); |
121 | 530 |
s4o_incl.print(" "); |
531 |
symbol->structure_type_name->accept(*basedecl); |
|
532 |
s4o_incl.print(";\n"); |
|
70 | 533 |
return NULL; |
534 |
} |
|
535 |
||
536 |
/* structure_type_name ASSIGN structure_initialization */ |
|
537 |
/* structure_initialization may be NULL ! */ |
|
538 |
//SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization) |
|
539 |
void *visit(initialized_structure_c *symbol) { |
|
540 |
TRACE("initialized_structure_c"); |
|
164 | 541 |
|
542 |
symbol->structure_type_name->accept(*basedecl); |
|
543 |
||
70 | 544 |
return NULL; |
545 |
} |
|
546 |
||
547 |
/* helper symbol for structure_declaration */ |
|
548 |
/* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ |
|
549 |
/* structure_element_declaration_list structure_element_declaration ';' */ |
|
550 |
//SYM_LIST(structure_element_declaration_list_c) |
|
551 |
void *visit(structure_element_declaration_list_c *symbol) { |
|
552 |
TRACE("structure_element_declaration_list_c"); |
|
160 | 553 |
s4o_incl.print("struct {\n"); |
554 |
s4o_incl.indent_right(); |
|
555 |
s4o_incl.print(s4o_incl.indent_spaces); |
|
556 |
||
557 |
print_list_incl(symbol, "", s4o_incl.indent_spaces, ""); |
|
558 |
||
559 |
s4o_incl.indent_left(); |
|
560 |
s4o_incl.print(s4o_incl.indent_spaces); |
|
561 |
s4o_incl.print("}"); |
|
70 | 562 |
return NULL; |
563 |
} |
|
564 |
||
565 |
/* structure_element_name ':' spec_init */ |
|
566 |
//SYM_REF2(structure_element_declaration_c, structure_element_name, spec_init) |
|
567 |
void *visit(structure_element_declaration_c *symbol) { |
|
568 |
TRACE("structure_element_declaration_c"); |
|
569 |
||
570 |
symbol->spec_init->accept(*this); |
|
160 | 571 |
s4o_incl.print(" "); |
572 |
symbol->structure_element_name->accept(*basedecl); |
|
573 |
s4o_incl.print(";\n"); |
|
574 |
s4o_incl.print(s4o.indent_spaces); |
|
70 | 575 |
|
576 |
return NULL; |
|
577 |
} |
|
578 |
||
579 |
/* helper symbol for structure_initialization */ |
|
580 |
/* structure_initialization: '(' structure_element_initialization_list ')' */ |
|
581 |
/* structure_element_initialization_list ',' structure_element_initialization */ |
|
582 |
//SYM_LIST(structure_element_initialization_list_c) |
|
583 |
void *visit(structure_element_initialization_list_c *symbol) { |
|
584 |
TRACE("structure_element_initialization_list_c"); |
|
585 |
||
586 |
// TODO ??? |
|
160 | 587 |
//ERROR; |
70 | 588 |
return NULL; |
589 |
} |
|
590 |
||
591 |
/* structure_element_name ASSIGN value */ |
|
592 |
//SYM_REF2(structure_element_initialization_c, structure_element_name, value) |
|
593 |
void *visit(structure_element_initialization_c *symbol) { |
|
594 |
TRACE("structure_element_initialization_c"); |
|
595 |
||
596 |
// TODO ??? |
|
160 | 597 |
//ERROR; |
70 | 598 |
return NULL; |
599 |
} |
|
600 |
||
601 |
#if 0 |
|
602 |
/* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ |
|
603 |
/* |
|
604 |
* NOTE: |
|
605 |
* (Summary: Contrary to what is expected, the |
|
606 |
* string_type_declaration_c is not used to store |
|
607 |
* simple string type declarations that do not include |
|
608 |
* size limits. |
|
609 |
* For e.g.: |
|
610 |
* str1_type: STRING := "hello!" |
|
611 |
* will be stored in a simple_type_declaration_c |
|
612 |
* instead of a string_type_declaration_c. |
|
613 |
* The following: |
|
614 |
* str2_type: STRING [64] := "hello!" |
|
615 |
* will be stored in a sring_type_declaration_c |
|
616 |
* |
|
617 |
* Read on for why this is done... |
|
618 |
* End Summary) |
|
619 |
* |
|
620 |
* According to the spec, the valid construct |
|
621 |
* TYPE new_str_type : STRING := "hello!"; END_TYPE |
|
622 |
* has two possible routes to type_declaration... |
|
623 |
* |
|
624 |
* Route 1: |
|
625 |
* type_declaration: single_element_type_declaration |
|
626 |
* single_element_type_declaration: simple_type_declaration |
|
627 |
* simple_type_declaration: identifier ':' simple_spec_init |
|
628 |
* simple_spec_init: simple_specification ASSIGN constant |
|
629 |
* (shift: identifier <- 'new_str_type') |
|
630 |
* simple_specification: elementary_type_name |
|
631 |
* elementary_type_name: STRING |
|
632 |
* (shift: elementary_type_name <- STRING) |
|
633 |
* (reduce: simple_specification <- elementary_type_name) |
|
634 |
* (shift: constant <- "hello!") |
|
635 |
* (reduce: simple_spec_init: simple_specification ASSIGN constant) |
|
636 |
* (reduce: ...) |
|
637 |
* |
|
638 |
* |
|
639 |
* Route 2: |
|
640 |
* type_declaration: string_type_declaration |
|
641 |
* string_type_declaration: identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init |
|
642 |
* (shift: identifier <- 'new_str_type') |
|
643 |
* elementary_string_type_name: STRING |
|
644 |
* (shift: elementary_string_type_name <- STRING) |
|
645 |
* (shift: string_type_declaration_size <- empty ) |
|
646 |
* string_type_declaration_init: ASSIGN character_string |
|
647 |
* (shift: character_string <- "hello!") |
|
648 |
* (reduce: string_type_declaration_init <- ASSIGN character_string) |
|
649 |
* (reduce: string_type_declaration <- identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init ) |
|
650 |
* (reduce: type_declaration <- string_type_declaration) |
|
651 |
* |
|
652 |
* |
|
653 |
* At first glance it seems that removing route 1 would make |
|
654 |
* the most sense. Unfortunately the construct 'simple_spec_init' |
|
655 |
* shows up multiple times in other rules, so changing this construct |
|
656 |
* would also mean changing all the rules in which it appears. |
|
657 |
* I (Mario) therefore chose to remove route 2 instead. This means |
|
658 |
* that the above declaration gets stored in a |
|
659 |
* simple_type_declaration_c, and not in a string_type_declaration_c |
|
660 |
* as would be expected! |
|
661 |
*/ |
|
662 |
/* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ |
|
663 |
SYM_REF4(string_type_declaration_c, string_type_name, |
|
664 |
elementary_string_type_name, |
|
665 |
string_type_declaration_size, |
|
666 |
string_type_declaration_init) /* may be == NULL! */ |
|
667 |
#endif |
|
668 |
||
669 |
/*********************/ |
|
670 |
/* B 1.4 - Variables */ |
|
671 |
/*********************/ |
|
672 |
/* done in base class(es) */ |
|
673 |
||
674 |
/********************************************/ |
|
675 |
/* B.1.4.1 Directly Represented Variables */ |
|
676 |
/********************************************/ |
|
677 |
// direct_variable: direct_variable_token {$$ = new direct_variable_c($1);}; |
|
678 |
void *visit(direct_variable_c *symbol) { |
|
679 |
TRACE("direct_variable_c"); |
|
680 |
/* Do not use print_token() as it will change everything into uppercase */ |
|
681 |
if (strlen(symbol->value) == 0) ERROR; |
|
682 |
return s4o.printlocation(symbol->value + 1); |
|
683 |
} |
|
684 |
||
685 |
||
686 |
/*************************************/ |
|
687 |
/* B.1.4.2 Multi-element Variables */ |
|
688 |
/*************************************/ |
|
689 |
/* done in base class(es) */ |
|
690 |
||
691 |
/******************************************/ |
|
692 |
/* B 1.4.3 - Declaration & Initialisation */ |
|
693 |
/******************************************/ |
|
694 |
/* leave for derived classes... */ |
|
695 |
||
696 |
/**************************************/ |
|
697 |
/* B.1.5 - Program organization units */ |
|
698 |
/**************************************/ |
|
699 |
/***********************/ |
|
700 |
/* B 1.5.1 - Functions */ |
|
701 |
/***********************/ |
|
702 |
/* leave for derived classes... */ |
|
703 |
||
704 |
/*****************************/ |
|
705 |
/* B 1.5.2 - Function Blocks */ |
|
706 |
/*****************************/ |
|
707 |
/* leave for derived classes... */ |
|
708 |
||
709 |
/**********************/ |
|
710 |
/* B 1.5.3 - Programs */ |
|
711 |
/**********************/ |
|
712 |
/* leave for derived classes... */ |
|
713 |
||
714 |
/*********************************************/ |
|
715 |
/* B.1.6 Sequential function chart elements */ |
|
716 |
/*********************************************/ |
|
717 |
||
718 |
/********************************/ |
|
719 |
/* B 1.7 Configuration elements */ |
|
720 |
/********************************/ |
|
721 |
/* leave for derived classes... */ |
|
722 |
||
723 |
/****************************************/ |
|
724 |
/* B.2 - Language IL (Instruction List) */ |
|
725 |
/****************************************/ |
|
726 |
/***********************************/ |
|
727 |
/* B 2.1 Instructions and Operands */ |
|
728 |
/***********************************/ |
|
729 |
/* leave for derived classes... */ |
|
730 |
||
731 |
/*******************/ |
|
732 |
/* B 2.2 Operators */ |
|
733 |
/*******************/ |
|
734 |
/* leave for derived classes... */ |
|
735 |
||
736 |
||
737 |
/***************************************/ |
|
738 |
/* B.3 - Language ST (Structured Text) */ |
|
739 |
/***************************************/ |
|
740 |
/***********************/ |
|
741 |
/* B 3.1 - Expressions */ |
|
742 |
/***********************/ |
|
743 |
/* leave for derived classes... */ |
|
744 |
||
745 |
/********************/ |
|
746 |
/* B 3.2 Statements */ |
|
747 |
/********************/ |
|
748 |
/* leave for derived classes... */ |
|
749 |
||
750 |
/*********************************/ |
|
751 |
/* B 3.2.1 Assignment Statements */ |
|
752 |
/*********************************/ |
|
753 |
/* leave for derived classes... */ |
|
754 |
||
755 |
/*****************************************/ |
|
756 |
/* B 3.2.2 Subprogram Control Statements */ |
|
757 |
/*****************************************/ |
|
758 |
/* leave for derived classes... */ |
|
759 |
||
760 |
/********************************/ |
|
761 |
/* B 3.2.3 Selection Statements */ |
|
762 |
/********************************/ |
|
763 |
/* leave for derived classes... */ |
|
764 |
||
765 |
/********************************/ |
|
766 |
/* B 3.2.4 Iteration Statements */ |
|
767 |
/********************************/ |
|
768 |
/* leave for derived classes... */ |
|
769 |
||
770 |
||
771 |
||
772 |
||
773 |
}; /* generate_c_typedecl_c */ |
|
774 |
||
775 |
||
776 |