115 lines
No EOL
3.1 KiB
C
115 lines
No EOL
3.1 KiB
C
#include <table.h>
|
|
#include <stdlib.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;
|
|
|
|
/*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 */
|
|
|
|
/* a avoir
|
|
tableau, length, esp, indexTab */
|
|
|
|
/* Fonction d'affichage des erreurs */
|
|
void error(char * mess){
|
|
printf("ERROR : %s\n", &mess);
|
|
exit(-1);
|
|
}
|
|
|
|
|
|
/* Récupérer l'addresse à partir du nom */
|
|
int getOffset(int esp, char* name, Symbol *tab, int endingIndex){
|
|
return (esp + getStruct(&tab, endingIndex, &name).offset);
|
|
}
|
|
|
|
/* Récupérer la structure d'un élement à partir du nom */
|
|
Symbol getStruct(Symbol* tab, int endingIndex, char* name){
|
|
for(int i=0; i < endingIndex; i++){
|
|
if (tab[i].name == name){
|
|
return tab[i];
|
|
}
|
|
}
|
|
error("No structure found");
|
|
}
|
|
|
|
/* 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){
|
|
return i;
|
|
}
|
|
}
|
|
error("No index found");
|
|
}
|
|
|
|
|
|
/* Toggle le init */
|
|
void toggleInit(char *name, Symbol *tab, int endingIndex){
|
|
tab[getIndex(&tab, endingIndex, &name)].init = true;
|
|
}
|
|
|
|
/* Ajoute un élément */
|
|
void addElement(Symbol *tab, int endingIndex, Symbol element){
|
|
if (endingIndex == 0){
|
|
tab[0] = element;
|
|
}
|
|
else {
|
|
tab[endingIndex].name = element.name;
|
|
tab[endingIndex].init = element.init;
|
|
tab[endingIndex].varType = element.varType;
|
|
tab[endingIndex].offset = initOffset(&tab, endingIndex -1);
|
|
tab[endingIndex].deep = element.deep;
|
|
}
|
|
endingIndex ++; // faudra la mettre en variable globale
|
|
}
|
|
|
|
/* Calcule l'offset à partir de l'offset de l'élément précédent */
|
|
int initOffset(Symbol *tab, int index){
|
|
int offset = 0;
|
|
switch (tab[index].varType){
|
|
case INT:
|
|
return (tab[index].offset + 1);
|
|
break;
|
|
case FLOAT:
|
|
return (tab[index].offset + 1);
|
|
break;
|
|
default:
|
|
error("Impossible to compute the nexw offset");
|
|
}
|
|
}
|
|
|
|
|
|
/* Supprimer un élément de la table des symboles */
|
|
suppressElement(Symbol *tab, int endingIndex, Symbol element){
|
|
for(int i = getIndex(&tab, endingIndex, element.name); i < (endingIndex - 1); i ++){
|
|
tab[i] = tab[i+1];
|
|
}
|
|
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;
|
|
} |