|
1 /* |
|
2 * matiec - a compiler for the programming languages defined in IEC 61131-3 |
|
3 * |
|
4 * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) |
|
5 * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant |
|
6 * |
|
7 * This program is free software: you can redistribute it and/or modify |
|
8 * it under the terms of the GNU General Public License as published by |
|
9 * the Free Software Foundation, either version 3 of the License, or |
|
10 * (at your option) any later version. |
|
11 * |
|
12 * This program is distributed in the hope that it will be useful, |
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 * GNU General Public License for more details. |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License |
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19 * |
|
20 * |
|
21 * This code is made available on the understanding that it will not be |
|
22 * used in safety-critical situations without a full and competent review. |
|
23 */ |
|
24 |
|
25 /* |
|
26 * An IEC 61131-3 compiler. |
|
27 * |
|
28 * Based on the |
|
29 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
30 * |
|
31 */ |
|
32 |
|
33 |
|
34 /* |
|
35 * Array dimension iterator. |
|
36 * Iterate through the dimensions of array specification. |
|
37 * |
|
38 * This is part of the 4th stage that generates |
|
39 * a c++ source program equivalent to the IL and ST |
|
40 * code. |
|
41 */ |
|
42 |
|
43 /* Given a array_specification_c, iterate through |
|
44 * each subrange, returning the symbol of each subrange |
|
45 * ...array_dimension_iterator_c |
|
46 */ |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 #include "array_dimension_iterator.hh" |
|
52 |
|
53 |
|
54 //#define DEBUG |
|
55 #ifdef DEBUG |
|
56 #define TRACE(classname) printf("\n____%s____\n",classname); |
|
57 #else |
|
58 #define TRACE(classname) |
|
59 #endif |
|
60 |
|
61 |
|
62 #define ERROR error_exit(__FILE__,__LINE__) |
|
63 /* function defined in main.cc */ |
|
64 extern void error_exit(const char *file_name, int line_no); |
|
65 |
|
66 void* array_dimension_iterator_c::iterate_list(list_c *list) { |
|
67 void *res; |
|
68 for (int i = 0; i < list->n; i++) { |
|
69 res = list->elements[i]->accept(*this); |
|
70 if (res != NULL) |
|
71 return res; |
|
72 } |
|
73 return NULL; |
|
74 } |
|
75 |
|
76 /* start off at the first case element once again... */ |
|
77 void array_dimension_iterator_c::reset(void) { |
|
78 current_array_dimension = NULL; |
|
79 } |
|
80 |
|
81 |
|
82 /* initialize the iterator object. |
|
83 * We must be given a reference to a array_specification_c that will be analyzed... |
|
84 */ |
|
85 array_dimension_iterator_c::array_dimension_iterator_c(symbol_c *symbol) { |
|
86 /* do some consistency check... */ |
|
87 array_specification_c* array_spec = dynamic_cast<array_specification_c*>(symbol); |
|
88 |
|
89 if (NULL == array_spec) ERROR; |
|
90 |
|
91 /* OK. Now initialize this object... */ |
|
92 this->array_specification = symbol; |
|
93 reset(); |
|
94 } |
|
95 |
|
96 |
|
97 |
|
98 /* Skip to the next subrange. After object creation, |
|
99 * the object references on subrange _before_ the first, so |
|
100 * this function must be called once to get the object to |
|
101 * reference the first subrange... |
|
102 * |
|
103 * Returns the subrange symbol! |
|
104 */ |
|
105 symbol_c *array_dimension_iterator_c::next(void) { |
|
106 void *res = array_specification->accept(*this); |
|
107 if (res == NULL) |
|
108 return NULL; |
|
109 |
|
110 return current_array_dimension; |
|
111 } |
|
112 |
|
113 /********************************/ |
|
114 /* B 1.3.3 - Derived data types */ |
|
115 /********************************/ |
|
116 /* signed_integer DOTDOT signed_integer */ |
|
117 void *array_dimension_iterator_c::visit(subrange_c *symbol) { |
|
118 if (current_array_dimension == symbol) { |
|
119 current_array_dimension = NULL; |
|
120 } |
|
121 else if (current_array_dimension == NULL) { |
|
122 current_array_dimension = symbol; |
|
123 return symbol; |
|
124 } |
|
125 |
|
126 /* Not found! */ |
|
127 return NULL; |
|
128 } |
|
129 |
|
130 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
|
131 void *array_dimension_iterator_c::visit(array_specification_c *symbol) { |
|
132 return symbol->array_subrange_list->accept(*this); |
|
133 } |
|
134 |
|
135 /* array_subrange_list ',' subrange */ |
|
136 void *array_dimension_iterator_c::visit(array_subrange_list_c *symbol) { |
|
137 return iterate_list(symbol); |
|
138 } |
|
139 |