diff --git a/table.c b/table.c index d343cbb..0ee714d 100644 --- a/table.c +++ b/table.c @@ -1,11 +1,12 @@ -#include "table.h" #include #include +#include "table.h" #define VERBOSITY 0 // 1 -> displays the table, 0 no display int memorySizes[2] = {1,1}; 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*/ static Symbol* symbolTable; @@ -159,15 +160,15 @@ int addTempINTAndGetAddress(){ char name[NAME_MAX_LENGTH]; if (tempCounter == 0){ // we create the first temporary variable and use it - addElement("0_TEMP",INT); - strcpy(name, "0_TEMP"); + addElement("0_TEMP_INT",INT); + strcpy(name, "0_TEMP_INT"); } else if (tempCounter == 1) { // we create the second temporary variable and use it - addElement("1_TEMP",INT); - strcpy(name, "1_TEMP"); + addElement("1_TEMP_INT",INT); + strcpy(name, "1_TEMP_INT"); } else { // we use the right temporary variable - sprintf(name, "%d_TEMP", tempCounter % 2); + sprintf(name, "%d_TEMP_INT", tempCounter % 2); } tempCounter++; return getOffset(name); @@ -238,14 +239,14 @@ void displayTable(){ } /*removes all temporary variables used for INTs*/ -void suppressTempINTElement(){ +void suppressTempINTElements(){ if (tempCounter == 1){ - suppressElement("0_TEMP"); + suppressElement("0_TEMP_INT"); esp--; } else { if (tempCounter > 1){ - suppressElement("0_TEMP"); - suppressElement("1_TEMP"); + suppressElement("0_TEMP_INT"); + suppressElement("1_TEMP_INT"); esp-= 2; } } @@ -266,4 +267,38 @@ void line(){ } void doubleLine(){ printf("============================================================\n"); -} \ No newline at end of file +} + +/*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); +} diff --git a/table.h b/table.h index 3d4f4e7..6bc27c6 100644 --- a/table.h +++ b/table.h @@ -64,6 +64,9 @@ int addElementAndGetAddress(char* name, enumVarType type); /* Adds a temporary Int element and returns the offset of it */ int addTempINTAndGetAddress(); +/* Adds a temporary Conditional element and returns the offset of it */ +int addTempCONDAndGetAddress(); + /*creates a new structure and updates variables*/ Symbol createNewStructure(char* name, enumVarType type); @@ -78,7 +81,10 @@ void setInit(char *name); void suppressElement(char* name); /*removes all temporary variables used for INTs*/ -void suppressTempINTElement(); +void suppressTempINTElements(); + +/*removes all temporary variables used for CONDITIONS*/ +void suppressCONDElements(); /*============================ Element Access diff --git a/yacc.y b/yacc.y index 631ad43..f4a7342 100644 --- a/yacc.y +++ b/yacc.y @@ -20,7 +20,7 @@ void yyerror (const char *); %union {char str[NAME_MAX_LENGTH]; int nbInt; int addr; enumVarType type; } /*loops keywords*/ -%token tWHILE /*tIF declared below*/ tELSE +%token /*tWHILE tIF declared below*/ tELSE /*reserved keywords*/ %token tRETURN tPRINT /*types : integers, floats or void*/ @@ -33,7 +33,7 @@ void yyerror (const char *); /*comparisons*/ %left tLT tGT tNE tEQ tGE tLE /*boolean operators*/ -%left tAND tOR tNOT +%left tAND tOR tNOT /*syntaxic symbols*/ %token tLBRACE tRBRACE tLPAR tRPAR tSEMI tCOMMA @@ -47,9 +47,11 @@ void yyerror (const char *); %type Expression %type Declaration %type NbOrVariable -%token tIF %type IfStatement1 +%token tIF +%token tWHILE + %start Program %% @@ -61,11 +63,11 @@ Program : FunctionDef | Line Lines; Line : IfStatement - | WhileStatement + | WhileStatement | Assignment | Declarations | FunctionCall - | Return + | Return | Print; /*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 : tID | 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 | tNOT ConditionalExpression /* replaced by the two following lines @@ -93,12 +95,12 @@ ConditionalExpression : tID /*NbOrVariable is either a number or a variable of type int*/ NbOrVariable : tID { $$ = getOffset($1);} | tNB {$$ = operation_afc_nb_tmp($1);}; - + /*List of all numerical operators*/ NumericalOperator : tLE | tGE | tEQ | tNE | tLT | tGT; - /*any arithmetic operation -Operation: tADD | tMUL | tSUB | tDIV; + /*any arithmetic operation +Operation: tADD | tMUL | tSUB | tDIV; */ IfStatement1 : %empty { @@ -114,11 +116,19 @@ IfStatement : tIF Condition IfStatement1 InnerBlock tELSE { int current = getCurrentLineNumber(); printf("current Line %d", current); setJumpLine($3, current); }; -WhileStatement : tWHILE {} - Condition InnerBlock {}; +WhileStatement : tWHILE { + $1 = getCurrentLineNumber(); +} Condition { + addLine("JMPF"); +} InnerBlock { + addLine("JMP"); + int current = getCurrentLineNumber(); + setJumpLine($1, current); + setJumpLine(current-1, $1); +}; 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*/ @@ -161,9 +171,9 @@ Type : tINT {$$ = INT;} Declarations : Type { t = $1; } Declaration Declarations1 tSEMI ; 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 {};