util/symtable.cc
changeset 973 f86d5d6bb04e
parent 971 8aee27d46208
child 974 a47c2df5ae3d
equal deleted inserted replaced
972:bc90dd4bbf4f 973:f86d5d6bb04e
    45 symtable_c<value_type>::symtable_c(void) {inner_scope = NULL;}
    45 symtable_c<value_type>::symtable_c(void) {inner_scope = NULL;}
    46 
    46 
    47 
    47 
    48  /* clear all entries... */
    48  /* clear all entries... */
    49 template<typename value_type>
    49 template<typename value_type>
    50 void symtable_c<value_type>::reset(void) {
    50 void symtable_c<value_type>::clear(void) {
    51   _base.clear();
    51   _base.clear();
    52 }
    52 }
    53 
    53 
    54  /* create new inner scope */
    54  /* create new inner scope */
    55 template<typename value_type>
    55 template<typename value_type>
   140   insert(name->value, new_value);
   140   insert(name->value, new_value);
   141 }
   141 }
   142 
   142 
   143 
   143 
   144 template<typename value_type>
   144 template<typename value_type>
   145 typename symtable_c<value_type>::iterator symtable_c<value_type>::end(void) {return _base.end();}
   145 int symtable_c<value_type>::count(const       char *identifier_str) {return _base.count(identifier_str)+((inner_scope == NULL)?0:inner_scope->count(identifier_str));}
       
   146 template<typename value_type>
       
   147 int symtable_c<value_type>::count(const std::string identifier_str) {return _base.count(identifier_str)+((inner_scope == NULL)?0:inner_scope->count(identifier_str));}
       
   148 
       
   149 
       
   150 // in the operator[] we delegate to find(), since that method will also search in the inner scopes!
       
   151 template<typename value_type>
       
   152 typename symtable_c<value_type>::value_t symtable_c<value_type>::operator[] (const       char *identifier_str) {return find(identifier_str)->second;}
       
   153 template<typename value_type>
       
   154 typename symtable_c<value_type>::value_t symtable_c<value_type>::operator[] (const std::string identifier_str) {return find(identifier_str)->second;}
       
   155 
       
   156 
       
   157 template<typename value_type>
       
   158 typename symtable_c<value_type>::iterator symtable_c<value_type>::end  (void) {return _base.end  ();}
       
   159 
       
   160 template<typename value_type>
       
   161 typename symtable_c<value_type>::iterator symtable_c<value_type>::begin(void) {return _base.begin();}
   146 
   162 
   147 /* returns end() if not found! */
   163 /* returns end() if not found! */
   148 template<typename value_type>
   164 template<typename value_type>
   149 typename symtable_c<value_type>::iterator symtable_c<value_type>::find(const char *identifier_str) {
   165 typename symtable_c<value_type>::iterator symtable_c<value_type>::find(const       char *identifier_str) {
   150   if (inner_scope != NULL) {
   166   iterator i;
   151     iterator i = inner_scope->find(identifier_str);
   167   if ((inner_scope != NULL) && ((i = inner_scope->find(identifier_str)) != inner_scope->end()))  // NOTE: must use the end() value of the inner scope!
   152     if (i != inner_scope->end())  // NOTE: must use the end() value of the inner scope!
   168       return i;  // found in the lower level
   153       /* found in the lower level */
       
   154       return i;
       
   155   }
       
   156 
       
   157   /* if no lower level, or not found in lower level... */
   169   /* if no lower level, or not found in lower level... */
   158   return _base.find(identifier_str);
   170   return _base.find(identifier_str);
   159 }
   171 }
   160 
   172 
   161 
   173 
   162 template<typename value_type>
   174 template<typename value_type>
   163 typename symtable_c<value_type>::iterator symtable_c<value_type>::find(const symbol_c *symbol) {
   175 typename symtable_c<value_type>::iterator symtable_c<value_type>::find(const std::string identifier_str) {
       
   176   iterator i;
       
   177   if ((inner_scope != NULL) && ((i = inner_scope->find(identifier_str)) != inner_scope->end()))  // NOTE: must use the end() value of the inner scope!
       
   178       return i;  // found in the lower level
       
   179   /* if no lower level, or not found in lower level... */
       
   180   return _base.find(identifier_str);
       
   181 }
       
   182 
       
   183 
       
   184 template<typename value_type>
       
   185 typename symtable_c<value_type>::iterator symtable_c<value_type>::find(const   symbol_c *symbol) {
   164   const token_c *name = dynamic_cast<const token_c *>(symbol);
   186   const token_c *name = dynamic_cast<const token_c *>(symbol);
   165   if (name == NULL)
   187   if (name == NULL)
   166     ERROR;
   188     ERROR;
   167   return find(name->value);
   189   return find(name->value);
   168 }
   190 }