|
1 /* |
|
2 * (c) 2005 Mario de Sousa |
|
3 * |
|
4 * Offered to the public under the terms of the GNU General Public License |
|
5 * as published by the Free Software Foundation; either version 2 of the |
|
6 * License, or (at your option) any later version. |
|
7 * |
|
8 * This program is distributed in the hope that it will be useful, but |
|
9 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
11 * Public License for more details. |
|
12 * |
|
13 * This code is made available on the understanding that it will not be |
|
14 * used in safety-critical situations without a full and competent review. |
|
15 */ |
|
16 |
|
17 /* |
|
18 * An IEC 61131-3 IL and ST compiler. |
|
19 * |
|
20 * Based on the |
|
21 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
22 * |
|
23 */ |
|
24 |
|
25 |
|
26 /* |
|
27 * A generic symbol table that allows duplicate values. |
|
28 * |
|
29 * This is used to create a symbol table of previously defined |
|
30 * functions. Duplicate are allowed since the standard permits function\ |
|
31 * overloading in the standard library. |
|
32 */ |
|
33 |
|
34 |
|
35 #include <iostream> |
|
36 #include "symtable.hh" |
|
37 |
|
38 |
|
39 |
|
40 /* A macro for printing out internal parser errors... */ |
|
41 #define ERROR error_exit(__FILE__,__LINE__) |
|
42 /* function defined in main.cc */ |
|
43 extern void error_exit(const char *file_name, int line_no); |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 /* clear all entries... */ |
|
49 template<typename value_type, value_type null_value> |
|
50 void dsymtable_c<value_type, null_value>::reset(void) { |
|
51 _base.clear(); |
|
52 } |
|
53 |
|
54 |
|
55 template<typename value_type, value_type null_value> |
|
56 void dsymtable_c<value_type, null_value>::insert(const char *identifier_str, value_t new_value) { |
|
57 // std::cout << "store_identifier(" << identifier_str << "): \n"; |
|
58 std::pair<const char *, value_t> new_element(identifier_str, new_value); |
|
59 /* iterator res = */ _base.insert(new_element); |
|
60 } |
|
61 |
|
62 |
|
63 template<typename value_type, value_type null_value> |
|
64 void dsymtable_c<value_type, null_value>::insert(const symbol_c *symbol, value_t new_value) { |
|
65 const token_c *name = dynamic_cast<const token_c *>(symbol); |
|
66 if (name == NULL) |
|
67 ERROR; |
|
68 insert(name->value, new_value); |
|
69 } |
|
70 |
|
71 |
|
72 #if 0 |
|
73 template<typename value_type, value_type null_value> |
|
74 void dsymtable_c<value_type, null_value>::insert_noduplicate(const char *identifier_str, value_t new_value) { |
|
75 if (find_value(identifier_str) != null_value) |
|
76 /* already present in the set! */ |
|
77 ERROR; |
|
78 |
|
79 // std::cout << "store_identifier(" << identifier_str << "): \n"; |
|
80 std::pair<const char *, value_t> new_element(identifier_str, new_value); |
|
81 /* iterator res = */ _base.insert(new_element); |
|
82 } |
|
83 |
|
84 |
|
85 template<typename value_type, value_type null_value> |
|
86 void dsymtable_c<value_type, null_value>::insert_noduplicate(const symbol_c *symbol, value_t new_value) { |
|
87 const token_c *name = dynamic_cast<const token_c *>(symbol); |
|
88 if (name == NULL) |
|
89 ERROR; |
|
90 insert_noduplicate(name->value, new_value); |
|
91 } |
|
92 #endif |
|
93 |
|
94 |
|
95 /* returns null_value if not found! */ |
|
96 template<typename value_type, value_type null_value> |
|
97 value_type dsymtable_c<value_type, null_value>::find_value(const char *identifier_str) { |
|
98 iterator i = _base.find(identifier_str); |
|
99 |
|
100 if (i == _base.end()) |
|
101 return null_value; |
|
102 else |
|
103 return i->second; |
|
104 } |
|
105 |
|
106 |
|
107 template<typename value_type, value_type null_value> |
|
108 value_type dsymtable_c<value_type, null_value>::find_value(const symbol_c *symbol) { |
|
109 const token_c *name = dynamic_cast<const token_c *>(symbol); |
|
110 if (name == NULL) |
|
111 ERROR; |
|
112 return find_value(name->value); |
|
113 } |
|
114 |
|
115 |
|
116 /* debuging function... */ |
|
117 template<typename value_type, value_type null_value> |
|
118 void dsymtable_c<value_type, null_value>::print(void) { |
|
119 for(iterator i = _base.begin(); |
|
120 i != _base.end(); |
|
121 i++) |
|
122 std::cout << i->second << ":" << i->first << "\n"; |
|
123 std::cout << "=====================\n"; |
|
124 } |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |