added getters and setters
This commit is contained in:
parent
fbb27f6d31
commit
45187b58a0
2 changed files with 87 additions and 3 deletions
84
table.c
84
table.c
|
@ -1,2 +1,86 @@
|
||||||
#include <table.h>
|
#include <table.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
6
table.h
6
table.h
|
@ -1,14 +1,14 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
enum enumVarType {INT, FLOAT, CHAR, STRING};
|
enum enumVarType {INT, FLOAT}; // a adapter
|
||||||
|
|
||||||
struct symbol {
|
typedef struct {
|
||||||
char* name;
|
char* name;
|
||||||
bool init;
|
bool init;
|
||||||
enumVarType varType;
|
enumVarType varType;
|
||||||
int offset;
|
int offset;
|
||||||
int deep;
|
int deep;
|
||||||
};
|
} Symbol;
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue