|
1 /* |
|
2 * (c) 2003 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. |
|
28 * |
|
29 * This is used to create symbol tables such as a list of |
|
30 * variables currently in scope, a list of previously defined |
|
31 * functions, etc... |
|
32 */ |
|
33 |
|
34 |
|
35 |
|
36 #ifndef _SYMTABLE_HH |
|
37 #define _SYMTABLE_HH |
|
38 |
|
39 #include "../absyntax/absyntax.hh" |
|
40 |
|
41 #include <map> |
|
42 #include <string> |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 template<typename value_type, value_type null_value> class symtable_c { |
|
48 /* Case insensitive string compare copied from |
|
49 * "The C++ Programming Language" - 3rd Edition |
|
50 * by Bjarne Stroustrup, ISBN 0201889544. |
|
51 */ |
|
52 class nocase_c { |
|
53 public: |
|
54 bool operator() (const std::string& x, const std::string& y) const { |
|
55 std::string::const_iterator ix = x.begin(); |
|
56 std::string::const_iterator iy = y.begin(); |
|
57 |
|
58 for(; (ix != x.end()) && (iy != y.end()) && (toupper(*ix) == toupper(*iy)); ++ix, ++iy); |
|
59 if (ix == x.end()) return (iy != y.end()); |
|
60 if (iy == y.end()) return false; |
|
61 return (toupper(*ix) < toupper(*iy)); |
|
62 }; |
|
63 }; |
|
64 |
|
65 public: |
|
66 typedef value_type value_t; |
|
67 |
|
68 private: |
|
69 /* Comparison between identifiers must ignore case, therefore the use of nocase_c */ |
|
70 typedef std::map<std::string, value_t, nocase_c> base_t; |
|
71 base_t _base; |
|
72 |
|
73 public: |
|
74 typedef typename base_t::iterator iterator; |
|
75 typedef typename base_t::const_iterator const_iterator; |
|
76 typedef typename base_t::reverse_iterator reverse_iterator; |
|
77 typedef typename base_t::const_reverse_iterator const_reverse_iterator; |
|
78 |
|
79 private: |
|
80 /* pointer to symbol table of the next inner scope */ |
|
81 symtable_c *inner_scope; |
|
82 void reset(void); /* clear all entries... */ |
|
83 |
|
84 public: |
|
85 symtable_c(void); |
|
86 |
|
87 void push(void); /* create new inner scope */ |
|
88 int pop(void); /* clear most inner scope */ |
|
89 |
|
90 void set(const char *identifier_str, value_t value); |
|
91 void set(const symbol_c *symbol, value_t value); |
|
92 void insert(const char *identifier_str, value_t value); |
|
93 void insert(const symbol_c *symbol, value_t value); |
|
94 |
|
95 /* Search for an entry. Will return end_value() if not found */ |
|
96 value_t end_value(void) {return null_value;} |
|
97 value_t find_value(const char *identifier_str); |
|
98 value_t find_value(const symbol_c *symbol); |
|
99 |
|
100 iterator find(const char *identifier_str) {return _base.find(identifier_str);} |
|
101 |
|
102 /* iterators pointing to beg/end of map... */ |
|
103 iterator begin() {return _base.begin();} |
|
104 const_iterator begin() const {return _base.begin();} |
|
105 iterator end() {return _base.end();} |
|
106 const_iterator end() const {return _base.end();} |
|
107 reverse_iterator rbegin() {return _base.rbegin();} |
|
108 const_reverse_iterator rbegin() const {return _base.rbegin();} |
|
109 reverse_iterator rend() {return _base.rend();} |
|
110 const_reverse_iterator rend() const {return _base.rend();} |
|
111 |
|
112 /* debuging function... */ |
|
113 void print(void); |
|
114 }; |
|
115 |
|
116 |
|
117 |
|
118 /* Templates must include the source into the code! */ |
|
119 #include "symtable.cc" |
|
120 |
|
121 #endif /* _SYMTABLE_HH */ |