diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..306d866 Binary files /dev/null and b/.DS_Store differ 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