Merge remote-tracking branch 'origin/master'

# Conflicts:
#	table.c
#	table.h
This commit is contained in:
alejeune 2023-04-06 11:08:57 +02:00
commit de931ee17f
2 changed files with 46 additions and 4 deletions

29
table.c
View file

@ -1,4 +1,22 @@
#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
@ -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;
}

13
table.h
View file

@ -1,7 +1,17 @@
#ifndef TABLE_H
#define TABLE_H
#include <stdio.h>
#include <stdbool.h>
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