void test_code() { /* * * class Test { * int a, b; * int fun(int c) { * a = c; * b = 2*c; * } * } * */ SymTable global; Entity cls_test("Test", "class"); SymTable *classTable = cls_test.getSymTable(); global.insert(&cls_test); Entity a("a", "int"); Entity b("b", "int"); Entity meth_fun("fun", "meth"); Entity c("c", "int"); classTable->insert(&a); classTable->insert(&b); classTable->insert(&meth_fun); SymTable *methTable = meth_fun.getSymTable(); methTable->find("params")->getSymTable()->insert(&c); assert(true); global.printTable(); //TODO }
void test_ClassSymbolTableInsertion() { /* class hello {} */ SymTable global; Entity s("hello", "int"); global.insert(&s); assert(global.find("hello")==&s); }
void test_SymbolTableHasName() { SymTable global; Entity s("hello", "int"); global.insert(&s); assert(global.hasName("hello")); assert(!global.hasName("yes")); }
SymTable* Parser::ParseRecDecl(vector<string>* &flds) { vector<string> names; Token t1 = sc.GetNextToken(); t = t1; SymTable* fldTable = new SymTable(); while(t.GetValue() != "end") { do { t1 = t; t = sc.GetNextToken(); if (t1.GetType() != identifier && t1.GetValue() != "," && t1.GetValue() != ":") throw Error("incorrect record declaration", t); if(t1.GetType() == identifier) { Check(t1.GetValue(), fldTable); names.push_back(t1.GetValue()); } } while(t.GetValue() != ":"); SymType* type = ParseType(false); for (vector<string>::iterator it = names.begin(); it != names.end(); ++it) { if (fldTable->find(*it) != fldTable->end()) throw Error("incorrect record declaration", t); fldTable->insert(SymElem (*it, new SymVarLocal(*it, type))); flds->push_back((*it)); } names.clear(); t = sc.GetNextToken(); t = RequireToken(";" , "\";\" expected"); } return fldTable; }
SymTable* Parser::ParseArguments(vector<string>* &arguments) { SymTable* argTable = new SymTable; if (t.GetValue() != "(") return argTable; vector<string> argNames; bool isVar = false; do { t = sc.GetNextToken(); if (t.GetValue() == "var") { isVar = true; t = sc.GetNextToken(); } if (t.GetType() != identifier) throw Error("incorrect name of argument", t); argNames.push_back(t.GetValue()); arguments->push_back(t.GetValue()); t = sc.GetNextToken(); if (t.GetValue() == ",") continue; if (t.GetValue() == ":") { SymType* Type = ParseType(false); if (IsExplType(Type->GetName())) throw Error("arguments type mismatch", t); t = sc.GetNextToken(); for(vector<string>::iterator it= argNames.begin(); it != argNames.end(); ++it) { if (argTable->find(*it) != argTable->end()) throw Error("identifier already declared",t); if (isVar) argTable->insert(pair<string, SymVar*> (*it, new SymVarParam_var(*it, Type))); else argTable->insert(pair<string, SymVar*> (*it, new SymVarParam(*it, Type))); } isVar = false; argNames.clear(); if (t.GetValue() != ";" && t.GetValue() != ")") throw Error("incorrect function defenition", t); } } while(t.GetValue() != ")"); t = sc.GetNextToken(); return argTable; }
void test_ScopeStackInsert() { SymTable t; Entity s("a", "class"); t.insert(&s); ScopeStack stack; stack.push(&t); assert(stack.getEntityByName("a")==&s); }
void test_offset() { SymTable global; Entity A("A", "class"); SymTable *A_tab = A.getSymTable(); global.insert(&A); Entity a("a", "int"), b("b", "char"), c("c", "int"), d("d", "char"); Entity meth("func", "meth"); A_tab->insert(&a); A_tab->insert(&b); A_tab->insert(&meth); SymTable *meth_tab = meth.getSymTable(); SymTable *p_tab = meth_tab->find("params")->getSymTable(); p_tab->insert(&c); p_tab->insert(&d); }