Debut fonctions
This commit is contained in:
parent
2001f717a6
commit
e7fd215110
6 changed files with 69 additions and 33 deletions
|
@ -2,4 +2,8 @@ int main(){
|
||||||
int b[3];
|
int b[3];
|
||||||
int * c = &b[2];
|
int * c = &b[2];
|
||||||
int * f = &b[1];
|
int * f = &b[1];
|
||||||
|
if(1){
|
||||||
|
int v;
|
||||||
|
}
|
||||||
|
int y;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "../Tables/Instructions/tab_instruc.h"
|
#include "../Tables/Instructions/tab_instruc.h"
|
||||||
|
#include "../Tables/Fonctions/tab_fonctions.h"
|
||||||
#define TAILLE 1024
|
#define TAILLE 1024
|
||||||
|
|
||||||
struct type_t type_courant;
|
struct type_t type_courant;
|
||||||
|
@ -41,7 +42,7 @@ int nbs_instructions_to_patch[10];
|
||||||
%left tADD tSUB
|
%left tADD tSUB
|
||||||
%left tMUL tDIV
|
%left tMUL tDIV
|
||||||
|
|
||||||
%type<nombre> E Invocation DebutAff SuiteAffPointeur DebutAffPointeur EBis ETer
|
%type<nombre> E DebutAff SuiteAffPointeur DebutAffPointeur EBis ETer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -55,10 +56,18 @@ Main : tINT tMAIN tOBRACE Params tCBRACE Body { print(); create_asm();} ;
|
||||||
|
|
||||||
Params : { printf("Sans Params\n"); } ;
|
Params : { printf("Sans Params\n"); } ;
|
||||||
Params : Param SuiteParams ;
|
Params : Param SuiteParams ;
|
||||||
Param : Type tID { printf("Prametre : %s\n", $2); };
|
Param : Type tID { printf("Parametre : %s\n", $2); };
|
||||||
SuiteParams : tCOMA Param SuiteParams ;
|
SuiteParams : tCOMA Param SuiteParams ;
|
||||||
SuiteParams : ;
|
SuiteParams : ;
|
||||||
|
|
||||||
|
|
||||||
|
Args : Arg ArgSuite;
|
||||||
|
Args : ;
|
||||||
|
Arg : Type tID {int addr = push($2,1, type_courant);};
|
||||||
|
ArgSuite : tCOMA Arg ArgSuite {} ;
|
||||||
|
ArgSuite : ;
|
||||||
|
|
||||||
|
|
||||||
Body : tOBRACKET {inc_prof();} Instructions tCBRACKET {print(); reset_prof();} ;
|
Body : tOBRACKET {inc_prof();} Instructions tCBRACKET {print(); reset_prof();} ;
|
||||||
|
|
||||||
|
|
||||||
|
@ -66,7 +75,7 @@ Instructions : Instruction Instructions ;
|
||||||
Instructions : ;
|
Instructions : ;
|
||||||
Instruction : Aff {};
|
Instruction : Aff {};
|
||||||
Instruction : Decl {};
|
Instruction : Decl {};
|
||||||
Instruction : Invocation tPV{};
|
//Instruction : Invocation tPV{};
|
||||||
Instruction : If {};
|
Instruction : If {};
|
||||||
Instruction : While {};
|
Instruction : While {};
|
||||||
|
|
||||||
|
@ -122,7 +131,7 @@ E : E tMUL E { printf("Mul\n"); add_operation(MUL,$1,$1,$3); $$ = $1; pop();};
|
||||||
E : E tDIV E { printf("Div\n"); add_operation(DIV, $1,$1,$3); $$ = $1; pop();};
|
E : E tDIV E { printf("Div\n"); add_operation(DIV, $1,$1,$3); $$ = $1; pop();};
|
||||||
E : E tSUB E { printf("Sub\n"); add_operation(SOU,$1,$1,$3); $$ = $1; pop();};
|
E : E tSUB E { printf("Sub\n"); add_operation(SOU,$1,$1,$3); $$ = $1; pop();};
|
||||||
E : E tADD E { printf("Add\n"); add_operation(ADD,$1,$1,$3); $$ = $1; pop();};
|
E : E tADD E { printf("Add\n"); add_operation(ADD,$1,$1,$3); $$ = $1; pop();};
|
||||||
E : Invocation { printf("Invoc\n"); int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
|
//E : Invocation { printf("Invoc\n"); int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
|
||||||
E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2;};
|
E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2;};
|
||||||
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();};
|
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();};
|
||||||
E : E tEQCOND E { printf("==\n"); add_operation(EQU,$1,$1,$3); $$ = $1; pop();};
|
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);};
|
||||||
FinDeclConst : tPV;
|
FinDeclConst : tPV;
|
||||||
FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
|
FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
|
||||||
|
|
||||||
|
Fonction : Type tID {push_fonction($2,type_courant,get_current_index());} tOBRACE {} Args {} tCBRACE Body { printf("Déclaration de la fonction %s\n", $1); } ;)
|
||||||
|
|
||||||
/* //Créer un champ isConst dans la table des symboles
|
|
||||||
DeclType : tINT {type_courant = INT; printf("Type int\n");} ;
|
|
||||||
|
|
||||||
Decl : tCONST DeclType SuiteDeclConst { } ;
|
|
||||||
SuiteDeclConst : tCOMA tID SuiteDeclConst ;
|
|
||||||
SuiteDeclConst : tEQ E tPV { };
|
|
||||||
SuiteDeclConst : tPV { };
|
|
||||||
|
|
||||||
|
|
||||||
Decl : DeclType Decl SuiteDecl { } ;
|
|
||||||
Decl : tID {push($1, 0, type_courant);};
|
|
||||||
Decl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(AFC, addr,$3,0);} ;
|
|
||||||
SuiteDecl : tCOMA Decl SuiteDecl { };
|
|
||||||
SuiteDecl : tPV { };
|
|
||||||
*/
|
|
||||||
|
|
||||||
Invocation : tPRINTF tOBRACE tID tCBRACE { printf("Appel de printf sur %s\n", $3); } ;
|
|
||||||
|
|
||||||
/*S : E tPV
|
|
||||||
{ printf("RES: %d\n", $1); }
|
|
||||||
S
|
|
||||||
| { printf("END\n"); }
|
|
||||||
;
|
|
||||||
|
|
||||||
E : E tADD E { $$ = $1 + $3; }
|
|
||||||
| E tSUB E { $$ = $1 - $3; }
|
|
||||||
| tOB E tCB { $$ = $2; }
|
|
||||||
| tNB { $$ = $1; }
|
|
||||||
;*/
|
|
||||||
|
|
||||||
%%
|
%%
|
||||||
void main(void) {
|
void main(void) {
|
||||||
|
|
29
Tables/Fonctions/tab_fonctions.c
Normal file
29
Tables/Fonctions/tab_fonctions.c
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
struct fonction_t tab_fonctions[MAX_TAILLE_FONC];
|
||||||
|
int index = 0;
|
||||||
|
|
||||||
|
|
||||||
|
struct fonction_t get_fonction(char * name){
|
||||||
|
int not_found = 1;
|
||||||
|
int i = 0;
|
||||||
|
struct fonction_t res = NULL;
|
||||||
|
while (not_found && (i <= index)){
|
||||||
|
if (!strcmp(name,tab_fonctions[i].name){
|
||||||
|
res = tab_fonctions[i];
|
||||||
|
not_found = 0;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void push_fonction(char * name, struct type_t type, int line){
|
||||||
|
if (index < MAX_TAILLE_FONC){
|
||||||
|
struct fonction_t fonc;
|
||||||
|
strcpy(fonc.name,name);
|
||||||
|
fonc.type = type;
|
||||||
|
fonc.first_instruction_line = line;
|
||||||
|
tab_fonctions[i] = fonc;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
16
Tables/Fonctions/tab_fonctions.h
Normal file
16
Tables/Fonctions/tab_fonctions.h
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#include "tab_symboles.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define MAX_TAILLE_FONC 50
|
||||||
|
|
||||||
|
//Struct dans le tableau qui permet d'identifier une fonction
|
||||||
|
struct fonction_t {
|
||||||
|
char * name;
|
||||||
|
struct type_t return_type;
|
||||||
|
int first_instruction_line;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct fonction_t get_fonction(char * name);
|
||||||
|
|
||||||
|
void push_fonction(char * name, struct type_t type, int line);
|
||||||
|
|
|
@ -147,6 +147,10 @@ void inc_prof() {
|
||||||
profondeur++;
|
profondeur++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void decrement_prof(){
|
||||||
|
profondeur--;
|
||||||
|
}
|
||||||
|
|
||||||
int get_prof() {
|
int get_prof() {
|
||||||
return profondeur;
|
return profondeur;
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,6 +73,8 @@ void inc_prof();
|
||||||
int get_prof();
|
int get_prof();
|
||||||
//Détruit toutes les variables de la profondeur actuelle puis décrémente la profondeur
|
//Détruit toutes les variables de la profondeur actuelle puis décrémente la profondeur
|
||||||
void reset_prof();
|
void reset_prof();
|
||||||
|
//Décrémente la profondeur sans reset les variables
|
||||||
|
void decrement_prof();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue