author | Mario de Sousa <msousa@fe.up.pt> |
Fri, 26 Aug 2011 10:48:09 +0100 | |
changeset 367 | 6d94128ba5ad |
parent 366 | 1aeb29ee9381 |
child 433 | 1355adcdad58 |
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:
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> |
|
366
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
53 |
#include <string.h> /* required for strlen() */ |
350
2c3c4dc34979
Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents:
338
diff
changeset
|
54 |
#include <stdlib.h> /* required for atoi() */ |
181 | 55 |
|
56 |
#include "../util/symtable.hh" |
|
57 |
#include "../util/dsymtable.hh" |
|
58 |
#include "../absyntax/visitor.hh" |
|
59 |
||
60 |
||
61 |
||
62 |
//#define DEBUG |
|
63 |
#ifdef DEBUG |
|
64 |
#define TRACE(classname) printf("\n____%s____\n",classname); |
|
65 |
#else |
|
66 |
#define TRACE(classname) |
|
67 |
#endif |
|
68 |
||
69 |
#define ERROR error_exit(__FILE__,__LINE__) |
|
70 |
/* function defined in main.cc */ |
|
71 |
extern void error_exit(const char *file_name, int line_no); |
|
72 |
||
73 |
||
74 |
/***********************************************************************/ |
|
75 |
/***********************************************************************/ |
|
76 |
/***********************************************************************/ |
|
77 |
/***********************************************************************/ |
|
78 |
||
79 |
||
80 |
/* returns 0 if the names are equal!! */ |
|
81 |
/* NOTE: it must ignore case!! */ |
|
82 |
int compare_identifiers(symbol_c *ident1, symbol_c *ident2) { |
|
83 |
||
84 |
token_c *name1 = dynamic_cast<token_c *>(ident1); |
|
85 |
token_c *name2 = dynamic_cast<token_c *>(ident2); |
|
86 |
||
87 |
if ((name1 == NULL) || (name2 == NULL)) |
|
88 |
/* invalid identifiers... */ |
|
89 |
return -1; |
|
90 |
||
91 |
if (strcasecmp(name1->value, name2->value) == 0) |
|
92 |
return 0; |
|
93 |
||
94 |
/* identifiers do not match! */ |
|
95 |
return 1; |
|
96 |
} |
|
97 |
||
98 |
||
350
2c3c4dc34979
Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents:
338
diff
changeset
|
99 |
/* extract the value of an integer from an integer_c object !! */ |
2c3c4dc34979
Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents:
338
diff
changeset
|
100 |
/* NOTE: it must ignore underscores! */ |
2c3c4dc34979
Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents:
338
diff
changeset
|
101 |
int extract_integer(symbol_c *sym) { |
366
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
102 |
std::string str = ""; |
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
103 |
integer_c *integer; |
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
104 |
neg_integer_c * neg_integer; |
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
105 |
|
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
106 |
if ((neg_integer = dynamic_cast<neg_integer_c *>(sym)) != NULL) |
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
107 |
return - extract_integer((integer_c *)neg_integer->exp); |
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
108 |
|
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
109 |
if ((integer = dynamic_cast<integer_c *>(sym)) == NULL) ERROR; |
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
110 |
|
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
111 |
for(unsigned int i = 0; i < strlen(integer->value); i++) |
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
112 |
if (integer->value[i] != '_') str += integer->value[i]; |
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
113 |
|
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
114 |
return atoi(str.c_str()); |
350
2c3c4dc34979
Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents:
338
diff
changeset
|
115 |
} |
2c3c4dc34979
Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents:
338
diff
changeset
|
116 |
|
181 | 117 |
|
366
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
118 |
|
181 | 119 |
/***********************************************************************/ |
120 |
/***********************************************************************/ |
|
121 |
/***********************************************************************/ |
|
122 |
/***********************************************************************/ |
|
123 |
||
124 |
||
125 |
||
126 |
/* A symbol table with all globally declared functions... */ |
|
127 |
function_declaration_c null_symbol1(NULL,NULL,NULL,NULL); |
|
128 |
dsymtable_c<function_declaration_c *, &null_symbol1> function_symtable; |
|
129 |
||
130 |
/* A symbol table with all globally declared functions block types... */ |
|
131 |
function_block_declaration_c null_symbol2(NULL,NULL,NULL); |
|
132 |
symtable_c<function_block_declaration_c *, &null_symbol2> function_block_type_symtable; |
|
133 |
||
134 |
/* A symbol table with all globally declared program types... */ |
|
135 |
program_declaration_c null_symbol3(NULL,NULL,NULL); |
|
136 |
symtable_c<program_declaration_c *, &null_symbol3> program_type_symtable; |
|
137 |
||
138 |
/* A symbol table with all user declared type definitions... */ |
|
139 |
/* Note that function block types and program types have their |
|
140 |
* 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
|
141 |
* |
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
|
142 |
* The symbol_c * associated to the value will point to the data type declaration. |
181 | 143 |
*/ |
144 |
symbol_c null_symbol4; |
|
145 |
symtable_c<symbol_c *, &null_symbol4> type_symtable; |
|
146 |
||
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
|
147 |
/* 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
|
148 |
/* 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
|
149 |
* - 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
|
150 |
* 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
|
151 |
* |
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
|
152 |
* - 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
|
153 |
* (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
|
154 |
* 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
|
155 |
* |
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
|
156 |
* - 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
|
157 |
* (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
|
158 |
*/ |
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
|
159 |
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
|
160 |
symtable_c<symbol_c *, &null_symbol5> enumerated_value_symtable; |
181 | 161 |
|
162 |
||
163 |
/***********************************************************************/ |
|
164 |
/***********************************************************************/ |
|
165 |
/***********************************************************************/ |
|
166 |
/***********************************************************************/ |
|
167 |
||
168 |
||
169 |
class populate_symtables_c: public iterator_visitor_c { |
|
170 |
||
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
|
171 |
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
|
172 |
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
|
173 |
|
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
|
174 |
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
|
175 |
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
|
176 |
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
|
177 |
}; |
181 | 178 |
virtual ~populate_symtables_c(void) {} |
179 |
||
180 |
||
181 |
public: |
|
182 |
||
183 |
/*************************/ |
|
184 |
/* B.1 - Common elements */ |
|
185 |
/*************************/ |
|
186 |
/*******************************************/ |
|
187 |
/* B 1.1 - Letters, digits and identifiers */ |
|
188 |
/*******************************************/ |
|
189 |
/*********************/ |
|
190 |
/* B 1.2 - Constants */ |
|
191 |
/*********************/ |
|
192 |
/******************************/ |
|
193 |
/* B 1.2.1 - Numeric Literals */ |
|
194 |
/******************************/ |
|
195 |
/*******************************/ |
|
196 |
/* B.1.2.2 Character Strings */ |
|
197 |
/*******************************/ |
|
198 |
/***************************/ |
|
199 |
/* B 1.2.3 - Time Literals */ |
|
200 |
/***************************/ |
|
201 |
/************************/ |
|
202 |
/* B 1.2.3.1 - Duration */ |
|
203 |
/************************/ |
|
204 |
/************************************/ |
|
205 |
/* B 1.2.3.2 - Time of day and Date */ |
|
206 |
/************************************/ |
|
207 |
/**********************/ |
|
208 |
/* B.1.3 - Data types */ |
|
209 |
/**********************/ |
|
210 |
/***********************************/ |
|
211 |
/* B 1.3.1 - Elementary Data Types */ |
|
212 |
/***********************************/ |
|
213 |
/********************************/ |
|
214 |
/* B.1.3.2 - Generic data types */ |
|
215 |
/********************************/ |
|
216 |
/********************************/ |
|
217 |
/* B 1.3.3 - Derived data types */ |
|
218 |
/********************************/ |
|
219 |
||
220 |
/* subrange_type_name ':' subrange_spec_init */ |
|
221 |
void *visit(subrange_type_declaration_c *symbol) { |
|
222 |
TRACE("subrange_type_declaration_c"); |
|
223 |
type_symtable.insert(symbol->subrange_type_name, symbol->subrange_spec_init); |
|
224 |
return NULL; |
|
225 |
} |
|
226 |
||
227 |
||
228 |
/* enumerated_type_name ':' enumerated_spec_init */ |
|
229 |
void *visit(enumerated_type_declaration_c *symbol) { |
|
230 |
TRACE("enumerated_type_declaration_c"); |
|
231 |
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
|
232 |
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
|
233 |
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
|
234 |
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
|
235 |
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
|
236 |
} |
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
|
237 |
|
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 |
/* 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
|
239 |
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
|
240 |
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
|
241 |
} |
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
|
242 |
|
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_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
|
244 |
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
|
245 |
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
|
246 |
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
|
247 |
|
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 |
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
|
249 |
/* 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
|
250 |
* 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
|
251 |
* 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
|
252 |
* 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
|
253 |
* 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
|
254 |
* 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
|
255 |
* 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
|
256 |
*/ |
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
|
257 |
/* 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
|
258 |
|
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
|
259 |
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
|
260 |
/* 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
|
261 |
* 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
|
262 |
*/ |
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
|
263 |
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
|
264 |
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
|
265 |
/* 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
|
266 |
* 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
|
267 |
*/ |
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
|
268 |
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
|
269 |
} |
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
|
270 |
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
|
271 |
} |
181 | 272 |
|
273 |
/* identifier ':' array_spec_init */ |
|
274 |
void *visit(array_type_declaration_c *symbol) { |
|
275 |
TRACE("array_type_declaration_c"); |
|
276 |
type_symtable.insert(symbol->identifier, symbol->array_spec_init); |
|
277 |
return NULL; |
|
278 |
} |
|
279 |
||
280 |
||
281 |
/* simple_type_name ':' simple_spec_init */ |
|
282 |
void *visit(simple_type_declaration_c *symbol) { |
|
283 |
TRACE("simple_type_declaration_c"); |
|
284 |
type_symtable.insert(symbol->simple_type_name, symbol->simple_spec_init); |
|
285 |
return NULL; |
|
286 |
} |
|
287 |
||
288 |
||
289 |
/* structure_type_name ':' structure_specification */ |
|
290 |
void *visit(structure_type_declaration_c *symbol) { |
|
291 |
TRACE("structure_type_declaration_c"); |
|
292 |
type_symtable.insert(symbol->structure_type_name, symbol->structure_specification); |
|
293 |
return NULL; |
|
294 |
} |
|
295 |
||
296 |
||
297 |
||
298 |
/*********************/ |
|
299 |
/* B 1.4 - Variables */ |
|
300 |
/*********************/ |
|
301 |
/********************************************/ |
|
302 |
/* B.1.4.1 Directly Represented Variables */ |
|
303 |
/********************************************/ |
|
304 |
/*************************************/ |
|
305 |
/* B.1.4.2 Multi-element Variables */ |
|
306 |
/*************************************/ |
|
307 |
/******************************************/ |
|
308 |
/* B 1.4.3 - Declaration & Initialisation */ |
|
309 |
/******************************************/ |
|
310 |
/**************************************/ |
|
311 |
/* B.1.5 - Program organization units */ |
|
312 |
/**************************************/ |
|
313 |
/***********************/ |
|
314 |
/* B 1.5.1 - Functions */ |
|
315 |
/***********************/ |
|
316 |
public: |
|
317 |
/* FUNCTION derived_function_name ':' elementary_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */ |
|
318 |
/* | FUNCTION derived_function_name ':' derived_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */ |
|
319 |
void *visit(function_declaration_c *symbol) { |
|
320 |
TRACE("function_declaration_c"); |
|
321 |
function_symtable.insert(symbol->derived_function_name, symbol); |
|
322 |
||
323 |
/* symbol->derived_function_name->accept(*this); */ /* Function name */ |
|
324 |
/* symbol->type_name->accept(*this); */ /* return data type */ |
|
325 |
/* symbol->var_declarations_list->accept(*this); */ /* Function parameters and variables */ |
|
326 |
/* symbol->function_body->accept(*this); */ /* Function body */ |
|
327 |
return NULL; |
|
328 |
} |
|
329 |
||
330 |
||
331 |
/*****************************/ |
|
332 |
/* B 1.5.2 - Function Blocks */ |
|
333 |
/*****************************/ |
|
334 |
public: |
|
335 |
/* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ |
|
336 |
//SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused) |
|
337 |
void *visit(function_block_declaration_c *symbol) { |
|
338 |
TRACE("function_block_declaration_c"); |
|
339 |
function_block_type_symtable.insert(symbol->fblock_name, symbol); |
|
340 |
/* |
|
341 |
symbol->fblock_name->accept(*this); |
|
342 |
symbol->var_declarations->accept(*this); |
|
343 |
symbol->fblock_body->accept(*this); |
|
344 |
*/ |
|
345 |
return NULL; |
|
346 |
} |
|
347 |
||
348 |
||
349 |
/**********************/ |
|
350 |
/* B 1.5.3 - Programs */ |
|
351 |
/**********************/ |
|
352 |
public: |
|
353 |
/* PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */ |
|
354 |
//SYM_REF4(program_declaration_c, program_type_name, var_declarations, function_block_body, unused) |
|
355 |
void *visit(program_declaration_c *symbol) { |
|
356 |
TRACE("program_declaration_c"); |
|
357 |
program_type_symtable.insert(symbol->program_type_name, symbol); |
|
358 |
/* |
|
359 |
symbol->program_type_name->accept(*this); |
|
360 |
symbol->var_declarations->accept(*this); |
|
361 |
symbol->function_block_body->accept(*this); |
|
362 |
*/ |
|
363 |
return NULL; |
|
364 |
} |
|
365 |
||
366 |
}; /* populate_symtables_c */ |
|
367 |
||
368 |
||
369 |
||
370 |
||
371 |
||
372 |
void absyntax_utils_init(symbol_c *tree_root) { |
|
373 |
populate_symtables_c populate_symbols; |
|
374 |
||
375 |
tree_root->accept(populate_symbols); |
|
376 |
} |
|
377 |