This commit is contained in:
Raphaël LACROIX 2023-04-13 10:03:02 +02:00
parent 1a3d71e8bf
commit 1a8e9766a0
4 changed files with 19 additions and 12 deletions

View file

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
int memorySizes[2] = {1,1};
/*At the start of the execution : the whole array is empty*/ /*At the start of the execution : the whole array is empty*/
static Symbol* symbolTable; static Symbol* symbolTable;
@ -85,8 +86,8 @@ void clearOutOfScopeVariable(){
} }
/* Toggles the init state of the symbol */ /* sets the init state of the symbol to true */
void toggleInit(char *name){ void setInit(char *name){
symbolTable[getIndex(name)].init = true; symbolTable[getIndex(name)].init = true;
} }

10
table.h
View file

@ -12,7 +12,7 @@
typedef enum enumVarType {INT, FLOAT} enumVarType; typedef enum enumVarType {INT, FLOAT} enumVarType;
// a list of all type's sizes // a list of all type's sizes
extern int memorySizes[2] = {1,1}; // TODO : PROBLEM DOES'NT COMPILE extern int memorySizes[2]; // TODO : PROBLEM DOES'NT COMPILE
typedef struct { typedef struct {
char name[NAME_MAX_LENGTH]; char name[NAME_MAX_LENGTH];
@ -56,8 +56,8 @@ Symbol createNewStructure(char* name, enumVarType type);
Element Edition Element Edition
============================*/ ============================*/
/* Toggles the init state of the symbol */ /* sets the init state of the symbol to true */
void toggleInit(char *name); void setInit(char *name);
/*============================ /*============================
Element Access Element Access
@ -91,4 +91,8 @@ void error(char* mess);
void line(); void line();
void doubleLine(); void doubleLine();
/*displays the entire table at this moment including all information
* regarding the symbols and the current depth*/
void displayTable();
#endif #endif

View file

@ -1,4 +1,6 @@
int compute(int a, int d) { int compute(int a, int d) {
int b;
int c;
b = a; b = a;
while (c > 0) { while (c > 0) {
b = b + a * 4; b = b + a * 4;

14
yacc.y
View file

@ -11,8 +11,8 @@
void yyerror (const char *); void yyerror (const char *);
} }
// TODO : PROBLEM DOES'NT COMPILE (enumVarType doenst exist) // TODO : PROBLEM DOES'NT COMPILE (enumVarType doesn't exist)
%union {int nbInt; enumVarType int type; char* string;} %union {int nbInt; /*enumVarType*/ int type; char* string;}
/*loops keywords*/ /*loops keywords*/
%token tWHILE tIF tELSE %token tWHILE tIF tELSE
/*reserved keywords*/ /*reserved keywords*/
@ -91,7 +91,7 @@ IfStatement : tIF Condition InnerBlock
WhileStatement : tWHILE Condition InnerBlock; WhileStatement : tWHILE Condition InnerBlock;
Assignment : tID tASSIGN Expression tSEMI {toggleInit($1);}; Assignment : tID tASSIGN Expression tSEMI {setInit($1);};
/*Expression operation applied on variables or values*/ /*Expression operation applied on variables or values*/
Expression : NbOrVariable Expression : NbOrVariable
@ -125,11 +125,11 @@ VarsWithType : VarWithType
VarWithType : Type tID; VarWithType : Type tID;
/*the return type or argument type*/ /*the return type or argument type*/
Type : tINT {$$ = INT;} Type : tINT {$$ = /*INT*/ 0;}
| tFLOAT {$$ = FLOAT;}; | tFLOAT {$$ = /*FLOAT*/ 1;};
Declaration : Type tID tSEMI {addElement($2, $1);} Declaration : Type tID tSEMI {addElement($2, (enumVarType) $1);}
| Type tID tASSIGN Expression tSEMI {addElement($2, $1);toggleInit($2);} | Type tID tASSIGN Expression tSEMI {addElement($2, $1);setInit($2);}
/* Potential improvement : take care of multiple definition on same line*/ /* Potential improvement : take care of multiple definition on same line*/
| Type tID tCOMMA VarsCommaSeparated tSEMI {yyerror("[Beta] you cannot -still- define several variable on the same line");} | Type tID tCOMMA VarsCommaSeparated tSEMI {yyerror("[Beta] you cannot -still- define several variable on the same line");}