aboutsummaryrefslogtreecommitdiff
path: root/src/tpc.y
diff options
context:
space:
mode:
authorAdam NAILI2018-05-04 11:19:08 +0200
committerAdam NAILI2018-05-04 11:19:08 +0200
commit13e036c9ffcf670869b4682f8f6993cc6667ab07 (patch)
treea64fd28439b60634930a2ba8ad40145ec8038185 /src/tpc.y
parent2ebd7d03e6d9ff5d8badc637ca4c6ab3c6db4e81 (diff)
downloadtpc-compiler-13e036c9ffcf670869b4682f8f6993cc6667ab07.tar.gz
IF/ELSE
Diffstat (limited to 'src/tpc.y')
-rw-r--r--src/tpc.y15
1 files changed, 10 insertions, 5 deletions
diff --git a/src/tpc.y b/src/tpc.y
index 41fa1d2..340ce5c 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -9,12 +9,13 @@
9 * 9 *
10 * 10 *
11 * TODO : 11 * TODO :
12 * ------
12 * - Gérer les globales avec .bss (Il faut donc décaler le début du programme après l'analyse des globales pour savoir combien de place réserver.) 13 * - Gérer les globales avec .bss (Il faut donc décaler le début du programme après l'analyse des globales pour savoir combien de place réserver.)
13 * - Evaluation paresseuse 14 * - Evaluation paresseuse
14 * - Gestion des tableaux 15 * - Gestion des tableaux
15 * 16 * - Tableau des fonctions
16 * 17 *
17 * 18 * - remettre car conflit pour l'instant->
18 */ 19 */
19 20
20#include <stdio.h> 21#include <stdio.h>
@@ -26,7 +27,8 @@ void yyerror(char *);
26#define GLOBAL 0 27#define GLOBAL 0
27#define LOCAL 1 28#define LOCAL 1
28static int status = GLOBAL; 29static int status = GLOBAL;
29 30static int num_label = 0;
31static int num_if = 0;
30%} 32%}
31 33
32%union { 34%union {
@@ -155,11 +157,14 @@ Instr:
155 | READC '(' IDENT ')' ';' {if(status == GLOBAL) glo_lookup($<ident>3); 157 | READC '(' IDENT ')' ';' {if(status == GLOBAL) glo_lookup($<ident>3);
156 else loc_lookup($<ident>3);} 158 else loc_lookup($<ident>3);}
157 | PRINT '(' Exp ')' ';' {printf("pop rax\ncall print\n");} 159 | PRINT '(' Exp ')' ';' {printf("pop rax\ncall print\n");}
158 | IF '(' Exp ')' Instr 160 | IF '(' Exp IfHandling')' Instr IfEndHandling
159 | IF '(' Exp ')' Instr ELSE Instr 161 | IF '(' Exp IfHandling')' Instr IfEndHandling ELSE Instr IfElseEndHandling
160 | WHILE '(' Exp ')' Instr 162 | WHILE '(' Exp ')' Instr
161 | '{' SuiteInstr '}' 163 | '{' SuiteInstr '}'
162 ; 164 ;
165IfHandling: {printf("pop rax\ncmp rax,0\njz .end_if%d\n",$<num>$ = num_if++);};
166IfEndHandling: {printf(".end_if%d\n",$<num>-2);};
167IfElseEndHandling: {printf(".end_if%d\n",$<num>-4);};
163Exp: 168Exp:
164 LValue '=' Exp {if(status == GLOBAL){ 169 LValue '=' Exp {if(status == GLOBAL){
165 $$ = glo_lookup($<ident>1); 170 $$ = glo_lookup($<ident>1);