diff --git a/table.c b/table.c index 74f8c5f..43980ff 100644 --- a/table.c +++ b/table.c @@ -1,6 +1,24 @@ #include +#include -/* + +/*At the start of the execution : the whole array is empty*/ +symbolTable = malloc(sizeof(symbol) * START_TABLE_SIZE) +currentIndex = 0; +maxIndex = START_TABLE_SIZE; + +/*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); + } + } +} + +/* Suppress variable dont la profondeur est plus grande add element */ @@ -32,7 +50,7 @@ Symbol getStruct(Symbol* tab, int endingIndex, char* name){ /* Récupérer l'index d'une valeur dans le tableau*/ int getIndex(Symbol* tab, int endingIndex, char* name){ for(int i=0; i < endingIndex; i++){ - if (strcmp(tab[i].name, &name) == 0){ + if (strcmp(tab[i].name, &name) == 0){ return i; } } @@ -54,7 +72,7 @@ void addElement(Symbol *tab, int endingIndex, Symbol element){ tab[endingIndex].name = element.name; tab[endingIndex].init = element.init; tab[endingIndex].varType = element.varType; - tab[endingIndex].offset = initOffset(&tab, endingIndex -1); + tab[endingIndex].offset = initOffset(&tab, endingIndex -1); tab[endingIndex].deep = element.deep; } endingIndex ++; // faudra la mettre en variable globale @@ -84,3 +102,14 @@ suppressElement(Symbol *tab, int endingIndex, Symbol element){ endingIndex --; // variable globale } +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; +} \ No newline at end of file diff --git a/table.h b/table.h index def0186..6999196 100644 --- a/table.h +++ b/table.h @@ -1,7 +1,17 @@ +#ifndef TABLE_H +#define TABLE_H + #include #include -enum enumVarType {INT, FLOAT}; // a adapter +enum enumVarType {INT, FLOAT}; // a adapter +/*defined constants*/ +#define START_TABLE_SIZE 128; + +/*indexes in the array*/ +static int currentIndex; +static int maxIndex; +static symbol* symbolTable; typedef struct { char* name; @@ -11,4 +21,7 @@ typedef struct { int deep; } Symbol; +void reallocateArray(int size); +void checkArraySanity(); +#endif