Compare commits
2 commits
a669e59abc
...
de931ee17f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de931ee17f | ||
|
|
45187b58a0 |
2 changed files with 87 additions and 4 deletions
84
table.c
84
table.c
|
|
@ -18,6 +18,90 @@ void checkArraySanity(){
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
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
|
||||
|
|
|
|||
7
table.h
7
table.h
|
|
@ -4,6 +4,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
enum enumVarType {INT, FLOAT}; // a adapter
|
||||
/*defined constants*/
|
||||
#define START_TABLE_SIZE 128;
|
||||
|
||||
|
|
@ -12,15 +13,13 @@ static int currentIndex;
|
|||
static int maxIndex;
|
||||
static symbol* symbolTable;
|
||||
|
||||
enum enumVarType {INT, FLOAT, CHAR, STRING};
|
||||
|
||||
struct symbol {
|
||||
typedef struct {
|
||||
char* name;
|
||||
bool init;
|
||||
enumVarType varType;
|
||||
int offset;
|
||||
int deep;
|
||||
};
|
||||
} Symbol;
|
||||
|
||||
void reallocateArray(int size);
|
||||
void checkArraySanity();
|
||||
|
|
|
|||
Loading…
Reference in a new issue