Skip to content

Commit

Permalink
Allow map reference variable to be initialized with a struct variable
Browse files Browse the repository at this point in the history
  • Loading branch information
positively-charged committed May 18, 2017
1 parent aece2c0 commit 48d58c1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
24 changes: 15 additions & 9 deletions src/codegen/chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,9 @@ static void do_mini( struct codegen* codegen ) {
c_add_int( codegen,
var->value->more.string.string->index_runtime );
break;
case VALUE_STRUCTREF:
c_add_int( codegen, var->value->more.structref.offset );
break;
case VALUE_FUNCREF:
{
struct func_user* impl = var->value->more.funcref.func->impl;
Expand Down Expand Up @@ -653,6 +656,8 @@ static int nonzero_value_end_index( struct codegen* codegen,
// Offset of the array initializer and offset of the dimension
// information.
return value->index + 2;
case VALUE_STRUCTREF:
return value->index + 1;
case VALUE_FUNCREF:
{
struct func_user* impl = value->more.funcref.func->impl;
Expand Down Expand Up @@ -718,18 +723,22 @@ static void write_value( struct codegen* codegen,
writing->count += string->length;
}
break;
case VALUE_ARRAYREF:
c_add_int( codegen, value->more.arrayref.offset );
c_add_int( codegen, value->more.arrayref.diminfo );
writing->count += 2;
break;
case VALUE_STRUCTREF:
c_add_int( codegen, value->more.structref.offset );
++writing->count;
break;
case VALUE_FUNCREF:
{
struct func_user* impl = value->more.funcref.func->impl;
c_add_int( codegen, impl->index );
++writing->count;
}
break;
case VALUE_ARRAYREF:
c_add_int( codegen, value->more.arrayref.offset );
c_add_int( codegen, value->more.arrayref.diminfo );
writing->count += 2;
break;
default:
UNREACHABLE();
c_bail( codegen );
Expand Down Expand Up @@ -1120,6 +1129,7 @@ static void write_atag_chunk( struct codegen* codegen,
case VALUE_EXPR:
case VALUE_STRINGINITZ:
case VALUE_ARRAYREF:
case VALUE_STRUCTREF:
break;
default:
UNREACHABLE();
Expand Down Expand Up @@ -1165,10 +1175,6 @@ static void write_atag_chunk( struct codegen* codegen,
c_add_byte( codegen, TAG_FUNCTION );
++written;
break;
case VALUE_EXPR:
case VALUE_STRINGINITZ:
case VALUE_ARRAYREF:
break;
default:
UNREACHABLE();
c_bail( codegen );
Expand Down
8 changes: 7 additions & 1 deletion src/codegen/phase.c
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,10 @@ static void patch_value( struct codegen* codegen, struct value* value ) {
value->more.arrayref.var->index + value->expr->value;
value->more.arrayref.diminfo = value->more.arrayref.var->diminfo_start;
break;
case VALUE_STRUCTREF:
value->more.structref.offset = value->more.structref.var->index +
value->expr->value;
break;
case VALUE_STRING:
c_append_string( codegen, value->more.string.string );
break;
Expand Down Expand Up @@ -590,7 +594,7 @@ bool c_is_scalar_var( struct var* var ) {

static bool is_initz_zero( struct value* value ) {
if ( value ) {
STATIC_ASSERT( VALUE_TOTAL == 5 );
STATIC_ASSERT( VALUE_TOTAL == 6 );
switch ( value->type ) {
case VALUE_EXPR:
return ( value->expr->value == 0 );
Expand All @@ -602,6 +606,8 @@ static bool is_initz_zero( struct value* value ) {
return ( impl->index == 0 );
}
break;
case VALUE_STRUCTREF:
return false;
default:
break;
}
Expand Down
5 changes: 4 additions & 1 deletion src/semantic/dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1387,11 +1387,14 @@ static bool test_scalar_initz( struct semantic* semantic,
value->more.arrayref.var = expr.var;
value->type = VALUE_ARRAYREF;
break;
case TYPEDESC_STRUCTREF:
value->more.structref.var = expr.var;
value->type = VALUE_STRUCTREF;
break;
case TYPEDESC_FUNCREF:
value->more.funcref.func = expr.func;
value->type = VALUE_FUNCREF;
break;
case TYPEDESC_STRUCTREF:
case TYPEDESC_NULLREF:
break;
case TYPEDESC_PRIMITIVE:
Expand Down
7 changes: 6 additions & 1 deletion src/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -710,13 +710,18 @@ struct value {
int offset;
int diminfo;
} arrayref;
struct {
struct var* var;
int offset;
} structref;
} more;
enum {
VALUE_EXPR,
VALUE_STRING,
VALUE_STRINGINITZ,
VALUE_FUNCREF,
VALUE_ARRAYREF,
VALUE_STRUCTREF,
VALUE_FUNCREF,
VALUE_TOTAL,
} type;
int index;
Expand Down

0 comments on commit 48d58c1

Please sign in to comment.