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: mjsousa@971: 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: mjsousa@973: void clear(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: mjsousa@971: void set(const char *identifier_str, value_t value); // Will change value associated to string if already in map. Will create new entry if string not in map. mjsousa@971: void set(const symbol_c *symbol, value_t value); // Will change value associated to string if already in map. Will create new entry if string not in map. mjsousa@971: void insert(const char *identifier_str, value_t value); // insert a new (string,value) pair. Give an error if string already in map associated to different value! mjsousa@971: void insert(const symbol_c *symbol, value_t value); // insert a new (string,value) pair. Give an error if string already in map associated to different value! etisserant@0: mjsousa@974: value_t& operator[](const char *identifier_str); mjsousa@974: value_t& operator[](const std::string identifier_str); mjsousa@974: // value_t& operator[](const symbol_c *identifier ); // not yet implemented mjsousa@973: mjsousa@973: /* Since symtable_c does not allow duplicates in each level, count() will return mjsousa@973: * - 0 : if not found in any level mjsousa@973: * - n : number of level containing that entry (max is current number of levels!) mjsousa@973: */ mjsousa@973: int count(const char *identifier_str); mjsousa@973: int count(const std::string identifier_str); mjsousa@973: // int count(const symbol_c *identifier ); // not yet implemented mjsousa@973: mjsousa@971: /* Search for an entry. Will return end() if not found */ mjsousa@973: iterator begin(void); mjsousa@971: iterator end (void); mjsousa@973: iterator find (const char *identifier_str); mjsousa@973: iterator find (const std::string identifier_str); mjsousa@973: iterator find (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@971: iterator find (const char *identifier_str) mjsousa@971: iterator begin() mjsousa@971: const_iterator begin() const mjsousa@971: iterator end() mjsousa@971: const_iterator end() const mjsousa@971: reverse_iterator rbegin() mjsousa@971: const_reverse_iterator rbegin() const mjsousa@971: reverse_iterator rend() mjsousa@971: const_reverse_iterator rend() const 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 */