etisserant@0: /* Edouard@279: * matiec - a compiler for the programming languages defined in IEC 61131-3 etisserant@0: * Edouard@279: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant etisserant@0: * Edouard@279: * This program is free software: you can redistribute it and/or modify Edouard@279: * it under the terms of the GNU General Public License as published by Edouard@279: * the Free Software Foundation, either version 3 of the License, or Edouard@279: * (at your option) any later version. Edouard@279: * Edouard@279: * This program is distributed in the hope that it will be useful, Edouard@279: * but WITHOUT ANY WARRANTY; without even the implied warranty of Edouard@279: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Edouard@279: * GNU General Public License for more details. Edouard@279: * Edouard@279: * You should have received a copy of the GNU General Public License Edouard@279: * along with this program. If not, see . Edouard@279: * 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: * 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: etisserant@0: public: etisserant@0: symtable_c(void); etisserant@0: msousa@716: void reset(void); /* clear all entries... */ msousa@716: 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: mjsousa@951: /* iterators ... */ mjsousa@951: /* NOTE: These member functions are incorrect, as the returned iterator will not iterate through the inner_scopes!! */ mjsousa@951: /* We simply comment it all out, as they are not currently needed! */ mjsousa@951: #if 0 mjsousa@951: iterator find (const char *identifier_str) {return _base.find(identifier_str);} mjsousa@951: iterator begin() {return _base.begin();} mjsousa@951: const_iterator begin() const {return _base.begin();} mjsousa@951: iterator end() {return _base.end();} mjsousa@951: const_iterator end() const {return _base.end();} mjsousa@951: reverse_iterator rbegin() {return _base.rbegin();} mjsousa@951: const_reverse_iterator rbegin() const {return _base.rbegin();} mjsousa@951: reverse_iterator rend() {return _base.rend();} mjsousa@951: const_reverse_iterator rend() const {return _base.rend();} mjsousa@951: #endif 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 */