aboutsummaryrefslogtreecommitdiff
path: root/src/tpc.y
diff options
context:
space:
mode:
Diffstat (limited to 'src/tpc.y')
-rw-r--r--src/tpc.y64
1 files changed, 50 insertions, 14 deletions
diff --git a/src/tpc.y b/src/tpc.y
index 3b902be..e7b5249 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -11,6 +11,9 @@ extern int lineno;
11int yylex(); 11int yylex();
12void yyerror(char *); 12void yyerror(char *);
13 13
14#define GLOBAL 0
15#define LOCAL 1
16static int status = GLOBAL;
14%} 17%}
15 18
16%union { 19%union {
@@ -31,15 +34,48 @@ void yyerror(char *);
31%token OR AND CONST IF ELSE WHILE RETURN VOID PRINT READC READE 34%token OR AND CONST IF ELSE WHILE RETURN VOID PRINT READC READE
32%token <type> TYPE 35%token <type> TYPE
33 36
34%type <num> Exp EB TB FB M E T F LValue 37%type <num> Exp EB TB FB M E T F
38%type <ident> LValue
35 39
36%left ',' 40%left ','
37%precedence ')' 41%precedence ')'
38%precedence ELSE 42%precedence ELSE
39 43
40%% 44%%
41Prog: 45Prog:{printf("section .data\n");
42 DeclConsts DeclVars DeclFoncts {display_table();} 46 printf("format_entier db \"%%d \n\", 10,0");
47 printf("section .bss\nsection .text\nglobal _start\n");
48 printf("print:\n");
49 printf("push rbp\n");
50 printf("mov rbp, rsp\n");
51
52 printf("push rax\n");
53 printf("push rcx\n");
54 printf("push rdx\n");
55 printf("push rdi\n");
56 printf("push rsi\n");
57 printf("push r8\n");
58
59 printf("mov r8, rdx \n");
60 printf("mov rcx, rcx\n");
61 printf("mov rdx, rbx\n");
62 printf(" mov rdi, format_entier\n");
63 printf(" mov rax, 0\n");
64 printf(" call printf WRT ..plt\n");
65
66 printf("pop r8\n");
67 printf("pop rsi\n");
68 printf("pop rdi\n");
69 printf("pop rdx\n");
70 printf("pop rcx\n");
71 printf("pop rax\n");
72
73 printf("pop rbp\n");
74 printf("ret\n");
75 }
76 DeclConsts DeclVars DeclFoncts {
77
78 glo_display_table();}
43 ; 79 ;
44DeclConsts: 80DeclConsts:
45 DeclConsts CONST ListConst ';' 81 DeclConsts CONST ListConst ';'
@@ -62,8 +98,8 @@ DeclVars:
62 | 98 |
63 ; 99 ;
64Declarateurs: 100Declarateurs:
65 Declarateurs ',' Declarateur {addVar($<ident>3, $<type>0);} 101 Declarateurs ',' Declarateur {glo_addVar($<ident>3, $<type>0);}
66 | Declarateur {addVar($<ident>1, $<type>0);} 102 | Declarateur {glo_addVar($<ident>1, $<type>0);}
67 ; 103 ;
68Declarateur: 104Declarateur:
69 IDENT 105 IDENT
@@ -74,7 +110,7 @@ DeclFoncts:
74 | DeclFonct 110 | DeclFonct
75 ; 111 ;
76DeclFonct: 112DeclFonct:
77 EnTeteFonct Corps 113 EnTeteFonct {status = LOCAL;} Corps {status = GLOBAL;}
78 ; 114 ;
79EnTeteFonct: 115EnTeteFonct:
80 TYPE IDENT '(' Parametres ')' 116 TYPE IDENT '(' Parametres ')'
@@ -85,8 +121,8 @@ Parametres:
85 | ListTypVar 121 | ListTypVar
86 ; 122 ;
87ListTypVar: 123ListTypVar:
88 ListTypVar ',' TYPE IDENT {addVar($<ident>4, $<type>3);} 124 ListTypVar ',' TYPE IDENT {glo_addVar($<ident>4, $<type>3);}
89 | TYPE IDENT {addVar($<ident>2, $<type>1);} 125 | TYPE IDENT {glo_addVar($<ident>2, $<type>1);}
90 ; 126 ;
91Corps: 127Corps:
92 '{' DeclConsts DeclVars SuiteInstr '}' 128 '{' DeclConsts DeclVars SuiteInstr '}'
@@ -99,8 +135,8 @@ Instr:
99 | ';' 135 | ';'
100 | RETURN Exp ';' 136 | RETURN Exp ';'
101 | RETURN ';' 137 | RETURN ';'
102 | READE '(' IDENT ')' ';' {lookup($<ident>3);} 138 | READE '(' IDENT ')' ';' {glo_lookup($<ident>3);}
103 | READC '(' IDENT ')' ';' {lookup($<ident>3);} 139 | READC '(' IDENT ')' ';' {glo_lookup($<ident>3);}
104 | PRINT '(' Exp ')' ';' 140 | PRINT '(' Exp ')' ';'
105 | IF '(' Exp ')' Instr 141 | IF '(' Exp ')' Instr
106 | IF '(' Exp ')' Instr ELSE Instr 142 | IF '(' Exp ')' Instr ELSE Instr
@@ -108,7 +144,7 @@ Instr:
108 | '{' SuiteInstr '}' 144 | '{' SuiteInstr '}'
109 ; 145 ;
110Exp: 146Exp:
111 LValue '=' Exp 147 LValue '=' Exp {$$ = glo_lookup($<ident>1);}
112 | EB 148 | EB
113 ; 149 ;
114EB: 150EB:
@@ -139,14 +175,14 @@ F:
139 ADDSUB F {$$ = $2;} //on fait remonter le type 175 ADDSUB F {$$ = $2;} //on fait remonter le type
140 | '!' F {$$ = $2;} 176 | '!' F {$$ = $2;}
141 | '(' Exp ')' {$$ = $2;} 177 | '(' Exp ')' {$$ = $2;}
142 | LValue {$$ = $1;} 178 | LValue {$$ = glo_lookup($<ident>1);}
143 | NUM {$$ = INT;} // on stocke les types pour l'analyse sémantique 179 | NUM {$$ = INT;} // on stocke les types pour l'analyse sémantique
144 | CARACTERE {$$ = CHAR;} 180 | CARACTERE {$$ = CHAR;}
145 | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement 181 | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement
146 ; 182 ;
147LValue: 183LValue:
148 IDENT {lookup($<ident>1);get_type($<ident>1);} 184 IDENT {glo_lookup($<ident>1);}
149 | IDENT '[' Exp ']' {lookup($<ident>1);get_type($<ident>1);} 185 | IDENT '[' Exp ']' {glo_lookup($<ident>1);}
150 ; 186 ;
151Arguments: 187Arguments:
152 ListExp 188 ListExp