Skip to content

Commit

Permalink
Refactor "splat" parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Nov 11, 2024
1 parent 746046c commit c46933a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
26 changes: 13 additions & 13 deletions src/compiler/parse_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,16 +525,7 @@ bool parse_arg_list(ParseContext *c, Expr ***result, TokenType param_end, bool v
ASSIGN_EXPR_OR_RET(expr, parse_vasplat(c), false);
goto DONE;
}
if (try_consume(c, TOKEN_ELLIPSIS))
{
expr = expr_new(EXPR_SPLAT, start_span);
ASSIGN_EXPR_OR_RET(expr->inner_expr, parse_expr(c), false);
RANGE_EXTEND_PREV(expr);
}
else
{
ASSIGN_EXPR_OR_RET(expr, parse_expr(c), false);
}
ASSIGN_EXPR_OR_RET(expr, parse_expr(c), false);
DONE:
vec_add(*result, expr);
if (!try_consume(c, TOKEN_COMMA))
Expand Down Expand Up @@ -659,10 +650,19 @@ Expr *parse_ct_expression_list(ParseContext *c, bool allow_decl)
* @param left must be null.
* @return Expr*
*/
static Expr *parse_type_identifier(ParseContext *context, Expr *left)
static Expr *parse_type_identifier(ParseContext *c, Expr *left)
{
ASSERT0(!left && "Unexpected left hand side");
return parse_type_expression_with_path(context, NULL);
return parse_type_expression_with_path(c, NULL);
}

static Expr *parse_splat(ParseContext *c, Expr *left)
{
ASSERT0(!left && "Unexpected left hand side");
Expr *expr = expr_new(EXPR_SPLAT, c->span);
advance_and_verify(c, TOKEN_ELLIPSIS);
ASSIGN_EXPR_OR_RET(expr->inner_expr, parse_expr(c), poisoned_expr);
return expr;
}

/**
Expand Down Expand Up @@ -2084,7 +2084,7 @@ ParseRule rules[TOKEN_EOF + 1] = {
[TOKEN_HASH_IDENT] = { parse_hash_ident, NULL, PREC_NONE },
[TOKEN_AT_IDENT] = { parse_identifier, NULL, PREC_NONE },
//[TOKEN_HASH_TYPE_IDENT] = { parse_type_identifier, NULL, PREC_NONE }

[TOKEN_ELLIPSIS] = { parse_splat, NULL, PREC_NONE },
[TOKEN_FN] = { parse_lambda, NULL, PREC_NONE },
[TOKEN_CT_CONCATFN] = {parse_ct_concat_append, NULL, PREC_NONE },
[TOKEN_CT_APPEND] = { parse_ct_concat_append, NULL, PREC_NONE },
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/sema_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -8674,7 +8674,6 @@ static inline bool sema_expr_analyse_ct_defined(SemaContext *context, Expr *expr
case EXPR_SLICE:
case EXPR_SLICE_ASSIGN:
case EXPR_SLICE_COPY:
case EXPR_SPLAT:
case EXPR_SWIZZLE:
case EXPR_SUBSCRIPT_ADDR:
case EXPR_SUBSCRIPT_ASSIGN:
Expand Down Expand Up @@ -8706,6 +8705,7 @@ static inline bool sema_expr_analyse_ct_defined(SemaContext *context, Expr *expr
case EXPR_TYPEID_INFO:
case EXPR_TYPECALL:
case EXPR_MEMBER_GET:
case EXPR_SPLAT:
if (!sema_analyse_expr(active_context, main_expr)) return false;
break;
}
Expand Down Expand Up @@ -9085,13 +9085,14 @@ static inline bool sema_analyse_expr_dispatch(SemaContext *context, Expr *expr,
case EXPR_NAMED_ARGUMENT:
case EXPR_NOP:
case EXPR_OPERATOR_CHARS:
case EXPR_SPLAT:
case EXPR_SWIZZLE:
case EXPR_TEST_HOOK:
case EXPR_TRY_UNWRAP:
case EXPR_TRY_UNWRAP_CHAIN:
case EXPR_TYPEID_INFO:
UNREACHABLE
case EXPR_SPLAT:
RETURN_SEMA_ERROR(expr, "Splat ('...') may only appear in initializers and calls.");
case EXPR_TYPECALL:
RETURN_SEMA_ERROR(expr, "Expected '()' after this.");
case EXPR_OTHER_CONTEXT:
Expand Down
6 changes: 2 additions & 4 deletions src/compiler/sema_initializers.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ static inline bool sema_expr_analyse_struct_plain_initializer(SemaContext *conte
{
if (vec_size(assigned->strukt.members) > 1 && vec_size(elements) > 1)
{
SEMA_ERROR(elements[0], "Bitstructs with @overlap must use designated initialization.");
return false;
RETURN_SEMA_ERROR(elements[0], "Bitstructs with @overlap must use designated initialization.");
}
}

Expand All @@ -229,8 +228,7 @@ static inline bool sema_expr_analyse_struct_plain_initializer(SemaContext *conte
if (i >= elements_needed)
{
ASSERT0(i < size);
SEMA_ERROR(elements[i], "Too many elements in initializer, expected only %d.", elements_needed);
return false;
RETURN_SEMA_ERROR(elements[i], "Too many elements in initializer, expected only %d.", elements_needed);
}
// 5. We might have anonymous members
Decl *member = members[i];
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/sema_stmts.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ static inline bool sema_expr_valid_try_expression(Expr *expr)
case EXPR_LAST_FAULT:
case EXPR_TYPECALL:
case EXPR_MEMBER_GET:
case EXPR_SPLAT:
return false;
case EXPR_BITACCESS:
case EXPR_BUILTIN:
Expand Down Expand Up @@ -698,7 +699,6 @@ static inline bool sema_expr_valid_try_expression(Expr *expr)
case EXPR_SLICE:
case EXPR_SLICE_ASSIGN:
case EXPR_SLICE_COPY:
case EXPR_SPLAT:
case EXPR_STRINGIFY:
case EXPR_SUBSCRIPT:
case EXPR_SWIZZLE:
Expand Down

0 comments on commit c46933a

Please sign in to comment.