From 42e264b8d5c61da4c3a8ea0ff63f5bd0e7adb0e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20LACROIX?= Date: Tue, 18 Apr 2023 09:44:04 +0200 Subject: [PATCH] Added operations --- Makefile | 2 +- operations.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ table.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- table.h | 16 +++++++++++++--- yacc.y | 33 ++++++++++++++++++--------------- 5 files changed, 126 insertions(+), 20 deletions(-) create mode 100644 operations.h diff --git a/Makefile b/Makefile index f87ce60..20f7e57 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ BIN=out CC=gcc CFLAGS=-Wall -g -OBJ=yacc.tab.o lex.yy.o table.o +OBJ=yacc.tab.o lex.yy.o table.o operations.o all: $(BIN) @touch testFile # to prevent an error in case of deletion diff --git a/operations.h b/operations.h new file mode 100644 index 0000000..9d46602 --- /dev/null +++ b/operations.h @@ -0,0 +1,47 @@ +// +// Created by chepycou on 4/14/23. +// + +#ifndef PROJET_SYSTEMES_INFORMATIQUES_OPERATIONS_H +#define PROJET_SYSTEMES_INFORMATIQUES_OPERATIONS_H + +/*prints the ASM instruction for the addition computation + * and returns the address of the temporary variable*/ +int operation_add(int addr1, int addr2); + +/*prints the ASM instruction for the subtraction computation + * and returns the address of the temporary variable*/ +int operation_sub(int addr1, int addr2); + +/*prints the ASM instruction for the multiplication computation + * and returns the address of the temporary variable*/ +int operation_mul(int addr1, int addr2); + +/*prints the ASM instruction for the integer division computation + * and returns the address of the temporary variable*/ +int operation_divInt(int addr1, int addr2); + +/*prints the ASM instruction for the remainder computation + * and returns the address of the temporary variable*/ +int operation_divRem(int addr1, int addr2); + +/*prints the ASM instruction for the affection of a variable + * EX : + * a = 2; + */ +void operation_afc_nb(int addr, int value); + +/*prints the ASM instruction for the affection of a temporary variable + * EX : + * "1_TEMP = 2" + * and returns the address of the temp variable*/ +int operation_afc_nb_tmp(int value); + +/*prints the ASM instruction for the affection of a temporary variable + * EX : + * a = b; + */ +void operation_afc_addr(int addr1, int addr2); + + +#endif //PROJET_SYSTEMES_INFORMATIQUES_OPERATIONS_H diff --git a/table.c b/table.c index bfbd20f..29c3f8a 100644 --- a/table.c +++ b/table.c @@ -5,6 +5,7 @@ #define VERBOSITY 1 // 1 -> displays the table, 0 no display int memorySizes[2] = {1,1}; +int tempCounter = 0; /*At the start of the execution : the whole array is empty*/ static Symbol* symbolTable; @@ -40,7 +41,7 @@ int getOffset(char* name){ /* Returns the structure with this name */ Symbol getStruct(char* name){ for(int i=0; i < currentIndex; i++){ - if (symbolTable[i].name == name){ + if (strcmp(symbolTable[i].name, name) == 0){ return symbolTable[i]; } } @@ -134,6 +135,30 @@ void addElement(char* name, enumVarType type){ } } +/* Adds an element and returns the offset of it */ +int addElementAndGetAddress(char* name, enumVarType type){ + addElement(name,type); + return getOffset(name); +} + +/* Adds a temporary Int element and returns the offset of it */ +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"); + } else if (tempCounter == 1) { + // we create the second temporary variable and use it + addElement("1_TEMP",INT); + strcpy(name, "1_TEMP"); + } else { + // we use the right temporary variable + sprintf(name, "%d_TEMP", tempCounter % 2); + } + tempCounter++; + return getOffset(name); +} /* removes all symbols */ void flushSymbolTable(){ @@ -199,6 +224,27 @@ void displayTable(){ doubleLine(); } +/*removes all temporary variables used for INTs*/ +void suppressTempINTElement(){ + if (tempCounter == 1){ + suppressElement("0_TEMP"); + } else { + if (tempCounter > 1){ + suppressElement("0_TEMP"); + suppressElement("1_TEMP"); + } + } +} + +/*removes one element*/ +void suppressElement(char* name){ + for(int i = getIndex(name); i < (currentIndex - 1); i ++){ + symbolTable[i] = symbolTable[i+1]; + } + currentIndex--; + checkArraySanity(); +} + void line(){ printf("---------------------------------\n"); } diff --git a/table.h b/table.h index f4bc062..52569c4 100644 --- a/table.h +++ b/table.h @@ -12,7 +12,7 @@ typedef enum enumVarType {INT, FLOAT} enumVarType; // a list of all type's sizes -extern int memorySizes[2]; // TODO : PROBLEM DOES'NT COMPILE +extern int memorySizes[2]; typedef struct { char name[NAME_MAX_LENGTH]; @@ -47,14 +47,18 @@ void clearOutOfScopeVariable(); /* removes all symbols */ void flushSymbolTable(); - /* Adds an element */ void addElement(char* name, enumVarType type); +/* Adds an element and returns the offset of it */ +int addElementAndGetAddress(char* name, enumVarType type); + +/* Adds a temporary Int element and returns the offset of it */ +int addTempINTAndGetAddress(); + /*creates a new structure and updates variables*/ Symbol createNewStructure(char* name, enumVarType type); - /*============================ Element Edition ============================*/ @@ -62,6 +66,12 @@ Symbol createNewStructure(char* name, enumVarType type); /* sets the init state of the symbol to true */ void setInit(char *name); +/*removes one element*/ +void suppressElement(char* name); + +/*removes all temporary variables used for INTs*/ +void suppressTempINTElement(); + /*============================ Element Access ============================*/ diff --git a/yacc.y b/yacc.y index 49a3713..94e88b1 100644 --- a/yacc.y +++ b/yacc.y @@ -4,6 +4,7 @@ #include #include #include "table.h" +#include "operations.h" int t; %} @@ -13,15 +14,16 @@ int yylex (void); void yyerror (const char *); } -%union {char str[NAME_MAX_LENGTH]; int nbInt; enumVarType type; } +%union {char str[NAME_MAX_LENGTH]; int nbInt; int addr; enumVarType type; } /*loops keywords*/ %token tWHILE tIF tELSE /*reserved keywords*/ %token tRETURN tPRINT /*types : integers, floats or void*/ %token tFLOAT tINT tVOID - /*operations*/ -%left tDIV tMUL tADD tSUB + /*operations, mul and div are precedent to sub and add*/ +%left tSUB tADD +%left tMUL tDIV /*Assignment*/ %left tASSIGN /*comparisons*/ @@ -38,7 +40,8 @@ void yyerror (const char *); /* represents types with the values used in the table, see table.h */ %type Type - +%type Expression +%type NbOrVariable %start Program %% @@ -80,8 +83,8 @@ ConditionalExpression : tID /*NbOrVariable is either a number or a variable of type int*/ -NbOrVariable : tID {/* copy */} - | tNB {/* Affectation */}; +NbOrVariable : tID { $$ = getOffset($1);} + | tNB {$$ = operation_afc_nb_tmp($1);}; /*List of all numerical operators*/ NumericalOperator : tLE | tGE | tEQ | tNE | tLT | tGT; @@ -95,19 +98,19 @@ IfStatement : tIF Condition InnerBlock WhileStatement : tWHILE Condition InnerBlock; -Assignment : tID tASSIGN Expression tSEMI {setInit($1);}; +Assignment : tID tASSIGN Expression tSEMI {setInit($1); operation_afc_addr(getOffset($1),$3); suppressTempINTElement();}; /*Expression operation applied on variables or values*/ -Expression : NbOrVariable - | FunctionCall - | tLPAR Expression tRPAR +Expression : NbOrVariable {$$ = $1;} + | FunctionCall{$$ = 0;} // TODO : wait untile functions are implemented + | tLPAR Expression tRPAR {$$ = $2;} /* replaced by the four following lines //| Expression Operation Expression */ - | Expression tADD Expression - | Expression tSUB Expression - | Expression tMUL Expression - | Expression tDIV Expression; + | Expression tADD Expression {$$ = operation_add($1, $3);} + | Expression tSUB Expression {$$ = operation_sub($1, $3);} + | Expression tMUL Expression {$$ = operation_mul($1, $3);} + | Expression tDIV Expression {$$ = operation_divInt($1, $3);}; /*end of added bloat*/ Expressions : Expression @@ -127,7 +130,7 @@ VarsWithType : VarWithType | VarWithType tCOMMA VarsWithType; /*VarWithType = a variable associated to its type = int a*/ -VarWithType : Type tID; +VarWithType : Type tID {addElement($2,$1);setInit($2);}; /*the return type or argument type*/ Type : tINT {$$ = INT;}