Added operations
This commit is contained in:
parent
322afd388c
commit
42e264b8d5
5 changed files with 126 additions and 20 deletions
2
Makefile
2
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
|
||||
|
|
47
operations.h
Normal file
47
operations.h
Normal file
|
@ -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
|
48
table.c
48
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");
|
||||
}
|
||||
|
|
16
table.h
16
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
|
||||
============================*/
|
||||
|
|
33
yacc.y
33
yacc.y
|
@ -4,6 +4,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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
|
||||
|
||||
%type <addr> Expression
|
||||
%type <addr> 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;}
|
||||
|
|
Loading…
Reference in a new issue