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 |
|
|
46 |
|
|
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);
|
|
206 |
s4o_incl.print(";\n\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 |
|
|
379 |
/* array_specification [ASSIGN array_initialization} */
|
|
380 |
/* array_initialization may be NULL ! */
|
|
381 |
void *visit(array_spec_init_c *symbol) {
|
|
382 |
TRACE("array_spec_init_c");
|
|
383 |
current_typedefinition = array_td;
|
|
384 |
symbol->array_specification->accept(*this);
|
|
385 |
current_typedefinition = none_td;
|
|
386 |
return NULL;
|
|
387 |
}
|
|
388 |
|
|
389 |
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
|
|
390 |
void *visit(array_specification_c *symbol) {
|
|
391 |
switch (current_basetypedeclaration) {
|
|
392 |
case arraybasetype_bd:
|
|
393 |
symbol->non_generic_type_name->accept(*this);
|
|
394 |
break;
|
121
|
395 |
case arraybasetypeincl_bd:
|
|
396 |
symbol->non_generic_type_name->accept(*basedecl);
|
|
397 |
break;
|
98
|
398 |
case arraysubrange_bd:
|
|
399 |
case arraytranslateindex_bd:
|
|
400 |
symbol->array_subrange_list->accept(*this);
|
|
401 |
break;
|
|
402 |
default:
|
|
403 |
break;
|
|
404 |
}
|
|
405 |
return NULL;
|
|
406 |
}
|
|
407 |
|
|
408 |
/* helper symbol for array_specification */
|
|
409 |
/* array_subrange_list ',' subrange */
|
|
410 |
void *visit(array_subrange_list_c *symbol) {
|
|
411 |
if (current_basetypedeclaration == arraytranslateindex_bd) {
|
|
412 |
for (int i = 0; i < symbol->n; i++) {
|
|
413 |
s4o.print("#define __");
|
|
414 |
current_type_name->accept(*this);
|
|
415 |
s4o.print("_TRANSIDX");
|
|
416 |
print_integer(i);
|
|
417 |
s4o.print("(index) (index) - ");
|
|
418 |
symbol->elements[i]->accept(*this);
|
|
419 |
s4o.print("\n");
|
|
420 |
}
|
|
421 |
}
|
|
422 |
else
|
|
423 |
print_list(symbol);
|
70
|
424 |
return NULL;
|
|
425 |
}
|
|
426 |
|
|
427 |
/* TYPE type_declaration_list END_TYPE */
|
|
428 |
void *visit(data_type_declaration_c *symbol) {
|
|
429 |
TRACE("data_type_declaration_c");
|
|
430 |
symbol->type_declaration_list->accept(*this);
|
|
431 |
s4o.print("\n\n");
|
|
432 |
return NULL;
|
|
433 |
}
|
|
434 |
|
|
435 |
/* helper symbol for data_type_declaration */
|
|
436 |
void *visit(type_declaration_list_c *symbol) {
|
|
437 |
TRACE("type_declaration_list_c");
|
98
|
438 |
return print_list(symbol, "", "\n");
|
70
|
439 |
}
|
|
440 |
|
|
441 |
/* simple_type_name ':' simple_spec_init */
|
|
442 |
void *visit(simple_type_declaration_c *symbol) {
|
|
443 |
TRACE("simple_type_declaration_c");
|
|
444 |
/* add this type declaration to the type symbol table... */
|
|
445 |
type_symtable.insert(symbol->simple_type_name, symbol->simple_spec_init);
|
|
446 |
|
121
|
447 |
s4o_incl.print("typedef ");
|
70
|
448 |
symbol->simple_spec_init->accept(*this);
|
121
|
449 |
s4o_incl.print(" ");
|
|
450 |
symbol->simple_type_name->accept(*basedecl);
|
|
451 |
s4o_incl.print(";\n");
|
70
|
452 |
return NULL;
|
|
453 |
}
|
|
454 |
|
|
455 |
/* simple_specification [ASSIGN constant] */
|
|
456 |
//SYM_REF2(simple_spec_init_c, simple_specification, constant)
|
|
457 |
// <constant> may be NULL
|
|
458 |
void *visit(simple_spec_init_c *symbol) {
|
|
459 |
TRACE("simple_spec_init_c");
|
121
|
460 |
symbol->simple_specification->accept(*basedecl);
|
70
|
461 |
return NULL;
|
|
462 |
}
|
|
463 |
|
|
464 |
#if 0
|
|
465 |
/* subrange_type_name ':' subrange_spec_init */
|
|
466 |
SYM_REF2(subrange_type_declaration_c, subrange_type_name, subrange_spec_init)
|
|
467 |
|
|
468 |
/* subrange_specification ASSIGN signed_integer */
|
|
469 |
SYM_REF2(subrange_spec_init_c, subrange_specification, signed_integer)
|
|
470 |
|
|
471 |
/* integer_type_name '(' subrange')' */
|
|
472 |
SYM_REF2(subrange_specification_c, integer_type_name, subrange)
|
|
473 |
|
|
474 |
/* signed_integer DOTDOT signed_integer */
|
|
475 |
SYM_REF2(subrange_c, lower_limit, upper_limit)
|
|
476 |
|
|
477 |
/* enumerated_type_name ':' enumerated_spec_init */
|
|
478 |
SYM_REF2(enumerated_type_declaration_c, enumerated_type_name, enumerated_spec_init)
|
|
479 |
|
|
480 |
/* enumerated_specification ASSIGN enumerated_value */
|
|
481 |
SYM_REF2(enumerated_spec_init_c, enumerated_specification, enumerated_value)
|
|
482 |
|
|
483 |
/* helper symbol for enumerated_specification->enumerated_spec_init */
|
|
484 |
/* enumerated_value_list ',' enumerated_value */
|
|
485 |
SYM_LIST(enumerated_value_list_c)
|
|
486 |
|
|
487 |
/* enumerated_type_name '#' identifier */
|
|
488 |
SYM_REF2(enumerated_value_c, type, value)
|
|
489 |
|
|
490 |
/* identifier ':' array_spec_init */
|
|
491 |
SYM_REF2(array_type_declaration_c, identifier, array_spec_init)
|
|
492 |
|
|
493 |
/* array_specification [ASSIGN array_initialization} */
|
|
494 |
/* array_initialization may be NULL ! */
|
|
495 |
SYM_REF2(array_spec_init_c, array_specification, array_initialization)
|
|
496 |
|
|
497 |
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
|
|
498 |
SYM_REF2(array_specification_c, array_subrange_list, non_generic_type_name)
|
|
499 |
|
|
500 |
/* helper symbol for array_specification */
|
|
501 |
/* array_subrange_list ',' subrange */
|
|
502 |
SYM_LIST(array_subrange_list_c)
|
|
503 |
|
|
504 |
/* array_initialization: '[' array_initial_elements_list ']' */
|
|
505 |
/* helper symbol for array_initialization */
|
|
506 |
/* array_initial_elements_list ',' array_initial_elements */
|
|
507 |
SYM_LIST(array_initial_elements_list_c)
|
|
508 |
|
|
509 |
/* integer '(' [array_initial_element] ')' */
|
|
510 |
/* array_initial_element may be NULL ! */
|
|
511 |
SYM_REF2(array_initial_elements_c, integer, array_initial_element)
|
|
512 |
#endif
|
|
513 |
|
|
514 |
/* structure_type_name ':' structure_specification */
|
|
515 |
//SYM_REF2(structure_type_declaration_c, structure_type_name, structure_specification)
|
|
516 |
void *visit(structure_type_declaration_c *symbol) {
|
|
517 |
TRACE("structure_type_declaration_c");
|
|
518 |
/* add this type declaration to the type symbol table... */
|
|
519 |
type_symtable.insert(symbol->structure_type_name, symbol->structure_specification);
|
|
520 |
|
121
|
521 |
s4o_incl.print("typedef ");
|
70
|
522 |
symbol->structure_specification->accept(*this);
|
121
|
523 |
s4o_incl.print(" ");
|
|
524 |
symbol->structure_type_name->accept(*basedecl);
|
|
525 |
s4o_incl.print(";\n");
|
70
|
526 |
return NULL;
|
|
527 |
}
|
|
528 |
|
|
529 |
/* structure_type_name ASSIGN structure_initialization */
|
|
530 |
/* structure_initialization may be NULL ! */
|
|
531 |
//SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
|
|
532 |
void *visit(initialized_structure_c *symbol) {
|
|
533 |
TRACE("initialized_structure_c");
|
|
534 |
symbol->structure_type_name->accept(*this);
|
|
535 |
return NULL;
|
|
536 |
}
|
|
537 |
|
|
538 |
/* helper symbol for structure_declaration */
|
|
539 |
/* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */
|
|
540 |
/* structure_element_declaration_list structure_element_declaration ';' */
|
|
541 |
//SYM_LIST(structure_element_declaration_list_c)
|
|
542 |
void *visit(structure_element_declaration_list_c *symbol) {
|
|
543 |
TRACE("structure_element_declaration_list_c");
|
|
544 |
s4o.print("struct {\n");
|
|
545 |
s4o.indent_right();
|
|
546 |
s4o.print(s4o.indent_spaces);
|
|
547 |
|
|
548 |
print_list(symbol);
|
|
549 |
|
|
550 |
s4o.indent_left();
|
|
551 |
s4o.print(s4o.indent_spaces);
|
|
552 |
s4o.print("}");
|
|
553 |
return NULL;
|
|
554 |
}
|
|
555 |
|
|
556 |
/* structure_element_name ':' spec_init */
|
|
557 |
//SYM_REF2(structure_element_declaration_c, structure_element_name, spec_init)
|
|
558 |
void *visit(structure_element_declaration_c *symbol) {
|
|
559 |
TRACE("structure_element_declaration_c");
|
|
560 |
|
|
561 |
symbol->spec_init->accept(*this);
|
|
562 |
s4o.print(" ");
|
|
563 |
symbol->structure_element_name->accept(*this);
|
|
564 |
s4o.print(";\n");
|
|
565 |
s4o.print(s4o.indent_spaces);
|
|
566 |
|
|
567 |
return NULL;
|
|
568 |
}
|
|
569 |
|
|
570 |
/* helper symbol for structure_initialization */
|
|
571 |
/* structure_initialization: '(' structure_element_initialization_list ')' */
|
|
572 |
/* structure_element_initialization_list ',' structure_element_initialization */
|
|
573 |
//SYM_LIST(structure_element_initialization_list_c)
|
|
574 |
void *visit(structure_element_initialization_list_c *symbol) {
|
|
575 |
TRACE("structure_element_initialization_list_c");
|
|
576 |
|
|
577 |
// TODO ???
|
|
578 |
ERROR;
|
|
579 |
return NULL;
|
|
580 |
}
|
|
581 |
|
|
582 |
/* structure_element_name ASSIGN value */
|
|
583 |
//SYM_REF2(structure_element_initialization_c, structure_element_name, value)
|
|
584 |
void *visit(structure_element_initialization_c *symbol) {
|
|
585 |
TRACE("structure_element_initialization_c");
|
|
586 |
|
|
587 |
// TODO ???
|
|
588 |
ERROR;
|
|
589 |
return NULL;
|
|
590 |
}
|
|
591 |
|
|
592 |
#if 0
|
|
593 |
/* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
|
|
594 |
/*
|
|
595 |
* NOTE:
|
|
596 |
* (Summary: Contrary to what is expected, the
|
|
597 |
* string_type_declaration_c is not used to store
|
|
598 |
* simple string type declarations that do not include
|
|
599 |
* size limits.
|
|
600 |
* For e.g.:
|
|
601 |
* str1_type: STRING := "hello!"
|
|
602 |
* will be stored in a simple_type_declaration_c
|
|
603 |
* instead of a string_type_declaration_c.
|
|
604 |
* The following:
|
|
605 |
* str2_type: STRING [64] := "hello!"
|
|
606 |
* will be stored in a sring_type_declaration_c
|
|
607 |
*
|
|
608 |
* Read on for why this is done...
|
|
609 |
* End Summary)
|
|
610 |
*
|
|
611 |
* According to the spec, the valid construct
|
|
612 |
* TYPE new_str_type : STRING := "hello!"; END_TYPE
|
|
613 |
* has two possible routes to type_declaration...
|
|
614 |
*
|
|
615 |
* Route 1:
|
|
616 |
* type_declaration: single_element_type_declaration
|
|
617 |
* single_element_type_declaration: simple_type_declaration
|
|
618 |
* simple_type_declaration: identifier ':' simple_spec_init
|
|
619 |
* simple_spec_init: simple_specification ASSIGN constant
|
|
620 |
* (shift: identifier <- 'new_str_type')
|
|
621 |
* simple_specification: elementary_type_name
|
|
622 |
* elementary_type_name: STRING
|
|
623 |
* (shift: elementary_type_name <- STRING)
|
|
624 |
* (reduce: simple_specification <- elementary_type_name)
|
|
625 |
* (shift: constant <- "hello!")
|
|
626 |
* (reduce: simple_spec_init: simple_specification ASSIGN constant)
|
|
627 |
* (reduce: ...)
|
|
628 |
*
|
|
629 |
*
|
|
630 |
* Route 2:
|
|
631 |
* type_declaration: string_type_declaration
|
|
632 |
* string_type_declaration: identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init
|
|
633 |
* (shift: identifier <- 'new_str_type')
|
|
634 |
* elementary_string_type_name: STRING
|
|
635 |
* (shift: elementary_string_type_name <- STRING)
|
|
636 |
* (shift: string_type_declaration_size <- empty )
|
|
637 |
* string_type_declaration_init: ASSIGN character_string
|
|
638 |
* (shift: character_string <- "hello!")
|
|
639 |
* (reduce: string_type_declaration_init <- ASSIGN character_string)
|
|
640 |
* (reduce: string_type_declaration <- identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init )
|
|
641 |
* (reduce: type_declaration <- string_type_declaration)
|
|
642 |
*
|
|
643 |
*
|
|
644 |
* At first glance it seems that removing route 1 would make
|
|
645 |
* the most sense. Unfortunately the construct 'simple_spec_init'
|
|
646 |
* shows up multiple times in other rules, so changing this construct
|
|
647 |
* would also mean changing all the rules in which it appears.
|
|
648 |
* I (Mario) therefore chose to remove route 2 instead. This means
|
|
649 |
* that the above declaration gets stored in a
|
|
650 |
* simple_type_declaration_c, and not in a string_type_declaration_c
|
|
651 |
* as would be expected!
|
|
652 |
*/
|
|
653 |
/* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
|
|
654 |
SYM_REF4(string_type_declaration_c, string_type_name,
|
|
655 |
elementary_string_type_name,
|
|
656 |
string_type_declaration_size,
|
|
657 |
string_type_declaration_init) /* may be == NULL! */
|
|
658 |
#endif
|
|
659 |
|
|
660 |
/*********************/
|
|
661 |
/* B 1.4 - Variables */
|
|
662 |
/*********************/
|
|
663 |
/* done in base class(es) */
|
|
664 |
|
|
665 |
/********************************************/
|
|
666 |
/* B.1.4.1 Directly Represented Variables */
|
|
667 |
/********************************************/
|
|
668 |
// direct_variable: direct_variable_token {$$ = new direct_variable_c($1);};
|
|
669 |
void *visit(direct_variable_c *symbol) {
|
|
670 |
TRACE("direct_variable_c");
|
|
671 |
/* Do not use print_token() as it will change everything into uppercase */
|
|
672 |
if (strlen(symbol->value) == 0) ERROR;
|
|
673 |
return s4o.printlocation(symbol->value + 1);
|
|
674 |
}
|
|
675 |
|
|
676 |
|
|
677 |
/*************************************/
|
|
678 |
/* B.1.4.2 Multi-element Variables */
|
|
679 |
/*************************************/
|
|
680 |
/* done in base class(es) */
|
|
681 |
|
|
682 |
/******************************************/
|
|
683 |
/* B 1.4.3 - Declaration & Initialisation */
|
|
684 |
/******************************************/
|
|
685 |
/* leave for derived classes... */
|
|
686 |
|
|
687 |
/**************************************/
|
|
688 |
/* B.1.5 - Program organization units */
|
|
689 |
/**************************************/
|
|
690 |
/***********************/
|
|
691 |
/* B 1.5.1 - Functions */
|
|
692 |
/***********************/
|
|
693 |
/* leave for derived classes... */
|
|
694 |
|
|
695 |
/*****************************/
|
|
696 |
/* B 1.5.2 - Function Blocks */
|
|
697 |
/*****************************/
|
|
698 |
/* leave for derived classes... */
|
|
699 |
|
|
700 |
/**********************/
|
|
701 |
/* B 1.5.3 - Programs */
|
|
702 |
/**********************/
|
|
703 |
/* leave for derived classes... */
|
|
704 |
|
|
705 |
/*********************************************/
|
|
706 |
/* B.1.6 Sequential function chart elements */
|
|
707 |
/*********************************************/
|
|
708 |
|
|
709 |
/********************************/
|
|
710 |
/* B 1.7 Configuration elements */
|
|
711 |
/********************************/
|
|
712 |
/* leave for derived classes... */
|
|
713 |
|
|
714 |
/****************************************/
|
|
715 |
/* B.2 - Language IL (Instruction List) */
|
|
716 |
/****************************************/
|
|
717 |
/***********************************/
|
|
718 |
/* B 2.1 Instructions and Operands */
|
|
719 |
/***********************************/
|
|
720 |
/* leave for derived classes... */
|
|
721 |
|
|
722 |
/*******************/
|
|
723 |
/* B 2.2 Operators */
|
|
724 |
/*******************/
|
|
725 |
/* leave for derived classes... */
|
|
726 |
|
|
727 |
|
|
728 |
/***************************************/
|
|
729 |
/* B.3 - Language ST (Structured Text) */
|
|
730 |
/***************************************/
|
|
731 |
/***********************/
|
|
732 |
/* B 3.1 - Expressions */
|
|
733 |
/***********************/
|
|
734 |
/* leave for derived classes... */
|
|
735 |
|
|
736 |
/********************/
|
|
737 |
/* B 3.2 Statements */
|
|
738 |
/********************/
|
|
739 |
/* leave for derived classes... */
|
|
740 |
|
|
741 |
/*********************************/
|
|
742 |
/* B 3.2.1 Assignment Statements */
|
|
743 |
/*********************************/
|
|
744 |
/* leave for derived classes... */
|
|
745 |
|
|
746 |
/*****************************************/
|
|
747 |
/* B 3.2.2 Subprogram Control Statements */
|
|
748 |
/*****************************************/
|
|
749 |
/* leave for derived classes... */
|
|
750 |
|
|
751 |
/********************************/
|
|
752 |
/* B 3.2.3 Selection Statements */
|
|
753 |
/********************************/
|
|
754 |
/* leave for derived classes... */
|
|
755 |
|
|
756 |
/********************************/
|
|
757 |
/* B 3.2.4 Iteration Statements */
|
|
758 |
/********************************/
|
|
759 |
/* leave for derived classes... */
|
|
760 |
|
|
761 |
|
|
762 |
|
|
763 |
|
|
764 |
}; /* generate_c_typedecl_c */
|
|
765 |
|
|
766 |
|
|
767 |
|