Added conditional functions
This commit is contained in:
parent
b354d938ea
commit
6661eb3865
4 changed files with 101 additions and 11 deletions
|
@ -4,6 +4,8 @@
|
||||||
#include "stdlib.h"
|
#include "stdlib.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
static int labelCounter = 0;
|
||||||
|
|
||||||
/*At the start of the execution : the whole array is empty*/
|
/*At the start of the execution : the whole array is empty*/
|
||||||
static ASMLine* asmTable;
|
static ASMLine* asmTable;
|
||||||
static int lineCounter = 0;
|
static int lineCounter = 0;
|
||||||
|
|
60
operations.c
60
operations.c
|
@ -105,3 +105,63 @@ void operation_copy(int addr1, int addr2){
|
||||||
sprintf(s, "COP @%d @%d\n", addr1, addr2);
|
sprintf(s, "COP @%d @%d\n", addr1, addr2);
|
||||||
printOp(s);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
23
operations.h
23
operations.h
|
@ -51,5 +51,28 @@ int operation_afc_nb_tmp(int value);
|
||||||
*/
|
*/
|
||||||
void operation_copy(int addr1, int addr2);
|
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
|
#endif //PROJET_SYSTEMES_INFORMATIQUES_OPERATIONS_H
|
||||||
|
|
25
yacc.y
25
yacc.y
|
@ -45,6 +45,7 @@ void yyerror (const char *);
|
||||||
/* represents types with the values used in the table, see table.h */
|
/* represents types with the values used in the table, see table.h */
|
||||||
%type <type> Type
|
%type <type> Type
|
||||||
%type <addr> Expression
|
%type <addr> Expression
|
||||||
|
%type <addr> ConditionalExpression
|
||||||
%type <addr> Declaration
|
%type <addr> Declaration
|
||||||
%type <addr> NbOrVariable
|
%type <addr> NbOrVariable
|
||||||
%type <nbInt> IfStatement1
|
%type <nbInt> IfStatement1
|
||||||
|
@ -79,16 +80,18 @@ InnerBlock : tLBRACE tRBRACE // a function or while loop can be empty cf GCC
|
||||||
Condition : tLPAR ConditionalExpression tRPAR;
|
Condition : tLPAR ConditionalExpression tRPAR;
|
||||||
|
|
||||||
/*ConditionalExpression = expression that evaluates to a boolean*/
|
/*ConditionalExpression = expression that evaluates to a boolean*/
|
||||||
ConditionalExpression : tID
|
ConditionalExpression : tID { $$ = getOffset($1);}
|
||||||
| tNB
|
| tNB {$$ = operation_afc_nb_tmp($1);}
|
||||||
| tLPAR ConditionalExpression tRPAR // for cases like if((a or b) and (a or c)) where there are parenthesis inside
|
| tLPAR ConditionalExpression tRPAR {$$ = $2;}// for cases like if((a or b) and (a or c)) where there are parenthesis inside
|
||||||
| NbOrVariable NumericalOperator NbOrVariable
|
| NbOrVariable tLE NbOrVariable {$$ = cond_not(cond_sup($1, $3));}
|
||||||
| tNOT ConditionalExpression
|
| NbOrVariable tGE NbOrVariable {$$ = cond_not(cond_inf($1, $3));}
|
||||||
/* replaced by the two following lines
|
| NbOrVariable tEQ NbOrVariable {$$ = cond_eq($1, $3);}
|
||||||
| ConditionalExpression BinaryLogicalOperator ConditionalExpression
|
| NbOrVariable tNE NbOrVariable {$$ = cond_not(cond_eq($1, $3));}
|
||||||
*/
|
| NbOrVariable tLT NbOrVariable {$$ = cond_inf($1, $3);}
|
||||||
| ConditionalExpression tOR ConditionalExpression
|
| NbOrVariable tGT NbOrVariable {$$ = cond_sup($1, $3);}
|
||||||
| ConditionalExpression tAND ConditionalExpression;
|
| tNOT ConditionalExpression {$$ = cond_not($2);}
|
||||||
|
| ConditionalExpression tOR ConditionalExpression {$$ = cond_or($1, $3);}
|
||||||
|
| ConditionalExpression tAND ConditionalExpression {$$ = cond_and($1, $3);};
|
||||||
/*end of added bloat*/
|
/*end of added bloat*/
|
||||||
|
|
||||||
|
|
||||||
|
@ -97,7 +100,9 @@ NbOrVariable : tID { $$ = getOffset($1);}
|
||||||
| tNB {$$ = operation_afc_nb_tmp($1);};
|
| tNB {$$ = operation_afc_nb_tmp($1);};
|
||||||
|
|
||||||
/*List of all numerical operators*/
|
/*List of all numerical operators*/
|
||||||
|
/*
|
||||||
NumericalOperator : tLE | tGE | tEQ | tNE | tLT | tGT;
|
NumericalOperator : tLE | tGE | tEQ | tNE | tLT | tGT;
|
||||||
|
*/
|
||||||
|
|
||||||
/*any arithmetic operation
|
/*any arithmetic operation
|
||||||
Operation: tADD | tMUL | tSUB | tDIV;
|
Operation: tADD | tMUL | tSUB | tDIV;
|
||||||
|
|
Loading…
Reference in a new issue