diff options
author | Adam NAILI | 2018-05-04 11:19:08 +0200 |
---|---|---|
committer | Adam NAILI | 2018-05-04 11:19:08 +0200 |
commit | 13e036c9ffcf670869b4682f8f6993cc6667ab07 (patch) | |
tree | a64fd28439b60634930a2ba8ad40145ec8038185 /src/tpc.y | |
parent | 2ebd7d03e6d9ff5d8badc637ca4c6ab3c6db4e81 (diff) | |
download | tpc-compiler-13e036c9ffcf670869b4682f8f6993cc6667ab07.tar.gz |
IF/ELSE
Diffstat (limited to 'src/tpc.y')
-rw-r--r-- | src/tpc.y | 15 |
1 files changed, 10 insertions, 5 deletions
@@ -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 |
28 | static int status = GLOBAL; | 29 | static int status = GLOBAL; |
29 | 30 | static int num_label = 0; | |
31 | static 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 | ; |
165 | IfHandling: {printf("pop rax\ncmp rax,0\njz .end_if%d\n",$<num>$ = num_if++);}; | ||
166 | IfEndHandling: {printf(".end_if%d\n",$<num>-2);}; | ||
167 | IfElseEndHandling: {printf(".end_if%d\n",$<num>-4);}; | ||
163 | Exp: | 168 | Exp: |
164 | LValue '=' Exp {if(status == GLOBAL){ | 169 | LValue '=' Exp {if(status == GLOBAL){ |
165 | $$ = glo_lookup($<ident>1); | 170 | $$ = glo_lookup($<ident>1); |