Merge remote-tracking branch 'origin/master'
# Conflicts: # table.c # table.h
This commit is contained in:
commit
de931ee17f
2 changed files with 46 additions and 4 deletions
35
table.c
35
table.c
|
@ -1,6 +1,24 @@
|
|||
#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 */
|
||||
|
||||
|
@ -32,7 +50,7 @@ Symbol getStruct(Symbol* tab, int endingIndex, char* name){
|
|||
/* 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){
|
||||
if (strcmp(tab[i].name, &name) == 0){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
@ -54,7 +72,7 @@ void addElement(Symbol *tab, int endingIndex, Symbol element){
|
|||
tab[endingIndex].name = element.name;
|
||||
tab[endingIndex].init = element.init;
|
||||
tab[endingIndex].varType = element.varType;
|
||||
tab[endingIndex].offset = initOffset(&tab, endingIndex -1);
|
||||
tab[endingIndex].offset = initOffset(&tab, endingIndex -1);
|
||||
tab[endingIndex].deep = element.deep;
|
||||
}
|
||||
endingIndex ++; // faudra la mettre en variable globale
|
||||
|
@ -84,3 +102,14 @@ suppressElement(Symbol *tab, int endingIndex, Symbol element){
|
|||
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;
|
||||
}
|
15
table.h
15
table.h
|
@ -1,7 +1,17 @@
|
|||
#ifndef TABLE_H
|
||||
#define TABLE_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
enum enumVarType {INT, FLOAT}; // a adapter
|
||||
enum enumVarType {INT, FLOAT}; // a adapter
|
||||
/*defined constants*/
|
||||
#define START_TABLE_SIZE 128;
|
||||
|
||||
/*indexes in the array*/
|
||||
static int currentIndex;
|
||||
static int maxIndex;
|
||||
static symbol* symbolTable;
|
||||
|
||||
typedef struct {
|
||||
char* name;
|
||||
|
@ -11,4 +21,7 @@ typedef struct {
|
|||
int deep;
|
||||
} Symbol;
|
||||
|
||||
void reallocateArray(int size);
|
||||
void checkArraySanity();
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue