From 2b9f6bde6901c02f8a5a7c3ad66233934f6e692b Mon Sep 17 00:00:00 2001 From: Nahom Date: Wed, 31 Mar 2021 21:08:45 +0200 Subject: [PATCH] Table des symboles --- .DS_Store | Bin 0 -> 8196 bytes Table_Symboles/main.c | 13 +++++ Table_Symboles/table_symboles.c | 100 ++++++++++++++++++++++++++++++++ Table_Symboles/table_symboles.h | 73 +++++++++++++++++++++++ 4 files changed, 186 insertions(+) create mode 100644 .DS_Store create mode 100644 Table_Symboles/main.c create mode 100644 Table_Symboles/table_symboles.c create mode 100644 Table_Symboles/table_symboles.h diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..306d8668200b39c3f41b181ae8f6404ac2832933 GIT binary patch literal 8196 zcmeI1O>Wab6vyB61Dv2jgeo=cyg_17MG2r*2qC2*U35_i;R+Uj(l`lCRL5gthY&&_ z8}0zyfD^D{#flSf4>$n)=YuG=Tbc?9ftZnI{*&>0Z)Seu#Cv1<_YX%GfL%0@gxYn>>Su866!2c}(_PBEBr$8}qn zL(5txN|ie?nbdLRF8%fQj2Qnwdfgjs3AvNh$Ba4-UEm@?-Ow@rQ(E$=u?Do5$y@qB4WukQIK94 zTT4XD&lyq7;39G(zC!l;H{-Pv@xCSErO6$=<6%6GJb^beCtzvf1@IcC_aRBg?RT-` zL(qH-t~i@`e919xA4uvhJiH_91*cj24Be$|+M+7$LJQQTW?}|GBJL65Zs}n)0nWRC z`zXOq?3r0Jdp=Kl^3=qgRU-%c?&1u&BoW4EqYv<1nb+4Hgn@or_1O))P8b!7U)aLp z((;+JmX*zBSF(?sHXk@qJ?e**dhdbwbf+G*UESZ~+?)36?!8WHP|K~}=6+P~_^qCF z?lfC4d3e9$H~FB#`+hT&aU6BQvND-WEtemSN;fvv?W?8Yc-(@5M!dllym@M$pnbY^J#~7%$NdAW1uV?-f95<**ASS4z@oCri~N84`1k*FL^39ZfFba& z5@1VJr&_`4fBsF8+=$4vZPa~KQH0$@sdPaj({ZRw$Dz}I7@}{(RQQy&PLyH<#k>fR M7))ab{80iw0Ol7#i~s-t literal 0 HcmV?d00001 diff --git a/Table_Symboles/main.c b/Table_Symboles/main.c new file mode 100644 index 0000000..ad357d1 --- /dev/null +++ b/Table_Symboles/main.c @@ -0,0 +1,13 @@ +#include +#include "table_symboles.h" + +int main() { + Table_Symboles T; + initialise_table(&T); + add_symbole_top(&T, "toto", 0, 1); + add_symbole_top(&T, "titi", 1, 0); + add_symbole_bottom(&T, "titi", 0, 1); + print_table(&T); + + return 0; +} diff --git a/Table_Symboles/table_symboles.c b/Table_Symboles/table_symboles.c new file mode 100644 index 0000000..38c74a2 --- /dev/null +++ b/Table_Symboles/table_symboles.c @@ -0,0 +1,100 @@ +// +// Created by Nahom Belay on 31/03/2021. +// + +#include "table_symboles.h" +#include +#include + + +void initialise_table(Table_Symboles * table){ + table->indexAvailableBottom = TABLE_SIZE - 1; + table->indexAvailableTop = 0; +} + +int variable_exists(Table_Symboles * table, char * varName){ + for (int i = 0; i < table->indexAvailableTop; i++){ + if (strcmp(varName, table->array[i].Variable_Name) == 0){ + return -1; + } + } + + for (int i = (table->indexAvailableBottom + 1); i < TABLE_SIZE; i++){ + if (strcmp(varName, table->array[i].Variable_Name) == 0){ + return -1; + } + } + return 0; +} +int add_symbole_top(Table_Symboles * table, char * varName, enum Symbole_Type type, enum Initialised_Variable init){ + Symbole symbole; + symbole.Variable_Name = varName; + symbole.addr = table->indexAvailableTop; + symbole.init = init; + symbole.type = type; + if (table->indexAvailableTop >= table->indexAvailableBottom){ + return -1; + } else if (variable_exists(table, varName) == -1){ + return -2; + } else { + table->array[table->indexAvailableTop] = symbole; + table->indexAvailableTop++; + } + return 0; +} + +int add_symbole_bottom(Table_Symboles * table, char * varName, enum Symbole_Type type, enum Initialised_Variable init){ + Symbole symbole; + symbole.Variable_Name = varName; + symbole.addr = table->indexAvailableBottom; + symbole.init = init; + symbole.type = type; + if (table->indexAvailableTop >= table->indexAvailableBottom){ + return -1; + } else if (variable_exists(table, varName) == 1){ + return -2; + } else { + table->array[table->indexAvailableBottom] = symbole; + table->indexAvailableBottom--; + } + return 0; +} + +void print_symbole(Symbole * symbole){ + char * var = symbole->Variable_Name; + int addr = symbole->addr; + int type = symbole->type; + char * typeStr; + if (type == 0){ + typeStr = "INT"; + } else{ + typeStr = "CONST_INT"; + } + int init = symbole->init; + char * initStr; + if (type == 0){ + initStr = "INITIALISED"; + } else{ + initStr = "NOT_INITIALISED"; + } + printf("%-20s\t\t %-12s\t\t %-12d\t %-12s\n", var, typeStr, addr, initStr); +} + +void print_table(Table_Symboles * table){ + printf("%-20s\t\t %-12s\t\t %-12s\t %-12s\n", "Variable Name", "Type", "Address", "Initialised"); + int indexTop = table->indexAvailableTop; + int indexBottom = table->indexAvailableBottom; + Symbole symbole; + for (int i = 0; i < indexTop; i++){ + symbole = table->array[i]; + print_symbole(&symbole); + } + if (table->indexAvailableBottom != TABLE_SIZE - 1){ + printf("%-20s\t\t %-12s\t\t %-12s\t %-12s\n", "...", "...", "...", "..."); + for (int i = (indexBottom + 1); i < TABLE_SIZE; i++){ + symbole = table->array[i]; + print_symbole(&symbole); + } + } + +} diff --git a/Table_Symboles/table_symboles.h b/Table_Symboles/table_symboles.h new file mode 100644 index 0000000..a070d8f --- /dev/null +++ b/Table_Symboles/table_symboles.h @@ -0,0 +1,73 @@ +// +// Created by Nahom Belay on 31/03/2021. +// + +#ifndef TABLE_SYMBOLES_H +#define TABLE_SYMBOLES_H + +#define TABLE_SIZE 50 + +enum Symbole_Type {TYPE_INT , TYPE_CONST_INT}; +enum Initialised_Variable{INITIALISED , NOT_INITIALISED}; + +typedef struct Symboles { + char * Variable_Name; + int addr ; + enum Symbole_Type type; + enum Initialised_Variable init; +} Symbole; + +typedef struct Table_Symboles { + Symbole array[TABLE_SIZE]; + int indexAvailableTop; + int indexAvailableBottom; +} Table_Symboles; + +/** + * Initialises indexAvailableTop at 0 and indexAvailableBottom at TABLE_SIZE - 1 + * @param table + */ +void initialise_table(Table_Symboles * table); + +/** + * Adds a symbole at the top (regular varaibles) + * @param table + * @param varName + * @param type + * @param init + * @return if symbole added successfully, -1 if the table is full and -2 if the varaible already exists in the table + */ +int add_symbole_top(Table_Symboles * table, char * varName, enum Symbole_Type type , enum Initialised_Variable init); + +/** + * Adds a symbole at the bottom (temp variables) + * @param table + * @param varName + * @param type + * @param init + * @return 0 if symbole added successfully, -1 if the table is full and -2 if the varaible already exists in the table + */ +int add_symbole_bottom(Table_Symboles * table, char * varName, enum Symbole_Type type , enum Initialised_Variable init); + +/** + * Verifies if a varaible name is already present in the table to avoid duplicates + * @param table + * @param varName + * @return -1 if the varaible name exists, 0 if it doesn't + */ +int variable_exists(Table_Symboles * table, char * varName); + +/** + * Prints a symbole with this format + * varName | Type | Address | Initialised/Not_Initialised + * @param symbole + */ +void print_symbole(Symbole * symbole); + +/** + * Prints the table + * @param table + */ +void print_table(Table_Symboles * table); + +#endif TABLE_SYMBOLES_H