etisserant@0: /* etisserant@0: * (c) 2005 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 that allows duplicate values. etisserant@0: * etisserant@0: * This is used to create a symbol table of previously defined etisserant@0: * functions. Duplicate are allowed since the standard permits function\ etisserant@0: * overloading in the standard library. etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: #include etisserant@0: #include "symtable.hh" etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* A macro for printing out internal parser errors... */ etisserant@0: #define ERROR error_exit(__FILE__,__LINE__) etisserant@0: /* function defined in main.cc */ etisserant@0: extern void error_exit(const char *file_name, int line_no); etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* clear all entries... */ etisserant@0: template etisserant@0: void dsymtable_c::reset(void) { etisserant@0: _base.clear(); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: template etisserant@0: void dsymtable_c::insert(const char *identifier_str, value_t new_value) { etisserant@0: // std::cout << "store_identifier(" << identifier_str << "): \n"; etisserant@0: std::pair new_element(identifier_str, new_value); etisserant@0: /* iterator res = */ _base.insert(new_element); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: template etisserant@0: void dsymtable_c::insert(const symbol_c *symbol, value_t new_value) { 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: etisserant@0: #if 0 etisserant@0: template etisserant@0: void dsymtable_c::insert_noduplicate(const char *identifier_str, value_t new_value) { etisserant@0: if (find_value(identifier_str) != null_value) etisserant@0: /* already present in the set! */ etisserant@0: ERROR; etisserant@0: etisserant@0: // std::cout << "store_identifier(" << identifier_str << "): \n"; etisserant@0: std::pair new_element(identifier_str, new_value); etisserant@0: /* iterator res = */ _base.insert(new_element); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: template etisserant@0: void dsymtable_c::insert_noduplicate(const symbol_c *symbol, value_t new_value) { etisserant@0: const token_c *name = dynamic_cast(symbol); etisserant@0: if (name == NULL) etisserant@0: ERROR; etisserant@0: insert_noduplicate(name->value, new_value); etisserant@0: } etisserant@0: #endif etisserant@0: etisserant@0: etisserant@0: /* returns null_value if not found! */ etisserant@0: template etisserant@0: value_type dsymtable_c::find_value(const char *identifier_str) { etisserant@0: iterator i = _base.find(identifier_str); etisserant@0: etisserant@0: if (i == _base.end()) etisserant@0: return null_value; etisserant@0: else etisserant@0: return i->second; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: template etisserant@0: value_type dsymtable_c::find_value(const symbol_c *symbol) { etisserant@0: const token_c *name = dynamic_cast(symbol); etisserant@0: if (name == NULL) etisserant@0: ERROR; etisserant@0: return find_value(name->value); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /* debuging function... */ etisserant@0: template etisserant@0: void dsymtable_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: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: