Browse Source

Debut fonctions

Elies Tali 2 years ago
parent
commit
e7fd215110

+ 4
- 0
Fichiers_Tests/progC View File

@@ -2,4 +2,8 @@ int main(){
2 2
 	int b[3];
3 3
 	int * c = &b[2];
4 4
 	int * f = &b[1];
5
+	if(1){
6
+		int v;
7
+	}
8
+	int y;
5 9
 }

+ 14
- 33
Lex_Yacc/as.y View File

@@ -8,6 +8,7 @@
8 8
 #include <string.h>
9 9
 #include <stdlib.h>
10 10
 #include "../Tables/Instructions/tab_instruc.h"
11
+#include "../Tables/Fonctions/tab_fonctions.h"
11 12
 #define TAILLE 1024
12 13
 
13 14
 struct type_t type_courant;
@@ -41,7 +42,7 @@ int nbs_instructions_to_patch[10];
41 42
 %left tADD tSUB
42 43
 %left tMUL tDIV
43 44
 
44
-%type<nombre> E Invocation DebutAff SuiteAffPointeur DebutAffPointeur EBis ETer
45
+%type<nombre> E DebutAff SuiteAffPointeur DebutAffPointeur EBis ETer
45 46
 
46 47
 
47 48
 
@@ -55,10 +56,18 @@ Main : tINT tMAIN tOBRACE Params tCBRACE Body { print(); create_asm();} ;
55 56
 
56 57
 Params : { printf("Sans Params\n"); } ;
57 58
 Params : Param SuiteParams ;
58
-Param : Type tID { printf("Prametre : %s\n", $2); };
59
+Param : Type tID { printf("Parametre : %s\n", $2); };
59 60
 SuiteParams : tCOMA Param SuiteParams ;
60 61
 SuiteParams : ;
61 62
 
63
+
64
+Args : Arg ArgSuite;
65
+Args : ;
66
+Arg : Type tID {int addr = push($2,1, type_courant);};
67
+ArgSuite : tCOMA Arg ArgSuite {} ;
68
+ArgSuite : ; 
69
+
70
+
62 71
 Body : tOBRACKET {inc_prof();} Instructions tCBRACKET {print(); reset_prof();} ;
63 72
 
64 73
 
@@ -66,7 +75,7 @@ Instructions : Instruction Instructions ;
66 75
 Instructions : ;
67 76
 Instruction : Aff {};
68 77
 Instruction : Decl {};
69
-Instruction : Invocation tPV{};
78
+//Instruction : Invocation tPV{};
70 79
 Instruction : If {};
71 80
 Instruction : While {};
72 81
 
@@ -122,7 +131,7 @@ E : E tMUL E { printf("Mul\n"); add_operation(MUL,$1,$1,$3); $$ = $1; pop();};
122 131
 E : E tDIV E { printf("Div\n");  add_operation(DIV, $1,$1,$3); $$ = $1; pop();};
123 132
 E : E tSUB E { printf("Sub\n"); add_operation(SOU,$1,$1,$3); $$ = $1; pop();};
124 133
 E : E tADD E { printf("Add\n"); add_operation(ADD,$1,$1,$3); $$ = $1; pop();};
125
-E : Invocation { printf("Invoc\n"); int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
134
+//E : Invocation { printf("Invoc\n"); int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
126 135
 E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2;};
127 136
 E : tSUB E { printf("Moins\n");  int addr = push("0_TEMPORARY", 1, integer);  add_operation(AFC, addr,0,0); add_operation(SOU, $2,$2,addr);  $$ = $2; pop();};
128 137
 E : E tEQCOND E { printf("==\n"); add_operation(EQU,$1,$1,$3); $$ = $1; pop();};
@@ -161,36 +170,8 @@ SuiteDeclConst : tID tEQ E {pop(); int addr = push($1,1, type_courant);};
161 170
 FinDeclConst : tPV;
162 171
 FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
163 172
 
