author | Mario de Sousa <msousa@fe.up.pt> |
Fri, 17 Aug 2012 19:03:13 +0100 | |
changeset 624 | c2546c6e0cfa |
parent 596 | 4efb11e44065 |
child 719 | 19595fce59f0 |
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:
181
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:
181
diff
changeset
|
3 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
4 |
* Copyright (C) 2009-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:
181
diff
changeset
|
6 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
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:
181
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:
181
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:
181
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:
181
diff
changeset
|
11 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
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:
181
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:
181
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:
181
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:
181
diff
changeset
|
16 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
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:
181
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:
181
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:
181
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 |
/* |
|
35 |
* This is the main stage 3a file. |
|
36 |
* |
|
37 |
* In stage 3a some helpful symbol tables are instanciated and populated. |
|
38 |
* These symbol tables wll then be used by stage3b and atage4 code generators. |
|
39 |
*/ |
|
40 |
||
41 |
||
42 |
||
43 |
||
44 |
||
45 |
||
46 |
// #include <stdio.h> /* required for NULL */ |
|
47 |
#include <string> |
|
48 |
#include <iostream> |
|
49 |
#include <sstream> |
|
50 |
#include <typeinfo> |
|
51 |
#include <list> |
|
52 |
#include <strings.h> |
|
596
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
591
diff
changeset
|
53 |
// #include <string.h> /* required for strlen() */ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
591
diff
changeset
|
54 |
// #include <stdlib.h> /* required for atoi() */ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
591
diff
changeset
|
55 |
// #include <errno.h> /* required for errno */ |
181 | 56 |
|
57 |
#include "../util/symtable.hh" |
|
58 |
#include "../util/dsymtable.hh" |
|
59 |
#include "../absyntax/visitor.hh" |
|
596
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
591
diff
changeset
|
60 |
#include "../main.hh" // required for ERROR() and ERROR_MSG() macros. |
181 | 61 |
|
62 |
||
63 |
||
64 |
//#define DEBUG |
|
65 |
#ifdef DEBUG |
|
66 |
#define TRACE(classname) printf("\n____%s____\n",classname); |
|
67 |
#else |
|
68 |
#define TRACE(classname) |
|
69 |
#endif |
|
70 |
||
71 |
||
72 |
||
73 |
/***********************************************************************/ |
|
74 |
/***********************************************************************/ |
|
75 |
/***********************************************************************/ |
|
76 |
/***********************************************************************/ |
|
77 |
||
78 |
||
79 |
/* returns 0 if the names are equal!! */ |
|
80 |
/* NOTE: it must ignore case!! */ |
|
81 |
int compare_identifiers(symbol_c *ident1, symbol_c *ident2) { |
|
82 |
||
83 |
token_c *name1 = dynamic_cast<token_c *>(ident1); |
|
84 |
token_c *name2 = dynamic_cast<token_c *>(ident2); |
|
85 |
||
86 |
if ((name1 == NULL) || (name2 == NULL)) |
|
87 |
/* invalid identifiers... */ |
|
88 |
return -1; |
|
89 |
||
90 |
if (strcasecmp(name1->value, name2->value) == 0) |
|
91 |
return 0; |
|
92 |
||
93 |
/* identifiers do not match! */ |
|
94 |
return 1; |
|
95 |
} |
|
96 |
||
97 |
||
589
de4c2a058767
Make compiler portable (do not assume int64_t is long long int)
Mario de Sousa <msousa@fe.up.pt>
parents:
587
diff
changeset
|
98 |
|
181 | 99 |
/***********************************************************************/ |
100 |
/***********************************************************************/ |
|
101 |
/***********************************************************************/ |
|
102 |
/***********************************************************************/ |
|
103 |
||
104 |
||
105 |
||
106 |
/* A symbol table with all globally declared functions... */ |
|
107 |
function_declaration_c null_symbol1(NULL,NULL,NULL,NULL); |
|
108 |
dsymtable_c<function_declaration_c *, &null_symbol1> function_symtable; |
|
109 |
||
110 |
/* A symbol table with all globally declared functions block types... */ |
|
111 |
function_block_declaration_c null_symbol2(NULL,NULL,NULL); |
|
112 |
symtable_c<function_block_declaration_c *, &null_symbol2> function_block_type_symtable; |
|
113 |
||
114 |
/* A symbol table with all globally declared program types... */ |
|
115 |
program_declaration_c null_symbol3(NULL,NULL,NULL); |
|
116 |
symtable_c<program_declaration_c *, &null_symbol3> program_type_symtable; |
|
117 |
||
118 |
/* A symbol table with all user declared type definitions... */ |
|
119 |
/* Note that function block types and program types have their |
|
120 |
* own symbol tables, so do not get placed in this symbol table! |
|
338
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
121 |
* |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
122 |
* The symbol_c * associated to the value will point to the data type declaration. |
181 | 123 |
*/ |
124 |
symbol_c null_symbol4; |
|
125 |
symtable_c<symbol_c *, &null_symbol4> type_symtable; |
|
126 |
||
328
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
127 |
/* A symbol table with all values declared for enumerated type... */ |
338
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
128 |
/* Notes: |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
129 |
* - if the value is defined multiple times the value |
328
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
130 |
* is the null pointer. |
338
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
131 |
* |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
132 |
* - The stored symbol_c * associated to the value points to the enumerated_type_name |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
133 |
* (i.e. the name of the enumerated data type) in which the the value/identifier |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
134 |
* is used/declared. |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
135 |
* |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
136 |
* - We could re-use the null_symbol4 object, but it is safer to use a distinct object |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
137 |
* (i.e. it might make it easier to find strange bugs). |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
138 |
*/ |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
139 |
symbol_c null_symbol5; |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
140 |
symtable_c<symbol_c *, &null_symbol5> enumerated_value_symtable; |
181 | 141 |
|
142 |
||
143 |
/***********************************************************************/ |
|
144 |
/***********************************************************************/ |
|
145 |
/***********************************************************************/ |
|
146 |
/***********************************************************************/ |
|
147 |
||
148 |
||
149 |
class populate_symtables_c: public iterator_visitor_c { |
|
150 |
||
328
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
151 |
private: |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
152 |
symbol_c *current_enumerated_type; |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
153 |
|
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
154 |
public: |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
155 |
populate_symtables_c(void) { |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
156 |
current_enumerated_type = NULL; |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
157 |
}; |
181 | 158 |
virtual ~populate_symtables_c(void) {} |
159 |
||
160 |
||
161 |
public: |
|
162 |
||
163 |
/*************************/ |
|
164 |
/* B.1 - Common elements */ |
|
165 |
/*************************/ |
|
166 |
/*******************************************/ |
|
167 |
/* B 1.1 - Letters, digits and identifiers */ |
|
168 |
/*******************************************/ |
|
169 |
/*********************/ |
|
170 |
/* B 1.2 - Constants */ |
|
171 |
/*********************/ |
|
172 |
/******************************/ |
|
173 |
/* B 1.2.1 - Numeric Literals */ |
|
174 |
/******************************/ |
|
175 |
/*******************************/ |
|
176 |
/* B.1.2.2 Character Strings */ |
|
177 |
/*******************************/ |
|
178 |
/***************************/ |
|
179 |
/* B 1.2.3 - Time Literals */ |
|
180 |
/***************************/ |
|
181 |
/************************/ |
|
182 |
/* B 1.2.3.1 - Duration */ |
|
183 |
/************************/ |
|
184 |
/************************************/ |
|
185 |
/* B 1.2.3.2 - Time of day and Date */ |
|
186 |
/************************************/ |
|
187 |
/**********************/ |
|
188 |
/* B.1.3 - Data types */ |
|
189 |
/**********************/ |
|
190 |
/***********************************/ |
|
191 |
/* B 1.3.1 - Elementary Data Types */ |
|
192 |
/***********************************/ |
|
193 |
/********************************/ |
|
194 |
/* B.1.3.2 - Generic data types */ |
|
195 |
/********************************/ |
|
196 |
/********************************/ |
|
197 |
/* B 1.3.3 - Derived data types */ |
|
198 |
/********************************/ |
|
199 |
||
200 |
/* subrange_type_name ':' subrange_spec_init */ |
|
201 |
void *visit(subrange_type_declaration_c *symbol) { |
|
202 |
TRACE("subrange_type_declaration_c"); |
|
203 |
type_symtable.insert(symbol->subrange_type_name, symbol->subrange_spec_init); |
|
204 |
return NULL; |
|
205 |
} |
|
206 |
||
207 |
||
208 |
/* enumerated_type_name ':' enumerated_spec_init */ |
|
209 |
void *visit(enumerated_type_declaration_c *symbol) { |
|
210 |
TRACE("enumerated_type_declaration_c"); |
|
211 |
type_symtable.insert(symbol->enumerated_type_name, symbol->enumerated_spec_init); |
|
328
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
212 |
current_enumerated_type = symbol->enumerated_type_name; |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
213 |
symbol->enumerated_spec_init->accept(*this); |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
214 |
current_enumerated_type = NULL; |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
215 |
return NULL; |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
216 |
} |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
217 |
|
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
218 |
/* enumerated_specification ASSIGN enumerated_value */ |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
219 |
void *visit(enumerated_spec_init_c *symbol) { |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
220 |
return symbol->enumerated_specification->accept(*this); |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
221 |
} |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
222 |
|
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
223 |
/* [enumerated_type_name '#'] identifier */ |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
224 |
void *visit(enumerated_value_c *symbol) { |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
225 |
if (current_enumerated_type != NULL) { |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
226 |
if (symbol->type != NULL) ERROR; |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
227 |
|
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
228 |
symbol_c *value_type = enumerated_value_symtable.find_value(symbol->value); |
338
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
229 |
/* NOTE: The following condition checks whether the same identifier is used more than once |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
230 |
* when defining the enumerated values of the type declaration of the new enumerated type. |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
231 |
* If this occurs, then the program beeing compiled contains a semantic error, which |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
232 |
* must be caught and reported by the semantic analyser. However, since |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
233 |
* this code is run before the semantic analyser, we must not yet raise the ERROR (internal |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
234 |
* compiler error message). |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
235 |
* For this reason, the follosing check is commented out. |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
236 |
*/ |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
237 |
/* if (value_type == current_enumerated_type) ERROR; */ |
328
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
238 |
|
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
239 |
if (value_type == enumerated_value_symtable.end_value()) |
338
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
240 |
/* This identifier has not yet been used in any previous declaration of an enumeration data type. |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
241 |
* so we add it to the symbol table. |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
242 |
*/ |
328
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
243 |
enumerated_value_symtable.insert(symbol->value, current_enumerated_type); |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
244 |
else if (value_type != NULL) |
338
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
245 |
/* This identifier has already been used in a previous declaration of an enumeration data type. |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
246 |
* so we set the symbol in symbol table pointing to NULL. |
3037bb7e8a82
Adding some comments, and removing a check for a semantic error in code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents:
328
diff
changeset
|
247 |
*/ |
328
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
248 |
enumerated_value_symtable.set(symbol->value, NULL); |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
249 |
} |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
250 |
return NULL; |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
251 |
} |
181 | 252 |
|
253 |
/* identifier ':' array_spec_init */ |
|
254 |
void *visit(array_type_declaration_c *symbol) { |
|
255 |
TRACE("array_type_declaration_c"); |
|
256 |
type_symtable.insert(symbol->identifier, symbol->array_spec_init); |
|
257 |
return NULL; |
|
258 |
} |
|
259 |
||
260 |
||
261 |
/* simple_type_name ':' simple_spec_init */ |
|
262 |
void *visit(simple_type_declaration_c *symbol) { |
|
263 |
TRACE("simple_type_declaration_c"); |
|
264 |
type_symtable.insert(symbol->simple_type_name, symbol->simple_spec_init); |
|
265 |
return NULL; |
|
266 |
} |
|
267 |
||
268 |
||
269 |
/* structure_type_name ':' structure_specification */ |
|
270 |
void *visit(structure_type_declaration_c *symbol) { |
|
271 |
TRACE("structure_type_declaration_c"); |
|
272 |
type_symtable.insert(symbol->structure_type_name, symbol->structure_specification); |
|
273 |
return NULL; |
|
274 |
} |
|
275 |
||
276 |
||
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:
366
diff
changeset
|
277 |
/* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ |
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:
366
diff
changeset
|
278 |
// 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:
366
diff
changeset
|
279 |
// 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:
366
diff
changeset
|
280 |
// 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:
366
diff
changeset
|
281 |
// string_type_declaration_init) /* may be == NULL! */ |
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:
366
diff
changeset
|
282 |
void *visit(string_type_declaration_c *symbol) { |
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:
366
diff
changeset
|
283 |
TRACE("string_type_declaration_c"); |
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:
366
diff
changeset
|
284 |
type_symtable.insert(symbol->string_type_name, symbol); |
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:
366
diff
changeset
|
285 |
return NULL; |
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:
366
diff
changeset
|
286 |
} |
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:
366
diff
changeset
|
287 |
|
181 | 288 |
/*********************/ |
289 |
/* B 1.4 - Variables */ |
|
290 |
/*********************/ |
|
291 |
/********************************************/ |
|
292 |
/* B.1.4.1 Directly Represented Variables */ |
|
293 |
/********************************************/ |
|
294 |
/*************************************/ |
|
295 |
/* B.1.4.2 Multi-element Variables */ |
|
296 |
/*************************************/ |
|
297 |
/******************************************/ |
|
298 |
/* B 1.4.3 - Declaration & Initialisation */ |
|
299 |
/******************************************/ |
|
300 |
/**************************************/ |
|
301 |
/* B.1.5 - Program organization units */ |
|
302 |
/**************************************/ |
|
303 |
/***********************/ |
|
304 |
/* B 1.5.1 - Functions */ |
|
305 |
/***********************/ |
|
306 |
public: |
|
307 |
/* FUNCTION derived_function_name ':' elementary_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */ |
|
308 |
/* | FUNCTION derived_function_name ':' derived_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */ |
|
309 |
void *visit(function_declaration_c *symbol) { |
|
310 |
TRACE("function_declaration_c"); |
|
311 |
function_symtable.insert(symbol->derived_function_name, symbol); |
|
312 |
||
313 |
/* symbol->derived_function_name->accept(*this); */ /* Function name */ |
|
314 |
/* symbol->type_name->accept(*this); */ /* return data type */ |
|
315 |
/* symbol->var_declarations_list->accept(*this); */ /* Function parameters and variables */ |
|
316 |
/* symbol->function_body->accept(*this); */ /* Function body */ |
|
317 |
return NULL; |
|
318 |
} |
|
319 |
||
320 |
||
321 |
/*****************************/ |
|
322 |
/* B 1.5.2 - Function Blocks */ |
|
323 |
/*****************************/ |
|
324 |
public: |
|
325 |
/* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ |
|
326 |
//SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused) |
|
327 |
void *visit(function_block_declaration_c *symbol) { |
|
328 |
TRACE("function_block_declaration_c"); |
|
329 |
function_block_type_symtable.insert(symbol->fblock_name, symbol); |
|
330 |
/* |
|
331 |
symbol->fblock_name->accept(*this); |
|
332 |
symbol->var_declarations->accept(*this); |
|
333 |
symbol->fblock_body->accept(*this); |
|
334 |
*/ |
|
335 |
return NULL; |
|
336 |
} |
|
337 |
||
338 |
||
339 |
/**********************/ |
|
340 |
/* B 1.5.3 - Programs */ |
|
341 |
/**********************/ |
|
342 |
public: |
|
343 |
/* PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */ |
|
344 |
//SYM_REF4(program_declaration_c, program_type_name, var_declarations, function_block_body, unused) |
|
345 |
void *visit(program_declaration_c *symbol) { |
|
346 |
TRACE("program_declaration_c"); |
|
347 |
program_type_symtable.insert(symbol->program_type_name, symbol); |
|
348 |
/* |
|
349 |
symbol->program_type_name->accept(*this); |
|
350 |
symbol->var_declarations->accept(*this); |
|
351 |
symbol->function_block_body->accept(*this); |
|
352 |
*/ |
|
353 |
return NULL; |
|
354 |
} |
|
355 |
||
356 |
}; /* populate_symtables_c */ |
|
357 |
||
358 |
||
359 |
||
360 |
||
361 |
||
362 |
void absyntax_utils_init(symbol_c *tree_root) { |
|
363 |
populate_symtables_c populate_symbols; |
|
364 |
||
365 |
tree_root->accept(populate_symbols); |
|
366 |
} |
|
367 |