From 58e0712bb4f624e02bffc877bf39f5fd45acc2e4 Mon Sep 17 00:00:00 2001 From: pacien Date: Wed, 6 Jun 2018 14:36:25 +0200 Subject: extract assign and val-retrieval --- src/generator.c | 24 ++++++++++++++++++++---- src/generator.h | 2 -- src/tpc.y | 20 ++------------------ 3 files changed, 22 insertions(+), 24 deletions(-) (limited to 'src') 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) { // ----- OPERATORS ----- -int gen_assign(const char ident[], Scope scope) { +static int gen_assign_simple(const char ident[], Scope scope) { int l_addr = loc_get_addr(ident); int g_addr = glo_get_addr(ident); @@ -291,7 +291,7 @@ int gen_assign(const char ident[], Scope scope) { } } -int gen_assign_tab(const char ident[], Scope scope) { +static int gen_assign_tab(const char ident[], Scope scope) { int l_addr = loc_get_addr(ident); int g_addr = glo_get_addr(ident); @@ -317,6 +317,13 @@ int gen_assign_tab(const char ident[], Scope scope) { } } +int gen_assign(const char ident[], Scope scope) { + switch (loc_lookup(ident)) { + case TAB: return gen_assign_tab(ident, scope); + default: return gen_assign_simple(ident, scope); + } +} + void gen_or(int left, int right, int idx) { check_expected_types(left, INT,TAB); check_expected_types(right, INT,TAB); @@ -447,7 +454,7 @@ int gen_negate_expr(int type) { return type; } -int gen_value(const char ident[], Scope scope) { +static int gen_value_simple(const char ident[], Scope scope) { int l_addr = loc_get_addr(ident); switch (scope) { @@ -470,7 +477,8 @@ int gen_value(const char ident[], Scope scope) { exit(1); } } -int gen_value_tab(const char ident[], Scope scope) { + +static int gen_value_tab(const char ident[], Scope scope) { int l_addr = loc_get_addr(ident); int g_addr = glo_get_addr(ident); switch (scope) { @@ -489,6 +497,14 @@ int gen_value_tab(const char ident[], Scope scope) { exit(1); } } + +int gen_value(const char ident[], Scope scope) { + switch (loc_lookup(ident)) { + case TAB: return gen_value_tab(ident, scope); + default: return gen_value_simple(ident, scope); + } +} + int gen_num(int value, Scope scope) { fprintf(output, "push %d\n", value); 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); void gen_ifelse_end(int idx); int gen_assign(const char ident[], Scope scope); -int gen_assign_tab(const char ident[], Scope scope); void gen_or(int left, int right, int idx); void gen_and(int left, int right, int idx); @@ -51,7 +50,6 @@ void gen_divstar(char op, int left, int right); int gen_signed_expr(char op, int type); int gen_negate_expr(int type); int gen_value(const char ident[], Scope scope); -int gen_value_tab(const char ident[], Scope scope); int gen_num(int value, Scope scope); int gen_char(int value, Scope scope); diff --git a/src/tpc.y b/src/tpc.y index 74d31c6..6e733d0 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -2,11 +2,6 @@ /* * UPEM / Compilation / Projet * Pacien TRAN-GIRARD, Adam NAILI - * - * TODO : - * ------ - * - arrays - * */ int nb_globals = 0; @@ -147,13 +142,7 @@ IfHandling: { gen_if_start($$ = num_if++); }; IfEndHandling: { gen_if_end($-3); }; IfElseEndHandling: { gen_ifelse_end($-5); }; Exp: - LValue '=' Exp { - if(loc_lookup($1) != TAB){ - $$ = gen_assign($1, scope); - }else{ - $$ = gen_assign_tab($1,scope); - } - } + LValue '=' Exp { $$ = gen_assign($1, scope); } | EB ; EB: @@ -184,12 +173,7 @@ F: ADDSUB F { $$ = gen_signed_expr($1, $2); } | '!' F { $$ = gen_negate_expr($2); } | '(' Exp ')' { $$ = $2; } -| LValue { if(loc_lookup($1) != TAB){ - $$ = gen_value($1, scope); - }else{ - $$ = gen_value_tab($1,scope); - } - } +| LValue { $$ = gen_value($1, scope); } | NUM { $$ = gen_num($1, scope); } | CARACTERE { $$ = gen_char($1, scope); } | IDENT '(' Arguments ')' { $$ = gen_function_call($1,$3); } -- cgit v1.2.3