author | Mario de Sousa <msousa@fe.up.pt> |
Mon, 04 Apr 2011 14:32:21 +0100 | |
changeset 274 | 8d36d1f81170 |
parent 265 | 4d222f46f8cc |
child 279 | c0453b7f99df |
permissions | -rwxr-xr-x |
181 | 1 |
/* |
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
2 |
* matiec - a compiler for the programming languages defined in IEC 61131-3 |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
3 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
4 |
* Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
5 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
6 |
* This program is free software: you can redistribute it and/or modify |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
7 |
* it under the terms of the GNU General Public License as published by |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
8 |
* the Free Software Foundation, either version 3 of the License, or |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
9 |
* (at your option) any later version. |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
10 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
11 |
* This program is distributed in the hope that it will be useful, |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
14 |
* GNU General Public License for more details. |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
15 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
16 |
* You should have received a copy of the GNU General Public License |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
18 |
* |
181 | 19 |
* |
20 |
* This code is made available on the understanding that it will not be |
|
21 |
* used in safety-critical situations without a full and competent review. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
25 |
* An IEC 61131-3 compiler. |
181 | 26 |
* |
27 |
* Based on the |
|
28 |
* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
29 |
* |
|
30 |
*/ |
|
31 |
||
32 |
||
33 |
/* |
|
34 |
* Determine the default initial value of a type declaration. |
|
35 |
* |
|
36 |
* This is part of the 4th stage that generates |
|
37 |
* a c++ source program equivalent to the IL and ST |
|
38 |
* code. |
|
39 |
*/ |
|
40 |
||
41 |
/* Given a type definition declration, determine its default |
|
42 |
* initial value. Note that types based on other types |
|
43 |
* may have to iterate through each type it is based on |
|
44 |
* to determine the initial value. |
|
45 |
* E.g. |
|
46 |
* TYPE |
|
47 |
* A_t : INT := 10; |
|
48 |
* B_t : A_t := 20; |
|
49 |
* C_t : B_t; |
|
50 |
* D_t : C_t := 40; |
|
51 |
* END_TYPE |
|
52 |
* Where the default initial value for C_t is 20! |
|
53 |
*/ |
|
54 |
/* NOTE: The main program only needs one instance of |
|
55 |
* this class of object. This class |
|
56 |
* is therefore a singleton. |
|
57 |
*/ |
|
58 |
||
59 |
class type_initial_value_c : public null_visitor_c { |
|
60 |
||
61 |
private: |
|
62 |
static type_initial_value_c *_instance; |
|
63 |
/* constants for the default values of elementary data types... */ |
|
64 |
static real_c *real_0; |
|
65 |
static integer_c *integer_0, *integer_1; |
|
66 |
static boolean_literal_c *bool_0; |
|
67 |
static date_literal_c *date_literal_0; |
|
68 |
static daytime_c *daytime_literal_0; |
|
69 |
static duration_c *time_0; |
|
70 |
static date_c *date_0; |
|
71 |
static time_of_day_c *tod_0; |
|
72 |
static date_and_time_c *dt_0; |
|
73 |
static single_byte_character_string_c *string_0; |
|
74 |
static double_byte_character_string_c *wstring_0; |
|
75 |
||
76 |
public: |
|
77 |
static type_initial_value_c *instance(void); |
|
78 |
||
79 |
protected: |
|
80 |
type_initial_value_c(void); |
|
81 |
||
82 |
public: |
|
83 |
symbol_c *get(identifier_c *type_name); |
|
84 |
||
85 |
||
86 |
private: |
|
87 |
void *handle_type_spec(symbol_c *base_type_name, symbol_c *type_spec_init); |
|
88 |
||
89 |
private: |
|
90 |
void *visit(identifier_c *type_name); |
|
91 |
||
92 |
/***********************************/ |
|
93 |
/* B 1.3.1 - Elementary Data Types */ |
|
94 |
/***********************************/ |
|
95 |
void *visit(time_type_name_c *symbol); |
|
96 |
void *visit(bool_type_name_c *symbol); |
|
97 |
void *visit(sint_type_name_c *symbol); |
|
98 |
void *visit(int_type_name_c *symbol); |
|
99 |
void *visit(dint_type_name_c *symbol); |
|
100 |
void *visit(lint_type_name_c *symbol); |
|
101 |
void *visit(usint_type_name_c *symbol); |
|
102 |
void *visit(uint_type_name_c *symbol); |
|
103 |
void *visit(udint_type_name_c *symbol); |
|
104 |
void *visit(ulint_type_name_c *symbol); |
|
105 |
void *visit(real_type_name_c *symbol); |
|
106 |
void *visit(lreal_type_name_c *symbol); |
|
107 |
void *visit(date_type_name_c *symbol); |
|
108 |
void *visit(tod_type_name_c *symbol); |
|
109 |
void *visit(dt_type_name_c *symbol); |
|
110 |
void *visit(byte_type_name_c *symbol); |
|
111 |
void *visit(word_type_name_c *symbol); |
|
112 |
void *visit(dword_type_name_c *symbol); |
|
113 |
void *visit(lword_type_name_c *symbol); |
|
114 |
void *visit(string_type_name_c *symbol); |
|
115 |
void *visit(wstring_type_name_c *symbol); |
|
116 |
||
257 | 117 |
void *visit(safetime_type_name_c *symbol); |
118 |
void *visit(safebool_type_name_c *symbol); |
|
119 |
void *visit(safesint_type_name_c *symbol); |
|
120 |
void *visit(safeint_type_name_c *symbol); |
|
121 |
void *visit(safedint_type_name_c *symbol); |
|
122 |
void *visit(safelint_type_name_c *symbol); |
|
123 |
void *visit(safeusint_type_name_c *symbol); |
|
124 |
void *visit(safeuint_type_name_c *symbol); |
|
125 |
void *visit(safeudint_type_name_c *symbol); |
|
126 |
void *visit(safeulint_type_name_c *symbol); |
|
127 |
void *visit(safereal_type_name_c *symbol); |
|
128 |
void *visit(safelreal_type_name_c *symbol); |
|
129 |
void *visit(safedate_type_name_c *symbol); |
|
130 |
void *visit(safetod_type_name_c *symbol); |
|
131 |
void *visit(safedt_type_name_c *symbol); |
|
132 |
void *visit(safebyte_type_name_c *symbol); |
|
133 |
void *visit(safeword_type_name_c *symbol); |
|
134 |
void *visit(safedword_type_name_c *symbol); |
|
135 |
void *visit(safelword_type_name_c *symbol); |
|
136 |
void *visit(safestring_type_name_c *symbol); |
|
137 |
void *visit(safewstring_type_name_c *symbol); |
|
138 |
||
181 | 139 |
/********************************/ |
140 |
/* B 1.3.3 - Derived data types */ |
|
141 |
/********************************/ |
|
142 |
/* simple_type_name ':' simple_spec_init */ |
|
143 |
void *visit(simple_type_declaration_c *symbol); |
|
144 |
||
145 |
/* simple_specification ASSIGN constant */ |
|
146 |
void *visit(simple_spec_init_c *symbol); |
|
147 |
||
148 |
/* subrange_type_name ':' subrange_spec_init */ |
|
149 |
void *visit(subrange_type_declaration_c *symbol); |
|
150 |
||
151 |
/* subrange_specification ASSIGN signed_integer */ |
|
152 |
void *visit(subrange_spec_init_c *symbol); |
|
153 |
||
154 |
/* integer_type_name '(' subrange')' */ |
|
155 |
void *visit(subrange_specification_c *symbol); |
|
156 |
||
157 |
/* signed_integer DOTDOT signed_integer */ |
|
158 |
void *visit(subrange_c *symbol); |
|
159 |
||
160 |
/* enumerated_type_name ':' enumerated_spec_init */ |
|
161 |
void *visit(enumerated_type_declaration_c *symbol); |
|
162 |
||
163 |
/* enumerated_specification ASSIGN enumerated_value */ |
|
164 |
void *visit(enumerated_spec_init_c *symbol); |
|
165 |
||
166 |
/* helper symbol for enumerated_specification->enumerated_spec_init */ |
|
167 |
/* enumerated_value_list ',' enumerated_value */ |
|
168 |
void *visit(enumerated_value_list_c *symbol); |
|
169 |
||
170 |
/* enumerated_type_name '#' identifier */ |
|
171 |
// SYM_REF2(enumerated_value_c, type, value) |
|
172 |
void *visit(enumerated_value_c *symbol); |
|
173 |
||
174 |
/* identifier ':' array_spec_init */ |
|
175 |
void *visit(array_type_declaration_c *symbol); |
|
176 |
||
177 |
/* array_specification [ASSIGN array_initialization} */ |
|
178 |
/* array_initialization may be NULL ! */ |
|
179 |
void *visit(array_spec_init_c *symbol); |
|
180 |
||
181 |
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
|
182 |
void *visit(array_specification_c *symbol); |
|
183 |
||
184 |
/* helper symbol for array_specification */ |
|
185 |
/* array_subrange_list ',' subrange */ |
|
186 |
void *visit(array_subrange_list_c *symbol); |
|
187 |
||
188 |
/* array_initialization: '[' array_initial_elements_list ']' */ |
|
189 |
/* helper symbol for array_initialization */ |
|
190 |
/* array_initial_elements_list ',' array_initial_elements */ |
|
191 |
void *visit(array_initial_elements_list_c *symbol); |
|
192 |
||
193 |
/* integer '(' [array_initial_element] ')' */ |
|
194 |
/* array_initial_element may be NULL ! */ |
|
195 |
void *visit(array_initial_elements_c *symbol); |
|
196 |
||
197 |
/* structure_type_name ':' structure_specification */ |
|
198 |
void *visit(structure_type_declaration_c *symbol); |
|
199 |
||
200 |
/* structure_type_name ASSIGN structure_initialization */ |
|
201 |
/* structure_initialization may be NULL ! */ |
|
202 |
void *visit(initialized_structure_c *symbol); |
|
203 |
||
204 |
/* helper symbol for structure_declaration */ |
|
205 |
/* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ |
|
206 |
/* structure_element_declaration_list structure_element_declaration ';' */ |
|
207 |
void *visit(structure_element_declaration_list_c *symbol); |
|
208 |
||
209 |
/* structure_element_name ':' *_spec_init */ |
|
210 |
void *visit(structure_element_declaration_c *symbol); |
|
211 |
||
212 |
/* helper symbol for structure_initialization */ |
|
213 |
/* structure_initialization: '(' structure_element_initialization_list ')' */ |
|
214 |
/* structure_element_initialization_list ',' structure_element_initialization */ |
|
215 |
void *visit(structure_element_initialization_list_c *symbol); |
|
216 |
||
217 |
/* structure_element_name ASSIGN value */ |
|
218 |
void *visit(structure_element_initialization_c *symbol); |
|
219 |
||
220 |
/* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ |
|
221 |
/* |
|
222 |
* NOTE: |
|
223 |
* (Summary: Contrary to what is expected, the |
|
224 |
* string_type_declaration_c is not used to store |
|
225 |
* simple string type declarations that do not include |
|
226 |
* size limits. |
|
227 |
* For e.g.: |
|
228 |
* str1_type: STRING := "hello!" |
|
229 |
* will be stored in a simple_type_declaration_c |
|
230 |
* instead of a string_type_declaration_c. |
|
231 |
* The following: |
|
232 |
* str2_type: STRING [64] := "hello!" |
|
233 |
* will be stored in a sring_type_declaration_c |
|
234 |
* |
|
235 |
* Read on for why this is done... |
|
236 |
* End Summary) |
|
237 |
* |
|
238 |
* According to the spec, the valid construct |
|
239 |
* TYPE new_str_type : STRING := "hello!"; END_TYPE |
|
240 |
* has two possible routes to type_declaration... |
|
241 |
* |
|
242 |
* Route 1: |
|
243 |
* type_declaration: single_element_type_declaration |
|
244 |
* single_element_type_declaration: simple_type_declaration |
|
245 |
* simple_type_declaration: identifier ':' simple_spec_init |
|
246 |
* simple_spec_init: simple_specification ASSIGN constant |
|
247 |
* (shift: identifier <- 'new_str_type') |
|
248 |
* simple_specification: elementary_type_name |
|
249 |
* elementary_type_name: STRING |
|
250 |
* (shift: elementary_type_name <- STRING) |
|
251 |
* (reduce: simple_specification <- elementary_type_name) |
|
252 |
* (shift: constant <- "hello!") |
|
253 |
* (reduce: simple_spec_init: simple_specification ASSIGN constant) |
|
254 |
* (reduce: ...) |
|
255 |
* |
|
256 |
* |
|
257 |
* Route 2: |
|
258 |
* type_declaration: string_type_declaration |
|
259 |
* string_type_declaration: identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init |
|
260 |
* (shift: identifier <- 'new_str_type') |
|
261 |
* elementary_string_type_name: STRING |
|
262 |
* (shift: elementary_string_type_name <- STRING) |
|
263 |
* (shift: string_type_declaration_size <- empty ) |
|
264 |
* string_type_declaration_init: ASSIGN character_string |
|
265 |
* (shift: character_string <- "hello!") |
|
266 |
* (reduce: string_type_declaration_init <- ASSIGN character_string) |
|
267 |
* (reduce: string_type_declaration <- identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init ) |
|
268 |
* (reduce: type_declaration <- string_type_declaration) |
|
269 |
* |
|
270 |
* |
|
271 |
* At first glance it seems that removing route 1 would make |
|
272 |
* the most sense. Unfortunately the construct 'simple_spec_init' |
|
273 |
* shows up multiple times in other rules, so changing this construct |
|
274 |
* would also mean changing all the rules in which it appears. |
|
275 |
* I (Mario) therefore chose to remove route 2 instead. This means |
|
276 |
* that the above declaration gets stored in a |
|
277 |
* simple_type_declaration_c, and not in a string_type_declaration_c |
|
278 |
* as would be expected! |
|
279 |
*/ |
|
280 |
/* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ |
|
281 |
#if 0 |
|
282 |
SYM_REF4(string_type_declaration_c, string_type_name, |
|
283 |
elementary_string_type_name, |
|
284 |
string_type_declaration_size, |
|
285 |
string_type_declaration_init) /* may be == NULL! */ |
|
286 |
#endif |
|
287 |
void *visit(string_type_declaration_c *symbol); |
|
288 |
}; // type_initial_value_c |
|
289 |
||
290 |
||
291 |