etisserant@0: /* etisserant@0: * (c) 2003 Mario de Sousa etisserant@0: * etisserant@0: * Offered to the public under the terms of the GNU General Public License etisserant@0: * as published by the Free Software Foundation; either version 2 of the etisserant@0: * License, or (at your option) any later version. etisserant@0: * etisserant@0: * This program is distributed in the hope that it will be useful, but etisserant@0: * WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General etisserant@0: * Public License for more details. etisserant@0: * etisserant@0: * This code is made available on the understanding that it will not be etisserant@0: * used in safety-critical situations without a full and competent review. etisserant@0: */ etisserant@0: etisserant@0: /* etisserant@0: * An IEC 61131-3 IL and ST compiler. etisserant@0: * etisserant@0: * Based on the etisserant@0: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) etisserant@0: * etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: /* etisserant@0: * A generic symbol table. etisserant@0: * etisserant@0: * This is used to create symbol tables such as a list of etisserant@0: * variables currently in scope, a list of previously defined etisserant@0: * functions, etc... etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: etisserant@0: #ifndef _SYMTABLE_HH etisserant@0: #define _SYMTABLE_HH etisserant@0: etisserant@0: #include "../absyntax/absyntax.hh" etisserant@0: etisserant@0: #include etisserant@0: #include etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: template class symtable_c { etisserant@0: /* Case insensitive string compare copied from etisserant@0: * "The C++ Programming Language" - 3rd Edition etisserant@0: * by Bjarne Stroustrup, ISBN 0201889544. etisserant@0: */ etisserant@0: class nocase_c { etisserant@0: public: etisserant@0: bool operator() (const std::string& x, const std::string& y) const { etisserant@0: std::string::const_iterator ix = x.begin(); etisserant@0: std::string::const_iterator iy = y.begin(); etisserant@0: etisserant@0: for(; (ix != x.end()) && (iy != y.end()) && (toupper(*ix) == toupper(*iy)); ++ix, ++iy); etisserant@0: if (ix == x.end()) return (iy != y.end()); etisserant@0: if (iy == y.end()) return false; etisserant@0: return (toupper(*ix) < toupper(*iy)); etisserant@0: }; etisserant@0: }; etisserant@0: etisserant@0: public: etisserant@0: typedef value_type value_t; etisserant@0: etisserant@0: private: etisserant@0: /* Comparison between identifiers must ignore case, therefore the use of nocase_c */ etisserant@0: typedef std::map base_t; etisserant@0: base_t _base; etisserant@0: etisserant@0: public: etisserant@0: typedef typename base_t::iterator iterator; etisserant@0: typedef typename base_t::const_iterator const_iterator; etisserant@0: typedef typename base_t::reverse_iterator reverse_iterator; etisserant@0: typedef typename base_t::const_reverse_iterator const_reverse_iterator; etisserant@0: etisserant@0: private: etisserant@0: /* pointer to symbol table of the next inner scope */ etisserant@0: symtable_c *inner_scope; etisserant@0: void reset(void); /* clear all entries... */ etisserant@0: etisserant@0: public: etisserant@0: symtable_c(void); etisserant@0: etisserant@0: void push(void); /* create new inner scope */ etisserant@0: int pop(void); /* clear most inner scope */ etisserant@0: etisserant@0: void set(const char *identifier_str, value_t value); etisserant@0: void set(const symbol_c *symbol, value_t value); etisserant@0: void insert(const char *identifier_str, value_t value); etisserant@0: void insert(const symbol_c *symbol, value_t value); etisserant@0: etisserant@0: /* Search for an entry. Will return end_value() if not found */ etisserant@0: value_t end_value(void) {return null_value;} etisserant@0: value_t find_value(const char *identifier_str); etisserant@0: value_t find_value(const symbol_c *symbol); etisserant@0: etisserant@0: iterator find(const char *identifier_str) {return _base.find(identifier_str);} etisserant@0: etisserant@0: /* iterators pointing to beg/end of map... */ etisserant@0: iterator begin() {return _base.begin();} etisserant@0: const_iterator begin() const {return _base.begin();} etisserant@0: iterator end() {return _base.end();} etisserant@0: const_iterator end() const {return _base.end();} etisserant@0: reverse_iterator rbegin() {return _base.rbegin();} etisserant@0: const_reverse_iterator rbegin() const {return _base.rbegin();} etisserant@0: reverse_iterator rend() {return _base.rend();} etisserant@0: const_reverse_iterator rend() const {return _base.rend();} etisserant@0: etisserant@0: /* debuging function... */ etisserant@0: void print(void); etisserant@0: }; etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* Templates must include the source into the code! */ etisserant@0: #include "symtable.cc" etisserant@0: etisserant@0: #endif /* _SYMTABLE_HH */