merged coding styles
This commit is contained in:
parent
de931ee17f
commit
2d954f1a29
2 changed files with 122 additions and 88 deletions
190
table.c
190
table.c
|
@ -1,12 +1,113 @@
|
||||||
#include <table.h>
|
#include <symbolTablele.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
/*At the start of the execution : the whole array is empty*/
|
/*At the start of the execution : the whole array is empty*/
|
||||||
symbolTable = malloc(sizeof(symbol) * START_TABLE_SIZE)
|
symbolTable = malloc(sizeof(symbol) * START_TABLE_SIZE);
|
||||||
|
|
||||||
currentIndex = 0;
|
currentIndex = 0;
|
||||||
maxIndex = START_TABLE_SIZE;
|
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*/
|
/*Checks for the length of the array and reallocates if necessary*/
|
||||||
void checkArraySanity(){
|
void checkArraySanity(){
|
||||||
if (currentIndex == maxIndex){
|
if (currentIndex == maxIndex){
|
||||||
|
@ -18,90 +119,7 @@ void checkArraySanity(){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*reallocates the array with the specified size*/
|
||||||
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){
|
void reallocateArray(int size){
|
||||||
Symbol* newSymbolTable = malloc(sizeof (symbol) * size); // we double the size of the array
|
Symbol* newSymbolTable = malloc(sizeof (symbol) * size); // we double the size of the array
|
||||||
Symbol* oldSymbolTable = symbolTable; // we double the size of the array
|
Symbol* oldSymbolTable = symbolTable; // we double the size of the array
|
||||||
|
|
20
table.h
20
table.h
|
@ -9,19 +9,35 @@ enum enumVarType {INT, FLOAT}; // a adapter
|
||||||
#define START_TABLE_SIZE 128;
|
#define START_TABLE_SIZE 128;
|
||||||
|
|
||||||
/*indexes in the array*/
|
/*indexes in the array*/
|
||||||
static int currentIndex;
|
static int currentIndex; // the next to index to be used
|
||||||
static int maxIndex;
|
static int maxIndex;
|
||||||
static symbol* symbolTable;
|
static symbol* symbolTable;
|
||||||
|
|
||||||
|
// stack pointers
|
||||||
|
static int esp;
|
||||||
|
static int ebp;
|
||||||
|
|
||||||
|
static int currentDepth;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char* name;
|
char* name;
|
||||||
bool init;
|
bool init;
|
||||||
enumVarType varType;
|
enumVarType varType;
|
||||||
int offset;
|
int offset;
|
||||||
int deep;
|
int depth;
|
||||||
} Symbol;
|
} Symbol;
|
||||||
|
|
||||||
|
// TODO : move comments here
|
||||||
|
|
||||||
void reallocateArray(int size);
|
void reallocateArray(int size);
|
||||||
void checkArraySanity();
|
void checkArraySanity();
|
||||||
|
void suppressElement(Symbol element);
|
||||||
|
int initOffset(int index);
|
||||||
|
void addElement(Symbol element);
|
||||||
|
void toggleInit(char *name);
|
||||||
|
int getIndex(char* name);
|
||||||
|
Symbol getStruct(char* name);
|
||||||
|
void error(char* mess);
|
||||||
|
int getOffset(char* name);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue