From 6661eb3865e160fe7af68627b1f37b13957c0584 Mon Sep 17 00:00:00 2001 From: alejeune Date: Thu, 20 Apr 2023 11:12:14 +0200 Subject: [PATCH] Added conditional functions --- asmTable.c | 2 ++ operations.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++- operations.h | 23 +++++++++++++++++++ yacc.y | 25 ++++++++++++--------- 4 files changed, 101 insertions(+), 11 deletions(-) diff --git a/asmTable.c b/asmTable.c index ab43810..e16b267 100644 --- a/asmTable.c +++ b/asmTable.c @@ -4,6 +4,8 @@ #include "stdlib.h" #include +static int labelCounter = 0; + /*At the start of the execution : the whole array is empty*/ static ASMLine* asmTable; static int lineCounter = 0; diff --git a/operations.c b/operations.c index 50de8e5..c5afa22 100644 --- a/operations.c +++ b/operations.c @@ -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); -} \ No newline at end of file +} + +/*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; +} + diff --git a/operations.h b/operations.h index 04a0eb5..c525bc2 100644 --- a/operations.h +++ b/operations.h @@ -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 diff --git a/yacc.y b/yacc.y index f4a7342..6fa974c 100644 --- a/yacc.y +++ b/yacc.y @@ -45,6 +45,7 @@ void yyerror (const char *); /* represents types with the values used in the table, see table.h */ %type Type %type Expression +%type ConditionalExpression %type Declaration %type NbOrVariable %type 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;