Skip to content

Commit

Permalink
Refactor: testing of member access
Browse files Browse the repository at this point in the history
  • Loading branch information
positively-charged committed May 5, 2017
1 parent a18662f commit be878ae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/semantic/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ static void test_subscript_str( struct semantic* semantic,
struct subscript* subscript );
static void test_access( struct semantic* semantic, struct expr_test* test,
struct result* result, struct access* access );
static bool is_ns( struct result* result );
static void test_access_struct( struct semantic* semantic,
struct expr_test* test, struct access* access, struct result* lside,
struct result* result );
Expand Down Expand Up @@ -1654,26 +1655,33 @@ static void test_access( struct semantic* semantic, struct expr_test* test,
struct result lside;
init_result( &lside );
test_suffix( semantic, test, &lside, access->lside );
if ( s_is_struct( &lside.type ) ) {
if ( s_is_struct_ref( &lside.type ) ) {
test_access_struct( semantic, test, access, &lside, result );
}
else if ( lside.object && lside.object->node.type == NODE_NAMESPACE ) {
else if ( is_ns( &lside ) ) {
test_access_ns( semantic, test, access, &lside, result );
}
else if ( s_is_array_ref( &lside.type ) ) {
test_access_array( semantic, test, access, &lside, result );
}
else if ( s_is_value_type( &lside.type ) &&
lside.type.spec == SPEC_STR ) {
else if ( s_is_str( &lside.type ) ) {
test_access_str( semantic, test, access, &lside, result );
}
else {
struct str string;
str_init( &string );
s_present_type( &lside.type, &string );
s_diag( semantic, DIAG_POS_ERR, &access->pos,
"left operand does not support member access" );
"left operand (`%s`) does not support member access", string.value );
str_deinit( &string );
s_bail( semantic );
}
}

static bool is_ns( struct result* result ) {
return ( result->object && result->object->node.type == NODE_NAMESPACE );
}

static void test_access_struct( struct semantic* semantic,
struct expr_test* test, struct access* access, struct result* lside,
struct result* result ) {
Expand Down
2 changes: 2 additions & 0 deletions src/semantic/phase.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,7 @@ bool s_common_type( struct type_info* a, struct type_info* b,
struct type_info* result );
bool s_is_void( struct type_info* type );
void s_reveal( struct type_info* type );
bool s_is_str( struct type_info* type );
bool s_is_struct_ref( struct type_info* type );

#endif
9 changes: 9 additions & 0 deletions src/semantic/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -706,3 +706,12 @@ void s_reveal( struct type_info* type ) {
break;
}
}

bool s_is_str( struct type_info* type ) {
return ( s_describe_type( type ) == TYPEDESC_PRIMITIVE &&
type->spec == SPEC_STR );
}

bool s_is_struct_ref( struct type_info* type ) {
return ( s_describe_type( type ) == TYPEDESC_STRUCTREF );
}

0 comments on commit be878ae

Please sign in to comment.