diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2018-12-29 21:40:43 +0100 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2018-12-29 21:40:43 +0100 |
commit | b6b969f02fc9f1bd242f1bbb6a91b637422a67b3 (patch) | |
tree | 2a4abc1365cff1536ea8c4b0267266c0fc33746d /minie | |
parent | 3b685241b74cfe05ba8926df472029d7e87f5de5 (diff) | |
download | compilertests-b6b969f02fc9f1bd242f1bbb6a91b637422a67b3.tar.gz compilertests-b6b969f02fc9f1bd242f1bbb6a91b637422a67b3.tar.bz2 |
some more work on proper type mapping in function parameter
Diffstat (limited to 'minie')
-rw-r--r-- | minie/e2c.c | 86 |
1 files changed, 60 insertions, 26 deletions
diff --git a/minie/e2c.c b/minie/e2c.c index 18010de..9a26729 100644 --- a/minie/e2c.c +++ b/minie/e2c.c @@ -508,7 +508,9 @@ typedef enum { TYPE_ARRAY, TYPE_RECORD, TYPE_FUNCTION, - TYPE_CONSTANT + TYPE_CONSTANT, + TYPE_NONE, + TYPE_ANY } BasicType; static BasicType strToBasicType( char *name ) @@ -544,6 +546,7 @@ typedef struct RecordType { typedef struct FunctionType { char data[128*MAX_PARAMETERS]; struct Type *params[MAX_PARAMETERS]; + struct Type *return_value; int len; int internal; } FunctionType; @@ -597,6 +600,35 @@ static Type get_symbol_type( char *name ) Abort( "Unknown symbol '%s'", name ); } +static char *basicTypeToCType( BasicType type ) +{ + switch( type ) { + case TYPE_INTEGER: + return "signed int"; + case TYPE_BOOLEAN: + return "unsigned char"; + case TYPE_CHAR: + return "unsigned char"; + case TYPE_BYTE: + return "unsigned char"; + default: + Abort( "Unknown basic type" ); + } +} + +static char *typeToCType( Type *type ) +{ + static char s[MAX_IDENT_LEN]; + + if( type->type == TYPE_ARRAY ) { + sprintf( s, "%s*", basicTypeToCType( type->details.array.type->type ) ); + } else { + Abort( "Unknown type" ); + } + + return s; +} + /* INTERNAL FUNCTIONS */ static int length( char *name ) @@ -771,7 +803,7 @@ static void factor( void ) if( type.details.constant.type == TYPE_BOOLEAN ) { emit( "%d", type.details.constant.value ); } else { - Abort( "Unkown constant '%s'", varName ); + Abort( "Unknown constant '%s'", varName ); } } else if( type.type == TYPE_ARRAY ) { if( sym == S_lbracket ) { @@ -1104,7 +1136,11 @@ static void arrayType( void ) { sym = getSym( ); Expect( S_lbracket ); + /* TODO: should be a const expression */ number( ); + if( num == 0 ) { + Abort( "array of size 0 makes no sense" ); + } sym = getSym( ); Expect( S_rbracket ); /* array size is in num */ @@ -1172,9 +1208,11 @@ static void procedureName( void ) static void procedureDeclaration( void ) { char return_type[MAX_IDENT_LEN]; - int nof_params = 0; - Type params[MAX_PARAMETERS]; Type funcType; + + funcType.type = TYPE_FUNCTION; + funcType.details.function.internal = 0; + funcType.details.function.len = 0; Expect( S_procedure ); procedureName( ); @@ -1182,36 +1220,41 @@ static void procedureDeclaration( void ) if( sym == S_lparen ) { sym = getSym( ); do { - if( nof_params >= MAX_PARAMETERS ) { + if( funcType.details.function.len >= MAX_PARAMETERS ) { Abort( "Too many parameters in definition of procedure" ); } identifier( ); - strncpy( params[nof_params].name, ident, MAX_IDENT_LEN ); - params[nof_params].name[MAX_IDENT_LEN-1] = '\0'; + funcType.details.function.params[funcType.details.function.len] = (Type *)( &funcType.details.function.data + sizeof( Type ) * funcType.details.function.len ); + strncpy( funcType.details.function.params[funcType.details.function.len]->name, ident, MAX_IDENT_LEN ); + funcType.details.function.params[funcType.details.function.len]->name[MAX_IDENT_LEN-1] = '\0'; sym = getSym( ); Expect( S_colon ); if( sym == S_array ) { sym = getSym( ); Expect( S_of ); type( ); - /* TODO type */ + funcType.details.function.params[funcType.details.function.len]->type = TYPE_ARRAY; + funcType.details.function.params[funcType.details.function.len]->details.array.len = 0; + funcType.details.function.params[funcType.details.function.len]->details.array.type = (Type *)&funcType.details.function.params[funcType.details.function.len]->details.array.data; + funcType.details.function.params[funcType.details.function.len]->details.array.type->type = lastType.type; } else { type( ); - /* TODO type */ + funcType.details.function.params[funcType.details.function.len]->type = lastType.type; } - nof_params++; + funcType.details.function.len++; } while( sym == S_comma ); Expect( S_rparen ); if( sym == S_colon ) { sym = getSym( ); type( ); + /* TODO: use proper type, not type string */ strncpy( return_type, typeName, MAX_IDENT_LEN ); return_type[MAX_IDENT_LEN-1] = '\0'; } else { strncpy( return_type, "void", MAX_IDENT_LEN ); } /* do not allow empty parameter lists in C++ style */ - if( nof_params == 0 ) { + if( funcType.details.function.len == 0 ) { Abort( "Empty parameter list must not be enclosed with ( )" ); } } else if( sym == S_semicolon ) { @@ -1221,27 +1264,18 @@ static void procedureDeclaration( void ) Abort( "Expected '(' after procedure declaration" ); } Expect( S_semicolon ); - - /* TODO: properly map data types from above */ - funcType.type = TYPE_FUNCTION; - funcType.details.function.params[0] = (Type *)&funcType.details.function.data; - funcType.details.function.len = nof_params; - /* - type.details.function.params[0]->type = TYPE_ARRAY; - type.details.function.internal = 1; - */ - - funcType.details.function.internal = 0; + insert_symbol( funcType, procName ); emit( "%s %s( ", return_type, procName ); - if( nof_params == 0 ) { + if( funcType.details.function.len == 0 ) { emit( "void" ); } else { int i; - for( i = 0; i < nof_params; i++ ) { - /* TODO: map proper C types from type description */ - emit( "char *%s ", params[i].name ); + for( i = 0; i < funcType.details.function.len; i++ ) { + emit( "%s%s ", + typeToCType( funcType.details.function.params[i] ), + funcType.details.function.params[i]->name ); } } emitLn( ") {" ); |