From b4481597ec8901b59a108f0b73ee1c4b57fca439 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Fri, 20 Aug 2021 09:56:42 +0000 Subject: cc: simple global scope and duplicate global detection --- miniany/cc.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++------ miniany/test1.c | 2 +- 2 files changed, 78 insertions(+), 10 deletions(-) diff --git a/miniany/cc.c b/miniany/cc.c index fc253b1..8d36653 100644 --- a/miniany/cc.c +++ b/miniany/cc.c @@ -150,17 +150,17 @@ int getToken( ) case '/': c = getChar( ); if( c == '/' ) { - while( c != '\n' ) { + while( c != EOF && c != '\n' ) { c = getChar( ); } t = getToken( ); } else if( c == '*' ) { do { - while( c != '*' ) { + while( c != EOF && c != '*' ) { c = getChar( ); } c = getChar( ); - } while( c != '/' ); + } while( c != EOF && c != '/' ); c = getChar( ); t = getToken( ); } else { @@ -283,14 +283,12 @@ struct Symbol { struct Symbol *next; }; -struct Symbol *symbol; - struct Symbol *createSymbol( char *s ) { struct Symbol *sym; sym = (struct Symbol *)malloc( sizeof ( struct Symbol ) ); - sym->name = (char *)strdup( s ); + sym->name = strdup( s ); sym->next = NULL; return sym; @@ -302,6 +300,65 @@ void freeSymbol( struct Symbol *sym ) free( (char *)sym ); } +struct Scope { + char *name; + struct Symbol *sym; +}; + +struct Scope *createScope( char *name ) +{ + struct Scope *scope; + + scope = (struct Scope *)malloc( sizeof( struct Scope ) ); + scope->name = strdup( name ); + scope->sym = NULL; + + return scope; +} + +void freeScope( struct Scope *scope ) +{ + struct Symbol *sym, *next; + + free( scope->name ); + + sym = scope->sym; + while( sym != NULL ) { + next = sym->next; + freeSymbol( sym ); + sym = next; + } + + free( (char *)scope ); +} + +struct Scope *global_scope; + +struct Symbol *getSymbol( struct Scope *scope, char *name ) +{ + struct Symbol *sym; + + sym = scope->sym; + while( sym != NULL ) { + if( strcmp( name, sym->name ) == 0 ) { + return sym; + } + sym = sym->next; + } + + return NULL; +} + +void insertSymbol( struct Scope *scope, struct Symbol *sym ) +{ + if( scope->sym == NULL ) { + scope->sym = sym; + } else { + sym->next = scope->sym; + scope->sym = sym; + } +} + void parseDeclaration( ) { struct Symbol *sym; @@ -309,8 +366,18 @@ void parseDeclaration( ) expect( S_INT, "int" ); expect( S_IDENT, "identifier" ); putstring( "Adding glob: " ); putstring( ident ); putnl( ); - sym = createSymbol( ident ); - freeSymbol( sym ); + sym = getSymbol( global_scope, ident ); + if( sym == NULL ) { + sym = createSymbol( ident ); + insertSymbol( global_scope, sym ); + } else { + printErrorHeader( ); + putstring( "duplicate global '" ); + putstring( ident ); + putstring( "'" ); + putnl( ); + exit( EXIT_FAILURE ); + } expect( S_SEMICOLON, ";" ); } @@ -339,7 +406,7 @@ int main( int argc, char **argv ) row = 1; pushback = 0; DEBUG_SCANNER = 1; - symbol = NULL; + global_scope = createScope( "global" ); ident = (char *)malloc( MAX_IDENT_LEN+1 ); token = getToken( ); @@ -347,6 +414,7 @@ int main( int argc, char **argv ) parseStatement( ); } + freeScope( global_scope ); free( (char *)ident ); exit( EXIT_SUCCESS ); diff --git a/miniany/test1.c b/miniany/test1.c index ba8dd4f..94684fa 100644 --- a/miniany/test1.c +++ b/miniany/test1.c @@ -1,6 +1,6 @@ /* test1 */ int i; -int i; +int j; i = 12+25/5-2*3; // another comment -- cgit v1.2.3-54-g00ecf