author | Mario de Sousa <msousa@fe.up.pt> |
Sun, 10 Jun 2012 15:54:10 +0100 | |
changeset 575 | a1b63f776535 |
parent 571 | 97b85630e496 |
child 576 | 8368ec909825 |
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> |
|
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() */ |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
547
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" |
|
60 |
||
61 |
||
62 |
||
63 |
//#define DEBUG |
|
64 |
#ifdef DEBUG |
|
65 |
#define TRACE(classname) printf("\n____%s____\n",classname); |
|
66 |
#else |
|
67 |
#define TRACE(classname) |
|
68 |
#endif |
|
69 |
||
70 |
#define ERROR error_exit(__FILE__,__LINE__) |
|
71 |
/* function defined in main.cc */ |
|
72 |
extern void error_exit(const char *file_name, int line_no); |
|
73 |
||
74 |
||
75 |
/***********************************************************************/ |
|
76 |
/***********************************************************************/ |
|
77 |
/***********************************************************************/ |
|
78 |
/***********************************************************************/ |
|
79 |
||
80 |
||
81 |
/* returns 0 if the names are equal!! */ |
|
82 |
/* NOTE: it must ignore case!! */ |
|
83 |
int compare_identifiers(symbol_c *ident1, symbol_c *ident2) { |
|
84 |
||
85 |
token_c *name1 = dynamic_cast<token_c *>(ident1); |
|
86 |
token_c *name2 = dynamic_cast<token_c *>(ident2); |
|
87 |
||
88 |
if ((name1 == NULL) || (name2 == NULL)) |
|
89 |
/* invalid identifiers... */ |
|
90 |
return -1; |
|
91 |
||
92 |
if (strcasecmp(name1->value, name2->value) == 0) |
|
93 |
return 0; |
|
94 |
||
95 |
/* identifiers do not match! */ |
|
96 |
return 1; |
|
97 |
} |
|
98 |
||
99 |
||
350
2c3c4dc34979
Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents:
338
diff
changeset
|
100 |
/* 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
|
101 |
/* NOTE: it must ignore underscores! */ |
547
dab341e80664
Fix parsing os TIME literals & rename the extract_integer() function.
mjsousa <msousa@fe.up.pt>
parents:
433
diff
changeset
|
102 |
long long extract_integer_value(symbol_c *sym) { |
366
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
103 |
std::string str = ""; |
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
104 |
integer_c *integer; |
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
105 |
neg_integer_c * neg_integer; |
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
106 |
|
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
107 |
if ((neg_integer = dynamic_cast<neg_integer_c *>(sym)) != NULL) |
547
dab341e80664
Fix parsing os TIME literals & rename the extract_integer() function.
mjsousa <msousa@fe.up.pt>
parents:
433
diff
changeset
|
108 |
return - extract_integer_value(neg_integer->exp); |
366
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
109 |
|
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
110 |
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
|
111 |
|
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
112 |
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
|
113 |
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
|
114 |
|
547
dab341e80664
Fix parsing os TIME literals & rename the extract_integer() function.
mjsousa <msousa@fe.up.pt>
parents:
433
diff
changeset
|
115 |
/* return atoi(str.c_str()); */ |
dab341e80664
Fix parsing os TIME literals & rename the extract_integer() function.
mjsousa <msousa@fe.up.pt>
parents:
433
diff
changeset
|
116 |
return atoll(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
|
117 |
} |
2c3c4dc34979
Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents:
338
diff
changeset
|
118 |
|
567
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
119 |
|
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
120 |
/* extract the value of an hex integer from an hex_integer_c object !! */ |
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
121 |
/* NOTE: it must ignore underscores! */ |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
547
diff
changeset
|
122 |
uint64_t extract_hex_value(symbol_c *sym) { |
565
8acbddf75333
Fix a few bugs of previous commit.
Mario de Sousa <msousa@fe.up.pt>
parents:
564
diff
changeset
|
123 |
std::string str = ""; |
8acbddf75333
Fix a few bugs of previous commit.
Mario de Sousa <msousa@fe.up.pt>
parents:
564
diff
changeset
|
124 |
char *endptr; |
8acbddf75333
Fix a few bugs of previous commit.
Mario de Sousa <msousa@fe.up.pt>
parents:
564
diff
changeset
|
125 |
hex_integer_c * hex_integer; |
8acbddf75333
Fix a few bugs of previous commit.
Mario de Sousa <msousa@fe.up.pt>
parents:
564
diff
changeset
|
126 |
uint64_t ret; |
8acbddf75333
Fix a few bugs of previous commit.
Mario de Sousa <msousa@fe.up.pt>
parents:
564
diff
changeset
|
127 |
|
8acbddf75333
Fix a few bugs of previous commit.
Mario de Sousa <msousa@fe.up.pt>
parents:
564
diff
changeset
|
128 |
if ((hex_integer = dynamic_cast<hex_integer_c *>(sym)) == NULL) ERROR; |
8acbddf75333
Fix a few bugs of previous commit.
Mario de Sousa <msousa@fe.up.pt>
parents:
564
diff
changeset
|
129 |
for(unsigned int i = 3; i < strlen(hex_integer->value); i++) |
8acbddf75333
Fix a few bugs of previous commit.
Mario de Sousa <msousa@fe.up.pt>
parents:
564
diff
changeset
|
130 |
if (hex_integer->value[i] != '_') str += hex_integer->value[i]; |
567
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
131 |
|
571
97b85630e496
Re-insert errno=0 before calling strtoXX() (thanks go to Manuele)
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
132 |
errno = 0; // since strtoXX() may legally return 0, we must set errno to 0 to detect errors correctly! |
565
8acbddf75333
Fix a few bugs of previous commit.
Mario de Sousa <msousa@fe.up.pt>
parents:
564
diff
changeset
|
133 |
ret = strtoull(str.c_str(), &endptr, 16); |
8acbddf75333
Fix a few bugs of previous commit.
Mario de Sousa <msousa@fe.up.pt>
parents:
564
diff
changeset
|
134 |
if (errno != 0) ERROR; |
8acbddf75333
Fix a few bugs of previous commit.
Mario de Sousa <msousa@fe.up.pt>
parents:
564
diff
changeset
|
135 |
|
8acbddf75333
Fix a few bugs of previous commit.
Mario de Sousa <msousa@fe.up.pt>
parents:
564
diff
changeset
|
136 |
return ret; |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
547
diff
changeset
|
137 |
} |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
547
diff
changeset
|
138 |
|
181 | 139 |
|
567
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
140 |
/* extract the value of a real from an real_c object !! */ |
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
141 |
/* NOTE: it must ignore underscores! */ |
568
5f79478142d7
make the compiler platform independent (i.e. no longer assume sizeof(double)==8).
Mario de Sousa <msousa@fe.up.pt>
parents:
567
diff
changeset
|
142 |
real64_t extract_real_value(symbol_c *sym) { |
567
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
143 |
std::string str = ""; |
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
144 |
char *endptr; |
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
145 |
real_c * real_sym; |
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
146 |
uint64_t ret; |
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
147 |
|
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
148 |
if ((real_sym = dynamic_cast<real_c *>(sym)) == NULL) ERROR; |
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
149 |
for(unsigned int i = 3; i < strlen(real_sym->value); i++) |
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
150 |
if (real_sym->value[i] != '_') str += real_sym->value[i]; |
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
151 |
|
571
97b85630e496
Re-insert errno=0 before calling strtoXX() (thanks go to Manuele)
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
152 |
errno = 0; // since strtoXX() may legally return 0, we must set errno to 0 to detect errors correctly! |
568
5f79478142d7
make the compiler platform independent (i.e. no longer assume sizeof(double)==8).
Mario de Sousa <msousa@fe.up.pt>
parents:
567
diff
changeset
|
153 |
#if (real64_t == float) |
5f79478142d7
make the compiler platform independent (i.e. no longer assume sizeof(double)==8).
Mario de Sousa <msousa@fe.up.pt>
parents:
567
diff
changeset
|
154 |
ret = strtof(str.c_str(), NULL); |
5f79478142d7
make the compiler platform independent (i.e. no longer assume sizeof(double)==8).
Mario de Sousa <msousa@fe.up.pt>
parents:
567
diff
changeset
|
155 |
#elif (real64_t == double) |
5f79478142d7
make the compiler platform independent (i.e. no longer assume sizeof(double)==8).
Mario de Sousa <msousa@fe.up.pt>
parents:
567
diff
changeset
|
156 |
ret = strtod(str.c_str(), NULL); |
5f79478142d7
make the compiler platform independent (i.e. no longer assume sizeof(double)==8).
Mario de Sousa <msousa@fe.up.pt>
parents:
567
diff
changeset
|
157 |
#elif (real64_t == long_double) |
5f79478142d7
make the compiler platform independent (i.e. no longer assume sizeof(double)==8).
Mario de Sousa <msousa@fe.up.pt>
parents:
567
diff
changeset
|
158 |
ret = strtold(str.c_str(), NULL); |
5f79478142d7
make the compiler platform independent (i.e. no longer assume sizeof(double)==8).
Mario de Sousa <msousa@fe.up.pt>
parents:
567
diff
changeset
|
159 |
#else |
5f79478142d7
make the compiler platform independent (i.e. no longer assume sizeof(double)==8).
Mario de Sousa <msousa@fe.up.pt>
parents:
567
diff
changeset
|
160 |
#error Could not determine which data type is being used for real64_t (defined in absyntax.hh). Aborting! |
5f79478142d7
make the compiler platform independent (i.e. no longer assume sizeof(double)==8).
Mario de Sousa <msousa@fe.up.pt>
parents:
567
diff
changeset
|
161 |
#endif |
567
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
162 |
if (errno != 0) ERROR; |
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
163 |
|
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
164 |
return ret; |
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
165 |
} |
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
166 |
|
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
565
diff
changeset
|
167 |
|
366
1aeb29ee9381
Ignore underscores when extracting value of integer.
Mario de Sousa <msousa@fe.up.pt>
parents:
350
diff
changeset
|
168 |
|
181 | 169 |
/***********************************************************************/ |
170 |
/***********************************************************************/ |
|
171 |
/***********************************************************************/ |
|
172 |
/***********************************************************************/ |
|
173 |
||
174 |
||
175 |
||
176 |
/* A symbol table with all globally declared functions... */ |
|
177 |
function_declaration_c null_symbol1(NULL,NULL,NULL,NULL); |
|
178 |
dsymtable_c<function_declaration_c *, &null_symbol1> function_symtable; |
|
179 |
||
180 |
/* A symbol table with all globally declared functions block types... */ |
|
181 |
function_block_declaration_c null_symbol2(NULL,NULL,NULL); |
|
182 |
symtable_c<function_block_declaration_c *, &null_symbol2> function_block_type_symtable; |
|
183 |
||
184 |
/* A symbol table with all globally declared program types... */ |
|
185 |
program_declaration_c null_symbol3(NULL,NULL,NULL); |
|
186 |
symtable_c<program_declaration_c *, &null_symbol3> program_type_symtable; |
|
187 |
||
188 |
/* A symbol table with all user declared type definitions... */ |
|
189 |
/* Note that function block types and program types have their |
|
190 |
* 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
|
191 |
* |
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
|
192 |
* The symbol_c * associated to the value will point to the data type declaration. |
181 | 193 |
*/ |
194 |
symbol_c null_symbol4; |
|
195 |
symtable_c<symbol_c *, &null_symbol4> type_symtable; |
|
196 |
||
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
|
197 |
/* 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
|
198 |
/* 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
|
199 |
* - 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
|
200 |
* 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
|
201 |
* |
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
|
202 |
* - 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
|
203 |
* (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
|
204 |
* 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
|
205 |
* |
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
|
206 |
* - 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
|
207 |
* (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
|
208 |
*/ |
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
|
209 |
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
|
210 |
symtable_c<symbol_c *, &null_symbol5> enumerated_value_symtable; |
181 | 211 |
|
212 |
||
213 |
/***********************************************************************/ |
|
214 |
/***********************************************************************/ |
|
215 |
/***********************************************************************/ |
|
216 |
/***********************************************************************/ |
|
217 |
||
218 |
||
219 |
class populate_symtables_c: public iterator_visitor_c { |
|
220 |
||
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
|
221 |
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
|
222 |
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
|
223 |
|
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 |
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
|
225 |
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
|
226 |
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
|
227 |
}; |
181 | 228 |
virtual ~populate_symtables_c(void) {} |
229 |
||
230 |
||
231 |
public: |
|
232 |
||
233 |
/*************************/ |
|
234 |
/* B.1 - Common elements */ |
|
235 |
/*************************/ |
|
236 |
/*******************************************/ |
|
237 |
/* B 1.1 - Letters, digits and identifiers */ |
|
238 |
/*******************************************/ |
|
239 |
/*********************/ |
|
240 |
/* B 1.2 - Constants */ |
|
241 |
/*********************/ |
|
242 |
/******************************/ |
|
243 |
/* B 1.2.1 - Numeric Literals */ |
|
244 |
/******************************/ |
|
245 |
/*******************************/ |
|
246 |
/* B.1.2.2 Character Strings */ |
|
247 |
/*******************************/ |
|
248 |
/***************************/ |
|
249 |
/* B 1.2.3 - Time Literals */ |
|
250 |
/***************************/ |
|
251 |
/************************/ |
|
252 |
/* B 1.2.3.1 - Duration */ |
|
253 |
/************************/ |
|
254 |
/************************************/ |
|
255 |
/* B 1.2.3.2 - Time of day and Date */ |
|
256 |
/************************************/ |
|
257 |
/**********************/ |
|
258 |
/* B.1.3 - Data types */ |
|
259 |
/**********************/ |
|
260 |
/***********************************/ |
|
261 |
/* B 1.3.1 - Elementary Data Types */ |
|
262 |
/***********************************/ |
|
263 |
/********************************/ |
|
264 |
/* B.1.3.2 - Generic data types */ |
|
265 |
/********************************/ |
|
266 |
/********************************/ |
|
267 |
/* B 1.3.3 - Derived data types */ |
|
268 |
/********************************/ |
|
269 |
||
270 |
/* subrange_type_name ':' subrange_spec_init */ |
|
271 |
void *visit(subrange_type_declaration_c *symbol) { |
|
272 |
TRACE("subrange_type_declaration_c"); |
|
273 |
type_symtable.insert(symbol->subrange_type_name, symbol->subrange_spec_init); |
|
274 |
return NULL; |
|
275 |
} |
|
276 |
||
277 |
||
278 |
/* enumerated_type_name ':' enumerated_spec_init */ |
|
279 |
void *visit(enumerated_type_declaration_c *symbol) { |
|
280 |
TRACE("enumerated_type_declaration_c"); |
|
281 |
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
|
282 |
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
|
283 |
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
|
284 |
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
|
285 |
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
|
286 |
} |
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
|
287 |
|
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
|
288 |
/* 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
|
289 |
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
|
290 |
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
|
291 |
} |
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
|
292 |
|
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
|
293 |
/* [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
|
294 |
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
|
295 |
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
|
296 |
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
|
297 |
|
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
|
298 |
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
|
299 |
/* 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
|
300 |
* 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
|
301 |
* 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
|
302 |
* 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
|
303 |
* 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
|
304 |
* 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
|
305 |
* 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
|
306 |
*/ |
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
|
307 |
/* 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
|
308 |
|
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
|
309 |
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
|
310 |
/* 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
|
311 |
* 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
|
312 |
*/ |
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
|
313 |
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
|
314 |
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
|
315 |
/* 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
|
316 |
* 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
|
317 |
*/ |
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
|
318 |
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
|
319 |
} |
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
|
320 |
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
|
321 |
} |
181 | 322 |
|
323 |
/* identifier ':' array_spec_init */ |
|
324 |
void *visit(array_type_declaration_c *symbol) { |
|
325 |
TRACE("array_type_declaration_c"); |
|
326 |
type_symtable.insert(symbol->identifier, symbol->array_spec_init); |
|
327 |
return NULL; |
|
328 |
} |
|
329 |
||
330 |
||
331 |
/* simple_type_name ':' simple_spec_init */ |
|
332 |
void *visit(simple_type_declaration_c *symbol) { |
|
333 |
TRACE("simple_type_declaration_c"); |
|
334 |
type_symtable.insert(symbol->simple_type_name, symbol->simple_spec_init); |
|
335 |
return NULL; |
|
336 |
} |
|
337 |
||
338 |
||
339 |
/* structure_type_name ':' structure_specification */ |
|
340 |
void *visit(structure_type_declaration_c *symbol) { |
|
341 |
TRACE("structure_type_declaration_c"); |
|
342 |
type_symtable.insert(symbol->structure_type_name, symbol->structure_specification); |
|
343 |
return NULL; |
|
344 |
} |
|
345 |
||
346 |
||
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
|
347 |
/* 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
|
348 |
// 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
|
349 |
// 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
|
350 |
// 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
|
351 |
// 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
|
352 |
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
|
353 |
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
|
354 |
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
|
355 |
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
|
356 |
} |
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
|
357 |
|
181 | 358 |
/*********************/ |
359 |
/* B 1.4 - Variables */ |
|
360 |
/*********************/ |
|
361 |
/********************************************/ |
|
362 |
/* B.1.4.1 Directly Represented Variables */ |
|
363 |
/********************************************/ |
|
364 |
/*************************************/ |
|
365 |
/* B.1.4.2 Multi-element Variables */ |
|
366 |
/*************************************/ |
|
367 |
/******************************************/ |
|
368 |
/* B 1.4.3 - Declaration & Initialisation */ |
|
369 |
/******************************************/ |
|
370 |
/**************************************/ |
|
371 |
/* B.1.5 - Program organization units */ |
|
372 |
/**************************************/ |
|
373 |
/***********************/ |
|
374 |
/* B 1.5.1 - Functions */ |
|
375 |
/***********************/ |
|
376 |
public: |
|
377 |
/* FUNCTION derived_function_name ':' elementary_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */ |
|
378 |
/* | FUNCTION derived_function_name ':' derived_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */ |
|
379 |
void *visit(function_declaration_c *symbol) { |
|
380 |
TRACE("function_declaration_c"); |
|
381 |
function_symtable.insert(symbol->derived_function_name, symbol); |
|
382 |
||
383 |
/* symbol->derived_function_name->accept(*this); */ /* Function name */ |
|
384 |
/* symbol->type_name->accept(*this); */ /* return data type */ |
|
385 |
/* symbol->var_declarations_list->accept(*this); */ /* Function parameters and variables */ |
|
386 |
/* symbol->function_body->accept(*this); */ /* Function body */ |
|
387 |
return NULL; |
|
388 |
} |
|
389 |
||
390 |
||
391 |
/*****************************/ |
|
392 |
/* B 1.5.2 - Function Blocks */ |
|
393 |
/*****************************/ |
|
394 |
public: |
|
395 |
/* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ |
|
396 |
//SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused) |
|
397 |
void *visit(function_block_declaration_c *symbol) { |
|
398 |
TRACE("function_block_declaration_c"); |
|
399 |
function_block_type_symtable.insert(symbol->fblock_name, symbol); |
|
400 |
/* |
|
401 |
symbol->fblock_name->accept(*this); |
|
402 |
symbol->var_declarations->accept(*this); |
|
403 |
symbol->fblock_body->accept(*this); |
|
404 |
*/ |
|
405 |
return NULL; |
|
406 |
} |
|
407 |
||
408 |
||
409 |
/**********************/ |
|
410 |
/* B 1.5.3 - Programs */ |
|
411 |
/**********************/ |
|
412 |
public: |
|
413 |
/* PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */ |
|
414 |
//SYM_REF4(program_declaration_c, program_type_name, var_declarations, function_block_body, unused) |
|
415 |
void *visit(program_declaration_c *symbol) { |
|
416 |
TRACE("program_declaration_c"); |
|
417 |
program_type_symtable.insert(symbol->program_type_name, symbol); |
|
418 |
/* |
|
419 |
symbol->program_type_name->accept(*this); |
|
420 |
symbol->var_declarations->accept(*this); |
|
421 |
symbol->function_block_body->accept(*this); |
|
422 |
*/ |
|
423 |
return NULL; |
|
424 |
} |
|
425 |
||
426 |
}; /* populate_symtables_c */ |
|
427 |
||
428 |
||
429 |
||
430 |
||
431 |
||
432 |
void absyntax_utils_init(symbol_c *tree_root) { |
|
433 |
populate_symtables_c populate_symbols; |
|
434 |
||
435 |
tree_root->accept(populate_symbols); |
|
436 |
} |
|
437 |