diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2019-02-09 18:19:08 +0100 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2019-02-09 18:19:08 +0100 |
commit | 93cd4ea26f9dfc53d75d15a2458362ac25c42ac3 (patch) | |
tree | 57851e5842a94926a10487853b3588b04f403a1a /minie | |
parent | 60444dbdb4164464c84c2ae6d33939f4f32859cd (diff) | |
download | compilertests-93cd4ea26f9dfc53d75d15a2458362ac25c42ac3.tar.gz compilertests-93cd4ea26f9dfc53d75d15a2458362ac25c42ac3.tar.bz2 |
work on parameter lists, return values
Diffstat (limited to 'minie')
-rwxr-xr-x | minie/build.sh | 2 | ||||
-rw-r--r-- | minie/e2c.c | 102 | ||||
-rw-r--r-- | minie/ec.e | 35 | ||||
-rw-r--r-- | minie/test9.e | 3 |
4 files changed, 100 insertions, 42 deletions
diff --git a/minie/build.sh b/minie/build.sh index 5129401..01bb579 100755 --- a/minie/build.sh +++ b/minie/build.sh @@ -3,7 +3,7 @@ set -e gcc -m32 --stack-usage -g -O0 -Wall -pedantic -std=c89 -Wno-return-type -o e2c e2c.c -tcc -g -o e2c e2c.c +tcc -g -O0 -m32 -o e2c e2c.c pcc -g -Wall -O0 -g -D__float128="long double" -o e2c e2c.c clang -g -O0 -Wall -pedantic -std=c89 -Wno-return-type -o e2c e2c.c ./e2c < test1.e diff --git a/minie/e2c.c b/minie/e2c.c index e2388b7..759e23b 100644 --- a/minie/e2c.c +++ b/minie/e2c.c @@ -181,6 +181,7 @@ static int isCharacter( int c ) case '[': case ']': case ':': + case ',': /* TODO: allow more characters as we go along */ return 1; default: @@ -505,7 +506,8 @@ typedef enum { TYPE_ARRAY, TYPE_RECORD, TYPE_FUNCTION, - TYPE_CONSTANT + TYPE_CONSTANT, + TYPE_NONE } BasicType; static BasicType strToBasicType( char *name ) @@ -689,6 +691,7 @@ static void register_internal_functions( void ) type.details.function.internal = 1; insert_symbol( type, "length" ); + /* constructor for char type */ type.type = TYPE_FUNCTION; type.details.function.len = 1; type.details.function.params[0] = (Type *)type.details.function.data; @@ -698,13 +701,23 @@ static void register_internal_functions( void ) type.details.function.internal = 1; insert_symbol( type, "char" ); - /* register functions in module system, should be outside in the stage-1 compiler */ + /* TODO: register functions in module system, should be outside in the stage-1 compiler */ + type.type = TYPE_FUNCTION; type.details.function.len = 0; type.details.function.return_value = (Type *)type.details.function.data; type.details.function.return_value->type = TYPE_CHAR; type.details.function.internal = 1; insert_symbol( type, "system.readchar" ); + + type.type = TYPE_FUNCTION; + type.details.function.len = 1; + type.details.function.params[0] = (Type *)type.details.function.data; + type.details.function.params[0]->type = TYPE_ARRAY; + type.details.function.return_value = (Type *)type.details.function.data; + type.details.function.return_value->type = TYPE_NONE; + type.details.function.internal = 1; + insert_symbol( type, "system.readline" ); } static void register_internal_constants( void ) @@ -727,7 +740,19 @@ static void prologue( void ) emitLn( "#include <stdio.h>" ); emitLn( "#include <stdlib.h>" ); emitLn( "#include <string.h>" ); - emitLn( "int getc_wrapper( void ) { int c = getc( stdin ); if( c == EOF ) return '\\0'; else return c; }" ); + emitLn( \ +"int getc_wrapper( void ) {" \ +" int c = getc( stdin );" \ +" if( c == EOF ) {" \ +" if( feof( stdin ) ) {" \ +" return '\\0';" \ +" } else {" \ +" fprintf( stderr, \"ERROR: read error\\n\" );" \ +" }" \ +" } else {" \ +" return c;" \ +" }" \ +"}" ); register_internal_functions( ); register_internal_constants( ); @@ -1076,6 +1101,7 @@ static void parameterList( void ) } else if( strcmp( funcName, "system.halt" ) == 0 ) { emit( "exit( " ); } else if( strcmp( funcName, "system.readline" ) == 0 ) { + /* Type type = get_symbol_type( varName ); */ /* TODO: check if parameter is an array of char, get length of the defined array and put it into the parameter of getline */ @@ -1275,49 +1301,47 @@ static void procedureDeclaration( void ) sym = getSym( ); if( sym == S_lparen ) { sym = getSym( ); - do { - if( funcType.details.function.len >= MAX_PARAMETERS ) { - Abort( "Too many parameters in definition of procedure" ); - } - identifier( ); - 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 ) { + /* TODO: || S_var later */ + if( sym == S_ident ) { + do { + if( funcType.details.function.len >= MAX_PARAMETERS ) { + Abort( "Too many parameters in definition of procedure" ); + } + identifier( ); + 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_of ); - 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( ); - funcType.details.function.params[funcType.details.function.len]->type = lastType.type; - } - funcType.details.function.len++; - } while( sym == S_comma ); - Expect( S_rparen ); - if( sym == S_colon ) { - sym = getSym( ); - simpleType( ); - strncpy( return_type, - basicTypeToCType( lastType.type ), - MAX_IDENT_LEN ); - } else { - strncpy( return_type, "void", MAX_IDENT_LEN ); + Expect( S_colon ); + if( sym == S_array ) { + sym = getSym( ); + Expect( S_of ); + 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( ); + funcType.details.function.params[funcType.details.function.len]->type = lastType.type; + } + funcType.details.function.len++; + } while( sym == S_comma ); } + Expect( S_rparen ); /* do not allow empty parameter lists in C++ style */ if( funcType.details.function.len == 0 ) { Abort( "Empty parameter list must not be enclosed with ( )" ); } - } else if( sym == S_semicolon ) { - strncpy( return_type, "void", MAX_IDENT_LEN ); - /* skip, ok */ + } + if( sym == S_colon ) { + sym = getSym( ); + simpleType( ); + strncpy( return_type, + basicTypeToCType( lastType.type ), + MAX_IDENT_LEN ); } else { - Abort( "Expected '(' after procedure declaration" ); + strncpy( return_type, "void", MAX_IDENT_LEN ); } Expect( S_semicolon ); @@ -2,6 +2,34 @@ module ec; import system; +var + col : integer; + row : integer; + look : char; + +procedure init; +begin + col := 1; + row := 1; +end + +procedure getChar : char; +var + c : char; + +begin + c := system.readchar( ); + if ( c = char( 0 ) ) do + return c; + end; + col := col + 1; + if ( c = char( 10 ) ) do + col := 1; + row := row + 1; + end; + return c; +end + procedure prologue; begin system.writeline( "[bits 32]" ); @@ -15,7 +43,14 @@ begin end begin + init( ); prologue( ); epilogue( ); + + look := getChar( ); + while ( look <> char( 0 ) ) do + look := getChar( ); + end; + system.halt( 0 ); end diff --git a/minie/test9.e b/minie/test9.e index 9b087dd..2e8a87f 100644 --- a/minie/test9.e +++ b/minie/test9.e @@ -6,14 +6,13 @@ var len : integer; i : integer; s : array[64] of char; - d : array[24] of char; + d : array[10] of char; c : char; begin i := 0; len := length( s ); c := system.readchar( ); - (* C EOF is no char( 0 ) in e *) while ( i < len ) and ( c <> char( 0 ) ) do s[i] := c; i := i + 1; |