diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2020-05-26 20:42:16 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2020-05-26 20:42:16 +0200 |
commit | 8c3ad4c4acc3212ba5d1ff0a3aada3e714dad071 (patch) | |
tree | 116a0eb2dd4e768b630ce442c38a23ec86ceb998 | |
parent | 58a041e920116d7f06100a588172b4719e32a40b (diff) | |
download | compilertests-8c3ad4c4acc3212ba5d1ff0a3aada3e714dad071.tar.gz compilertests-8c3ad4c4acc3212ba5d1ff0a3aada3e714dad071.tar.bz2 |
some refactoring and started array types
-rw-r--r-- | ecomp-c/ec.c | 76 | ||||
-rw-r--r-- | ecomp-c/minie.ebnf | 3 | ||||
-rw-r--r-- | ecomp-c/test1.e | 1 |
3 files changed, 62 insertions, 18 deletions
diff --git a/ecomp-c/ec.c b/ecomp-c/ec.c index 65c5fad..784671f 100644 --- a/ecomp-c/ec.c +++ b/ecomp-c/ec.c @@ -27,6 +27,8 @@ typedef enum { S_module, S_begin, S_end, + S_array, + S_of, S_var, S_const, S_if, @@ -63,6 +65,8 @@ static char *symname[S_eof+1] = { "module", "begin", "end", + "array", + "of", "var", "const", "if", @@ -294,6 +298,8 @@ static S_Symbol getSym( void ) identifier( ); if( strcmp( ident, "and" ) == 0 ) { s = S_and; + } else if( strcmp( ident, "array" ) == 0 ) { + s = S_array; } else { s = S_ident; } @@ -364,6 +370,8 @@ static S_Symbol getSym( void ) identifier( ); if( strcmp( ident, "or" ) == 0 ) { s = S_or; + } else if( strcmp( ident, "of" ) == 0 ) { + s = S_of; } else { s = S_ident; } @@ -1370,6 +1378,54 @@ static void symbol_copy_node_value( ExpressionNode *from, Symbol *to ) } } +static Symbol *parseSimpleType( void ) +{ + Symbol *type = get_symbol( current_scope, ident ); + + if( type == NULL ) { + Abort( "Unknown type '%s'", ident ); + } + if( type->class != SYMBOL_CLASS_TYPE ) { + Abort( "'%s' is defined, but is not a type as expected", ident ); + } + + sym = getSym( ); + + return type; +} + +static Symbol *parseType( void ); + +static Symbol *parseArrayType( void ) +{ + Symbol *type = NULL; + + Expect( S_array ); + + if( sym == S_number ) { + sym = getSym( ); + } + + Expect( S_of ); + + parseType( ); + + return type; +} + +static Symbol *parseType( void ) +{ + Symbol *type; + + if( sym == S_array ) { + type = parseArrayType( ); + } else { + type = parseSimpleType( ); + } + + return type; +} + static void parseConstDeclaration( void ) { int nof_constants = 0; @@ -1393,15 +1449,8 @@ static void parseConstDeclaration( void ) Expect( S_colon ); - type = get_symbol( current_scope, ident ); - if( type == NULL ) { - Abort( "Unknown type '%s'", ident ); - } - if( type->class != SYMBOL_CLASS_TYPE ) { - Abort( "'%s' is defined, but is not a type as expected", ident ); - } - sym = getSym( ); - + type = parseType( ); + Expect( S_equals ); node = parseConstExpression( ); @@ -1467,15 +1516,8 @@ static void parseVariableDeclaration( void ) } while( sym == S_comma ); Expect( S_colon ); - type = get_symbol( current_scope, ident ); - if( type == NULL ) { - Abort( "Unknown type '%s'", ident ); - } - if( type->class != SYMBOL_CLASS_TYPE ) { - Abort( "'%s' is defined, but is not a type as expected", ident ); - } - sym = getSym( ); + type = parseType( ); if( sym == S_assign ) { sym = getSym( ); diff --git a/ecomp-c/minie.ebnf b/ecomp-c/minie.ebnf index a0558b6..1392c89 100644 --- a/ecomp-c/minie.ebnf +++ b/ecomp-c/minie.ebnf @@ -18,7 +18,8 @@ WhileStatement = "while" Expression "do" StatementSequence "end" . StatementSequence = Statement { ";" Statement } . StatementBlock = "begin" StatementList "end" . SimpleType = Identifier . -Type = SimpleType . +ArrayType = "array" [ Number ] "of" Type . +Type = SimpleType | ArrayType . ConstExpression = ( Number | Identifier ) . ConstDeclaration = Identifier { "," Identifier } ":" Type "=" ConstExpression . ConstBlock = "const" { ConstDeclaration ";" } . diff --git a/ecomp-c/test1.e b/ecomp-c/test1.e index 5648c9f..3ad3bac 100644 --- a/ecomp-c/test1.e +++ b/ecomp-c/test1.e @@ -24,6 +24,7 @@ var flag : boolean; i : integer; j : character := C; + s : array 100 of character; begin a := 1; |