diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2018-12-25 17:06:27 +0100 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2018-12-25 17:06:27 +0100 |
commit | 7081b1ff435807eacedb5276a8d1794cc390b285 (patch) | |
tree | 9075b5d02c1c69fea0061c9bd691d57f7ea20abc /minie | |
parent | 8f8d30e72eddbc8a57ab6ea211a4ca4610da30e0 (diff) | |
download | compilertests-7081b1ff435807eacedb5276a8d1794cc390b285.tar.gz compilertests-7081b1ff435807eacedb5276a8d1794cc390b285.tar.bz2 |
added constants (for true and false)
redone function, procedure code generation
Diffstat (limited to 'minie')
-rw-r--r-- | minie/e2c.c | 137 | ||||
-rw-r--r-- | minie/test6.e | 2 |
2 files changed, 93 insertions, 46 deletions
diff --git a/minie/e2c.c b/minie/e2c.c index 6c358a1..38845fd 100644 --- a/minie/e2c.c +++ b/minie/e2c.c @@ -96,6 +96,7 @@ static void Err( char *s, va_list args ) fprintf( stderr, "Error line %d, pos %d: ", row, col ); vfprintf( stderr, s, args ); fputs( "\n", stderr ); + fflush( stderr ); } static void Halt( ) @@ -416,7 +417,8 @@ typedef enum { TYPE_CHAR, TYPE_ARRAY, TYPE_RECORD, - TYPE_FUNCTION + TYPE_FUNCTION, + TYPE_CONSTANT } BasicType; static BasicType strToBasicType( char *name ) @@ -451,6 +453,15 @@ typedef struct FunctionType { int internal; } FunctionType; +typedef struct ConstantType { + BasicType type; + union { + int boolean; + int integer; + char character; + } value; +} ConstantType; + typedef struct Type { BasicType type; char name[MAX_IDENT_LEN]; @@ -458,6 +469,7 @@ typedef struct Type { ArrayType array; RecordType record; FunctionType function; + ConstantType constant; } details; } Type; @@ -527,6 +539,7 @@ static void emitLn( char *s, ... ) vprintf( s, args ); va_end( args ); puts( "" ); + fflush( stdout ); } static void emit( char *s, ... ) @@ -535,6 +548,7 @@ static void emit( char *s, ... ) va_start( args, s ); vprintf( s, args ); va_end( args ); + fflush( stdout ); } static void register_internal_functions( void ) @@ -549,6 +563,20 @@ static void register_internal_functions( void ) insert_symbol( type, "length" ); } +static void register_internal_constants( void ) +{ + Type type; + + type.type = TYPE_CONSTANT; + type.details.constant.type = TYPE_BOOLEAN; + type.details.constant.value.boolean = 1; + insert_symbol( type, "true" ); + + type.details.constant.type = TYPE_BOOLEAN; + type.details.constant.value.boolean = 0; + insert_symbol( type, "false" ); +} + static void prologue( void ) { emitLn( "/* generated with e2c */" ); @@ -557,6 +585,7 @@ static void prologue( void ) emitLn( "#include <string.h>" ); register_internal_functions( ); + register_internal_constants( ); } static void init( void ) @@ -626,24 +655,18 @@ static void factor( void ) emit( "\"%s\"", str ); sym = getSym( ); } else if( sym == S_ident ) { - type = get_symbol_type( ident ); + qualident( ); + type = get_symbol_type( varName ); if( type.type == TYPE_FUNCTION ) { - emit( "%s( ", ident ); - sym = getSym( ); parameterList( ); - emit( " )" ); - } else { - /* TODO: hacky, the question is also, should this - * be a keyword - */ - if( strcmp( ident, "true" ) == 0 ) { - emit( "1" ); - } else if( strcmp( ident, "false" ) == 0 ) { - emit( "0" ); + } else if( type.type == TYPE_CONSTANT ) { + if( type.details.constant.type == TYPE_BOOLEAN ) { + emit( "%d", type.details.constant.value ); } else { - emit( "%s", ident ); + Abort( "Unkown constant '%s'", varName ); } - sym = getSym( ); + } else { + emit( "%s", varName ); } } else if( sym == S_lparen ) { emit( "(" ); @@ -732,16 +755,50 @@ static void doIf( void ) static void parameterList( void ) { + char funcName[MAX_IDENT_LEN]; int n = 0; - - Expect( S_lparen ); + + /* varName contains the function/procedure name, + * not the best name, actually the precondition is + * to have parsed a qualident + */ + strncpy( funcName, varName, MAX_IDENT_LEN ); + + if( sym != S_lparen ) { + Abort( "Expected symbol '%s'", symname[sym] ); + } + sym = getSym( ); if( sym == S_rparen ) { - if( strcmp( varName, "length" ) == 0 ) { - emit( "%d", length( varName ) ); - } + emit( "%s( )", funcName ); sym = getSym( ); return; } + + /* prologue of 1-parameter function or procedure */ + if( sym != S_comma ) { + /* TODO: add internal function maps as symbols and procedures */ + if( strcmp( funcName, "length" ) == 0 ) { + emit( "%d", /* length( funcName ) */ 255 ); + /* handle internal 0-parameter functions and procedures */ + } else if( strcmp( funcName, "system.writeline" ) == 0 ) { + emit( "printf( \"%%s\\n\", " ); + } else if( strcmp( funcName, "system.writestring" ) == 0 ) { + emit( "printf( \"%%s\", " ); + } else if( strcmp( funcName, "system.writeinteger" ) == 0 ) { + emit( "printf( \"%%d\", " ); + } else if( strcmp( funcName, "system.halt" ) == 0 ) { + emit( "exit( " ); + } else if( strcmp( funcName, "system.readline" ) == 0 ) { + /* TODO: check if parameter is an array of char, + get length of the defined array and put it into the parameter + of getline */ + emit( "{ size_t _n = %d; fgets( (char *)&", 255 ); + } else { + emit( "%s( ", funcName ); + } + + } + /* TODO: no VAR parameters, strictly pass-by-value */ expression( ); n = 1; @@ -752,7 +809,21 @@ static void parameterList( void ) /* TODO: no VAR parameters, strictly pass-by-value */ expression( ); } + + /* epilogue of 1-parameter function or procedure */ + if( n == 1 ) { + if( strcmp( funcName, "system.readline" ) == 0 ) { + emit( ", _n, stdin ); }" ); + } else { + emit( ")" ); + } + } + Expect( S_rparen ); + +#if 0 +#endif + } static void statement( void ) @@ -766,32 +837,8 @@ static void statement( void ) } else if( sym == S_ident ) { qualident( ); if( sym == S_lparen ) { - /* TODO: add internal function maps as symbols and procedures */ - if( strcmp( varName, "system.writestring" ) == 0 || - strcmp( varName, "system.writeline" ) == 0 ) { - emit( "printf( \"%%s\", " ); - } else if( strcmp( varName, "system.writeinteger" ) == 0 ) { - emit( "printf( \"%%d\", " ); - } else if( strcmp( varName, "system.halt" ) == 0 ) { - emit( "exit( " ); - } else if( strcmp( varName, "system.readline" ) == 0 ) { - /* TODO: check if parameter is an array of char, - get length of the defined array and put it into the parameter - of getline */ - emit( "{ size_t _n = %d; fgets( (char *)&", 255 ); - } else { - emit( "%s( ", varName ); - } parameterList( ); - /* TODO: procedure call */ - if( strcmp( varName, "system.readline" ) == 0 ) { - emitLn( ", _n, stdin ); }" ); - } else { - emitLn( ");" ); - } - if( strcmp( varName, "system.writeline" ) == 0 ) { - emitLn( "puts( \"\" );" ); - } + emitLn( ";" ); } else { assignment( ); } diff --git a/minie/test6.e b/minie/test6.e index 42838ef..1068299 100644 --- a/minie/test6.e +++ b/minie/test6.e @@ -8,7 +8,7 @@ var begin system.readline( s ); - len := length( s ); + len := length( ); system.writestring( "String: " ); system.writeline( s ); system.writestring( "Length: " ); |