diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2018-09-24 10:06:56 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2018-09-24 10:06:56 +0200 |
commit | cdfa980cbf43e70123b1ec42becc58e657869259 (patch) | |
tree | 4f29155990ec6a937f1e142a4720eb6d4eacc651 /minie | |
parent | f6aaa114af25bbbab270c2c242650c67669c0ed4 (diff) | |
download | compilertests-cdfa980cbf43e70123b1ec42becc58e657869259.tar.gz compilertests-cdfa980cbf43e70123b1ec42becc58e657869259.tar.bz2 |
playing with some procedures in minie
Diffstat (limited to 'minie')
-rw-r--r-- | minie/e2c.c | 67 | ||||
-rw-r--r-- | minie/ec.e | 4 |
2 files changed, 58 insertions, 13 deletions
diff --git a/minie/e2c.c b/minie/e2c.c index bcd3210..869c65c 100644 --- a/minie/e2c.c +++ b/minie/e2c.c @@ -16,6 +16,7 @@ static char ident[MAX_IDENT_LEN+1]; static char moduleName[MAX_IDENT_LEN+1]; static char varName[MAX_IDENT_LEN+1]; static char typeName[MAX_IDENT_LEN+1]; +static char procName[MAX_IDENT_LEN+1]; static int num; typedef enum { @@ -26,6 +27,7 @@ typedef enum { S_begin, S_end, S_import, + S_procedure, S_var, S_if, S_do, @@ -51,6 +53,7 @@ char *symname[S_eof+1] = { "begin", "end", "import", + "procedure", "var", "if", "do", @@ -232,6 +235,11 @@ static Symbol getSym( ) case 'n': case 'o': case 'p': + identifier( ); + if( strcmp( ident, "procedure" ) == 0 ) { + return S_procedure; + } + return S_ident; case 'q': case 'r': case 's': @@ -429,14 +437,11 @@ static void expression( void ) static void assignment( void ) { - char v[MAX_IDENT_LEN+1]; - + /* qualident is variableName( ) and has been read already */ variableName( ); - strncpy( v, ident, MAX_IDENT_LEN ); - v[MAX_IDENT_LEN-1] = '\0'; sym = getSym( ); Expect( S_assign ); - emit( "%s = ", v ); + emit( "%s = ", varName ); expression( ); emitLn( ";" ); } @@ -482,10 +487,18 @@ static void statement( void ) /* TODO: S_else feels wrong here, just for the end ';' in an if-block */ } else if( sym == S_end || sym == S_else ) { return; - } else { + } else if( sym == S_ident ) { assignment( ); + } else { + Abort( "Illegal statement" ); } } +/* +QualifiedIdentifier = Identifier [ "." Identifier ]; +ParameterList = "(" ")" . +ProdecureCall = QualifiedIdentifier ParameterList . +Statement = Assignment | IfStatement | ProcedureCall . +*/ static void statementSequence( void ) { @@ -536,7 +549,7 @@ static void variableDeclaration( void ) emitLn( "static %s %s;", typeName, varName ); } -static void declarationBlock( void ) +static void variableBlock( void ) { Expect( S_var ); variableDeclaration( ); @@ -551,9 +564,43 @@ static void declarationBlock( void ) } } +static void procedureName( void ) +{ + identifier( ); + if( sym == S_ident ) { + strncpy( procName, ident, MAX_IDENT_LEN ); + procName[MAX_IDENT_LEN-1] = '\0'; + } +} + +static void procedureDeclaration( void ) +{ + Expect( S_procedure ); + procedureName( ); + sym = getSym( ); + /* TODO: procedure parameters */ + Expect( S_semicolon ); + statementBlock( ); +} + +static void procedureBlock( void ) +{ + while( sym == S_procedure ) { + procedureDeclaration( ); + } +} + +static void declarationBlock( void ) +{ + if( sym == S_var ) { + variableBlock( ); + } + procedureBlock( ); +} + static void handleImport( void ) { - fprintf( stderr, "Import module '%s'\n", ident ); + fprintf( stderr, "Importing module '%s'\n", ident ); } static void importBlock( void ) @@ -582,9 +629,7 @@ static void module( void ) if( sym == S_import ) { importBlock( ); } - if( sym == S_var ) { - declarationBlock( ); - } + declarationBlock( ); emitLn( "void module_%s_init( ) {", moduleName ); statementBlock( ); emitLn( "}" ); @@ -15,6 +15,6 @@ begin end begin - prologue; - epilogue; + prologue( ); + epilogue( ); end |