/* template version. This is going to be #incl in the .h file * this file contains the method definitions. * * See SymTab.h for interface documentation * * Modified 2/18/05 by CMB * Added "typename" keyword for iterators in template functions * to get it to compile with g++ 3.3.2 * * Modified 10/25/04 by CMB * Fixed bug in dumpKeys. (Before did not work for multi-scope SymTab.) * */ template void SymTab::add(Symbol *key, ValueType *value) { bindings[key] = value; // destroys old binding if key already exists } template ValueType *SymTab::lookupCurr(Symbol *key) const { typename Bindings::const_iterator iter = bindings.find(key); if (iter != bindings.end()) { return iter->second; // the value that goes with key } else { return NULL; } } template ValueType *SymTab::lookup(Symbol *key) const { ValueType *valPtr; if ((valPtr = lookupCurr(key))) { // found in innermost block return valPtr; } else if (!outer) { // else if we're at the outermost block return NULL; // return not found } else { // else check enclosing block recursively return outer->lookup(key); } } // uses recursion template void SymTab::dumpHelper(ostream &o) const { typename Bindings::const_iterator ci; for (ci = bindings.begin(); ci != bindings.end(); ci++) { o << " " << ci->first << " " << *(ci->second) << endl; } if (outer) { o << "Scope:" << endl; outer->dumpHelper(o); } // else base case: outer == NULL --> don't make a recursive call // because this is the outermost scope } // calls recursive dumpHelper template void SymTab::dump(ostream &o) const { o << "Innermost Scope:" << endl; dumpHelper(o); } // could have written dump and dumpKeys the same way. // but this one uses a loop instead. template void SymTab::dumpKeys(ostream &o) const { const SymTab *scope = this; // need const to be able to use "this" as value o << "Innermost "; while (scope != NULL) { // uses a loop instead of recursion o << "Scope:" << endl; typename Bindings::const_iterator ci; for (ci = scope->bindings.begin(); ci != scope->bindings.end(); ci++) { o << " " << ci->first << endl; } scope = scope->outer; } }