void initializeSymbolTable (void) { int i; symbolTable.currentLevel = 0; symbolTable.scopeDisplayElementCount = 256; symbolTable.scopeDisplay = (SymbolTableEntry **)malloc(sizeof(SymbolTableEntry *) * 256); symbolTable.scopeDisplay[0] = newSymbolTableEntry(0); for(i = 0; i < HASH_TABLE_SIZE; i++) symbolTable.hashTable[i] = newSymbolTableEntry(0); SymbolAttribute *int1, *float1, *void1, *read, *fread, *write; Parameter *w; int1 = (SymbolAttribute *)malloc(sizeof(SymbolAttribute)); float1 = (SymbolAttribute *)malloc(sizeof(SymbolAttribute)); void1 = (SymbolAttribute *)malloc(sizeof(SymbolAttribute)); read = (SymbolAttribute *)malloc(sizeof(SymbolAttribute)); fread = (SymbolAttribute *)malloc(sizeof(SymbolAttribute)); write = (SymbolAttribute *)malloc(sizeof(SymbolAttribute)); int1->attributeKind = TYPE_ATTRIBUTE; float1->attributeKind = TYPE_ATTRIBUTE; void1->attributeKind = TYPE_ATTRIBUTE; read->attributeKind = FUNCTION_SIGNATURE; fread->attributeKind = FUNCTION_SIGNATURE; write->attributeKind = FUNCTION_SIGNATURE; int1->attr.typeDescriptor = (TypeDescriptor *)malloc(sizeof(TypeDescriptor)); float1->attr.typeDescriptor = (TypeDescriptor *)malloc(sizeof(TypeDescriptor)); void1->attr.typeDescriptor = (TypeDescriptor *)malloc(sizeof(TypeDescriptor)); read->attr.functionSignature = (FunctionSignature *)malloc(sizeof(FunctionSignature)); fread->attr.functionSignature = (FunctionSignature *)malloc(sizeof(FunctionSignature)); write->attr.functionSignature = (FunctionSignature *)malloc(sizeof(FunctionSignature)); w = (Parameter *)malloc(sizeof(Parameter)); w->type = (TypeDescriptor *)malloc(sizeof(TypeDescriptor)); w->next = NULL; w->type->kind = SCALAR_TYPE_DESCRIPTOR; w->type->properties.dataType = VOID_TYPE; w->parameterName = "yooo"; int1->attr.typeDescriptor->kind = SCALAR_TYPE_DESCRIPTOR; float1->attr.typeDescriptor->kind = SCALAR_TYPE_DESCRIPTOR; void1->attr.typeDescriptor->kind = SCALAR_TYPE_DESCRIPTOR; read->attr.functionSignature->parametersCount = 0; fread->attr.functionSignature->parametersCount = 0; write->attr.functionSignature->parametersCount = 1; read->attr.functionSignature->parameterList = NULL; fread->attr.functionSignature->parameterList = NULL; write->attr.functionSignature->parameterList = w; read->attr.functionSignature->returnType = INT_TYPE; fread->attr.functionSignature->returnType = FLOAT_TYPE; write->attr.functionSignature->returnType = VOID_TYPE; int1->attr.typeDescriptor->properties.dataType = INT_TYPE; float1->attr.typeDescriptor->properties.dataType = FLOAT_TYPE; void1->attr.typeDescriptor->properties.dataType = VOID_TYPE; enterSymbol(SYMBOL_TABLE_INT_NAME, int1); enterSymbol(SYMBOL_TABLE_FLOAT_NAME, float1); enterSymbol(SYMBOL_TABLE_VOID_NAME, void1); enterSymbol(SYMBOL_TABLE_SYS_LIB_READ, read); enterSymbol(SYMBOL_TABLE_SYS_LIB_FREAD, fread); enterSymbol(SYMBOL_TABLE_SYS_LIB_WRITE, write); }
void openScope (void) { symbolTable.currentLevel++; if (symbolTable.currentLevel >= symbolTable.scopeDisplayElementCount) { symbolTable.scopeDisplayElementCount *= 2; symbolTable.scopeDisplay = (SymbolTableEntry **)realloc(symbolTable.scopeDisplay, sizeof(SymbolTableEntry *) * symbolTable.scopeDisplayElementCount); } symbolTable.scopeDisplay[symbolTable.currentLevel] = newSymbolTableEntry(0); }
SymbolTableEntry *enterSymbol (char *symbolName, SymbolAttribute *attribute) { SymbolTableEntry *newSymbol = newSymbolTableEntry(symbolTable.currentLevel); newSymbol->name = (char *)malloc(sizeof(char) * (strlen(symbolName)+1)); strncpy(newSymbol->name, symbolName, strlen(symbolName)); newSymbol->attribute = attribute; enterIntoHashChain(HASH(newSymbol->name), newSymbol); return newSymbol; }
SymbolTableEntry* enterSymbol(char* symbolName, SymbolAttribute* attribute) { int hashIndex = HASH(symbolName); SymbolTableEntry* hashChain = symbolTable.hashTable[hashIndex]; SymbolTableEntry* newEntry = newSymbolTableEntry(symbolTable.currentLevel); newEntry->attribute = attribute; newEntry->name = symbolName; if(attribute->attributeKind == VARIABLE_ATTRIBUTE) { if(symbolTable.currentLevel != 0) { if(attribute->attr.typeDescriptor->kind == SCALAR_TYPE_DESCRIPTOR) { ARoffset -= 4; }else { int i, size = 4; ArrayProperties temp = attribute->attr.typeDescriptor->properties.arrayProperties; for(i=0; i<temp.dimension; i++) size *= temp.sizeInEachDimension[i]; ARoffset -= size; } } newEntry->offset = ARoffset; } while(hashChain) { if(strcmp(hashChain->name, symbolName) == 0) { if(hashChain->nestingLevel == symbolTable.currentLevel) { printf("void enterSymbol(...): ID \'%s\' is redeclared(at the same level#%d).\n", symbolName, symbolTable.currentLevel); free(newEntry); return NULL; } else { removeFromHashTrain(hashIndex, hashChain); newEntry->sameNameInOuterLevel = hashChain; break; } } else { hashChain = hashChain->nextInHashChain; } } enterIntoHashTrain(hashIndex, newEntry); newEntry->nextInSameLevel = symbolTable.scopeDisplay[symbolTable.currentLevel]; symbolTable.scopeDisplay[symbolTable.currentLevel] = newEntry; return newEntry; }
SymbolTableEntry* enterSymbol(const char * symbolName, SymbolAttribute* attribute){ if(declaredLocally(symbolName)){ return NULL; } int hashv = HASH(symbolName); SymbolTableEntry * entry = newSymbolTableEntry(); entry->next = symbolTable.tables->hashTable[hashv]; symbolTable.tables->hashTable[hashv] = entry; if(entry->next != NULL){ entry->next->prev = entry; } entry->prev = NULL; entry->name = symbolName; entry->symbolAttribute = attribute; return entry; }
SymbolTableEntry* enterSymbol(char* symbolName, SymbolAttribute* attribute) { int hashIndex = HASH(symbolName); SymbolTableEntry* hashChain = symbolTable.hashTable[hashIndex]; SymbolTableEntry* newEntry = newSymbolTableEntry(symbolTable.currentLevel); newEntry->attribute = attribute; newEntry->name = symbolName; if(attribute->attributeKind == VARIABLE_ATTRIBUTE) { newEntry->offset = ARoffset; ARoffset -= calc_size(attribute->attr.typeDescriptor); } while(hashChain) { if(strcmp(hashChain->name, symbolName) == 0) { if(hashChain->nestingLevel == symbolTable.currentLevel) { printf("void enterSymbol(...): ID \'%s\' is redeclared(at the same level#%d).\n", symbolName, symbolTable.currentLevel); free(newEntry); return NULL; } else { removeFromHashTrain(hashIndex, hashChain); newEntry->sameNameInOuterLevel = hashChain; break; } } else { hashChain = hashChain->nextInHashChain; } } enterIntoHashTrain(hashIndex, newEntry); newEntry->nextInSameLevel = symbolTable.scopeDisplay[symbolTable.currentLevel]; symbolTable.scopeDisplay[symbolTable.currentLevel] = newEntry; return newEntry; }
SymbolTableEntry* enterSymbol(char* symbolName, SymbolAttribute* attribute) { SymbolTableEntry* oldsym = retrieveSymbol(symbolName); if(oldsym != NULL && oldsym.nestingLevel == sybolTable.currentLevel) { printf("Error: %s has been redecalred!"); g_anyErrorOccur = 1; return oldsym; } //new entry SymbolTableEntry* newsym = newSymbolTableEntry(symbolName, sybolTable.currentLevel); newsym.attribute = attribute;//the space of attribute may be changed,need to be careful. //add to scope display newsym.nextInSameLevel = symbolTable.scopeDisplay[symbolTable.currentLevel]; symbolTable.scopeDisplay[symbolTable.currentLevel] = newsym; //add to hash table if(oldsym == NULL) enterIntoHashTrain(newsym); else { removeFromHashTrain(oldsym); enterIntoHashTrain(newsym); } newsym.sameNameInOuterLevel = oldsym; }