diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2020-06-05 20:25:24 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2020-06-05 20:25:24 +0200 |
commit | 5d780e12179ac5f951e166413058a76442fe84d3 (patch) | |
tree | b1458467061984a2d1a7a56f7798519758a99f7e | |
parent | e2808b15950df51127726bc4f75d765fcd676d9b (diff) | |
download | compilertests-5d780e12179ac5f951e166413058a76442fe84d3.tar.gz compilertests-5d780e12179ac5f951e166413058a76442fe84d3.tar.bz2 |
handling strings now better, keep defined dimension of character array
also to better comment printing with strings
-rw-r--r-- | ecomp-c/ec.c | 34 | ||||
-rw-r--r-- | ecomp-c/tests/string_variable.e | 8 | ||||
-rw-r--r-- | ecomp-c/tests/string_variable.easm | 14 | ||||
-rw-r--r-- | ecomp-c/tests/string_variable.erun | 44 |
4 files changed, 59 insertions, 41 deletions
diff --git a/ecomp-c/ec.c b/ecomp-c/ec.c index 5612bb5..001fb28 100644 --- a/ecomp-c/ec.c +++ b/ecomp-c/ec.c @@ -1430,8 +1430,13 @@ static void generate_symbol_comment( char *mode, Symbol *constant ) } else if( constant->type == character_type ) { Emit( "'%c'", constant->character_value ); } else if( constant->type->class == SYMBOL_CLASS_ARRAY_TYPE ) { - /* TODO: iterate all elements of basic type and issue value */ - Emit( "array %d of %s = { ... }", constant->type->dim, constant->type->type->name ); + if( constant->type->type == character_type ) { + /* TODO: handl non-printables */ + Emit( "\"%s\"", constant->string_value ); + } else { + /* TODO: iterate all elements of basic type and issue value */ + Emit( "array %d of %s = { ... }", constant->type->dim, constant->type->type->name ); + } } else { Abort( "Unhandled symbol (%s) comment case for type '%s'", mode, constant->type->name ); } @@ -1450,22 +1455,23 @@ static void generate_variable_comment( Symbol *variable ) static void symbol_copy( Symbol *from, Symbol *to ) { - /* TODO: type conversion */ - to->type = from->type; - - if( !is_compatible_type( from->type, to->type ) ) { + if( !is_compatible_type( to->type, from->type ) ) { Abort( "type mismatch when copying symbol value (from: '%s', to: '%s')", from->type->name, to->type->name ); } if( from->type == integer_type ) { + to->type = integer_type; to->integer_value = from->integer_value; } else if( from->type == boolean_type ) { + to->type = boolean_type; to->boolean_value = from->boolean_value; } else if( from->type == character_type ) { + to->type = character_type; to->character_value = from->character_value; } else if( from->type->class == SYMBOL_CLASS_ARRAY_TYPE ) { if( from->type->type == character_type ) { + /* type remains, initializer is smaller than dimensioned string */ to->string_value = AllocateAndCopyStr( from->string_value ); } else { Abort( "Unhandled case for array type '%s' when copying value of symbol", from->type->name ); @@ -1477,22 +1483,23 @@ static void symbol_copy( Symbol *from, Symbol *to ) static void symbol_copy_node( ExpressionNode *from, Symbol *to ) { - /* TODO: type conversion */ - to->type = from->actual_type; - - if( from->actual_type != to->type ) { + if( !is_compatible_type( to->type, from->actual_type ) ) { Abort( "type mismatch when copying node to symbol value (from: '%s', to: '%s')", from->actual_type->name, to->type->name ); } if( from->actual_type == integer_type ) { + to->type = integer_type; to->integer_value = from->integer_value; } else if( from->actual_type == boolean_type ) { + to->type = boolean_type; to->boolean_value = from->boolean_value; } else if( from->actual_type == character_type ) { + to->type = character_type; to->character_value = from->character_value; } else if( from->actual_type->class == SYMBOL_CLASS_ARRAY_TYPE ) { if( from->actual_type->type == character_type ) { + /* type remains, initializer is smaller than dimensioned string */ to->string_value = AllocateAndCopyStr( from->string_value ); } else { Abort( "Unhandled case for array type '%s' when copying value of symbol from expression node", from->actual_type->name ); @@ -1606,6 +1613,7 @@ static void parseConstDeclaration( Scope *current_scope ) } for( i = 0; i < nof_constants; i++ ) { + constant[i]->type = type; symbol_copy_from_node( node, constant[i] ); } @@ -1666,6 +1674,7 @@ static void parseVariableDeclaration( Scope *current_scope ) } for( i = 0; i < nof_variables; i++ ) { + variable[i]->type = type; symbol_copy_from_node( node, variable[i] ); } @@ -1678,8 +1687,9 @@ static void parseVariableDeclaration( Scope *current_scope ) node->integer_value = 0; node->boolean_value = 0; /* false */ node->character_value = 0; /* NUL */ - node->string_value = NULL; + node->string_value = AllocateAndCopyStr( "" ); node->actual_type = type; + variable[i]->type = type; symbol_copy_from_node( node, variable[i] ); free_expression_node( node ); } @@ -1799,7 +1809,7 @@ static void reserve_initialize( Symbol *symbol ) if( symbol->type->type == character_type ) { i = 0; Emit( "db " ); - if( symbol->string_value != NULL ) { + if( symbol->string_value != NULL && strcmp( symbol->string_value, "" ) != 0 ) { Emit( "\"" ); len = strlen( symbol->string_value ); while( i < len ) { diff --git a/ecomp-c/tests/string_variable.e b/ecomp-c/tests/string_variable.e index 5b92d6a..707b116 100644 --- a/ecomp-c/tests/string_variable.e +++ b/ecomp-c/tests/string_variable.e @@ -5,12 +5,12 @@ module string_variable; const - S : array 12 of character = "hello world"; + S : array 14 of character = "hello world"; var - s1 : array 12 of character := S; - s2 : array 18 of character := "hello again world"; - s3 : array 12 of character; + s1 : array 18 of character := S; + s2 : array 19 of character := "hello again world"; + s3 : array 20 of character; begin end diff --git a/ecomp-c/tests/string_variable.easm b/ecomp-c/tests/string_variable.easm index 0663ddc..6620177 100644 --- a/ecomp-c/tests/string_variable.easm +++ b/ecomp-c/tests/string_variable.easm @@ -1,11 +1,11 @@ format binary use32 org $1000000 -; CONST S -> array 12 of character, array 12 of character = { ... } -; DECL s1 -> array 12 of character, array 12 of character = { ... } -; DECL s2 -> array 18 of character, array 18 of character = { ... } -; DECL s3 -> array 12 of character, array 12 of character = { ... } +; CONST S -> array 14 of character, "hello world" +; DECL s1 -> array 18 of character, "hello world" +; DECL s2 -> array 19 of character, "hello again world" +; DECL s3 -> array 20 of character, "" hlt -s3: db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -s2: db "hello again world", 0 -s1: db "hello world", 0 +s3: db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +s2: db "hello again world", 0, 0 +s1: db "hello world", 0, 0, 0, 0, 0, 0, 0 diff --git a/ecomp-c/tests/string_variable.erun b/ecomp-c/tests/string_variable.erun index 61b323b..d383390 100644 --- a/ecomp-c/tests/string_variable.erun +++ b/ecomp-c/tests/string_variable.erun @@ -1,20 +1,24 @@ -Read 43 bytes of code and static data.. +Read 58 bytes of code and static data.. 1000000: F4 hlt data: 01000001: 00000000 01000005: 00000000 01000009: 00000000 -0100000D: 68656C6C -01000011: 6F206167 -01000015: 61696E20 -01000019: 776F726C -0100001D: 64006865 -01000021: 6C6C6F20 -01000025: 776F726C -01000029: 64000000 +0100000D: 00000000 +01000011: 00000000 +01000015: 68656C6C +01000019: 6F206167 +0100001D: 61696E20 +01000021: 776F726C +01000025: 64000068 +01000029: 656C6C6F +0100002D: 20776F72 +01000031: 6C640000 +01000035: 00000000 +01000039: 00000000 core start 1000000 data start 1000001 -data size 2a +data size 39 stack start 1800000 Single step execution: -- iteration 1 @@ -33,11 +37,15 @@ data: 01000001: 00000000 01000005: 00000000 01000009: 00000000 -0100000D: 68656C6C -01000011: 6F206167 -01000015: 61696E20 -01000019: 776F726C -0100001D: 64006865 -01000021: 6C6C6F20 -01000025: 776F726C -01000029: 64000000 +0100000D: 00000000 +01000011: 00000000 +01000015: 68656C6C +01000019: 6F206167 +0100001D: 61696E20 +01000021: 776F726C +01000025: 64000068 +01000029: 656C6C6F +0100002D: 20776F72 +01000031: 6C640000 +01000035: 00000000 +01000039: 00000000 |