Compiliation avec if/while et profondeur ok
This commit is contained in:
parent
3a4f2e256f
commit
ae49ecc714
7 changed files with 101 additions and 23 deletions
|
@ -1,3 +1,20 @@
|
|||
int main(){
|
||||
int a = 4;
|
||||
int b = 4 * 5;
|
||||
int a = 1;
|
||||
while (b){
|
||||
b = 2;
|
||||
if (a){
|
||||
int u = 8;
|
||||
}
|
||||
else{
|
||||
if (1){
|
||||
int j = 7;
|
||||
}
|
||||
else if (5){
|
||||
int g = 89;
|
||||
}
|
||||
int y = 9;
|
||||
}
|
||||
}
|
||||
int p = 85;
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ yyerror (char const *s)
|
|||
"," { return tCOMA; }
|
||||
"\n" {} //Ne pas les retourner à Yacc
|
||||
[a-zA-Z][a-zA-Z0-9_]* { strcpy(yylval.id, yytext); return tID; }
|
||||
. { return tERROR; }
|
||||
. { }//return tERROR; }
|
||||
|
||||
%%
|
||||
|
||||
|
|
|
@ -12,6 +12,9 @@
|
|||
|
||||
enum type_t type_courant;
|
||||
|
||||
int instructions_ligne_to_patch[10][20];
|
||||
int nbs_instructions_to_patch[10];
|
||||
|
||||
%}
|
||||
|
||||
%token tMAIN
|
||||
|
@ -25,7 +28,7 @@ enum type_t type_courant;
|
|||
%token<id> tID
|
||||
%token tPRINTF
|
||||
%token tERROR
|
||||
%token tIF tWHILE tELSE
|
||||
%token<nombre> tIF tWHILE tELSE
|
||||
%token tLT tGT tEQCOND
|
||||
%token tAND tOR
|
||||
|
||||
|
@ -65,25 +68,58 @@ Instruction : Invocation tPV{reset_temp_vars();};
|
|||
Instruction : If {reset_temp_vars();};
|
||||
Instruction : While {reset_temp_vars();};
|
||||
|
||||
|
||||
If : tIF tOBRACE Cond tCBRACE Body Else { printf("If reconnu\n"); };
|
||||
Else : tELSE If { printf("Else if reconnu\n"); };
|
||||
Else : tELSE Body { printf("Else reconnu\n"); };
|
||||
Else : ;
|
||||
While : tWHILE tOBRACE Cond tCBRACE Body { printf("While reconnu\n"); };
|
||||
|
||||
Cond : E SuiteCond ;
|
||||
SuiteCond : ;
|
||||
SuiteCond : tAND E SuiteCond;
|
||||
SuiteCond : tOR E SuiteCond;
|
||||
//On considère que la première ligne du code en ASM est la ligne 0
|
||||
If : tIF tOBRACE E tCBRACE { profondeur++;
|
||||
add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;}
|
||||
Body {int current = get_current_index();
|
||||
patch($1,current + 1);
|
||||
add_operation(JMP,0,0,0);
|
||||
instructions_ligne_to_patch[profondeur][nbs_instructions_to_patch[profondeur]] = current;
|
||||
nbs_instructions_to_patch[profondeur]++;}
|
||||
Else { printf("If reconnu\n"); reset_pronf(); profondeur--;};
|
||||
|
||||
|
||||
Aff : tID tEQ E tPV { printf("%s prend une valeur\n", $1);} ; //besoin de get_address
|
||||
//On considère que la première ligne du code en ASM est la ligne 0
|
||||
ElseIf : tIF tOBRACE E tCBRACE {add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;}
|
||||
Body {int current = get_current_index();
|
||||
patch($1,current + 1);
|
||||
add_operation(JMP,0,0,0);
|
||||
instructions_ligne_to_patch[profondeur][nbs_instructions_to_patch[profondeur]] = current;
|
||||
nbs_instructions_to_patch[profondeur]++;}
|
||||
Else { printf("If reconnu\n");};
|
||||
|
||||
|
||||
|
||||
|
||||
Else : tELSE ElseIf { printf("Else if reconnu\n"); };
|
||||
Else : tELSE Body { printf("Else reconnu\n"); int current = get_current_index();
|
||||
for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
|
||||
patch(instructions_ligne_to_patch[profondeur][i],current);
|
||||
}
|
||||
nbs_instructions_to_patch[profondeur] = 0;
|
||||
};
|
||||
Else : {int current = get_current_index();
|
||||
for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
|
||||
patch(instructions_ligne_to_patch[profondeur][i],current);
|
||||
}
|
||||
nbs_instructions_to_patch[profondeur] = 0;};
|
||||
While : tWHILE tOBRACE E tCBRACE {profondeur++;
|
||||
add_operation(JMF,$3,0,0);
|
||||
$1 = get_current_index() - 1;}
|
||||
|
||||
Body { printf("While reconnu\n");
|
||||
int current = get_current_index();
|
||||
patch($1,current + 1);
|
||||
add_operation(JMP,$1,0,0);
|
||||
reset_pronf(); profondeur--;};
|
||||
|
||||
|
||||
Aff : tID tEQ E tPV { printf("%s prend une valeur\n", $1); struct symbole_t * symbole = get_variable($1); symbole->initialized = 1; add_operation(COP, symbole->adresse, $3,0);} ; //besoin de get_address
|
||||
|
||||
E : tNB { int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
|
||||
|
||||
E : tNBEXP { printf("Nombre exp\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
|
||||
E : tID { printf("Id\n"); /*Faire un get_address sur la pile*/};
|
||||
E : tID { printf("Id\n"); struct symbole_t * symbole = get_variable($1); int addr = allocate_mem_temp_var(symbole->type); add_operation(COP, addr,symbole->adresse,0); $$=addr;};
|
||||
E : E tMUL E { printf("Mul\n"); int addr = allocate_mem_temp_var(INT); add_operation(MUL, addr,$1,$3); $$ = addr;};
|
||||
E : E tDIV E { printf("Div\n"); int addr = allocate_mem_temp_var(INT); add_operation(DIV, addr,$1,$3); $$ = addr;};
|
||||
E : E tSUB E { printf("Sub\n"); int addr = allocate_mem_temp_var(INT); add_operation(SOU, addr,$1,$3); $$ = addr;};
|
||||
|
@ -95,6 +131,8 @@ E : E tEQCOND E { printf("==\n"); int addr = allocate_mem_temp_var(INT); add_ope
|
|||
E : E tGT E { printf(">\n"); int addr = allocate_mem_temp_var(INT); add_operation(SUP, addr,$1,$3); $$ = addr;};
|
||||
E : E tLT E { printf("<\n"); int addr = allocate_mem_temp_var(INT); add_operation(INF, addr,$1,$3); $$ = addr;};
|
||||
E : tNOT E { printf("!\n"); };
|
||||
E : E tAND E {int addr = allocate_mem_temp_var(INT); add_operation(MUL, addr, $1, $3); $$=addr;};
|
||||
E : E tOR E {int addr = allocate_mem_temp_var(INT); add_operation(ADD, addr, $1, $3); $$=addr;} ;
|
||||
|
||||
|
||||
|
||||
|
@ -105,11 +143,11 @@ Decl : DeclType SuiteDecl FinDecl ;
|
|||
Decl : tCONST DeclType SuiteDeclConst FinDeclConst;
|
||||
|
||||
SuiteDecl : tID {push($1, 0, type_courant); printf("Suite Decl\n");};
|
||||
SuiteDecl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(AFC, addr,$3,0);};
|
||||
SuiteDecl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(COP, addr,$3,0);};
|
||||
FinDecl : tPV { printf("Fin Decl\n");};
|
||||
FinDecl : tCOMA SuiteDecl FinDecl ;
|
||||
|
||||
SuiteDeclConst : tID tEQ E {int addr = push($1,1, type_courant); add_operation(AFC, addr,$3,0);};
|
||||
SuiteDeclConst : tID tEQ E {int addr = push($1,1, type_courant); add_operation(COP, addr,$3,0);};
|
||||
FinDeclConst : tPV;
|
||||
FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
|
||||
|
||||
|
|
|
@ -66,3 +66,16 @@ void create_asm(){
|
|||
free(line);
|
||||
}
|
||||
}
|
||||
|
||||
int get_current_index(){return current_index;}
|
||||
|
||||
|
||||
|
||||
void patch(int index, int arg){
|
||||
if (tab_op[index].opcode == JMP){
|
||||
tab_op[index].arg1 = arg;
|
||||
}
|
||||
else if (tab_op[index].opcode == JMF){
|
||||
tab_op[index].arg2 = arg;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,3 +17,5 @@ extern struct operation_t tab_op[MAXTAILLE];
|
|||
|
||||
void add_operation(enum opcode_t opcode, int arg1, int arg2, int arg3);
|
||||
void create_asm();
|
||||
int get_current_index();
|
||||
void patch(int index, int arg);
|
||||
|
|
|
@ -58,9 +58,9 @@ char * type_to_string(enum type_t type) {
|
|||
|
||||
void print_symbole(struct symbole_t symbole) {
|
||||
if (symbole.initialized) {
|
||||
printf("\t\t{nom:%s, adresse:%p, type:%s, initialized:OUI, profondeur : %d}\n", symbole.nom, (void *)(symbole.adresse), type_to_string(symbole.type), symbole.profondeur);
|
||||
printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:OUI, profondeur : %d}\n", symbole.nom, symbole.adresse, type_to_string(symbole.type), symbole.profondeur);
|
||||
} else {
|
||||
printf("\t\t{nom:%s, adresse:%p, type:%s, initialized:NON, profondeur : %d}\n", symbole.nom, (void *)(symbole.adresse), type_to_string(symbole.type),symbole.profondeur);
|
||||
printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:NON, profondeur : %d}\n", symbole.nom, symbole.adresse, type_to_string(symbole.type),symbole.profondeur);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -137,9 +137,9 @@ void print() {
|
|||
int i;
|
||||
for (i=0; i < pile->taille; i++) {
|
||||
if (aux->symbole.initialized) {
|
||||
printf("\t\t{nom:%s, adresse:%p, type:%s, initialized:OUI}\n", aux->symbole.nom, (void *)(aux->symbole.adresse), type_to_string(aux->symbole.type));
|
||||
printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:OUI, profondeur : %d}\n", aux->symbole.nom, aux->symbole.adresse, type_to_string(aux->symbole.type), aux->symbole.profondeur);
|
||||
} else {
|
||||
printf("\t\t{nom:%s, adresse:%p, type:%s, initialized:NON}\n", aux->symbole.nom, (void *)(aux->symbole.adresse), type_to_string(aux->symbole.type));
|
||||
printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:NON, profondeur : %d}\n", aux->symbole.nom, aux->symbole.adresse, type_to_string(aux->symbole.type), aux->symbole.profondeur);
|
||||
}
|
||||
aux = aux->suivant;
|
||||
}
|
||||
|
@ -160,3 +160,10 @@ void reset_temp_vars(){
|
|||
temp_addr = MAXADDR;
|
||||
}
|
||||
|
||||
|
||||
void reset_pronf(){
|
||||
while (pile->first->symbole.profondeur == profondeur){
|
||||
pop();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ struct symbole_t {
|
|||
char nom[30];
|
||||
uintptr_t adresse;
|
||||
enum type_t type;
|
||||
char initialized;
|
||||
int initialized;
|
||||
int profondeur;
|
||||
};
|
||||
|
||||
|
@ -52,3 +52,4 @@ int get_last_addr();
|
|||
struct symbole_t * get_variable(char * nom);
|
||||
int allocate_mem_temp_var(enum type_t type);
|
||||
void reset_temp_vars();
|
||||
void reset_pronf();
|
||||
|
|
Loading…
Reference in a new issue