From fef6b13abeee053871c60e0532c46c8b7ba767b5 Mon Sep 17 00:00:00 2001 From: Bernhard Urban Date: Tue, 30 Mar 2010 19:20:46 +0200 Subject: [PATCH] ag: noch ein paar bugs gefixt und symboltaballe weiter verschoenert --- ag/parser.y | 2 +- ag/symtable.c | 55 +++++++++++++++++++++++++++++---------------------- ag/symtable.h | 4 ++-- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/ag/parser.y b/ag/parser.y index af8f0a6..b678961 100644 --- a/ag/parser.y +++ b/ag/parser.y @@ -115,7 +115,7 @@ Statement: | VAR IDENT ASSIGN Expr @{ - @i @Statement.sout@ = tab_add_symbol(clone_tab(@Statement.sin@), @IDENT.name@, S_VAR, 0); + @i @Statement.sout@ = tab_add_symbol(clone_tab(@Statement.sin@), @IDENT.name@, S_VAR, 1); @i @Expr.s@ = @Statement.sin@; @} diff --git a/ag/symtable.c b/ag/symtable.c index 7242f06..590077d 100755 --- a/ag/symtable.c +++ b/ag/symtable.c @@ -3,6 +3,10 @@ #include #include "symtable.h" +#define DD + +static void check_gen(struct symbol *tab, char *ident, short type); + struct symbol *new_tab(void) { return SYMNULL; @@ -12,6 +16,9 @@ struct symbol *clone_tab(struct symbol *tab) { struct symbol *elm; struct symbol *new_tabx; +#ifdef DD + printf("clone_tab: tab(%08X)\n", tab); +#endif elm = tab; new_tabx = new_tab(); @@ -27,13 +34,16 @@ struct symbol *tab_add_symbol(struct symbol *tab, char *ident, short type, short { struct symbol *elm; struct symbol *new_elm; +#ifdef DD + printf("tab_add_symbol: tab(%08X), ident(%s), type(%i), check(%i)\n", tab, ident, type, check); +#endif - if(tab_lookup(tab, ident) != SYMNULL) { + if(tab_lookup(tab, ident, type) != SYMNULL) { if(check) { - fprintf(stderr, "Feld doppelt vorhanden: \"%s\"\n", ident); + fprintf(stderr, "Identifier doppelt vorhanden: \"%s\"\n", ident); exit(3); } else { - tab = tab_remove_symbol(tab, ident); + tab = tab_remove_symbol(tab, ident, type); } } @@ -55,7 +65,7 @@ struct symbol *tab_add_symbol(struct symbol *tab, char *ident, short type, short return tab; } -struct symbol *tab_lookup(struct symbol *tab, char *ident) +struct symbol *tab_lookup(struct symbol *tab, char *ident, short type) { struct symbol *elm = tab; @@ -64,7 +74,7 @@ struct symbol *tab_lookup(struct symbol *tab, char *ident) } do { - if(strcmp(elm->ident, ident) == 0) { + if((elm->type == type) && (strcmp(elm->ident, ident) == 0)) { return elm; } elm = elm->next; @@ -77,6 +87,9 @@ struct symbol *tab_merge(struct symbol *tab, struct symbol *to_add, short check) { struct symbol *elm = to_add; struct symbol *new_tab = clone_tab(tab); +#ifdef DD + printf("tab_merge: tab(%08X), to_add(%08X), check(%i), new_tab(%08X)\n", tab, to_add, check, new_tab); +#endif while(elm != SYMNULL) { new_tab = tab_add_symbol(new_tab, elm->ident, elm->type, check); @@ -86,13 +99,13 @@ struct symbol *tab_merge(struct symbol *tab, struct symbol *to_add, short check) return new_tab; } -struct symbol *tab_remove_symbol(struct symbol *tab, char *ident) +struct symbol *tab_remove_symbol(struct symbol *tab, char *ident, short type) { struct symbol *elm = tab; struct symbol *previous_elm = SYMNULL; while(elm != SYMNULL) { - if(strcmp(elm->ident, ident) == 0) { + if((elm->type == type) && (strcmp(elm->ident, ident) == 0)) { if(previous_elm == SYMNULL) { tab = elm->next; } else { @@ -111,27 +124,21 @@ struct symbol *tab_remove_symbol(struct symbol *tab, char *ident) void check_variable(struct symbol *tab, char *ident) { - struct symbol *elm = tab_lookup(tab, ident); - if(elm != SYMNULL) { - if(elm->type != S_VAR) { - fprintf(stderr, "Identifier ist keine Variable: \"%s\"\n", ident); - exit(3); - } - } else { - fprintf(stderr, "Unbekannter Identifier: \"%s\"\n", ident); - exit(3); - } + check_gen(tab, ident, S_VAR); } void check_field(struct symbol *tab, char *ident) { - struct symbol *elm = tab_lookup(tab, ident); - if(elm != SYMNULL) { - if(elm->type != S_FIELD) { - fprintf(stderr, "Identifier ist kein Feld: \"%s\"\n", ident); - exit(3); - } - } else { + check_gen(tab, ident, S_FIELD); +} + +static void check_gen(struct symbol *tab, char *ident, short type) +{ + struct symbol *elm = tab_lookup(tab, ident, type); +#ifdef DD + printf("check_variable: tab(%08X), ident(%s), type(%i), elm(%08X)\n", tab, ident, type, elm); +#endif + if(elm == SYMNULL) { fprintf(stderr, "Unbekannter Identifier: \"%s\"\n", ident); exit(3); } diff --git a/ag/symtable.h b/ag/symtable.h index 92bfc86..4897a74 100755 --- a/ag/symtable.h +++ b/ag/symtable.h @@ -15,8 +15,8 @@ struct symbol { struct symbol *clone_tab(struct symbol *tab); struct symbol *new_tab(void); struct symbol *tab_add_symbol(struct symbol *tab, char *ident, short type, short check); -struct symbol *tab_lookup(struct symbol *tab, char *ident); -struct symbol *tab_remove_symbol(struct symbol *tab, char *ident); +struct symbol *tab_lookup(struct symbol *tab, char *ident, short type); +struct symbol *tab_remove_symbol(struct symbol *tab, char *ident, short type); struct symbol *tab_merge(struct symbol *tab, struct symbol *to_add, short check); void check_variable(struct symbol *tab, char *ident); void check_field(struct symbol *tab, char *ident); -- 2.25.1