diff --git a/Makefile b/Makefile index 4cc4dda..e871dee 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ BIN=out CC=gcc CFLAGS=-Wall -g -OBJ=yacc.tab.o lex.yy.o table.o operations.o blocs.o +OBJ=yacc.tab.o lex.yy.o table.o operations.o blocs.o asmTable.o all: $(BIN) @touch testFile # to prevent an error in case of deletion diff --git a/asmTable.c b/asmTable.c index 253cf61..c8eb7d4 100644 --- a/asmTable.c +++ b/asmTable.c @@ -1,32 +1,50 @@ +#include #include "asmTable.h" +#include "stdlib.h" +#include -int labelCounter = 0; +static int labelCounter = 0; /*At the start of the execution : the whole array is empty*/ -static char** table; +static char** asmTable; static int lineCounter = 0; static int maxIndex = START_TABLE_SIZE; #include "asmTable.h" +/* Error display */ +void error(char* mess){ + printf("ERROR : %s\n", mess); + exit(-1); +} + /* /!\ To be called at the beginning * Initializes the array of Symbols*/ void initASMTable(){ - table = malloc(sizeof(Symbol) * START_TABLE_SIZE); + asmTable = malloc(sizeof(char) * LINE_MAX_LENGTH * START_TABLE_SIZE); } -/*resets the symbol table*/ -void resetASMTable(){ - currentIndex = 0; - maxIndex = START_TABLE_SIZE; - -// stack pointers - esp = 0; - ebp = 0; - - currentDepth = 0; +/*Checks for the length of the array and reallocates if necessary*/ +void checkArraySanity(){ + if (lineCounter == maxIndex){ + reallocateArray(maxIndex * 2); + } } -// todo inserer parser détecter les labels +/*reallocates the array with the specified size*/ +void reallocateArray(int size){ + char **temp = (char **)realloc(asmTable, (sizeof(char) * LINE_MAX_LENGTH * size)); + if (temp != NULL){ + asmTable = temp; + } + else { + error("Cannot allocate more memory.\n"); + } +} + +void addLine(char* s) { + strcpy(asmTable[lineCounter],s); + lineCounter++; +} diff --git a/asmTable.h b/asmTable.h index a64fd12..805f385 100644 --- a/asmTable.h +++ b/asmTable.h @@ -20,9 +20,7 @@ void checkArraySanity(); /* /!\ To be called at the beginning * Initializes the array of Symbols*/ -void initSymbolTable(); +void initASMTable(); -/*resets the symbol table*/ -void resetSymboltable(); #endif //PROJET_SYSTEMES_INFORMATIQUES_ASMTABLE_H diff --git a/table.h b/table.h index d6e653e..798a038 100644 --- a/table.h +++ b/table.h @@ -39,6 +39,8 @@ void initSymbolTable(); /*resets the symbol table*/ void resetSymboltable(); +void addLine(char* s); + /*============================ Element Management