While done

This commit is contained in:
Raphaël LACROIX 2023-04-20 11:05:52 +02:00
parent 6927b7da82
commit b354d938ea
3 changed files with 77 additions and 26 deletions

57
table.c
View file

@ -1,11 +1,12 @@
#include "table.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "table.h"
#define VERBOSITY 0 // 1 -> displays the table, 0 no display #define VERBOSITY 0 // 1 -> displays the table, 0 no display
int memorySizes[2] = {1,1}; int memorySizes[2] = {1,1};
int tempCounter = 0; int tempCounter = 0;
int condCounter = 0; // to store whether there is a conditional variable
/*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;
@ -159,15 +160,15 @@ int addTempINTAndGetAddress(){
char name[NAME_MAX_LENGTH]; char name[NAME_MAX_LENGTH];
if (tempCounter == 0){ if (tempCounter == 0){
// we create the first temporary variable and use it // we create the first temporary variable and use it
addElement("0_TEMP",INT); addElement("0_TEMP_INT",INT);
strcpy(name, "0_TEMP"); strcpy(name, "0_TEMP_INT");
} else if (tempCounter == 1) { } else if (tempCounter == 1) {
// we create the second temporary variable and use it // we create the second temporary variable and use it
addElement("1_TEMP",INT); addElement("1_TEMP_INT",INT);
strcpy(name, "1_TEMP"); strcpy(name, "1_TEMP_INT");
} else { } else {
// we use the right temporary variable // we use the right temporary variable
sprintf(name, "%d_TEMP", tempCounter % 2); sprintf(name, "%d_TEMP_INT", tempCounter % 2);
} }
tempCounter++; tempCounter++;
return getOffset(name); return getOffset(name);
@ -238,14 +239,14 @@ void displayTable(){
} }
/*removes all temporary variables used for INTs*/ /*removes all temporary variables used for INTs*/
void suppressTempINTElement(){ void suppressTempINTElements(){
if (tempCounter == 1){ if (tempCounter == 1){
suppressElement("0_TEMP"); suppressElement("0_TEMP_INT");
esp--; esp--;
} else { } else {
if (tempCounter > 1){ if (tempCounter > 1){
suppressElement("0_TEMP"); suppressElement("0_TEMP_INT");
suppressElement("1_TEMP"); suppressElement("1_TEMP_INT");
esp-= 2; esp-= 2;
} }
} }
@ -266,4 +267,38 @@ void line(){
} }
void doubleLine(){ void doubleLine(){
printf("============================================================\n"); printf("============================================================\n");
} }
/*removes all temporary variables used for CONDITIONS*/
void suppressCONDElements(){
if (condCounter == 1){
suppressElement("0_TEMP_COND");
esp--;
} else {
if (condCounter > 1){
suppressElement("0_TEMP_COND");
suppressElement("1_TEMP_COND");
esp-= 2;
}
}
condCounter = 0;
}
/* Adds a temporary Conditional element and returns the offset of it */
int addTempCONDAndGetAddress(){
char name[NAME_MAX_LENGTH];
if (condCounter == 0){
// we create the first temporary variable and use it
addElement("0_TEMP_COND",INT);
strcpy(name, "0_TEMP_COND");
} else if (condCounter == 1) {
// we create the second temporary variable and use it
addElement("1_TEMP_COND",INT);
strcpy(name, "1_TEMP_COND");
} else {
// we use the right temporary variable
sprintf(name, "%d_TEMP_COND", condCounter % 2);
}
condCounter++;
return getOffset(name);
}

View file

@ -64,6 +64,9 @@ int addElementAndGetAddress(char* name, enumVarType type);
/* Adds a temporary Int element and returns the offset of it */ /* Adds a temporary Int element and returns the offset of it */
int addTempINTAndGetAddress(); int addTempINTAndGetAddress();
/* Adds a temporary Conditional element and returns the offset of it */
int addTempCONDAndGetAddress();
/*creates a new structure and updates variables*/ /*creates a new structure and updates variables*/
Symbol createNewStructure(char* name, enumVarType type); Symbol createNewStructure(char* name, enumVarType type);
@ -78,7 +81,10 @@ void setInit(char *name);
void suppressElement(char* name); void suppressElement(char* name);
/*removes all temporary variables used for INTs*/ /*removes all temporary variables used for INTs*/
void suppressTempINTElement(); void suppressTempINTElements();
/*removes all temporary variables used for CONDITIONS*/
void suppressCONDElements();
/*============================ /*============================
Element Access Element Access

38
yacc.y
View file

@ -20,7 +20,7 @@ void yyerror (const char *);
%union {char str[NAME_MAX_LENGTH]; int nbInt; int addr; enumVarType type; } %union {char str[NAME_MAX_LENGTH]; int nbInt; int addr; enumVarType type; }
/*loops keywords*/ /*loops keywords*/
%token tWHILE /*tIF declared below*/ tELSE %token /*tWHILE tIF declared below*/ tELSE
/*reserved keywords*/ /*reserved keywords*/
%token tRETURN tPRINT %token tRETURN tPRINT
/*types : integers, floats or void*/ /*types : integers, floats or void*/
@ -33,7 +33,7 @@ void yyerror (const char *);
/*comparisons*/ /*comparisons*/
%left tLT tGT tNE tEQ tGE tLE %left tLT tGT tNE tEQ tGE tLE
/*boolean operators*/ /*boolean operators*/
%left tAND tOR tNOT %left tAND tOR tNOT
/*syntaxic symbols*/ /*syntaxic symbols*/
%token tLBRACE tRBRACE tLPAR tRPAR tSEMI tCOMMA %token tLBRACE tRBRACE tLPAR tRPAR tSEMI tCOMMA
@ -47,9 +47,11 @@ void yyerror (const char *);
%type <addr> Expression %type <addr> Expression
%type <addr> Declaration %type <addr> Declaration
%type <addr> NbOrVariable %type <addr> NbOrVariable
%token <nbInt> tIF
%type <nbInt> IfStatement1 %type <nbInt> IfStatement1
%token <nbInt> tIF
%token <nbInt> tWHILE
%start Program %start Program
%% %%
@ -61,11 +63,11 @@ Program : FunctionDef
| Line Lines; | Line Lines;
Line : IfStatement Line : IfStatement
| WhileStatement | WhileStatement
| Assignment | Assignment
| Declarations | Declarations
| FunctionCall | FunctionCall
| Return | Return
| Print; | Print;
/*Innerblock = the inside of an if/else/while statement = { ... } or function = f(){...}*/ /*Innerblock = the inside of an if/else/while statement = { ... } or function = f(){...}*/
@ -79,7 +81,7 @@ Condition : tLPAR ConditionalExpression tRPAR;
/*ConditionalExpression = expression that evaluates to a boolean*/ /*ConditionalExpression = expression that evaluates to a boolean*/
ConditionalExpression : tID ConditionalExpression : tID
| tNB | tNB
| tLPAR ConditionalExpression tRPAR // for cases like if((a or b) and (a or c)) where there are parenthesis inside | tLPAR ConditionalExpression tRPAR // for cases like if((a or b) and (a or c)) where there are parenthesis inside
| NbOrVariable NumericalOperator NbOrVariable | NbOrVariable NumericalOperator NbOrVariable
| tNOT ConditionalExpression | tNOT ConditionalExpression
/* replaced by the two following lines /* replaced by the two following lines
@ -93,12 +95,12 @@ ConditionalExpression : tID
/*NbOrVariable is either a number or a variable of type int*/ /*NbOrVariable is either a number or a variable of type int*/
NbOrVariable : tID { $$ = getOffset($1);} NbOrVariable : tID { $$ = getOffset($1);}
| tNB {$$ = operation_afc_nb_tmp($1);}; | tNB {$$ = operation_afc_nb_tmp($1);};
/*List of all numerical operators*/ /*List of all numerical operators*/
NumericalOperator : tLE | tGE | tEQ | tNE | tLT | tGT; NumericalOperator : tLE | tGE | tEQ | tNE | tLT | tGT;
/*any arithmetic operation /*any arithmetic operation
Operation: tADD | tMUL | tSUB | tDIV; Operation: tADD | tMUL | tSUB | tDIV;
*/ */
IfStatement1 : %empty { IfStatement1 : %empty {
@ -114,11 +116,19 @@ IfStatement : tIF Condition IfStatement1 InnerBlock tELSE {
int current = getCurrentLineNumber(); printf("current Line %d", current); setJumpLine($3, current); int current = getCurrentLineNumber(); printf("current Line %d", current); setJumpLine($3, current);
}; };
WhileStatement : tWHILE {} WhileStatement : tWHILE {
Condition InnerBlock {}; $1 = getCurrentLineNumber();
} Condition {
addLine("JMPF");
} InnerBlock {
addLine("JMP");
int current = getCurrentLineNumber();
setJumpLine($1, current);
setJumpLine(current-1, $1);
};
Assignment : tID tASSIGN Expression tSEMI { Assignment : tID tASSIGN Expression tSEMI {
setInit($1); operation_copy(getOffset($1),$3); suppressTempINTElement(); setInit($1); operation_copy(getOffset($1),$3); suppressTempINTElements();
}; };
/*Expression operation applied on variables or values*/ /*Expression operation applied on variables or values*/
@ -161,9 +171,9 @@ Type : tINT {$$ = INT;}
Declarations : Type { t = $1; } Declaration Declarations1 tSEMI ; Declarations : Type { t = $1; } Declaration Declarations1 tSEMI ;
Declaration : tID {addElement($1, (enumVarType) t);} Declaration : tID {addElement($1, (enumVarType) t);}
| tID {addElement($1, (enumVarType) t); setInit($1);} tASSIGN Expression {operation_copy(getOffset($1),$4); suppressTempINTElement();} ; | tID {addElement($1, (enumVarType) t); setInit($1);} tASSIGN Expression {operation_copy(getOffset($1),$4); suppressTempINTElements();} ;
Declarations1 : tCOMMA Declaration Declarations1 | {} ; Declarations1 : tCOMMA Declaration Declarations1 | %empty ;
Return : tRETURN Expression tSEMI {}; Return : tRETURN Expression tSEMI {};