diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2018-12-28 15:28:57 +0100 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2018-12-28 15:28:57 +0100 |
commit | e7dcf15572104e00b7b7b79a4ccbce28c3467638 (patch) | |
tree | a304fd3c8266070691d85ba379f45e4bf67acc80 /minie | |
parent | cb4b6b710f5f553c2775e1d5481aab9c4319b1aa (diff) | |
download | compilertests-e7dcf15572104e00b7b7b79a4ccbce28c3467638.tar.gz compilertests-e7dcf15572104e00b7b7b79a4ccbce28c3467638.tar.bz2 |
added while and return statements, only stubs for type maps in procedure declarations
Diffstat (limited to 'minie')
-rw-r--r-- | minie/e2c.c | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/minie/e2c.c b/minie/e2c.c index f9215be..9e1d441 100644 --- a/minie/e2c.c +++ b/minie/e2c.c @@ -35,6 +35,7 @@ typedef enum { S_end, S_import, S_procedure, + S_return, S_var, S_if, S_do, @@ -43,6 +44,7 @@ typedef enum { S_of, S_for, S_to, + S_while, S_dot, S_comma, S_semicolon, @@ -86,6 +88,7 @@ char *symname[S_eof+1] = { "of", "for", "to", + "while", ".", ",", ";", @@ -364,6 +367,10 @@ next: return S_ident; case 'q': case 'r': + identifier( ); + if( strcmp( ident, "return" ) == 0 ) { + return S_return; + } case 's': case 't': identifier( ); @@ -379,6 +386,11 @@ next: } return S_ident; case 'w': + identifier( ); + if( strcmp( ident, "while" ) == 0 ) { + return S_while; + } + return S_ident; case 'x': case 'y': case 'z': @@ -901,6 +913,24 @@ void static doFor( void ) emitLn( "}" ); } +static void doWhile( void ) +{ + emit( "while( " ); + expression( ); + emitLn( ") {" ); + Expect( S_do ); + statementSequence( ); + Expect( S_end ); + emitLn( "}" ); +} + +static void doReturn( void ) +{ + emit( "return " ); + expression( ); + emitLn( ";" ); +} + static void parameterList( void ) { char funcName[MAX_IDENT_LEN]; @@ -989,6 +1019,12 @@ static void statement( void ) } else if( sym == S_for ) { sym = getSym( ); doFor( ); + } else if( sym == S_while ) { + sym = getSym( ); + doWhile( ); + } else if( sym == S_return ) { + sym = getSym( ); + doReturn( ); /* TODO: S_else feels wrong here, just for the end ';' in an if-block */ } else if( sym == S_end || sym == S_else ) { return; @@ -1019,6 +1055,10 @@ static void statementBlock( void ) { Expect( S_begin ); statementSequence( ); + if( sym == S_return ) { + sym = getSym( ); + identifier( ); + } Expect( S_end ); } @@ -1125,7 +1165,8 @@ static void procedureDeclaration( void ) char return_type[MAX_IDENT_LEN]; int nof_params = 0; Type params[MAX_PARAMETERS]; - + Type funcType; + Expect( S_procedure ); procedureName( ); sym = getSym( ); @@ -1171,13 +1212,23 @@ 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.len = nof_params; +/* type.details.function.params[0] = TYPE_ARRAY; + */ + funcType.details.function.internal = 0; + insert_symbol( funcType, procName ); + emit( "%s %s( ", return_type, procName ); if( nof_params == 0 ) { emit( "void" ); } else { int i; for( i = 0; i < nof_params; i++ ) { - emit( "%s ", params[i].name ); + /* TODO: map proper C types from type description */ + emit( "char *%s ", params[i].name ); } } emitLn( ") {" ); |