void test_ScopeStackInsert() { SymTable t; Entity s("a", "class"); t.insert(&s); ScopeStack stack; stack.push(&t); assert(stack.getEntityByName("a")==&s); }
Name getCurrentName(const ScopeStack& scopeStack) { Name name = Name::Absolute(); for (size_t i = 0; i < scopeStack.size(); i++) { const auto& element = scopeStack[i]; if (element.hasName()) { name = name + element.name(); } } return name; }
int main() { std::cout << "\n Testing Scope Stack" << "\n =====================\n"; std::cout << "\n pushing items onto ScopeStack instance after adding startLineCount" << "\n --------------------------------------------------------------------"; ScopeStack<element*> testStack; element* pItem = new element; pItem->type = "function"; pItem->name = "fun1"; pItem->startLineCount = 33; testStack.push(pItem); pItem = new element; pItem->type = "if"; pItem->name = ""; pItem->startLineCount = 38; (testStack.top()->_children).push_back(pItem); testStack.push(pItem); pItem = new element; pItem->type = "for"; pItem->name = ""; pItem->startLineCount = 40; (testStack.top()->_children).push_back(pItem); testStack.push(pItem); showStack<element>(testStack, false); std::cout << std::endl; std::cout << "\n Popping two items off ScopeStack after adding endLineCount" << "\n ------------------------------------------------------------"; testStack.top()->endLineCount = 50; std::cout << "\n " << testStack.pop()->show(); testStack.top()->endLineCount = 55; std::cout << "\n " << testStack.pop()->show(); std::cout << "\n\n Pushing another item onto ScopeStack after adding startLineElement" << "\n --------------------------------------------------------------------"; pItem = new element; pItem->type = "while"; pItem->name = ""; pItem->startLineCount = 60; (testStack.top()->_children).push_back(pItem); testStack.push(pItem); std::cout << "\n Stack now contains:" << "\n ---------------------"; showStack<element>(testStack, false); std::cout << "\n\n Popping last elements off stack after adding endLineCount"; std::cout << "\n -----------------------------------------------------------"; testStack.top()->endLineCount = 70; element* pTop = testStack.pop(); std::cout << "\n " << pTop->show(); testStack.top()->endLineCount = 75; pTop = testStack.pop(); std::cout << "\n " << pTop->show(); std::cout << "\n\n Walking simulated AST tree:" << "\n -----------------------------"; TreeWalk(pTop); std::cout << "\n\n"; }
void newScope( void ) { _scopeStack.push_back( SymbolSet() ); }
bool isInCurrentScope( const std::string &varName ) { if ( _scopeStack.empty() ) return false; SymbolSet &symbolSet = _scopeStack.back(); return symbolSet.find( varName ) != symbolSet.end(); }
int main(int argc, char* argv[]) { std::cout << "\n Demonstrating Scope Analysis\n " << std::string(30,'=') << std::endl; if(argc < 2) { std::cout << "\n please enter name of file to process on command line\n\n"; return 1; } for(int i=1; i<argc; ++i) { std::cout << "\n Processing file " << argv[i]; std::cout << "\n " << std::string(16 + strlen(argv[i]),'-'); try { Toker toker(argv[i]); SemiExp se(&toker); ScopeStack<element> stack; while(se.get()) { if(se.length() < 1) continue; //std::cout << se.show().c_str() << std::endl; if(se[se.length() - 1] == "}") { stack.pop(); showStack<element>(stack); } if(se.length() < 3) continue; if(se[se.length() - 1] == "{") { element item; size_t pos = se.find("("); if(pos < se.length()) { if(isSpecialKeyWord(se[pos-1])) item.type = "control"; else item.type = "function"; item.name = se[pos-1]; stack.push(item); showStack<element>(stack); } else { item.type = se[se.length()-3]; item.name = se[se.length()-2]; stack.push(item); showStack<element>(stack); } } } std::cout << "\n\n"; } catch(std::exception ex) { std::cout << "\n " << ex.what() << "\n\n"; } } }