Browse Source

Compiliation avec if/while et profondeur ok

Elies Tali 3 years ago
parent
commit
ae49ecc714

+ 18
- 1
Fichiers_Tests/progC View File

@@ -1,3 +1,20 @@
1 1
 int main(){
2
-    int a = 4; 
2
+    int b = 4 * 5;
3
+	int a = 1;
4
+	while (b){
5
+		b = 2;
6
+		if (a){
7
+			int u = 8;		
8
+		}
9
+		else{
10
+			if (1){
11
+				int j = 7;
12
+			}
13
+			else if (5){
14
+				int g = 89;
15
+			}
16
+			int y = 9;
17
+		}
18
+	}	
19
+	int p = 85;
3 20
 }

+ 1
- 1
Lex_Yacc/al.lex View File

@@ -43,7 +43,7 @@ yyerror (char const *s)
43 43
 ","         { return tCOMA; }
44 44
 "\n"        {} //Ne pas les retourner à Yacc
45 45
 [a-zA-Z][a-zA-Z0-9_]* { strcpy(yylval.id, yytext); return tID; }
46
-.				{ return tERROR; }
46
+.				{ }//return tERROR; }
47 47
 
48 48
 %%
49 49
 

+ 52
- 14
Lex_Yacc/as.y View File

@@ -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
 

+ 13
- 0
Tables/Instructions/tab_instruc.c View File

@@ -66,3 +66,16 @@ void create_asm(){
66 66
 		free(line);
67 67
 	}
68 68
 }
69
+
70
+int get_current_index(){return current_index;}
71
+
72
+
73
+
74
+void patch(int index, int arg){
75
+	if (tab_op[index].opcode == JMP){
76
+		tab_op[index].arg1 = arg;
77
+	}
78
+	else if (tab_op[index].opcode == JMF){
79
+		tab_op[index].arg2 = arg;
80
+	}
81
+}

+ 2
- 0
Tables/Instructions/tab_instruc.h View File

@@ -17,3 +17,5 @@ extern struct operation_t tab_op[MAXTAILLE];
17 17
 
18 18
 void add_operation(enum opcode_t opcode, int arg1, int arg2, int arg3);
19 19
 void create_asm();
20
+int get_current_index();
21
+void patch(int index, int arg);

+ 11
- 4
Tables/Symboles/table_symboles.c View File

@@ -58,9 +58,9 @@ char * type_to_string(enum type_t type) {
58 58
 
59 59
 void print_symbole(struct symbole_t symbole) {
60 60
     if (symbole.initialized) {
61
-		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);
61
+		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);
62 62
 	} else {
63
-		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);
63
+		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);
64 64
 	}
65 65
 }
66 66
 
@@ -137,9 +137,9 @@ void print() {
137 137
 	int i;
138 138
 	for (i=0; i < pile->taille; i++) {
139 139
 		if (aux->symbole.initialized) {
140
-			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));
140
+			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);
141 141
 		} else {
142
-			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));
142
+			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);
143 143
 		}
144 144
 		aux = aux->suivant;
145 145
 	}
@@ -160,3 +160,10 @@ void reset_temp_vars(){
160 160
 	temp_addr = MAXADDR;
161 161
 }
162 162
 
163
+
164
+void reset_pronf(){
165
+	while (pile->first->symbole.profondeur == profondeur){
166
+		pop();
167
+	}
168
+}
169
+

+ 2
- 1
Tables/Symboles/table_symboles.h View File

@@ -35,7 +35,7 @@ struct symbole_t {
35 35
 	char nom[30];
36 36
 	uintptr_t adresse;
37 37
 	enum type_t type;
38
-	char initialized;
38
+	int initialized;
39 39
     int profondeur;
40 40
 };
41 41
 
@@ -52,3 +52,4 @@ int get_last_addr();
52 52
 struct symbole_t * get_variable(char * nom);
53 53
 int allocate_mem_temp_var(enum type_t type);
54 54
 void reset_temp_vars();
55
+void reset_pronf();

Loading…
Cancel
Save