Table des symboles
This commit is contained in:
parent
6ab787820b
commit
2b9f6bde69
4 changed files with 186 additions and 0 deletions
BIN
.DS_Store
vendored
Normal file
BIN
.DS_Store
vendored
Normal file
Binary file not shown.
13
Table_Symboles/main.c
Normal file
13
Table_Symboles/main.c
Normal file
|
@ -0,0 +1,13 @@
|
|||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
100
Table_Symboles/table_symboles.c
Normal file
100
Table_Symboles/table_symboles.c
Normal file
|
@ -0,0 +1,100 @@
|
|||
//
|
||||
// Created by Nahom Belay on 31/03/2021.
|
||||
//
|
||||
|
||||
#include "table_symboles.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
73
Table_Symboles/table_symboles.h
Normal file
73
Table_Symboles/table_symboles.h
Normal file
|
@ -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
|
Loading…
Reference in a new issue