summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2020-06-05 19:37:22 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2020-06-05 19:37:22 +0200
commite2808b15950df51127726bc4f75d765fcd676d9b (patch)
treee61929ad1b14cf9699e3d80ab93d3688e0c05c54
parent8e3a8018f52834414cbd1184664554e4ddad04cb (diff)
downloadcompilertests-e2808b15950df51127726bc4f75d765fcd676d9b.tar.gz
compilertests-e2808b15950df51127726bc4f75d765fcd676d9b.tar.bz2
done some refactoring in ec when assigning initial values from literals
-rw-r--r--ecomp-c/ec.c83
1 files changed, 46 insertions, 37 deletions
diff --git a/ecomp-c/ec.c b/ecomp-c/ec.c
index 4da751d..5612bb5 100644
--- a/ecomp-c/ec.c
+++ b/ecomp-c/ec.c
@@ -147,7 +147,13 @@ static void *Allocate( unsigned int size )
static char *AllocateAndCopyStr( char *s )
{
char *d;
- int len = strlen( s );
+ int len;
+
+ if( s == NULL ) {
+ return NULL;
+ }
+
+ len = strlen( s );
if( len > MAX_STRING_LEN ) {
Abort( "Too long string literal, should not happen" );
}
@@ -630,8 +636,8 @@ typedef struct Symbol {
struct Symbol *type;
/* constant */
int integer_value;
- char boolean_value;
- int character_value;
+ int boolean_value;
+ char character_value;
char *string_value;
/* variable */
int initialized;
@@ -773,7 +779,7 @@ typedef struct ExpressionNode {
struct ExpressionNode *left, *right;
int integer_value;
int boolean_value;
- int character_value;
+ char character_value;
char *string_value;
Symbol *symbol;
Symbol *actual_type;
@@ -1442,8 +1448,11 @@ static void generate_variable_comment( Symbol *variable )
generate_symbol_comment( "DECL", variable );
}
-static void symbol_copy_value( Symbol *from, Symbol *to )
+static void symbol_copy( Symbol *from, Symbol *to )
{
+ /* TODO: type conversion */
+ to->type = from->type;
+
if( !is_compatible_type( from->type, to->type ) ) {
Abort( "type mismatch when copying symbol value (from: '%s', to: '%s')",
from->type->name, to->type->name );
@@ -1466,8 +1475,11 @@ static void symbol_copy_value( Symbol *from, Symbol *to )
}
}
-static void symbol_copy_node_value( ExpressionNode *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 ) {
Abort( "type mismatch when copying node to symbol value (from: '%s', to: '%s')",
from->actual_type->name, to->type->name );
@@ -1490,6 +1502,17 @@ static void symbol_copy_node_value( ExpressionNode *from, Symbol *to )
}
}
+static void symbol_copy_from_node( ExpressionNode *from, Symbol *to )
+{
+ if( from->type == EXPRESSION_NODE_TYPE_CONST ) {
+ symbol_copy_node( from, to );
+ } else if( from->type == EXPRESSION_NODE_TYPE_VAR ) {
+ symbol_copy( from->symbol, to );
+ } else {
+ Abort( "Unknown symbol copy from expression node" );
+ }
+}
+
static Symbol *parseSimpleType( Scope *current_scope )
{
Symbol *type = get_symbol( current_scope, ident );
@@ -1581,21 +1604,11 @@ static void parseConstDeclaration( Scope *current_scope )
Abort( "Assigning a constant of type '%s' from an expression of type '%s'",
type->name, node->actual_type->name );
}
-
- if( node->type == EXPRESSION_NODE_TYPE_CONST ) {
- for( i = 0; i < nof_constants; i++ ) {
- /* TODO: type copying and type conversion */
- constant[i]->type = node->actual_type;
- symbol_copy_node_value( node, constant[i] );
- }
- } else if( node->type == EXPRESSION_NODE_TYPE_VAR ) {
- for( i = 0; i < nof_constants; i++ ) {
- /* TODO: type copying and type conversion */
- constant[i]->type = node->symbol->type;
- symbol_copy_value( node->symbol, constant[i] );
- }
+
+ for( i = 0; i < nof_constants; i++ ) {
+ symbol_copy_from_node( node, constant[i] );
}
-
+
for( i = 0; i < nof_constants; i++ ) {
generate_constant_comment( constant[i] );
}
@@ -1652,27 +1665,23 @@ static void parseVariableDeclaration( Scope *current_scope )
type->name, node->actual_type->name );
}
- if( node->type == EXPRESSION_NODE_TYPE_CONST ) {
- for( i = 0; i < nof_variables; i++ ) {
- /* TODO: type copying and type conversion */
- variable[i]->type = node->actual_type;
- symbol_copy_node_value( node, variable[i] );
- }
- } else if( node->type == EXPRESSION_NODE_TYPE_VAR ) {
- for( i = 0; i < nof_variables; i++ ) {
- /* TODO: type copying and type conversion */
- variable[i]->type = node->symbol->type;
- symbol_copy_value( node->symbol, variable[i] );
- }
+ for( i = 0; i < nof_variables; i++ ) {
+ symbol_copy_from_node( node, variable[i] );
}
+
free_expression_node( node );
} else {
for( i = 0; i < nof_variables; i++ ) {
- variable[i]->integer_value = 0;
- variable[i]->boolean_value = 0; /* false */
- variable[i]->character_value = 0; /* NUL */
- /* TODO: type copying and type conversion */
- variable[i]->type = type;
+ ExpressionNode *node;
+ node = create_expression_node( );
+ node->type = EXPRESSION_NODE_TYPE_CONST;
+ node->integer_value = 0;
+ node->boolean_value = 0; /* false */
+ node->character_value = 0; /* NUL */
+ node->string_value = NULL;
+ node->actual_type = type;
+ symbol_copy_from_node( node, variable[i] );
+ free_expression_node( node );
}
}