While done (and tested)

This commit is contained in:
Raphaël LACROIX 2023-04-20 11:29:52 +02:00
parent 6661eb3865
commit ff74c5be44
4 changed files with 34 additions and 11 deletions

View file

@ -43,6 +43,7 @@ void reallocateASMArray(int size){
void addLine(char* s) {
strcpy(asmTable[lineCounter].name,s);
asmTable[lineCounter].jumpLine = -1;
asmTable[lineCounter].conditionAddr = -1;
lineCounter++;
checkASMArraySanity();
displayASMTable();
@ -51,7 +52,13 @@ void addLine(char* s) {
/*inserts the address in case of jumps*/
void setJumpLine(int index, int addr) {
asmTable[index].jumpLine = addr;
printf("\n%d aaaaa %d\n",index,addr);
displayASMTable();
}
/*inserts the condition's address in case of jumps*/
void setConditionAddr(int index, int addr) {
asmTable[index].conditionAddr = addr;
displayASMTable();
}
@ -69,7 +76,11 @@ void displayASMTable(){
if(a.jumpLine == -1) {
printf("%d | %s", i, a.name);
} else {
printf("%d | %s %d\n", i, a.name,a.jumpLine);
if(a.conditionAddr == -1) {
printf("%d | %s @%d\n", i, a.name,a.jumpLine);
} else {
printf("%d | %s @%d @%d\n", i, a.name,a.conditionAddr,a.jumpLine);
}
}
if (i != lineCounter -1) {
line();

View file

@ -10,6 +10,7 @@
typedef struct {
char name[LINE_MAX_LENGTH];
int conditionAddr;
int jumpLine;
} ASMLine;
@ -33,6 +34,9 @@ void initASMTable();
/*inserts the address in case of jumps*/
void setJumpLine(int index, int addr);
/*inserts the condition's address in case of jumps*/
void setConditionAddr(int index, int addr);
/*returns the current line (i.e. next one to insert)*/
int getCurrentLineNumber();

View file

@ -109,7 +109,7 @@ 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){
int addr = addCondAndGetAddress();
int addr = addTempCONDAndGetAddress();
char s[ASM_TEXT_LEN];
sprintf(s, "INF @%d @%d @%d\n", addr, addr1, addr2);
printOp(s);
@ -119,7 +119,7 @@ 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){
int addr = addCondAndGetAddress();
int addr = addTempCONDAndGetAddress();
char s[ASM_TEXT_LEN];
sprintf(s, "SUP @%d @%d @%d\n", addr, addr1, addr2);
printOp(s);
@ -128,17 +128,18 @@ 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){
int addr = addCondAndGetAddress();
int cond_eq(int addr1, int addr2) {
int addr = addTempCONDAndGetAddress();
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();
int addr = addTempCONDAndGetAddress();
char s[ASM_TEXT_LEN];
sprintf(s, "NOT @%d @%d\n", addr, addr1);
printOp(s);
@ -148,7 +149,7 @@ 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){
int addr = addCondAndGetAddress();
int addr = addTempCONDAndGetAddress();
char s[ASM_TEXT_LEN];
sprintf(s, "AND @%d @%d @%d\n", addr, addr1, addr2);
printOp(s);
@ -158,7 +159,7 @@ 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){
int addr = addCondAndGetAddress();
int addr = addTempCONDAndGetAddress();
char s[ASM_TEXT_LEN];
sprintf(s, "OR @%d @%d @%d\n", addr, addr1, addr2);
printOp(s);

11
yacc.y
View file

@ -11,6 +11,7 @@
int t;
int labelWhileStart;
int labelWhileEnd;
int whileJumpAddr;
%}
%code provides{
@ -49,6 +50,8 @@ void yyerror (const char *);
%type <addr> Declaration
%type <addr> NbOrVariable
%type <nbInt> IfStatement1
%type <addr> Condition
%type <nbInt> InnerBlock
%token <nbInt> tIF
%token <nbInt> tWHILE
@ -77,7 +80,7 @@ InnerBlock : tLBRACE tRBRACE // a function or while loop can be empty cf GCC
/*Condition = the evaluated boolean expression for an if or while = ( ... ) */
Condition : tLPAR ConditionalExpression tRPAR;
Condition : tLPAR ConditionalExpression tRPAR {$$ = $2;};
/*ConditionalExpression = expression that evaluates to a boolean*/
ConditionalExpression : tID { $$ = getOffset($1);}
@ -124,11 +127,15 @@ IfStatement : tIF Condition IfStatement1 InnerBlock tELSE {
WhileStatement : tWHILE {
$1 = getCurrentLineNumber();
} Condition {
int current = getCurrentLineNumber();
addLine("JMPF");
setConditionAddr(current,$3);
whileJumpAddr = current;
suppressCONDElements();
} InnerBlock {
addLine("JMP");
int current = getCurrentLineNumber();
setJumpLine($1, current);
setJumpLine(whileJumpAddr, current);
setJumpLine(current-1, $1);
};