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: /* 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, etc... etisserant@0: * Note that the list of previously defined funstions uses the etisserant@0: * dsymtable_c instead, as it requires the table to store duplicate values. etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: #include etisserant@0: #include "symtable.hh" msousa@596: #include "../main.hh" // required for ERROR() and ERROR_MSG() macros. etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: mjsousa@971: template mjsousa@971: symtable_c::symtable_c(void) {inner_scope = NULL;} etisserant@0: etisserant@0: etisserant@0: /* clear all entries... */ mjsousa@971: template mjsousa@973: void symtable_c::clear(void) { etisserant@0: _base.clear(); etisserant@0: } etisserant@0: etisserant@0: /* create new inner scope */ mjsousa@971: template mjsousa@971: void symtable_c::push(void) { etisserant@0: if (inner_scope != NULL) { etisserant@0: inner_scope->push(); etisserant@0: } else { etisserant@0: inner_scope = new symtable_c(); etisserant@0: } etisserant@0: } etisserant@0: etisserant@0: /* clear most inner scope */ etisserant@0: /* returns 1 if this is the inner most scope */ etisserant@0: /* 0 otherwise */ mjsousa@971: template mjsousa@971: int symtable_c::pop(void) { etisserant@0: if (inner_scope != NULL) { etisserant@0: if (inner_scope->pop() == 1) { etisserant@0: delete inner_scope; etisserant@0: inner_scope = NULL; etisserant@0: } etisserant@0: return 0; etisserant@0: } else { etisserant@0: _base.clear(); etisserant@0: return 1; etisserant@0: } etisserant@0: } etisserant@0: mjsousa@971: template mjsousa@971: void symtable_c::set(const symbol_c *symbol, value_t new_value) { etisserant@0: if (inner_scope != NULL) { etisserant@0: inner_scope->set(symbol, new_value); etisserant@0: return; etisserant@0: } etisserant@0: etisserant@0: const token_c *name = dynamic_cast(symbol); etisserant@0: if (name == NULL) etisserant@0: ERROR; etisserant@0: set(name->value, new_value); etisserant@0: } etisserant@0: etisserant@0: mjsousa@971: template mjsousa@971: void symtable_c::set(const char *identifier_str, value_t new_value) { etisserant@0: if (inner_scope != NULL) { etisserant@0: inner_scope->set(identifier_str, new_value); etisserant@0: return; etisserant@0: } etisserant@0: etisserant@0: // std::cout << "set_identifier(" << identifier_str << "): \n"; etisserant@0: iterator i = _base.find(identifier_str); etisserant@0: if (i == _base.end()) etisserant@0: /* identifier not already in map! */ etisserant@0: ERROR; etisserant@0: etisserant@0: _base[identifier_str] = new_value; etisserant@0: } etisserant@0: mjsousa@971: template mjsousa@971: void symtable_c::insert(const char *identifier_str, value_t new_value) { etisserant@0: if (inner_scope != NULL) { etisserant@0: inner_scope->insert(identifier_str, new_value); etisserant@0: return; etisserant@0: } etisserant@0: etisserant@0: // std::cout << "store_identifier(" << identifier_str << "): \n"; mjsousa@952: iterator i = _base.find(identifier_str); mjsousa@952: if ((i != _base.end()) && (i->second != new_value)) {ERROR;} /* error inserting new identifier: identifier already in map associated to a different value */ mjsousa@952: if ((i != _base.end()) && (i->second == new_value)) {return;} /* identifier already in map associated with the same value */ mjsousa@952: etisserant@0: std::pair new_element(identifier_str, new_value); etisserant@0: std::pair res = _base.insert(new_element); mjsousa@952: if (!res.second) {ERROR;} /* unknown error inserting new identifier */ etisserant@0: } etisserant@0: mjsousa@971: template mjsousa@971: void symtable_c::insert(const symbol_c *symbol, value_t new_value) { etisserant@0: /* etisserant@0: // not required... etisserant@0: if (inner_scope != NULL) { etisserant@0: inner_scope->insert(symbol, new_value); etisserant@0: return; etisserant@0: } etisserant@0: */ etisserant@0: const token_c *name = dynamic_cast(symbol); etisserant@0: if (name == NULL) etisserant@0: ERROR; etisserant@0: insert(name->value, new_value); etisserant@0: } etisserant@0: etisserant@0: mjsousa@971: template mjsousa@973: int symtable_c::count(const char *identifier_str) {return _base.count(identifier_str)+((inner_scope == NULL)?0:inner_scope->count(identifier_str));} mjsousa@973: template mjsousa@973: int symtable_c::count(const std::string identifier_str) {return _base.count(identifier_str)+((inner_scope == NULL)?0:inner_scope->count(identifier_str));} mjsousa@973: mjsousa@973: mjsousa@973: // in the operator[] we delegate to find(), since that method will also search in the inner scopes! mjsousa@973: template mjsousa@974: typename symtable_c::value_t& symtable_c::operator[] (const char *identifier_str) {iterator i = find(identifier_str); return (i!=end())?i->second:_base[identifier_str];} mjsousa@974: template mjsousa@974: typename symtable_c::value_t& symtable_c::operator[] (const std::string identifier_str) {iterator i = find(identifier_str); return (i!=end())?i->second:_base[identifier_str];} mjsousa@973: mjsousa@973: mjsousa@973: template mjsousa@973: typename symtable_c::iterator symtable_c::end (void) {return _base.end ();} mjsousa@973: mjsousa@973: template mjsousa@973: typename symtable_c::iterator symtable_c::begin(void) {return _base.begin();} etisserant@0: mjsousa@971: /* returns end() if not found! */ mjsousa@971: template mjsousa@973: typename symtable_c::iterator symtable_c::find(const char *identifier_str) { mjsousa@973: iterator i; mjsousa@973: if ((inner_scope != NULL) && ((i = inner_scope->find(identifier_str)) != inner_scope->end())) // NOTE: must use the end() value of the inner scope! mjsousa@973: return i; // found in the lower level etisserant@0: /* if no lower level, or not found in lower level... */ mjsousa@971: return _base.find(identifier_str); etisserant@0: } etisserant@0: etisserant@0: mjsousa@971: template mjsousa@973: typename symtable_c::iterator symtable_c::find(const std::string identifier_str) { mjsousa@973: iterator i; mjsousa@973: if ((inner_scope != NULL) && ((i = inner_scope->find(identifier_str)) != inner_scope->end())) // NOTE: must use the end() value of the inner scope! mjsousa@973: return i; // found in the lower level mjsousa@973: /* if no lower level, or not found in lower level... */ mjsousa@973: return _base.find(identifier_str); mjsousa@973: } mjsousa@973: mjsousa@973: mjsousa@973: template mjsousa@973: typename symtable_c::iterator symtable_c::find(const symbol_c *symbol) { etisserant@0: const token_c *name = dynamic_cast(symbol); etisserant@0: if (name == NULL) etisserant@0: ERROR; mjsousa@971: return find(name->value); etisserant@0: } etisserant@0: etisserant@0: mjsousa@971: etisserant@0: /* debuging function... */ mjsousa@971: template mjsousa@971: void symtable_c::print(void) { etisserant@0: for(iterator i = _base.begin(); etisserant@0: i != _base.end(); etisserant@0: i++) etisserant@0: std::cout << i->second << ":" << i->first << "\n"; etisserant@0: std::cout << "=====================\n"; etisserant@0: etisserant@0: if (inner_scope != NULL) { etisserant@0: inner_scope->print(); etisserant@0: } etisserant@0: } etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: