133 lines
No EOL
3.3 KiB
C
133 lines
No EOL
3.3 KiB
C
#include <symbolTablele.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
|
|
/*At the start of the execution : the whole array is empty*/
|
|
symbolTable = malloc(sizeof(symbol) * START_TABLE_SIZE);
|
|
|
|
currentIndex = 0;
|
|
maxIndex = START_TABLE_SIZE;
|
|
|
|
currentDepth = 0;
|
|
|
|
/* Error display */
|
|
void error(char* mess){
|
|
printf("ERROR : %s\n", &mess);
|
|
exit(-1);
|
|
}
|
|
|
|
/* Returns the offset from EBP to the symbol in the stack */
|
|
int getOffset(char* name){
|
|
return (ebp + getStruct(name).offset);
|
|
}
|
|
|
|
/* Returns the structure with this name */
|
|
Symbol getStruct(char* name){
|
|
for(int i=0; i < currentIndex; i++){
|
|
if (symbolTable[i].name == name){
|
|
return symbolTable[i];
|
|
}
|
|
}
|
|
error("No structure found");
|
|
}
|
|
|
|
/* Returns the index with this name*/
|
|
int getIndex(char* name){
|
|
for(int i=0; i < currentIndex; i++){
|
|
if (strcmp(symbolTable[i].name, &name) == 0){
|
|
return i;
|
|
}
|
|
}
|
|
error("No index found");
|
|
}
|
|
|
|
/* removes all symbols associated with the current Depth*/
|
|
void clearOutOfScopeVariable(){
|
|
for(int i=0; i < currentIndex; i++){
|
|
if (symbolTable[i].depth == currentDepth){
|
|
suppressElement(symbolTable[i]);
|
|
}
|
|
}
|
|
error("No index found");
|
|
}
|
|
|
|
|
|
/* Toggles the init state of the symbol */
|
|
void toggleInit(char *name){
|
|
symbolTable[getIndex(&symbolTable, currentIndex, &name)].init = true;
|
|
}
|
|
|
|
/* Adds an element */
|
|
void addElement(Symbol element){
|
|
|
|
//checks for overflow
|
|
checkArraySanity();
|
|
|
|
if (currentIndex == 0){
|
|
symbolTable[0] = element;
|
|
} else {
|
|
symbolTable[currentIndex].name = element.name;
|
|
symbolTable[currentIndex].init = element.init;
|
|
symbolTable[currentIndex].varType = element.varType;
|
|
symbolTable[currentIndex].offset = initOffset(currentIndex -1); // TODO replace with ESP
|
|
symbolTable[currentIndex].depth = element.depth;
|
|
}
|
|
|
|
//TODO add ESP increment
|
|
|
|
currentIndex ++;
|
|
}
|
|
|
|
|
|
// TODO replace with ESP
|
|
/* Calcule l'offset à partir de l'offset de l'élément précédent */
|
|
int initOffset(int index){
|
|
switch (symbolTable[index].varType){
|
|
case INT:
|
|
return (symbolTable[index].offset + 1);
|
|
break;
|
|
case FLOAT:
|
|
return (symbolTable[index].offset + 1);
|
|
break;
|
|
default:
|
|
error("Impossible to compute the new offset");
|
|
}
|
|
}
|
|
|
|
|
|
/* Removes an element */
|
|
void suppressElement(Symbol element){
|
|
for(int i = getIndex(element.name); i < (currentIndex - 1); i ++){
|
|
symbolTable[i] = symbolTable[i+1];
|
|
}
|
|
currentIndex --;
|
|
|
|
// checks if there is a need to reduce the size of the array
|
|
checkArraySanity();
|
|
}
|
|
|
|
|
|
/*Checks for the length of the array and reallocates if necessary*/
|
|
void checkArraySanity(){
|
|
if (currentIndex == maxIndex){
|
|
reallocateArray(maxIndex * 2);
|
|
} else {
|
|
if (currentIndex < maxIndex / 2){
|
|
reallocateArray(maxIndex / 2);
|
|
}
|
|
}
|
|
}
|
|
|
|
/*reallocates the array with the specified size*/
|
|
void reallocateArray(int size){
|
|
Symbol* newSymbolTable = malloc(sizeof (symbol) * size); // we double the size of the array
|
|
Symbol* oldSymbolTable = symbolTable; // we double the size of the array
|
|
free(oldSymbolTable);
|
|
|
|
for(int i = 0; i < currentIndex; i++){
|
|
newSymbolTable[i] = oldSymbolTable[i];
|
|
}
|
|
|
|
symbolTable = newSymbolTable;
|
|
} |