lang: add match-expression parser/generator

This commit is contained in:
2025-11-04 10:35:52 +00:00
parent b444a565a2
commit 58fa746aa1
4 changed files with 141 additions and 0 deletions

View File

@@ -132,12 +132,15 @@ static struct token_parse_result parse_arrow(
state->s_prev_token = IVY_SYM_EQUAL_RIGHT_ANGLE;
/* the next component is the branch body */
#if 0
struct expr_parser_state *expr
= (struct expr_parser_state *)parser_push_state(
ctx, IVY_AST_EXPR, 0);
expr_add_terminator(expr, IVY_KW_END);
expr_add_terminator(expr, IVY_SYM_COMMA);
expr->s_subexpr_depth = 1;
#endif
return PARSE_RESULT(IVY_OK, 0);
}
@@ -199,6 +202,46 @@ static struct token_parse_result parse_end(
return PARSE_RESULT(IVY_OK, 0);
}
static struct token_parse_result parse_block(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct match_parser_state *state
= parser_get_state(ctx, struct match_parser_state);
if (state->s_prev_token != IVY_SYM_EQUAL_RIGHT_ANGLE) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
struct block_parser_state *block
= (struct block_parser_state *)parser_push_state(
ctx, IVY_AST_BLOCK, 0);
return PARSE_RESULT(IVY_OK, 0);
}
static struct token_parse_result parse_expr(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct match_parser_state *state
= parser_get_state(ctx, struct match_parser_state);
if (state->s_prev_token != IVY_SYM_EQUAL_RIGHT_ANGLE) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
struct expr_parser_state *expr
= (struct expr_parser_state *)parser_push_state(
ctx, IVY_AST_EXPR, 0);
if (tok->t_type != IVY_TOK_KEYWORD) {
expr_add_terminator(expr, IVY_KW_END);
}
expr_add_terminator(expr, IVY_SYM_COMMA);
expr->s_subexpr_depth = 1;
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
}
static enum ivy_status add_child(
struct parser_state *parent, struct ivy_ast_node *child)
{
@@ -233,10 +276,12 @@ struct ast_node_type match_node_ops = {
.n_collect_children = match_collect_children,
.n_state_size = sizeof(struct match_parser_state),
.n_node_size = sizeof(struct ivy_ast_match_node),
.n_expr_parser = parse_expr,
.n_keyword_parsers = {
KW_PARSER(MATCH, parse_match),
KW_PARSER(IN, parse_in),
KW_PARSER(END, parse_end),
KW_PARSER(DO, parse_block),
},
.n_symbol_parsers = {
SYM_PARSER(EQUAL_RIGHT_ANGLE, parse_arrow),