author | mjsousa |
Sun, 15 Feb 2015 16:08:56 +0000 | |
changeset 995 | ce997a27c516 |
parent 971 | 8aee27d46208 |
child 1041 | 56ebe2a31b5b |
permissions | -rw-r--r-- |
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) |
279
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
265
diff
changeset
|
5 |
* Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant |
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
6 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
7 |
* 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
|
8 |
* 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
|
9 |
* 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
|
10 |
* (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
|
11 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
12 |
* 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
|
13 |
* 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
|
14 |
* 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
|
15 |
* 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
|
16 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
17 |
* 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
|
18 |
* 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
|
19 |
* |
181 | 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 |
/* |
|
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
26 |
* An IEC 61131-3 compiler. |
181 | 27 |
* |
28 |
* Based on the |
|
29 |
* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
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 |
/* Given a type definition declration, determine its default |
|
41 |
* initial value. Note that types based on other types |
|
42 |
* may have to iterate through each type it is based on |
|
43 |
* to determine the initial value. |
|
44 |
* E.g. |
|
45 |
* TYPE |
|
46 |
* A_t : INT := 10; |
|
47 |
* B_t : A_t := 20; |
|
48 |
* C_t : B_t; |
|
49 |
* D_t : C_t := 40; |
|
50 |
* END_TYPE |
|
51 |
* Where the default initial value for C_t is 20! |
|
52 |
*/ |
|
53 |
/* NOTE: The main program only needs one instance of |
|
54 |
* this class of object. This class |
|
55 |
* is therefore a singleton. |
|
56 |
*/ |
|
57 |
||
58 |
||
59 |
#include "absyntax_utils.hh" |
|
60 |
||
61 |
//#define DEBUG |
|
62 |
#ifdef DEBUG |
|
63 |
#define TRACE(classname) printf("\n____%s____\n",classname); |
|
64 |
#else |
|
65 |
#define TRACE(classname) |
|
66 |
#endif |
|
67 |
||
68 |
||
69 |
type_initial_value_c *type_initial_value_c::instance(void) { |
|
70 |
if (_instance != NULL) |
|
71 |
return _instance; |
|
72 |
||
73 |
_instance = new type_initial_value_c; |
|
74 |
||
925 | 75 |
null_literal = new ref_value_null_literal_c(); |
76 |
real_0 = new real_c("0"); |
|
77 |
integer_0 = new integer_c("0"); |
|
78 |
integer_1 = new integer_c("1"); |
|
79 |
bool_0 = new boolean_literal_c(new bool_type_name_c(),new boolean_false_c()); |
|
181 | 80 |
/* FIXME: Our current implementation only allows dates from 1970 onwards, |
81 |
* but the standard defines the date 0001-01-01 as the default value |
|
82 |
* for the DATE data type. Untill we fix our implementation, we use 1970-01-01 |
|
83 |
* as our default value!! |
|
84 |
*/ |
|
762 | 85 |
//date_literal_0 = new date_literal_c(integer_1, integer_1, integer_1); |
925 | 86 |
date_literal_0 = new date_literal_c(new integer_c("1970"), integer_1, integer_1); |
181 | 87 |
daytime_literal_0 = new daytime_c(integer_0, integer_0, real_0); |
925 | 88 |
time_0 = new duration_c (new time_type_name_c(), NULL, new interval_c(NULL, NULL, NULL, integer_0, NULL)); // T#0s |
89 |
date_0 = new date_c (new date_type_name_c(), date_literal_0); // D#0001-01-01 |
|
90 |
tod_0 = new time_of_day_c (new tod_type_name_c(), daytime_literal_0); // TOD#00:00:00 |
|
91 |
dt_0 = new date_and_time_c(new dt_type_name_c(), date_literal_0, daytime_literal_0); // DT#0001-01-01-00:00:00 |
|
92 |
string_0 = new single_byte_character_string_c("''"); |
|
93 |
wstring_0 = new double_byte_character_string_c("\"\""); |
|
181 | 94 |
|
95 |
return _instance; |
|
96 |
} |
|
97 |
||
98 |
type_initial_value_c::type_initial_value_c(void) {} |
|
99 |
||
100 |
||
101 |
||
762 | 102 |
symbol_c *type_initial_value_c::get(symbol_c *type) { |
181 | 103 |
TRACE("type_initial_value_c::get(): called "); |
762 | 104 |
return (symbol_c *)type->accept(*type_initial_value_c::instance()); |
181 | 105 |
} |
106 |
||
107 |
||
108 |
||
109 |
||
110 |
void *type_initial_value_c::handle_type_spec(symbol_c *base_type_name, symbol_c *type_spec_init) { |
|
111 |
if (type_spec_init != NULL) |
|
112 |
return type_spec_init; |
|
958
7474d2cd1d6e
Add a new pou_typename_c object to the AST to store references to previously declared Functions, FB, and Programs.
mjsousa
parents:
945
diff
changeset
|
113 |
/* no initial value specified, so we return the initial value of the type this type is based on... */ |
181 | 114 |
return base_type_name->accept(*this); |
115 |
} |
|
116 |
||
117 |
||
958
7474d2cd1d6e
Add a new pou_typename_c object to the AST to store references to previously declared Functions, FB, and Programs.
mjsousa
parents:
945
diff
changeset
|
118 |
void *type_initial_value_c::handle_type_name(symbol_c *type_name) { |
945
477393b00f95
Add support for relaxed datatype model for array datatypes.
mjsousa
parents:
925
diff
changeset
|
119 |
/* look up the type declaration... */ |
971
8aee27d46208
Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents:
958
diff
changeset
|
120 |
type_symtable_t::iterator iter = type_symtable.find(type_name); |
945
477393b00f95
Add support for relaxed datatype model for array datatypes.
mjsousa
parents:
925
diff
changeset
|
121 |
/* Type declaration not found!! */ |
958
7474d2cd1d6e
Add a new pou_typename_c object to the AST to store references to previously declared Functions, FB, and Programs.
mjsousa
parents:
945
diff
changeset
|
122 |
/* NOTE: Variables declared out of function block 'data types',for eg: VAR timer: TON; END_VAR |
7474d2cd1d6e
Add a new pou_typename_c object to the AST to store references to previously declared Functions, FB, and Programs.
mjsousa
parents:
945
diff
changeset
|
123 |
* do not have a default value, so (TON) will never be found in the type symbol table. This means |
7474d2cd1d6e
Add a new pou_typename_c object to the AST to store references to previously declared Functions, FB, and Programs.
mjsousa
parents:
945
diff
changeset
|
124 |
* we cannot simply consider this an error and abort, but must rather return a NULL. |
945
477393b00f95
Add support for relaxed datatype model for array datatypes.
mjsousa
parents:
925
diff
changeset
|
125 |
*/ |
971
8aee27d46208
Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents:
958
diff
changeset
|
126 |
if (iter == type_symtable.end()) return NULL; |
8aee27d46208
Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents:
958
diff
changeset
|
127 |
|
8aee27d46208
Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents:
958
diff
changeset
|
128 |
return iter->second->accept(*this); // iter->second is the type_decl |
945
477393b00f95
Add support for relaxed datatype model for array datatypes.
mjsousa
parents:
925
diff
changeset
|
129 |
} |
477393b00f95
Add support for relaxed datatype model for array datatypes.
mjsousa
parents:
925
diff
changeset
|
130 |
|
958
7474d2cd1d6e
Add a new pou_typename_c object to the AST to store references to previously declared Functions, FB, and Programs.
mjsousa
parents:
945
diff
changeset
|
131 |
/* visitor for identifier_c should no longer be necessary. All references to derived datatypes are now stored in then */ |
7474d2cd1d6e
Add a new pou_typename_c object to the AST to store references to previously declared Functions, FB, and Programs.
mjsousa
parents:
945
diff
changeset
|
132 |
/* AST using either poutype_identifier_c or derived_datatype_identifier_c. In principe, the following should not be necesasry */ |
7474d2cd1d6e
Add a new pou_typename_c object to the AST to store references to previously declared Functions, FB, and Programs.
mjsousa
parents:
945
diff
changeset
|
133 |
void *type_initial_value_c::visit( identifier_c *symbol) {return handle_type_name(symbol);} /* should never occur */ |
7474d2cd1d6e
Add a new pou_typename_c object to the AST to store references to previously declared Functions, FB, and Programs.
mjsousa
parents:
945
diff
changeset
|
134 |
void *type_initial_value_c::visit( poutype_identifier_c *symbol) {return handle_type_name(symbol);} /* in practice it might never get called, as FB, Functions and Programs do not have initial value */ |
7474d2cd1d6e
Add a new pou_typename_c object to the AST to store references to previously declared Functions, FB, and Programs.
mjsousa
parents:
945
diff
changeset
|
135 |
void *type_initial_value_c::visit(derived_datatype_identifier_c *symbol) {return handle_type_name(symbol);} |
181 | 136 |
|
137 |
/***********************************/ |
|
138 |
/* B 1.3.1 - Elementary Data Types */ |
|
139 |
/***********************************/ |
|
257 | 140 |
void *type_initial_value_c::visit(time_type_name_c *symbol) {return (void *)time_0;} |
141 |
void *type_initial_value_c::visit(bool_type_name_c *symbol) {return (void *)bool_0;} |
|
142 |
void *type_initial_value_c::visit(sint_type_name_c *symbol) {return (void *)integer_0;} |
|
143 |
void *type_initial_value_c::visit(int_type_name_c *symbol) {return (void *)integer_0;} |
|
144 |
void *type_initial_value_c::visit(dint_type_name_c *symbol) {return (void *)integer_0;} |
|
145 |
void *type_initial_value_c::visit(lint_type_name_c *symbol) {return (void *)integer_0;} |
|
146 |
void *type_initial_value_c::visit(usint_type_name_c *symbol) {return (void *)integer_0;} |
|
147 |
void *type_initial_value_c::visit(uint_type_name_c *symbol) {return (void *)integer_0;} |
|
148 |
void *type_initial_value_c::visit(udint_type_name_c *symbol) {return (void *)integer_0;} |
|
149 |
void *type_initial_value_c::visit(ulint_type_name_c *symbol) {return (void *)integer_0;} |
|
150 |
void *type_initial_value_c::visit(real_type_name_c *symbol) {return (void *)real_0;} |
|
151 |
void *type_initial_value_c::visit(lreal_type_name_c *symbol) {return (void *)real_0;} |
|
152 |
void *type_initial_value_c::visit(date_type_name_c *symbol) {return (void *)date_0;} |
|
153 |
void *type_initial_value_c::visit(tod_type_name_c *symbol) {return (void *)tod_0;} |
|
154 |
void *type_initial_value_c::visit(dt_type_name_c *symbol) {return (void *)dt_0;} |
|
155 |
void *type_initial_value_c::visit(byte_type_name_c *symbol) {return (void *)integer_0;} |
|
156 |
void *type_initial_value_c::visit(word_type_name_c *symbol) {return (void *)integer_0;} |
|
157 |
void *type_initial_value_c::visit(dword_type_name_c *symbol) {return (void *)integer_0;} |
|
158 |
void *type_initial_value_c::visit(lword_type_name_c *symbol) {return (void *)integer_0;} |
|
159 |
void *type_initial_value_c::visit(string_type_name_c *symbol) {return (void *)string_0;} |
|
160 |
void *type_initial_value_c::visit(wstring_type_name_c *symbol) {return (void *)wstring_0;} |
|
161 |
||
162 |
void *type_initial_value_c::visit(safetime_type_name_c *symbol) {return (void *)time_0;} |
|
163 |
void *type_initial_value_c::visit(safebool_type_name_c *symbol) {return (void *)bool_0;} |
|
164 |
void *type_initial_value_c::visit(safesint_type_name_c *symbol) {return (void *)integer_0;} |
|
165 |
void *type_initial_value_c::visit(safeint_type_name_c *symbol) {return (void *)integer_0;} |
|
166 |
void *type_initial_value_c::visit(safedint_type_name_c *symbol) {return (void *)integer_0;} |
|
167 |
void *type_initial_value_c::visit(safelint_type_name_c *symbol) {return (void *)integer_0;} |
|
168 |
void *type_initial_value_c::visit(safeusint_type_name_c *symbol) {return (void *)integer_0;} |
|
169 |
void *type_initial_value_c::visit(safeuint_type_name_c *symbol) {return (void *)integer_0;} |
|
170 |
void *type_initial_value_c::visit(safeudint_type_name_c *symbol) {return (void *)integer_0;} |
|
171 |
void *type_initial_value_c::visit(safeulint_type_name_c *symbol) {return (void *)integer_0;} |
|
172 |
void *type_initial_value_c::visit(safereal_type_name_c *symbol) {return (void *)real_0;} |
|
173 |
void *type_initial_value_c::visit(safelreal_type_name_c *symbol) {return (void *)real_0;} |
|
174 |
void *type_initial_value_c::visit(safedate_type_name_c *symbol) {return (void *)date_0;} |
|
175 |
void *type_initial_value_c::visit(safetod_type_name_c *symbol) {return (void *)tod_0;} |
|
176 |
void *type_initial_value_c::visit(safedt_type_name_c *symbol) {return (void *)dt_0;} |
|
177 |
void *type_initial_value_c::visit(safebyte_type_name_c *symbol) {return (void *)integer_0;} |
|
178 |
void *type_initial_value_c::visit(safeword_type_name_c *symbol) {return (void *)integer_0;} |
|
179 |
void *type_initial_value_c::visit(safedword_type_name_c *symbol) {return (void *)integer_0;} |
|
180 |
void *type_initial_value_c::visit(safelword_type_name_c *symbol) {return (void *)integer_0;} |
|
181 |
void *type_initial_value_c::visit(safestring_type_name_c *symbol) {return (void *)string_0;} |
|
182 |
void *type_initial_value_c::visit(safewstring_type_name_c *symbol) {return (void *)wstring_0;} |
|
181 | 183 |
|
184 |
/********************************/ |
|
185 |
/* B 1.3.3 - Derived data types */ |
|
186 |
/********************************/ |
|
187 |
/* simple_type_name ':' simple_spec_init */ |
|
188 |
void *type_initial_value_c::visit(simple_type_declaration_c *symbol) { |
|
189 |
return symbol->simple_spec_init->accept(*this); |
|
190 |
} |
|
191 |
/* simple_specification ASSIGN constant */ |
|
192 |
void *type_initial_value_c::visit(simple_spec_init_c *symbol) { |
|
193 |
return handle_type_spec(symbol->simple_specification, symbol->constant); |
|
194 |
} |
|
195 |
/* subrange_type_name ':' subrange_spec_init */ |
|
196 |
void *type_initial_value_c::visit(subrange_type_declaration_c *symbol) { |
|
197 |
return symbol->subrange_spec_init->accept(*this); |
|
198 |
} |
|
199 |
/* subrange_specification ASSIGN signed_integer */ |
|
200 |
void *type_initial_value_c::visit(subrange_spec_init_c *symbol) { |
|
201 |
return handle_type_spec(symbol->subrange_specification, symbol->signed_integer); |
|
202 |
} |
|
203 |
/* integer_type_name '(' subrange')' */ |
|
204 |
void *type_initial_value_c::visit(subrange_specification_c *symbol) { |
|
205 |
/* if no initial value explicitly given, then use the lowest value of the subrange */ |
|
206 |
if (symbol->subrange != NULL) |
|
207 |
return symbol->subrange->accept(*this); |
|
208 |
else |
|
209 |
return symbol->integer_type_name->accept(*this); |
|
210 |
} |
|
211 |
/* signed_integer DOTDOT signed_integer */ |
|
212 |
void *type_initial_value_c::visit(subrange_c *symbol) {return symbol->lower_limit;} |
|
213 |
/* enumerated_type_name ':' enumerated_spec_init */ |
|
214 |
void *type_initial_value_c::visit(enumerated_type_declaration_c *symbol) { |
|
215 |
return symbol->enumerated_spec_init->accept(*this); |
|
216 |
} |
|
217 |
/* enumerated_specification ASSIGN enumerated_value */ |
|
218 |
void *type_initial_value_c::visit(enumerated_spec_init_c *symbol) { |
|
219 |
return handle_type_spec(symbol->enumerated_specification, symbol->enumerated_value); |
|
220 |
} |
|
221 |
/* helper symbol for enumerated_specification->enumerated_spec_init */ |
|
222 |
/* enumerated_value_list ',' enumerated_value */ |
|
223 |
void *type_initial_value_c::visit(enumerated_value_list_c *symbol) { |
|
850
484beb3b1dcb
Add assertion suggested by Manuele.
Mario de Sousa <msousa@fe.up.pt>
parents:
762
diff
changeset
|
224 |
/* stage1_2 never creates an enumerated_value_list_c with no entries. If this occurs, then something must have changed! */ |
484beb3b1dcb
Add assertion suggested by Manuele.
Mario de Sousa <msousa@fe.up.pt>
parents:
762
diff
changeset
|
225 |
if (symbol->n <= 0) ERROR; |
181 | 226 |
/* if no initial value explicitly given, then use the lowest value of the subrange */ |
227 |
return (void *)symbol->elements[0]; |
|
228 |
} |
|
229 |
/* enumerated_type_name '#' identifier */ |
|
230 |
// SYM_REF2(enumerated_value_c, type, value) |
|
231 |
void *type_initial_value_c::visit(enumerated_value_c *symbol) {ERROR; return NULL;} |
|
232 |
/* identifier ':' array_spec_init */ |
|
233 |
void *type_initial_value_c::visit(array_type_declaration_c *symbol) { |
|
234 |
return symbol->array_spec_init->accept(*this); |
|
235 |
} |
|
236 |
/* array_specification [ASSIGN array_initialization} */ |
|
237 |
/* array_initialization may be NULL ! */ |
|
238 |
void *type_initial_value_c::visit(array_spec_init_c *symbol) { |
|
239 |
return handle_type_spec(symbol->array_specification, symbol->array_initialization); |
|
240 |
} |
|
241 |
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
|
242 |
void *type_initial_value_c::visit(array_specification_c *symbol) { |
|
243 |
//symbol_c *init_value = (symbol_c *)symbol->non_generic_type_name->accept(*this); |
|
244 |
||
245 |
/* Now build a array_initial_elements_list_c list, and populate it |
|
246 |
* with 1 element of the array_initial_elements_c class |
|
247 |
*/ |
|
248 |
/* The array_initial_elements_c will contain a reference to the init_value, |
|
249 |
* and another constant representing the number of elements in the array. |
|
250 |
* In essence, we are building the equivilant of the following ST/IL code: |
|
251 |
* New_array_t : ARRAY [1..30, 51..60] of INT := [40(XXX)]; |
|
252 |
* from the user given code |
|
253 |
* New_array_t : ARRAY [1..30, 51..60] of INT; |
|
254 |
* and replacing XXX with the default initial value of INT. |
|
255 |
*/ |
|
256 |
/* now we need to determine the number of elements in the array... */ |
|
257 |
/* Easier said than done, as the array may have a list of subranges, as in the |
|
258 |
* example given above!! |
|
259 |
*/ |
|
260 |
/* TODO !!!!!*/ |
|
261 |
/* For now, just assume an array with 1 element. |
|
262 |
* I (Mario) want to finish off this part of the code before getting boged down |
|
263 |
* in something else... |
|
264 |
*/ |
|
265 |
// NOTE: We are leaking memory, as the integer will never get free'd!! |
|
266 |
//integer_c *integer = new integer_c("1"); |
|
267 |
// NOTE: We are leaking memory, as the array_initial_elements will never get free'd!! |
|
268 |
//array_initial_elements_c *array_initial_elements = new array_initial_elements_c(integer, init_value); |
|
269 |
// NOTE: We are leaking memory, as the array_initial_elements_list will never get free'd!! |
|
270 |
array_initial_elements_list_c *array_initial_elements_list = new array_initial_elements_list_c(); |
|
271 |
//array_initial_elements_list->add_element(array_initial_elements); |
|
272 |
return array_initial_elements_list; |
|
273 |
} |
|
274 |
/* helper symbol for array_specification */ |
|
275 |
/* array_subrange_list ',' subrange */ |
|
276 |
void *type_initial_value_c::visit(array_subrange_list_c *symbol) {ERROR; return NULL;} |
|
277 |
/* array_initialization: '[' array_initial_elements_list ']' */ |
|
278 |
/* helper symbol for array_initialization */ |
|
279 |
/* array_initial_elements_list ',' array_initial_elements */ |
|
280 |
void *type_initial_value_c::visit(array_initial_elements_list_c *symbol) {ERROR; return NULL;} |
|
281 |
/* integer '(' [array_initial_element] ')' */ |
|
282 |
/* array_initial_element may be NULL ! */ |
|
283 |
void *type_initial_value_c::visit(array_initial_elements_c *symbol) {ERROR; return NULL;} |
|
284 |
||
285 |
||
286 |
||
287 |
/* TODO: from this point forward... */ |
|
288 |
||
289 |
/* structure_type_name ':' structure_specification */ |
|
290 |
void *type_initial_value_c::visit(structure_type_declaration_c *symbol) {return NULL;} |
|
291 |
/* structure_type_name ASSIGN structure_initialization */ |
|
292 |
/* structure_initialization may be NULL ! */ |
|
293 |
void *type_initial_value_c::visit(initialized_structure_c *symbol) { |
|
294 |
return handle_type_spec(symbol->structure_type_name, symbol->structure_initialization); |
|
295 |
} |
|
296 |
/* helper symbol for structure_declaration */ |
|
297 |
/* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ |
|
298 |
/* structure_element_declaration_list structure_element_declaration ';' */ |
|
299 |
void *type_initial_value_c::visit(structure_element_declaration_list_c *symbol) { |
|
300 |
structure_element_initialization_list_c *structure_element_initialization_list = new structure_element_initialization_list_c(); |
|
301 |
return structure_element_initialization_list; |
|
302 |
} |
|
303 |
/* structure_element_name ':' *_spec_init */ |
|
304 |
void *type_initial_value_c::visit(structure_element_declaration_c *symbol) {return NULL;} |
|
305 |
/* helper symbol for structure_initialization */ |
|
306 |
/* structure_initialization: '(' structure_element_initialization_list ')' */ |
|
307 |
/* structure_element_initialization_list ',' structure_element_initialization */ |
|
308 |
void *type_initial_value_c::visit(structure_element_initialization_list_c *symbol) {return NULL;} |
|
309 |
/* structure_element_name ASSIGN value */ |
|
310 |
void *type_initial_value_c::visit(structure_element_initialization_c *symbol) {return NULL;} |
|
311 |
/* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ |
|
312 |
/* |
|
313 |
* NOTE: |
|
314 |
* (Summary: Contrary to what is expected, the |
|
315 |
* string_type_declaration_c is not used to store |
|
316 |
* simple string type declarations that do not include |
|
317 |
* size limits. |
|
318 |
* For e.g.: |
|
319 |
* str1_type: STRING := "hello!" |
|
320 |
* will be stored in a simple_type_declaration_c |
|
321 |
* instead of a string_type_declaration_c. |
|
322 |
* The following: |
|
323 |
* str2_type: STRING [64] := "hello!" |
|
324 |
* will be stored in a sring_type_declaration_c |
|
325 |
* |
|
326 |
* Read on for why this is done... |
|
327 |
* End Summary) |
|
328 |
* |
|
329 |
* According to the spec, the valid construct |
|
330 |
* TYPE new_str_type : STRING := "hello!"; END_TYPE |
|
331 |
* has two possible routes to type_declaration... |
|
332 |
* |
|
333 |
* Route 1: |
|
334 |
* type_declaration: single_element_type_declaration |
|
335 |
* single_element_type_declaration: simple_type_declaration |
|
336 |
* simple_type_declaration: identifier ':' simple_spec_init |
|
337 |
* simple_spec_init: simple_specification ASSIGN constant |
|
338 |
* (shift: identifier <- 'new_str_type') |
|
339 |
* simple_specification: elementary_type_name |
|
340 |
* elementary_type_name: STRING |
|
341 |
* (shift: elementary_type_name <- STRING) |
|
342 |
* (reduce: simple_specification <- elementary_type_name) |
|
343 |
* (shift: constant <- "hello!") |
|
344 |
* (reduce: simple_spec_init: simple_specification ASSIGN constant) |
|
345 |
* (reduce: ...) |
|
346 |
* |
|
347 |
* |
|
348 |
* Route 2: |
|
349 |
* type_declaration: string_type_declaration |
|
350 |
* string_type_declaration: identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init |
|
351 |
* (shift: identifier <- 'new_str_type') |
|
352 |
* elementary_string_type_name: STRING |
|
353 |
* (shift: elementary_string_type_name <- STRING) |
|
354 |
* (shift: string_type_declaration_size <- empty ) |
|
355 |
* string_type_declaration_init: ASSIGN character_string |
|
356 |
* (shift: character_string <- "hello!") |
|
357 |
* (reduce: string_type_declaration_init <- ASSIGN character_string) |
|
358 |
* (reduce: string_type_declaration <- identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init ) |
|
359 |
* (reduce: type_declaration <- string_type_declaration) |
|
360 |
* |
|
361 |
* |
|
362 |
* At first glance it seems that removing route 1 would make |
|
363 |
* the most sense. Unfortunately the construct 'simple_spec_init' |
|
364 |
* shows up multiple times in other rules, so changing this construct |
|
365 |
* would also mean changing all the rules in which it appears. |
|
366 |
* I (Mario) therefore chose to remove route 2 instead. This means |
|
367 |
* that the above declaration gets stored in a |
|
368 |
* simple_type_declaration_c, and not in a string_type_declaration_c |
|
369 |
* as would be expected! |
|
370 |
*/ |
|
371 |
/* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ |
|
433
1355adcdad58
Add, to types symbol table, string datatypes with limited length (my_string_type: STRING[33]) (Thanks Andreas!)
Mario de Sousa <msousa@fe.up.pt>
parents:
432
diff
changeset
|
372 |
// SYM_REF4(string_type_declaration_c, string_type_name, |
1355adcdad58
Add, to types symbol table, string datatypes with limited length (my_string_type: STRING[33]) (Thanks Andreas!)
Mario de Sousa <msousa@fe.up.pt>
parents:
432
diff
changeset
|
373 |
// elementary_string_type_name, |
1355adcdad58
Add, to types symbol table, string datatypes with limited length (my_string_type: STRING[33]) (Thanks Andreas!)
Mario de Sousa <msousa@fe.up.pt>
parents:
432
diff
changeset
|
374 |
// string_type_declaration_size, |
1355adcdad58
Add, to types symbol table, string datatypes with limited length (my_string_type: STRING[33]) (Thanks Andreas!)
Mario de Sousa <msousa@fe.up.pt>
parents:
432
diff
changeset
|
375 |
// string_type_declaration_init) /* may be == NULL! */ |
432
9c5ad4be30fd
Correctly return the initial value of a string datatype, with limited length (STRING[33] := 'hello')
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
376 |
void *type_initial_value_c::visit(string_type_declaration_c *symbol) { |
9c5ad4be30fd
Correctly return the initial value of a string datatype, with limited length (STRING[33] := 'hello')
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
377 |
return handle_type_spec(symbol->elementary_string_type_name, symbol->string_type_declaration_init); |
9c5ad4be30fd
Correctly return the initial value of a string datatype, with limited length (STRING[33] := 'hello')
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
378 |
} |
181 | 379 |
|
380 |
||
925 | 381 |
/* REF_TO (non_generic_type_name | function_block_type_name) */ |
382 |
void *type_initial_value_c::visit(ref_spec_c *symbol) { |
|
383 |
return null_literal; |
|
384 |
} |
|
385 |
||
386 |
||
387 |
/* ref_spec [ ASSIGN ref_initialization ]; */ |
|
388 |
/* NOTE: ref_initialization may be NULL!! */ |
|
389 |
void *type_initial_value_c::visit(ref_spec_init_c *symbol) { |
|
390 |
return handle_type_spec(symbol->ref_spec, symbol->ref_initialization); |
|
391 |
} |
|
392 |
/* identifier ':' ref_spec_init */ |
|
393 |
void *type_initial_value_c::visit(ref_type_decl_c *symbol) { |
|
394 |
return symbol->ref_spec_init->accept(*this); |
|
395 |
} |
|
396 |
||
397 |
||
398 |
||
399 |
||
400 |
||
401 |
||
402 |
||
403 |
||
404 |
type_initial_value_c *type_initial_value_c::_instance = NULL; |
|
405 |
ref_value_null_literal_c *type_initial_value_c::null_literal = NULL; |
|
406 |
real_c *type_initial_value_c::real_0 = NULL; |
|
407 |
integer_c *type_initial_value_c::integer_0 = NULL; |
|
408 |
integer_c *type_initial_value_c::integer_1 = NULL; |
|
409 |
boolean_literal_c *type_initial_value_c::bool_0 = NULL; |
|
410 |
date_literal_c *type_initial_value_c::date_literal_0 = NULL; |
|
411 |
daytime_c *type_initial_value_c::daytime_literal_0 = NULL; |
|
412 |
duration_c *type_initial_value_c::time_0 = NULL; |
|
413 |
date_c *type_initial_value_c::date_0 = NULL; |
|
414 |
time_of_day_c *type_initial_value_c::tod_0 = NULL; |
|
415 |
date_and_time_c *type_initial_value_c::dt_0 = NULL; |
|
416 |
single_byte_character_string_c *type_initial_value_c::string_0 = NULL; |
|
417 |
double_byte_character_string_c *type_initial_value_c::wstring_0 = NULL; |