def test_find_in_table(): ST = SymbolTable("") Content1 = {'Type': "int" , 'Attribute': 'static' , 'TokenLocation': (10,2) } ST.InsertSymbol("age", Content1) ST.PushNewScope(); Content2 = {'Type': "float" , 'Attribute': None , 'TokenLocation': (11,2) } ST.InsertSymbol("temperature", Content2) ST.PushNewScope(); Content3 = {'Type': "char" , 'Attribute': None , 'TokenLocation': (13,2) } ST.InsertSymbol("letter", Content3) ST.PushNewScope(); assert( ST.FindSymbolInTable("letter")[0] == {1: Content3} ) assert( ST.FindSymbolInTable("temperature")[0] == {2: Content2} ) assert( (ST.FindSymbolInTable("age")[0] is {3: Content1}) == False ) #nonexistent symbol case assert( ST.FindSymbolInTable("foo") == False ) #searcing in current scope case assert( ST.FindSymbolInCurrentScope("age") == False ) return
def test_push_new_scope(): ST = SymbolTable("") ST2 = SymbolTable("") ST.PushNewScope() ST.PushNewScope() ST.PushNewScope() assert(len(ST2.Table) < len(ST.Table)) assert(len(ST.Table) == 3)
def test_write_symbol_table(): ST = SymbolTable("") ST.DebugMode = True Content1 = {'Type': "int" , 'Attribute': 'static' , 'TokenLocation': (10,2) } ST.InsertSymbol("age", Content1) ST.PushNewScope(); Content2 = {'Type': "float" , 'Attribute': None , 'TokenLocation': (11,2) } ST.InsertSymbol("temperature", Content2) Content2 = {'Type': "float" , 'Attribute': 'static' , 'TokenLocation': (12,2) } ST.InsertSymbol("t2", Content2) ST.PushNewScope(); Content3 = {'Type': "char" , 'Attribute': None , 'TokenLocation': (13,2) } ST.InsertSymbol("letter", Content3) ST.PushNewScope(); ST.WriteSymbolTableToFile("Debug.out")