lang: ast: implement parsing of tuples

This commit is contained in:
2024-12-08 12:28:47 +00:00
parent c6f1439835
commit 753afd7d87
4 changed files with 166 additions and 5 deletions

View File

@@ -619,8 +619,10 @@ struct token_parse_result arith_parse_left_paren(
struct expr_parser_state *sub_expr
= (struct expr_parser_state *)parser_push_state(
ctx, IVY_AST_EXPR, 0);
expr_copy_terminators(state, sub_expr);
sub_expr->s_paren_depth = state->s_paren_depth + 1;
sub_expr->s_subexpr_depth = state->s_subexpr_depth + 1;
sub_expr->s_sub_type = EXPR_SUBTYPE_PAREN;
return PARSE_RESULT(IVY_OK, 0);
}
@@ -631,6 +633,13 @@ struct token_parse_result arith_parse_right_paren(
struct expr_parser_state *state
= parser_get_state(ctx, struct expr_parser_state);
if (expr_terminates_at_token(state, IVY_SYM_RIGHT_PAREN)) {
struct token_parse_result result
= expr_finalise_and_return(ctx, state);
result.r_flags = PARSE_REPEAT_TOKEN;
return result;
}
if (state->s_type != EXPR_TYPE_ARITH) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
@@ -1146,11 +1155,31 @@ struct token_parse_result arith_parse_comma(
if (state->s_paren_depth > 0
&& (state->s_prev_component == EXPR_CMP_OPERAND
|| state->s_prev_component == EXPR_CMP_MSG)) {
/* tuple. */
return PARSE_RESULT(IVY_ERR_NOT_SUPPORTED, 0);
struct ivy_ast_node *item = NULL;
struct token_parse_result result
= expr_finalise(ctx, state, IVY_PRECEDENCE_ASSIGN, &item);
if (result.r_status != IVY_OK) {
return result;
}
if (!item) {
/* empty tuple member expression. */
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
if (state->s_sub_type == EXPR_SUBTYPE_PAREN && b_queue_empty(&state->s_output_queue) && b_queue_empty(&state->s_operator_stack)) {
parser_pop_state(ctx, 0);
} else {
/* the tuple parser will handle the parentheses for us. */
state->s_paren_depth--;
}
parser_push_state(ctx, IVY_AST_TUPLE, (uintptr_t)item);
return PARSE_RESULT(IVY_OK, 0);
}
state->s_prev_token = IVY_SYM_DOT;
state->s_prev_token = IVY_SYM_COMMA;
return expr_finalise_and_return(ctx, state);
}
@@ -1289,4 +1318,4 @@ enum ivy_status arith_add_child(
}
return IVY_OK;
}
}