173
+Fonction : Type tID {push_fonction($2,type_courant,get_current_index());} tOBRACE {} Args {} tCBRACE Body { printf("Déclaration de la fonction  %s\n", $1); } ;)
164 174
 
165
-/* //Créer un champ isConst dans la table des symboles
166
-DeclType : tINT {type_courant = INT; printf("Type int\n");} ;
167
-
168
-Decl : tCONST DeclType SuiteDeclConst { } ;
169
-SuiteDeclConst : tCOMA tID SuiteDeclConst ;
170
-SuiteDeclConst : tEQ E tPV { };
171
-SuiteDeclConst : tPV { };
172
-
173
-
174
-Decl : DeclType Decl SuiteDecl { } ;
175
-Decl : tID {push($1, 0, type_courant);};
176
-Decl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(AFC, addr,$3,0);} ;
177
-SuiteDecl : tCOMA Decl SuiteDecl { };
178
-SuiteDecl : tPV { };
179
-*/
180
-
181
-Invocation : tPRINTF tOBRACE tID tCBRACE { printf("Appel de printf sur %s\n", $3); } ;
182
-
183
-/*S : E tPV
184
-						{ printf("RES: %d\n", $1); }
185
-		S
186
-	|					{ printf("END\n"); }
187
-	;
188
-
189
-E : E tADD E	{ $$ = $1 + $3; }
190
-	| E tSUB E	{ $$ = $1 - $3; }
191
-	| tOB E tCB	{ $$ = $2; }
192
-	| tNB				{ $$ = $1; }
193
-	;*/
194 175
 
195 176
 %%
196 177
 void main(void) {

+ 29
- 0
Tables/Fonctions/tab_fonctions.c View File

@@ -0,0 +1,29 @@
1
+struct fonction_t tab_fonctions[MAX_TAILLE_FONC];
2
+int index = 0;
3
+
4
+
5
+struct fonction_t get_fonction(char * name){
6
+	int not_found = 1;
7
+	int i = 0;
8
+	struct fonction_t res = NULL;
9
+	while (not_found && (i <= index)){
10
+		if (!strcmp(name,tab_fonctions[i].name){
11
+			res = tab_fonctions[i];
12
+			not_found = 0;
13
+		}
14
+		i++;
15
+	}
16
+	return res;
17
+}
18
+
19
+void push_fonction(char * name, struct type_t type, int line){
20
+	if (index < MAX_TAILLE_FONC){
21
+		struct fonction_t fonc;
22
+		strcpy(fonc.name,name);
23
+		fonc.type = type;
24
+		fonc.first_instruction_line = line;
25
+		tab_fonctions[i] = fonc;
26
+		index++;
27
+	}
28
+}
29
+

+ 16
- 0
Tables/Fonctions/tab_fonctions.h View File

@@ -0,0 +1,16 @@
1
+#include "tab_symboles.h"
2
+#include <string.h>
3
+
4
+#define MAX_TAILLE_FONC 50
5
+
6
+//Struct dans le tableau qui permet d'identifier une fonction
7
+struct fonction_t {
8
+	char * name;
9
+	struct type_t return_type;
10
+	int first_instruction_line;
11
+};
12
+
13
+struct fonction_t get_fonction(char * name);
14
+
15
+void push_fonction(char * name, struct type_t type, int line);
16
+

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

@@ -147,6 +147,10 @@ void inc_prof() {
147 147
     profondeur++;
148 148
 }
149 149
 
150
+void decrement_prof(){
151
+	profondeur--;
152
+}
153
+
150 154
 int get_prof() {
151 155
     return profondeur;
152 156
 }

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

@@ -73,6 +73,8 @@ void inc_prof();
73 73
 int get_prof();
74 74
 //Détruit toutes les variables de la profondeur actuelle puis décrémente la profondeur
75 75
 void reset_prof();
76
+//Décrémente la profondeur sans reset les variables
77
+void decrement_prof();
76 78
 
77 79
 
78 80
 

Loading…
Cancel
Save