From 0f4b1600983f8afda41a02fec07424338785d81d Mon Sep 17 00:00:00 2001 From: pacien Date: Tue, 5 Jun 2018 22:41:15 +0200 Subject: Prevent assigning to const --- src/generator.c | 20 ++++++++++++++++++-- src/generator.h | 9 ++------- src/symbol_table.c | 20 ++++++++++++++++++++ src/symbol_table.h | 8 ++++++++ src/tpc.y | 4 ++-- 5 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/generator.c b/src/generator.c index 3ecfe0d..f32f468 100644 --- a/src/generator.c +++ b/src/generator.c @@ -159,7 +159,12 @@ void gen_check(const char name[], Scope scope) { } // ----- READ AND PRINT FUNCTIONS ----- -void gen_reade(const char name[]) { +void gen_reade(const char name[], Scope scope) { + if (is_read_only(name, scope)) { + fprintf(stderr, "Symbol \"%s\" at line %d is read only.\n", name, scope); + exit(1); + } + if (loc_lookup(name) != INT) { fprintf(stderr, "Need to be a INT in the reade() function\n"); return; @@ -174,7 +179,12 @@ void gen_reade(const char name[]) { fprintf(output, "mov rax,globals\nadd rax,%d\ncall reade\n", g_addr); } -void gen_readc(const char name[]) { +void gen_readc(const char name[], Scope scope) { + if (is_read_only(name, scope)) { + fprintf(stderr, "Symbol \"%s\" at line %d is read only.\n", name, scope); + exit(1); + } + if (loc_lookup(name) != CHAR) { fprintf(stderr, "Need to be a CHAR in the readc() function\n"); return; @@ -233,6 +243,12 @@ void gen_ifelse_end(int idx) { int gen_assign(const char ident[], Scope scope) { int l_addr = loc_get_addr(ident); int g_addr = glo_get_addr(ident); + + if (is_read_only(ident, scope)) { + fprintf(stderr, "Symbol \"%s\" at line %d is read only.\n", ident, scope); + exit(1); + } + switch (scope) { case GLOBAL: fprintf(output, "pop QWORD [globals + %d] ;%s\n", g_addr, diff --git a/src/generator.h b/src/generator.h index 6c132fd..d2ad5c2 100644 --- a/src/generator.h +++ b/src/generator.h @@ -10,11 +10,6 @@ #include #include "symbol_table.h" -typedef enum scope { - GLOBAL, - LOCAL -} Scope; - extern int nb_globals; extern int lineno; FILE *output; @@ -32,8 +27,8 @@ Type gen_function_call(const char name[], int nb_param); void gen_declaration(const char name[], int type, Scope scope); void gen_check(const char name[], Scope scope); -void gen_reade(const char name[]); -void gen_readc(const char name[]); +void gen_reade(const char name[], Scope scope); +void gen_readc(const char name[], Scope scope); void gen_print(int type); diff --git a/src/symbol_table.c b/src/symbol_table.c index 08124ac..68e24f0 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -228,3 +228,23 @@ void check_expected_type(int type_to_check, int type_expected) { string_of_type(type_expected), string_of_type(type_to_check), lineno); } + +/* returns false if symbol can't be found too */ +bool is_read_only(const char name[], Scope scope) { + int count; + + switch (scope) { + case LOCAL: + for (count = 0; count < loc_symbol_table.size; count++) + if (!strcmp(loc_symbol_table.entries[count].name, name)) + return loc_symbol_table.entries[count].read_only; + + case GLOBAL: + for (count = 0; count < glo_symbol_table.size; count++) + if (!strcmp(glo_symbol_table.entries[count].name, name)) + return glo_symbol_table.entries[count].read_only; + + default: + return false; + } +} diff --git a/src/symbol_table.h b/src/symbol_table.h index b2c8879..23cd618 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -15,6 +15,11 @@ #define MAXSYMBOLS 256 #define MAXFUNCTIONS 256 +typedef enum scope { + GLOBAL, + LOCAL +} Scope; + typedef enum type { INT, CHAR, @@ -54,12 +59,15 @@ void glo_addConst(const char name[]); int glo_lookup(const char name[]); int glo_get_addr(const char name[]); void glo_display_table(); + void loc_addVar(const char name[], int type); void loc_addConst(const char name[]); int loc_lookup(const char name[]); int loc_get_addr(const char name[]); void loc_display_table(); void loc_clean_table(); + void check_expected_type(int type_to_check, int type_expected); +bool is_read_only(const char name[], Scope scope); #endif diff --git a/src/tpc.y b/src/tpc.y index d8fd1f1..64652d9 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -121,8 +121,8 @@ Instr: | ';' | RETURN Exp ';' { gen_function_return(return_type, $2); } | RETURN ';' { gen_function_return(return_type, VOID_T);} -| READE '(' IDENT ')' ';' { gen_reade($3); } -| READC '(' IDENT ')' ';' { gen_readc($3); } +| READE '(' IDENT ')' ';' { gen_reade($3, scope); } +| READC '(' IDENT ')' ';' { gen_readc($3, scope); } | PRINT '(' Exp ')' ';' { gen_print($3);} | IF '(' Exp IfHandling')' Instr { gen_if_label($4); } | IF '(' Exp IfHandling')' Instr ELSE IfEndHandling Instr IfElseEndHandling -- cgit v1.2.3