Added conditional functions

This commit is contained in:
alejeune 2023-04-20 11:12:14 +02:00
parent b354d938ea
commit 6661eb3865
4 changed files with 101 additions and 11 deletions

View file

@ -4,6 +4,8 @@
#include "stdlib.h"
#include <string.h>
static int labelCounter = 0;
/*At the start of the execution : the whole array is empty*/
static ASMLine* asmTable;
static int lineCounter = 0;

View file

@ -104,4 +104,64 @@ void operation_copy(int addr1, int addr2){
char s[ASM_TEXT_LEN];
sprintf(s, "COP @%d @%d\n", addr1, addr2);
printOp(s);
}
}
/*prints the ASM instruction for the inferior condition
* and returns the address of the temporary variable*/
int cond_inf(int addr1, int addr2){
int addr = addCondAndGetAddress();
char s[ASM_TEXT_LEN];
sprintf(s, "INF @%d @%d @%d\n", addr, addr1, addr2);
printOp(s);
return addr;
}
/*prints the ASM instruction for the superior condition
* and returns the address of the temporary variable*/
int cond_sup(int addr1, int addr2){
int addr = addCondAndGetAddress();
char s[ASM_TEXT_LEN];
sprintf(s, "SUP @%d @%d @%d\n", addr, addr1, addr2);
printOp(s);
return addr;
}
/*prints the ASM instruction for the equality condition
* and returns the address of the temporary variable*/
int cond_eq(int addr1, int addr2){
int addr = addCondAndGetAddress();
char s[ASM_TEXT_LEN];
sprintf(s, "EQU @%d @%d @%d\n", addr, addr1, addr2);
printOp(s);
return addr;
/*prints the ASM instruction for the negation condition
* and returns the address of the temporary variable*/
int cond_not(int addr1){
int addr = addCondAndGetAddress();
char s[ASM_TEXT_LEN];
sprintf(s, "NOT @%d @%d\n", addr, addr1);
printOp(s);
return addr;
}
/*prints the ASM instruction for the and condition
* and returns the address of the temporary variable*/
int cond_and(int addr1, int addr2){
int addr = addCondAndGetAddress();
char s[ASM_TEXT_LEN];
sprintf(s, "AND @%d @%d @%d\n", addr, addr1, addr2);
printOp(s);
return addr;
}
/*prints the ASM instruction for the or condition
* and returns the address of the temporary variable*/
int cond_or(int addr1, int addr2){
int addr = addCondAndGetAddress();
char s[ASM_TEXT_LEN];
sprintf(s, "OR @%d @%d @%d\n", addr, addr1, addr2);
printOp(s);
return addr;
}

View file

@ -51,5 +51,28 @@ int operation_afc_nb_tmp(int value);
*/
void operation_copy(int addr1, int addr2);
/*prints the ASM instruction for the inferior condition
* and returns the address of the temporary variable*/
int cond_inf(int addr1, int addr2);
/*prints the ASM instruction for the superior condition
* and returns the address of the temporary variable*/
int cond_sup(int addr1, int addr2);
/*prints the ASM instruction for the equality condition
* and returns the address of the temporary variable*/
int cond_eq(int addr1, int addr2);
/*prints the ASM instruction for the negation condition
* and returns the address of the temporary variable*/
int cond_not(int addr1);
/*prints the ASM instruction for the and condition
* and returns the address of the temporary variable*/
int cond_and(int addr1, int addr2);
/*prints the ASM instruction for the or condition
* and returns the address of the temporary variable*/
int cond_or(int addr1, int addr2);
#endif //PROJET_SYSTEMES_INFORMATIQUES_OPERATIONS_H

25
yacc.y
View file

@ -45,6 +45,7 @@ void yyerror (const char *);
/* represents types with the values used in the table, see table.h */
%type <type> Type
%type <addr> Expression
%type <addr> ConditionalExpression
%type <addr> Declaration
%type <addr> NbOrVariable
%type <nbInt> IfStatement1
@ -79,16 +80,18 @@ InnerBlock : tLBRACE tRBRACE // a function or while loop can be empty cf GCC
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
| NbOrVariable NumericalOperator NbOrVariable
| tNOT ConditionalExpression
/* replaced by the two following lines
| ConditionalExpression BinaryLogicalOperator ConditionalExpression
*/
| ConditionalExpression tOR ConditionalExpression
| ConditionalExpression tAND ConditionalExpression;
ConditionalExpression : tID { $$ = getOffset($1);}
| tNB {$$ = operation_afc_nb_tmp($1);}
| tLPAR ConditionalExpression tRPAR {$$ = $2;}// for cases like if((a or b) and (a or c)) where there are parenthesis inside
| NbOrVariable tLE NbOrVariable {$$ = cond_not(cond_sup($1, $3));}
| NbOrVariable tGE NbOrVariable {$$ = cond_not(cond_inf($1, $3));}
| NbOrVariable tEQ NbOrVariable {$$ = cond_eq($1, $3);}
| NbOrVariable tNE NbOrVariable {$$ = cond_not(cond_eq($1, $3));}
| NbOrVariable tLT NbOrVariable {$$ = cond_inf($1, $3);}
| NbOrVariable tGT NbOrVariable {$$ = cond_sup($1, $3);}
| tNOT ConditionalExpression {$$ = cond_not($2);}
| ConditionalExpression tOR ConditionalExpression {$$ = cond_or($1, $3);}
| ConditionalExpression tAND ConditionalExpression {$$ = cond_and($1, $3);};
/*end of added bloat*/
@ -97,7 +100,9 @@ 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;