aboutsummaryrefslogtreecommitdiff
path: root/src/tpc.y
diff options
context:
space:
mode:
Diffstat (limited to 'src/tpc.y')
-rw-r--r--src/tpc.y225
1 files changed, 93 insertions, 132 deletions
diff --git a/src/tpc.y b/src/tpc.y
index 12ff20d..17b8718 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -50,167 +50,128 @@ static int num_if = 0;
50 50
51%% 51%%
52Prog: { gen_prologue(); } 52Prog: { gen_prologue(); }
53 DeclConsts DeclVars DeclFoncts { gen_const_declaration(); } 53 DeclConsts DeclVars DeclFoncts { gen_const_declaration(); }
54 ; 54;
55DeclConsts: 55DeclConsts:
56 DeclConsts CONST ListConst ';' 56 DeclConsts CONST ListConst ';'
57 | 57|
58 ; 58;
59ListConst: 59ListConst:
60 ListConst ',' IDENT '=' Litteral 60 ListConst ',' IDENT '=' Litteral
61 | IDENT '=' Litteral 61| IDENT '=' Litteral
62 ; 62;
63Litteral: 63Litteral:
64 NombreSigne 64 NombreSigne
65 | CARACTERE 65| CARACTERE
66 ; 66;
67NombreSigne: 67NombreSigne:
68 NUM 68 NUM
69 | ADDSUB NUM 69| ADDSUB NUM
70 ; 70;
71DeclVars: 71DeclVars:
72 DeclVars TYPE Declarateurs ';' 72 DeclVars TYPE Declarateurs ';'
73 | 73|
74 ; 74;
75Declarateurs: 75Declarateurs:
76 Declarateurs ',' Declarateur { gen_declaration($<ident>3, $<type>0, scope); } 76 Declarateurs ',' Declarateur { gen_declaration($<ident>3, $<type>0, scope); }
77 | Declarateur { gen_declaration($<ident>1, $<type>0, scope); } 77| Declarateur { gen_declaration($<ident>1, $<type>0, scope); }
78 ; 78;
79Declarateur: 79Declarateur:
80 IDENT 80 IDENT
81 | IDENT '[' NUM ']' 81| IDENT '[' NUM ']'
82 ; 82;
83DeclFoncts: 83DeclFoncts:
84 DeclFoncts DeclFonct 84 DeclFoncts DeclFonct
85 | DeclFonct 85| DeclFonct
86 ; 86;
87DeclFonct: 87DeclFonct:
88 EnTeteFonct { scope = LOCAL; } 88 EnTeteFonct { scope = LOCAL; }
89 Corps { scope = GLOBAL; } 89 Corps { scope = GLOBAL; }
90 ; 90;
91EnTeteFonct: 91EnTeteFonct:
92 TYPE IDENT '(' Parametres ')' 92 TYPE IDENT '(' Parametres ')'
93 | VOID IDENT '(' Parametres ')' 93| VOID IDENT '(' Parametres ')'
94 ; 94;
95Parametres: 95Parametres:
96 VOID 96 VOID
97 | ListTypVar 97| ListTypVar
98 ; 98;
99ListTypVar: 99ListTypVar:
100 ListTypVar ',' TYPE IDENT { gen_declaration($<ident>4, $<type>3, scope); } 100 ListTypVar ',' TYPE IDENT { gen_declaration($<ident>4, $<type>3, scope); }
101 | TYPE IDENT { gen_declaration($<ident>2, $<type>1, scope); } 101| TYPE IDENT { gen_declaration($<ident>2, $<type>1, scope); }
102 ; 102;
103Corps: 103Corps:
104 '{' DeclConsts DeclVars SuiteInstr '}' 104 '{' DeclConsts DeclVars SuiteInstr '}'
105 ; 105;
106SuiteInstr: 106SuiteInstr:
107 SuiteInstr Instr 107 SuiteInstr Instr
108 | 108|
109 ; 109;
110Instr: 110Instr:
111 Exp ';' 111 Exp ';'
112 | ';' 112| ';'
113 | RETURN Exp ';' 113| RETURN Exp ';'
114 | RETURN ';' 114| RETURN ';'
115 | READE '(' IDENT ')' ';' { gen_read($<ident>3, scope); } 115| READE '(' IDENT ')' ';' { gen_read($<ident>3, scope); }
116 | READC '(' IDENT ')' ';' { gen_read($<ident>3, scope); } 116| READC '(' IDENT ')' ';' { gen_read($<ident>3, scope); }
117 | PRINT '(' Exp ')' ';' { gen_print();} 117| PRINT '(' Exp ')' ';' { gen_print();}
118 | IF '(' Exp IfHandling')' Instr { gen_if_label($<num>4); } 118| IF '(' Exp IfHandling')' Instr { gen_if_label($<num>4); }
119 | IF '(' Exp IfHandling')' Instr ELSE IfEndHandling Instr IfElseEndHandling 119| IF '(' Exp IfHandling')' Instr ELSE IfEndHandling Instr IfElseEndHandling
120 | WHILE '(' Exp ')' Instr 120| WHILE '(' Exp ')' Instr
121 | '{' SuiteInstr '}' 121| '{' SuiteInstr '}'
122 ; 122;
123IfHandling: { gen_if_start($<num>$ = num_if++); }; 123IfHandling: { gen_if_start($<num>$ = num_if++); };
124IfEndHandling: { gen_if_end($<num>-3); }; 124IfEndHandling: { gen_if_end($<num>-3); };
125IfElseEndHandling: { gen_ifelse_end($<num>-5); }; 125IfElseEndHandling: { gen_ifelse_end($<num>-5); };
126Exp: 126Exp:
127 LValue '=' Exp { /* TODO: extract this into func */ 127 LValue '=' Exp { $$ = gen_assign($<ident>1, scope); }
128 if(scope == GLOBAL){ 128| EB
129 $$ = glo_lookup($<ident>1); 129;
130 printf("pop QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1);
131 }
132 else {
133 $$ = loc_lookup($<ident>1);
134 printf("pop QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1);
135 }
136 }
137 | EB
138 ;
139EB: 130EB:
140 EB OR TB { gen_or($1, $3, num_label++); } 131 EB OR TB { gen_or($1, $3, num_label++); }
141 | TB 132| TB
142 ; 133;
143TB: 134TB:
144 TB AND FB { gen_and($1, $3, num_label++); } 135 TB AND FB { gen_and($1, $3, num_label++); }
145 | FB 136| FB
146 ; 137;
147FB: 138FB:
148 FB EQ M { gen_eq($2, $1, $3, num_label++); } 139 FB EQ M { gen_eq($2, $1, $3, num_label++); }
149 | M 140| M
150 ; 141;
151M: 142M:
152 M ORDER E { gen_order($2, $1, $3, num_label++); } 143 M ORDER E { gen_order($2, $1, $3, num_label++); }
153 | E 144| E
154 ; 145;
155E: 146E:
156 E ADDSUB T { gen_addsub($2, $1, $3); } 147 E ADDSUB T { gen_addsub($2, $1, $3); }
157 | T 148| T
158 ; 149;
159T: 150T:
160 T DIVSTAR F { gen_divstar($2, $1, $3); } 151 T DIVSTAR F { gen_divstar($2, $1, $3); }
161 | F 152| F
162 ; 153 ;
163F: 154F:
164 ADDSUB F {$$ = $2;//on fait remonter le type 155 ADDSUB F { $$ = gen_signed_expr($1, $2); }
165 if($1 == '+') { 156| '!' F { $$ = gen_negate_expr($2); }
166 printf(";+F\n"); 157| '(' Exp ')' { $$ = $2; }
167 } 158| LValue { $$ = gen_value($<ident>1, scope); }
168 else{ 159| NUM { $$ = gen_num($1, scope); }
169 printf(";-F\npop rdx\nxor rax,rax\nsub rax,rdx\npush rax\n"); 160| CARACTERE { $$ = gen_char($1, scope); }
170 } 161| IDENT '(' Arguments ')' { $$ = INT; } // tableau d'entiers uniquement
171 } 162;
172 | '!' F {$$ = $2;printf(";!F\npop rax\nxor rax,1\npush rax\n");}
173 | '(' Exp ')' {$$ = $2;}
174 | LValue {
175 if(scope == GLOBAL) {
176 $$ = glo_lookup($<ident>1);
177 printf("push QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1);
178 }
179 else {
180 $$ = loc_lookup($<ident>1);
181 printf("push QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1);
182 }
183 }
184 | NUM {$$ = INT; if(scope == LOCAL) printf("push %d\n",$1);} // on stocke les types pour l'analyse sémantique
185 | CARACTERE {$$ = CHAR; if(scope == LOCAL) printf("push %d\n",$1);}
186 | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement
187 ;
188LValue: 163LValue:
189 IDENT { 164 IDENT { gen_read($<ident>1, scope); }
190 if(scope == GLOBAL) { 165| IDENT '[' Exp ']' { gen_read($<ident>1, scope); }
191 glo_lookup($<ident>1); 166;
192 }
193 else {
194 loc_lookup($<ident>1);
195 }
196 }
197 | IDENT '[' Exp ']' {
198 if(scope == GLOBAL) {
199 glo_lookup($<ident>1);
200 }
201 else {
202 loc_lookup($<ident>1);
203 }
204 }
205 ;
206Arguments: 167Arguments:
207 ListExp 168 ListExp
208 | 169|
209 ; 170;
210ListExp: 171ListExp:
211 ListExp ',' Exp 172 ListExp ',' Exp
212 | Exp 173| Exp
213 ; 174;
214%% 175%%
215 176
216void yyerror(char *msg){ 177void yyerror(char *msg){