diff options
author | Adam NAILI | 2018-05-06 19:03:46 +0200 |
---|---|---|
committer | Adam NAILI | 2018-05-06 19:03:46 +0200 |
commit | 351a51f956bf3de06799428ba139ce1c701cb95a (patch) | |
tree | c881c62c0406b1b2d1890468bb4103057a23d2d6 /src | |
parent | 13e036c9ffcf670869b4682f8f6993cc6667ab07 (diff) | |
download | tpc-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.tpc | 43 | ||||
-rw-r--r-- | src/tpc.y | 183 |
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 @@ | |||
1 | extern printf | ||
2 | section .data | ||
3 | format_int db "%d",10,0 | ||
4 | |||
5 | section .bss | ||
6 | section .text | ||
7 | |||
8 | global _start | ||
9 | print: ;print needs an argument in rax | ||
10 | push rbp | ||
11 | mov rbp, rsp | ||
12 | push rsi | ||
13 | mov rsi, rax | ||
14 | mov rdi, format_int | ||
15 | mov rax, 0 | ||
16 | call printf WRT ..plt | ||
17 | pop rsi | ||
18 | pop rbp | ||
19 | ret | ||
20 | |||
21 | _start: | ||
22 | push rbp | ||
23 | mov rbp, rsp | ||
24 | |||
25 | push 0 | ||
26 | push 1 | ||
27 | pop QWORD [rbp - 0] ;bool | ||
28 | push QWORD [rbp - 0] ;bool | ||
29 | pop rax | ||
30 | cmp rax,0 | ||
31 | jz .end_if0 | ||
32 | push QWORD [rbp - 0] ;bool | ||
33 | pop rax | ||
34 | call print | ||
35 | .end_if0 | ||
36 | mov rax,60 | ||
37 | mov rdi,0 | ||
38 | syscall | ||
39 | ;global table | ||
40 | |||
41 | ;local table | ||
42 | ;entier: bool, pos: 0 | ||
43 | |||
@@ -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 | ; |
109 | Declarateurs: | 108 | Declarateurs: |
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 | ; |
117 | Declarateur: | 120 | Declarateur: |
118 | IDENT | 121 | IDENT |
@@ -134,12 +137,16 @@ Parametres: | |||
134 | | ListTypVar | 137 | | ListTypVar |
135 | ; | 138 | ; |
136 | ListTypVar: | 139 | ListTypVar: |
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 | ; |
144 | Corps: | 151 | Corps: |
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 | ; |
165 | IfHandling: {printf("pop rax\ncmp rax,0\njz .end_if%d\n",$<num>$ = num_if++);}; | 176 | IfHandling: {printf("\n;BEGINIF\npop rax\ncmp rax,0\njz .end_if%d\n",$<num>$ = num_if++);}; |
166 | IfEndHandling: {printf(".end_if%d\n",$<num>-2);}; | 177 | IfEndHandling: {printf("jmp .end_ifelse%d\n.end_if%d:\n",$<num>-3,$<num>-3);}; |
167 | IfElseEndHandling: {printf(".end_if%d\n",$<num>-4);}; | 178 | IfElseEndHandling: {printf(".end_ifelse%d:\n;ENDIF\n\n",$<num>-5);}; |
168 | Exp: | 179 | Exp: |
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 | ; |
180 | EB: | 192 | EB: |
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 | ; |
185 | TB: | 202 | TB: |
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 | ; |
189 | FB: | 212 | FB: |
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 | ; |
193 | M: | 226 | M: |
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 | ; |
197 | E: | 244 | E: |
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 | ; |
208 | T: | 256 | T: |
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 == '*'){ |