|
@@ -12,6 +12,9 @@
|
12
|
12
|
|
13
|
13
|
enum type_t type_courant;
|
14
|
14
|
|
|
15
|
+int instructions_ligne_to_patch[10][20];
|
|
16
|
+int nbs_instructions_to_patch[10];
|
|
17
|
+
|
15
|
18
|
%}
|
16
|
19
|
|
17
|
20
|
%token tMAIN
|
|
@@ -25,7 +28,7 @@ enum type_t type_courant;
|
25
|
28
|
%token<id> tID
|
26
|
29
|
%token tPRINTF
|
27
|
30
|
%token tERROR
|
28
|
|
-%token tIF tWHILE tELSE
|
|
31
|
+%token<nombre> tIF tWHILE tELSE
|
29
|
32
|
%token tLT tGT tEQCOND
|
30
|
33
|
%token tAND tOR
|
31
|
34
|
|
|
@@ -65,25 +68,58 @@ Instruction : Invocation tPV{reset_temp_vars();};
|
65
|
68
|
Instruction : If {reset_temp_vars();};
|
66
|
69
|
Instruction : While {reset_temp_vars();};
|
67
|
70
|
|
|
71
|
+//On considère que la première ligne du code en ASM est la ligne 0
|
|
72
|
+If : tIF tOBRACE E tCBRACE { profondeur++;
|
|
73
|
+add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;}
|
|
74
|
+Body {int current = get_current_index();
|
|
75
|
+ patch($1,current + 1);
|
|
76
|
+ add_operation(JMP,0,0,0);
|
|
77
|
+ instructions_ligne_to_patch[profondeur][nbs_instructions_to_patch[profondeur]] = current;
|
|
78
|
+ nbs_instructions_to_patch[profondeur]++;}
|
|
79
|
+Else { printf("If reconnu\n"); reset_pronf(); profondeur--;};
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+//On considère que la première ligne du code en ASM est la ligne 0
|
|
83
|
+ElseIf : tIF tOBRACE E tCBRACE {add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;}
|
|
84
|
+Body {int current = get_current_index();
|
|
85
|
+ patch($1,current + 1);
|
|
86
|
+ add_operation(JMP,0,0,0);
|
|
87
|
+ instructions_ligne_to_patch[profondeur][nbs_instructions_to_patch[profondeur]] = current;
|
|
88
|
+ nbs_instructions_to_patch[profondeur]++;}
|
|
89
|
+Else { printf("If reconnu\n");};
|
|
90
|
+
|
68
|
91
|
|
69
|
|
-If : tIF tOBRACE Cond tCBRACE Body Else { printf("If reconnu\n"); };
|
70
|
|
-Else : tELSE If { printf("Else if reconnu\n"); };
|
71
|
|
-Else : tELSE Body { printf("Else reconnu\n"); };
|
72
|
|
-Else : ;
|
73
|
|
-While : tWHILE tOBRACE Cond tCBRACE Body { printf("While reconnu\n"); };
|
74
|
92
|
|
75
|
|
-Cond : E SuiteCond ;
|
76
|
|
-SuiteCond : ;
|
77
|
|
-SuiteCond : tAND E SuiteCond;
|
78
|
|
-SuiteCond : tOR E SuiteCond;
|
|
93
|
+
|
|
94
|
+Else : tELSE ElseIf { printf("Else if reconnu\n"); };
|
|
95
|
+Else : tELSE Body { printf("Else reconnu\n"); int current = get_current_index();
|
|
96
|
+for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
|
|
97
|
+ patch(instructions_ligne_to_patch[profondeur][i],current);
|
|
98
|
+}
|
|
99
|
+nbs_instructions_to_patch[profondeur] = 0;
|
|
100
|
+};
|
|
101
|
+Else : {int current = get_current_index();
|
|
102
|
+for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
|
|
103
|
+ patch(instructions_ligne_to_patch[profondeur][i],current);
|
|
104
|
+}
|
|
105
|
+nbs_instructions_to_patch[profondeur] = 0;};
|
|
106
|
+While : tWHILE tOBRACE E tCBRACE {profondeur++;
|
|
107
|
+add_operation(JMF,$3,0,0);
|
|
108
|
+$1 = get_current_index() - 1;}
|
|
109
|
+
|
|
110
|
+Body { printf("While reconnu\n");
|
|
111
|
+int current = get_current_index();
|
|
112
|
+patch($1,current + 1);
|
|
113
|
+add_operation(JMP,$1,0,0);
|
|
114
|
+reset_pronf(); profondeur--;};
|
79
|
115
|
|
80
|
116
|
|
81
|
|
-Aff : tID tEQ E tPV { printf("%s prend une valeur\n", $1);} ; //besoin de get_address
|
|
117
|
+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
|
82
|
118
|
|
83
|
119
|
E : tNB { int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
|
84
|
120
|
|
85
|
121
|
E : tNBEXP { printf("Nombre exp\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
|
86
|
|
-E : tID { printf("Id\n"); /*Faire un get_address sur la pile*/};
|
|
122
|
+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;};
|
87
|
123
|
E : E tMUL E { printf("Mul\n"); int addr = allocate_mem_temp_var(INT); add_operation(MUL, addr,$1,$3); $$ = addr;};
|
88
|
124
|
E : E tDIV E { printf("Div\n"); int addr = allocate_mem_temp_var(INT); add_operation(DIV, addr,$1,$3); $$ = addr;};
|
89
|
125
|
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
|
95
|
131
|
E : E tGT E { printf(">\n"); int addr = allocate_mem_temp_var(INT); add_operation(SUP, addr,$1,$3); $$ = addr;};
|
96
|
132
|
E : E tLT E { printf("<\n"); int addr = allocate_mem_temp_var(INT); add_operation(INF, addr,$1,$3); $$ = addr;};
|
97
|
133
|
E : tNOT E { printf("!\n"); };
|
|
134
|
+E : E tAND E {int addr = allocate_mem_temp_var(INT); add_operation(MUL, addr, $1, $3); $$=addr;};
|
|
135
|
+E : E tOR E {int addr = allocate_mem_temp_var(INT); add_operation(ADD, addr, $1, $3); $$=addr;} ;
|
98
|
136
|
|
99
|
137
|
|
100
|
138
|
|
|
@@ -105,11 +143,11 @@ Decl : DeclType SuiteDecl FinDecl ;
|
105
|
143
|
Decl : tCONST DeclType SuiteDeclConst FinDeclConst;
|
106
|
144
|
|
107
|
145
|
SuiteDecl : tID {push($1, 0, type_courant); printf("Suite Decl\n");};
|
108
|
|
-SuiteDecl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(AFC, addr,$3,0);};
|
|
146
|
+SuiteDecl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(COP, addr,$3,0);};
|
109
|
147
|
FinDecl : tPV { printf("Fin Decl\n");};
|
110
|
148
|
FinDecl : tCOMA SuiteDecl FinDecl ;
|
111
|
149
|
|
112
|
|
-SuiteDeclConst : tID tEQ E {int addr = push($1,1, type_courant); add_operation(AFC, addr,$3,0);};
|
|
150
|
+SuiteDeclConst : tID tEQ E {int addr = push($1,1, type_courant); add_operation(COP, addr,$3,0);};
|
113
|
151
|
FinDeclConst : tPV;
|
114
|
152
|
FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
|
115
|
153
|
|