summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2021-08-20 09:56:42 +0000
committerAndreas Baumann <mail@andreasbaumann.cc>2021-08-20 09:56:42 +0000
commitb4481597ec8901b59a108f0b73ee1c4b57fca439 (patch)
treea2f5f24ee81068366025cae99390f2daa3e86668
parent1a6b63ad270d09ece8a520e64235ba8d8017ed22 (diff)
downloadcompilertests-b4481597ec8901b59a108f0b73ee1c4b57fca439.tar.gz
compilertests-b4481597ec8901b59a108f0b73ee1c4b57fca439.tar.bz2
cc: simple global scope and duplicate global detection
-rw-r--r--miniany/cc.c86
-rw-r--r--miniany/test1.c2
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