diff options
author | pacien | 2018-06-06 14:36:25 +0200 |
---|---|---|
committer | pacien | 2018-06-06 14:36:25 +0200 |
commit | 58e0712bb4f624e02bffc877bf39f5fd45acc2e4 (patch) | |
tree | 21e206eedf4f06eb968e87d0c8a200a3dece4650 /src | |
parent | 8a5ccc3b6e5e7a3ad064cc5cce1c4a1a9ac74aa8 (diff) | |
download | tpc-compiler-58e0712bb4f624e02bffc877bf39f5fd45acc2e4.tar.gz |
extract assign and val-retrieval
Diffstat (limited to 'src')
-rw-r--r-- | src/generator.c | 24 | ||||
-rw-r--r-- | src/generator.h | 2 | ||||
-rw-r--r-- | src/tpc.y | 20 |
3 files changed, 22 insertions, 24 deletions
diff --git a/src/generator.c b/src/generator.c index 44fdeab..b75f7a8 100644 --- a/src/generator.c +++ b/src/generator.c | |||
@@ -265,7 +265,7 @@ void gen_ifelse_end(int idx) { | |||
265 | 265 | ||
266 | // ----- OPERATORS ----- | 266 | // ----- OPERATORS ----- |
267 | 267 | ||
268 | int gen_assign(const char ident[], Scope scope) { | 268 | static int gen_assign_simple(const char ident[], Scope scope) { |
269 | int l_addr = loc_get_addr(ident); | 269 | int l_addr = loc_get_addr(ident); |
270 | int g_addr = glo_get_addr(ident); | 270 | int g_addr = glo_get_addr(ident); |
271 | 271 | ||
@@ -291,7 +291,7 @@ int gen_assign(const char ident[], Scope scope) { | |||
291 | } | 291 | } |
292 | } | 292 | } |
293 | 293 | ||
294 | int gen_assign_tab(const char ident[], Scope scope) { | 294 | static int gen_assign_tab(const char ident[], Scope scope) { |
295 | int l_addr = loc_get_addr(ident); | 295 | int l_addr = loc_get_addr(ident); |
296 | int g_addr = glo_get_addr(ident); | 296 | int g_addr = glo_get_addr(ident); |
297 | 297 | ||
@@ -317,6 +317,13 @@ int gen_assign_tab(const char ident[], Scope scope) { | |||
317 | } | 317 | } |
318 | } | 318 | } |
319 | 319 | ||
320 | int gen_assign(const char ident[], Scope scope) { | ||
321 | switch (loc_lookup(ident)) { | ||
322 | case TAB: return gen_assign_tab(ident, scope); | ||
323 | default: return gen_assign_simple(ident, scope); | ||
324 | } | ||
325 | } | ||
326 | |||
320 | void gen_or(int left, int right, int idx) { | 327 | void gen_or(int left, int right, int idx) { |
321 | check_expected_types(left, INT,TAB); | 328 | check_expected_types(left, INT,TAB); |
322 | check_expected_types(right, INT,TAB); | 329 | check_expected_types(right, INT,TAB); |
@@ -447,7 +454,7 @@ int gen_negate_expr(int type) { | |||
447 | return type; | 454 | return type; |
448 | } | 455 | } |
449 | 456 | ||
450 | int gen_value(const char ident[], Scope scope) { | 457 | static int gen_value_simple(const char ident[], Scope scope) { |
451 | int l_addr = loc_get_addr(ident); | 458 | int l_addr = loc_get_addr(ident); |
452 | 459 | ||
453 | switch (scope) { | 460 | switch (scope) { |
@@ -470,7 +477,8 @@ int gen_value(const char ident[], Scope scope) { | |||
470 | exit(1); | 477 | exit(1); |
471 | } | 478 | } |
472 | } | 479 | } |
473 | int gen_value_tab(const char ident[], Scope scope) { | 480 | |
481 | static int gen_value_tab(const char ident[], Scope scope) { | ||
474 | int l_addr = loc_get_addr(ident); | 482 | int l_addr = loc_get_addr(ident); |
475 | int g_addr = glo_get_addr(ident); | 483 | int g_addr = glo_get_addr(ident); |
476 | switch (scope) { | 484 | switch (scope) { |
@@ -489,6 +497,14 @@ int gen_value_tab(const char ident[], Scope scope) { | |||
489 | exit(1); | 497 | exit(1); |
490 | } | 498 | } |
491 | } | 499 | } |
500 | |||
501 | int gen_value(const char ident[], Scope scope) { | ||
502 | switch (loc_lookup(ident)) { | ||
503 | case TAB: return gen_value_tab(ident, scope); | ||
504 | default: return gen_value_simple(ident, scope); | ||
505 | } | ||
506 | } | ||
507 | |||
492 | int gen_num(int value, Scope scope) { | 508 | int gen_num(int value, Scope scope) { |
493 | fprintf(output, "push %d\n", value); | 509 | fprintf(output, "push %d\n", value); |
494 | return INT; | 510 | return INT; |
diff --git a/src/generator.h b/src/generator.h index 9d6dd65..e1e47df 100644 --- a/src/generator.h +++ b/src/generator.h | |||
@@ -39,7 +39,6 @@ void gen_if_end(int idx); | |||
39 | void gen_ifelse_end(int idx); | 39 | void gen_ifelse_end(int idx); |
40 | 40 | ||
41 | int gen_assign(const char ident[], Scope scope); | 41 | int gen_assign(const char ident[], Scope scope); |
42 | int gen_assign_tab(const char ident[], Scope scope); | ||
43 | 42 | ||
44 | void gen_or(int left, int right, int idx); | 43 | void gen_or(int left, int right, int idx); |
45 | void gen_and(int left, int right, int idx); | 44 | void gen_and(int left, int right, int idx); |
@@ -51,7 +50,6 @@ void gen_divstar(char op, int left, int right); | |||
51 | int gen_signed_expr(char op, int type); | 50 | int gen_signed_expr(char op, int type); |
52 | int gen_negate_expr(int type); | 51 | int gen_negate_expr(int type); |
53 | int gen_value(const char ident[], Scope scope); | 52 | int gen_value(const char ident[], Scope scope); |
54 | int gen_value_tab(const char ident[], Scope scope); | ||
55 | 53 | ||
56 | int gen_num(int value, Scope scope); | 54 | int gen_num(int value, Scope scope); |
57 | int gen_char(int value, Scope scope); | 55 | int gen_char(int value, Scope scope); |
@@ -2,11 +2,6 @@ | |||
2 | /* | 2 | /* |
3 | * UPEM / Compilation / Projet | 3 | * UPEM / Compilation / Projet |
4 | * Pacien TRAN-GIRARD, Adam NAILI | 4 | * Pacien TRAN-GIRARD, Adam NAILI |
5 | * | ||
6 | * TODO : | ||
7 | * ------ | ||
8 | * - arrays | ||
9 | * | ||
10 | */ | 5 | */ |
11 | 6 | ||
12 | int nb_globals = 0; | 7 | int nb_globals = 0; |
@@ -147,13 +142,7 @@ IfHandling: { gen_if_start($<num>$ = num_if++); }; | |||
147 | IfEndHandling: { gen_if_end($<num>-3); }; | 142 | IfEndHandling: { gen_if_end($<num>-3); }; |
148 | IfElseEndHandling: { gen_ifelse_end($<num>-5); }; | 143 | IfElseEndHandling: { gen_ifelse_end($<num>-5); }; |
149 | Exp: | 144 | Exp: |
150 | LValue '=' Exp { | 145 | LValue '=' Exp { $$ = gen_assign($<ident>1, scope); } |
151 | if(loc_lookup($<ident>1) != TAB){ | ||
152 | $$ = gen_assign($<ident>1, scope); | ||
153 | }else{ | ||
154 | $$ = gen_assign_tab($<ident>1,scope); | ||
155 | } | ||
156 | } | ||
157 | | EB | 146 | | EB |
158 | ; | 147 | ; |
159 | EB: | 148 | EB: |
@@ -184,12 +173,7 @@ F: | |||
184 | ADDSUB F { $$ = gen_signed_expr($1, $2); } | 173 | ADDSUB F { $$ = gen_signed_expr($1, $2); } |
185 | | '!' F { $$ = gen_negate_expr($2); } | 174 | | '!' F { $$ = gen_negate_expr($2); } |
186 | | '(' Exp ')' { $$ = $2; } | 175 | | '(' Exp ')' { $$ = $2; } |
187 | | LValue { if(loc_lookup($<ident>1) != TAB){ | 176 | | LValue { $$ = gen_value($<ident>1, scope); } |
188 | $$ = gen_value($<ident>1, scope); | ||
189 | }else{ | ||
190 | $$ = gen_value_tab($<ident>1,scope); | ||
191 | } | ||
192 | } | ||
193 | | NUM { $$ = gen_num($1, scope); } | 177 | | NUM { $$ = gen_num($1, scope); } |
194 | | CARACTERE { $$ = gen_char($1, scope); } | 178 | | CARACTERE { $$ = gen_char($1, scope); } |
195 | | IDENT '(' Arguments ')' { $$ = gen_function_call($<ident>1,$<num>3); } | 179 | | IDENT '(' Arguments ')' { $$ = gen_function_call($<ident>1,$<num>3); } |