aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam NAILI2018-05-06 19:03:46 +0200
committerAdam NAILI2018-05-06 19:03:46 +0200
commit351a51f956bf3de06799428ba139ce1c701cb95a (patch)
treec881c62c0406b1b2d1890468bb4103057a23d2d6 /src
parent13e036c9ffcf670869b4682f8f6993cc6667ab07 (diff)
downloadtpc-compiler-351a51f956bf3de06799428ba139ce1c701cb95a.tar.gz
Lazy evaluation, Bool logic, IF and IFELSE structures implemented. Formatted a bit
Diffstat (limited to 'src')
-rw-r--r--src/test_if.tpc43
-rw-r--r--src/tpc.y183
2 files changed, 126 insertions, 100 deletions
diff --git a/src/test_if.tpc b/src/test_if.tpc
deleted file mode 100644
index 356061f..0000000
--- a/src/test_if.tpc
+++ /dev/null
@@ -1,43 +0,0 @@
1extern printf
2section .data
3format_int db "%d",10,0
4
5section .bss
6section .text
7
8global _start
9print: ;print needs an argument in rax
10push rbp
11mov rbp, rsp
12push rsi
13mov rsi, rax
14mov rdi, format_int
15mov rax, 0
16call printf WRT ..plt
17pop rsi
18pop rbp
19ret
20
21_start:
22push rbp
23mov rbp, rsp
24
25push 0
26push 1
27pop QWORD [rbp - 0] ;bool
28push QWORD [rbp - 0] ;bool
29pop rax
30cmp rax,0
31jz .end_if0
32push QWORD [rbp - 0] ;bool
33pop rax
34call print
35.end_if0
36mov rax,60
37mov rdi,0
38syscall
39;global table
40
41;local table
42;entier: bool, pos: 0
43
diff --git a/src/tpc.y b/src/tpc.y
index 340ce5c..db59e43 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -14,8 +14,7 @@
14 * - Evaluation paresseuse 14 * - Evaluation paresseuse
15 * - Gestion des tableaux 15 * - Gestion des tableaux
16 * - Tableau des fonctions 16 * - Tableau des fonctions
17 * 17 *
18 * - remettre car conflit pour l'instant->
19 */ 18 */
20 19
21#include <stdio.h> 20#include <stdio.h>
@@ -107,12 +106,16 @@ DeclVars:
107 | 106 |
108 ; 107 ;
109Declarateurs: 108Declarateurs:
110 Declarateurs ',' Declarateur {if(status == GLOBAL) glo_addVar($<ident>3, $<type>0); 109 Declarateurs ',' Declarateur {
111 else loc_addVar($<ident>3, $<type>0); 110 if(status == GLOBAL) glo_addVar($<ident>3, $<type>0);
112 printf("push 0\n");} 111 else loc_addVar($<ident>3, $<type>0);
113 | Declarateur {if(status == GLOBAL) glo_addVar($<ident>1, $<type>0); 112 printf("push 0\n");
114 else loc_addVar($<ident>1, $<type>0); 113 }
115 printf("push 0\n");} 114 | Declarateur {
115 if(status == GLOBAL) glo_addVar($<ident>1, $<type>0);
116 else loc_addVar($<ident>1, $<type>0);
117 printf("push 0\n");
118 }
116 ; 119 ;
117Declarateur: 120Declarateur:
118 IDENT 121 IDENT
@@ -134,12 +137,16 @@ Parametres:
134 | ListTypVar 137 | ListTypVar
135 ; 138 ;
136ListTypVar: 139ListTypVar:
137 ListTypVar ',' TYPE IDENT {if(status == GLOBAL) glo_addVar($<ident>4, $<type>3); 140 ListTypVar ',' TYPE IDENT {
138 else loc_addVar($<ident>4, $<type>3); 141 if(status == GLOBAL) glo_addVar($<ident>4, $<type>3);
139 printf("push 0\n");} 142 else loc_addVar($<ident>4, $<type>3);
140 | TYPE IDENT {if(status == GLOBAL) glo_addVar($<ident>2, $<type>1); 143 printf("push 0\n");
141 else loc_addVar($<ident>2, $<type>1); 144 }
142 printf("push 0\n");} 145 | TYPE IDENT {
146 if(status == GLOBAL) glo_addVar($<ident>2, $<type>1);
147 else loc_addVar($<ident>2, $<type>1);
148 printf("push 0\n");
149 }
143 ; 150 ;
144Corps: 151Corps:
145 '{' DeclConsts DeclVars SuiteInstr '}' 152 '{' DeclConsts DeclVars SuiteInstr '}'
@@ -152,74 +159,116 @@ Instr:
152 | ';' 159 | ';'
153 | RETURN Exp ';' 160 | RETURN Exp ';'
154 | RETURN ';' 161 | RETURN ';'
155 | READE '(' IDENT ')' ';' {if(status == GLOBAL) glo_lookup($<ident>3); 162 | READE '(' IDENT ')' ';' {
156 else loc_lookup($<ident>3);} 163 if(status == GLOBAL) glo_lookup($<ident>3);
157 | READC '(' IDENT ')' ';' {if(status == GLOBAL) glo_lookup($<ident>3); 164 else loc_lookup($<ident>3);
158 else loc_lookup($<ident>3);} 165 }
166 | READC '(' IDENT ')' ';' {
167 if(status == GLOBAL) glo_lookup($<ident>3);
168 else loc_lookup($<ident>3);
169 }
159 | PRINT '(' Exp ')' ';' {printf("pop rax\ncall print\n");} 170 | PRINT '(' Exp ')' ';' {printf("pop rax\ncall print\n");}
160 | IF '(' Exp IfHandling')' Instr IfEndHandling 171 | IF '(' Exp IfHandling')' Instr {printf(".end_if%d:\n;ENDIF\n\n",$<num>4);}
161 | IF '(' Exp IfHandling')' Instr IfEndHandling ELSE Instr IfElseEndHandling 172 | IF '(' Exp IfHandling')' Instr ELSE IfEndHandling Instr IfElseEndHandling
162 | WHILE '(' Exp ')' Instr 173 | WHILE '(' Exp ')' Instr
163 | '{' SuiteInstr '}' 174 | '{' SuiteInstr '}'
164 ; 175 ;
165IfHandling: {printf("pop rax\ncmp rax,0\njz .end_if%d\n",$<num>$ = num_if++);}; 176IfHandling: {printf("\n;BEGINIF\npop rax\ncmp rax,0\njz .end_if%d\n",$<num>$ = num_if++);};
166IfEndHandling: {printf(".end_if%d\n",$<num>-2);}; 177IfEndHandling: {printf("jmp .end_ifelse%d\n.end_if%d:\n",$<num>-3,$<num>-3);};
167IfElseEndHandling: {printf(".end_if%d\n",$<num>-4);}; 178IfElseEndHandling: {printf(".end_ifelse%d:\n;ENDIF\n\n",$<num>-5);};
168Exp: 179Exp:
169 LValue '=' Exp {if(status == GLOBAL){ 180 LValue '=' Exp {
181 if(status == GLOBAL){
170 $$ = glo_lookup($<ident>1); 182 $$ = glo_lookup($<ident>1);
171 printf("pop QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1); 183 printf("pop QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1);
172 } 184 }
173 else { 185 else {
174 $$ = loc_lookup($<ident>1); 186 $$ = loc_lookup($<ident>1);
175 printf("pop QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1); 187 printf("pop QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1);
176 } 188 }
177 } 189 }
178 | EB 190 | EB
179 ; 191 ;
180EB: 192EB:
181 EB {check_expected_type($1,INT); printf("pop rax\n");} 193 EB OR TB {
182 OR TB {check_expected_type($4,INT);} 194 check_expected_type($1,INT);check_expected_type($3,INT);
195 printf(";a OR c\npop rax\n");
196 printf("cmp rax,1\nje .true%d\npop rcx\ncmp rcx,1\njz .true%d\npush 0\njmp .false%d\n",num_label,num_label,num_label);
197 printf(".true%d:\npush 1\n.false%d:",num_label,num_label);
198 num_label++;
199 }
183 | TB 200 | TB
184 ; 201 ;
185TB: 202TB:
186 TB AND FB {check_expected_type($1,INT);check_expected_type($3,INT);} 203 TB AND FB {
204 check_expected_type($1,INT);check_expected_type($3,INT);
205 printf(";a AND c\npop rax\n");
206 printf("cmp rax,0\njz .false%d\npop rcx\ncmp rcx,0\njz .false%d\npush 1\njmp .true%d\n",num_label,num_label,num_label);
207 printf(".false%d:\npush 0\n.true%d:",num_label,num_label);
208 num_label++;
209 }
187 | FB 210 | FB
188 ; 211 ;
189FB: 212FB:
190 FB EQ M {check_expected_type($1,INT);check_expected_type($3,INT);} 213 FB EQ M {
214 check_expected_type($1,INT);check_expected_type($3,INT);
215 printf(";a EQ c\npop rax\npop rcx\ncmp rax,rcx\n");
216 if(!strcmp($2,"==")){
217 printf("je .true%d\n",num_label);
218 }else{
219 printf("jne .true%d\n",num_label);
220 }
221 printf("push 0\njmp .false%d\n.true%d:\npush 1\n.false%d:",num_label,num_label,num_label);
222 num_label++;
223 }
191 | M 224 | M
192 ; 225 ;
193M: 226M:
194 M ORDER E {check_expected_type($1,INT);check_expected_type($3,INT);} 227 M ORDER E {
228 check_expected_type($1,INT);check_expected_type($3,INT);
229 printf(";a ORDER c\npop rcx\npop rax\ncmp rax,rcx\n");
230 if(!strcmp($2,"<")){
231 printf("jl .true%d\n",num_label);
232 }else if(!strcmp($2,"<=")){
233 printf("jle .true%d\n",num_label);
234 }else if(!strcmp($2,">")){
235 printf("jg .true%d\n",num_label);
236 }else{
237 printf("jge .true%d\n",num_label);
238 }
239 printf("push 0\njmp .false%d\n.true%d:\npush 1\n.false%d:",num_label,num_label,num_label);
240 num_label++;
241 }
195 | E 242 | E
196 ; 243 ;
197E: 244E:
198 E ADDSUB T {check_expected_type($1,INT);check_expected_type($3,INT); 245 E ADDSUB T {
199 if($2 == '+'){ 246 check_expected_type($1,INT);check_expected_type($3,INT);
200 printf(";E + T\npop rcx\npop rax\nadd rax,rcx\npush rax\n"); 247 if($2 == '+'){
201 } 248 printf(";E + T\npop rcx\npop rax\nadd rax,rcx\npush rax\n");
202 else{ 249 }
203 printf(";E - T\npop rcx\npop rax\nsub rax,rcx\npush rax\n"); 250 else{
204 } 251 printf(";E - T\npop rcx\npop rax\nsub rax,rcx\npush rax\n");
252 }
205 } 253 }
206 | T 254 | T
207 ; 255 ;
208T: 256T:
209 T DIVSTAR F {check_expected_type($1,INT);check_expected_type($3,INT); 257 T DIVSTAR F {
210 if($2 == '*'){ 258 check_expected_type($1,INT);check_expected_type($3,INT);
211 printf(";E * T\npop rax\npop rcx\nimul rax,rcx\npush rax\n"); 259 if($2 == '*'){