lang: ast: implement parsing of match statements
This commit is contained in:
@@ -50,7 +50,7 @@ static void print_operand(struct ivy_ast_node *node)
|
||||
}
|
||||
}
|
||||
|
||||
void arith_push_operand(struct expr_parser_state *state, struct ivy_token *tok)
|
||||
enum ivy_status arith_push_operand(struct expr_parser_state *state, struct ivy_token *tok)
|
||||
{
|
||||
switch (tok->t_type) {
|
||||
case IVY_TOK_IDENT: {
|
||||
@@ -84,9 +84,20 @@ void arith_push_operand(struct expr_parser_state *state, struct ivy_token *tok)
|
||||
b_queue_push_back(&state->s_output_queue, &v->n_base.n_entry);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
case IVY_TOK_SYMBOL: {
|
||||
if (tok->t_symbol != IVY_SYM_UNDERSCORE) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
struct ivy_ast_node *v = ast_node_create(IVY_AST_DISCARD);
|
||||
b_queue_push_back(&state->s_output_queue, &v->n_entry);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static const struct ivy_operator *get_operator_from_token(struct ivy_token *tok)
|
||||
@@ -448,6 +459,20 @@ struct token_parse_result arith_parse_operator(
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
struct token_parse_result arith_parse_in(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct expr_parser_state *state
|
||||
= parser_get_state(ctx, struct expr_parser_state);
|
||||
|
||||
if (state->s_terminator == IVY_KW_IN) {
|
||||
state->s_prev_token = IVY_KW_IN;
|
||||
return expr_finalise_and_return(ctx, state);
|
||||
}
|
||||
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
struct token_parse_result arith_parse_ident(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
@@ -790,21 +815,21 @@ struct token_parse_result expr_finalise(
|
||||
struct ivy_ast_cascade_node *cascade
|
||||
= expr_finalise_cascade(state);
|
||||
|
||||
*result = cascade;
|
||||
*result = (struct ivy_ast_node *)cascade;
|
||||
return PARSE_RESULT(IVY_OK, flags);
|
||||
}
|
||||
|
||||
if (state->s_sub_type == EXPR_SUBTYPE_COMPLEX_MSG) {
|
||||
/* this is the end of a keyword-message */
|
||||
struct ivy_ast_msg_node *msg = expr_finalise_complex_msg(state);
|
||||
*result = msg;
|
||||
*result = (struct ivy_ast_node *)msg;
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
if (state->s_sub_type == EXPR_SUBTYPE_KEYWORD_MSG) {
|
||||
/* this is the end of a keyword-message */
|
||||
struct ivy_ast_msg_node *msg = expr_finalise_keyword_msg(state);
|
||||
*result = msg;
|
||||
*result = (struct ivy_ast_node *)msg;
|
||||
return PARSE_RESULT(IVY_OK, flags);
|
||||
}
|
||||
|
||||
@@ -854,6 +879,38 @@ struct token_parse_result arith_parse_dot(
|
||||
return expr_finalise_and_return(ctx, state);
|
||||
}
|
||||
|
||||
struct token_parse_result arith_parse_comma(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct expr_parser_state *state
|
||||
= parser_get_state(ctx, struct expr_parser_state);
|
||||
|
||||
if (state->s_type != EXPR_TYPE_ARITH) {
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
state->s_prev_token = IVY_SYM_DOT;
|
||||
return expr_finalise_and_return(ctx, state);
|
||||
}
|
||||
|
||||
extern struct token_parse_result arith_parse_equal_right_angle(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct expr_parser_state *state
|
||||
= parser_get_state(ctx, struct expr_parser_state);
|
||||
|
||||
state->s_prev_token = IVY_SYM_EQUAL_RIGHT_ANGLE;
|
||||
struct token_parse_result result = expr_finalise_and_return(ctx, state);
|
||||
|
||||
result.r_flags |= PARSE_REPEAT_TOKEN;
|
||||
return result;
|
||||
}
|
||||
|
||||
struct token_parse_result arith_parse_label(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
|
||||
@@ -32,12 +32,20 @@ struct ast_node_type expr_node_ops = {
|
||||
SYM_PARSER(LEFT_PAREN, arith_parse_left_paren),
|
||||
SYM_PARSER(RIGHT_PAREN, arith_parse_right_paren),
|
||||
SYM_PARSER(SEMICOLON, arith_parse_semicolon),
|
||||
SYM_PARSER(UNDERSCORE, arith_parse_operand),
|
||||
SYM_PARSER(COMMA, arith_parse_comma),
|
||||
SYM_PARSER(DOT, arith_parse_dot),
|
||||
SYM_PARSER(EQUAL_RIGHT_ANGLE, arith_parse_equal_right_angle),
|
||||
},
|
||||
.n_keyword_parsers = {
|
||||
/* statement keywords */
|
||||
KW_PARSER(MATCH, stmt_parse_match),
|
||||
KW_PARSER(IF, stmt_parse_if),
|
||||
KW_PARSER(THEN, stmt_parse_then),
|
||||
KW_PARSER(ELSE, stmt_parse_else),
|
||||
KW_PARSER(END, stmt_parse_end),
|
||||
|
||||
/* operator keywords */
|
||||
KW_PARSER(IN, arith_parse_in),
|
||||
}
|
||||
};
|
||||
|
||||
@@ -95,7 +95,7 @@ extern struct token_parse_result expr_finalise_and_return(
|
||||
|
||||
extern void arith_push_operator(
|
||||
struct expr_parser_state *state, struct ivy_ast_node *node);
|
||||
extern void arith_push_operand(
|
||||
extern enum ivy_status arith_push_operand(
|
||||
struct expr_parser_state *state, struct ivy_token *tok);
|
||||
|
||||
extern enum ivy_status arith_add_child(
|
||||
@@ -117,9 +117,17 @@ extern struct token_parse_result arith_parse_semicolon(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
extern struct token_parse_result arith_parse_dot(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
extern struct token_parse_result arith_parse_comma(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
extern struct token_parse_result arith_parse_equal_right_angle(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
extern struct token_parse_result arith_parse_in(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
|
||||
/* statement parser callbacks */
|
||||
|
||||
extern struct token_parse_result stmt_parse_match(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
extern struct token_parse_result stmt_parse_if(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
extern struct token_parse_result stmt_parse_then(
|
||||
|
||||
@@ -6,6 +6,39 @@
|
||||
#include <ivy/lang/operator.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct token_parse_result stmt_parse_match(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct expr_parser_state *state
|
||||
= parser_get_state(ctx, struct expr_parser_state);
|
||||
|
||||
if (state->s_prev_component == EXPR_CMP_OPERAND || state->s_prev_component == EXPR_CMP_MSG) {
|
||||
/* match statements are operands. */
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
struct ivy_ast_node *expr = NULL;
|
||||
struct token_parse_result result
|
||||
= expr_finalise(ctx, state, IVY_PRECEDENCE_IF_ELSE, &expr);
|
||||
if (result.r_status != IVY_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
state->s_prev_token = IVY_KW_MATCH;
|
||||
|
||||
if (b_queue_empty(&state->s_operator_stack) && b_queue_empty(&state->s_output_queue)) {
|
||||
parser_pop_state(ctx, 0);
|
||||
}
|
||||
|
||||
/* if expr is NULL, this is an if-then-else-end statement,
|
||||
* otherwise, this is an expr-if-else-expr. */
|
||||
struct cond_group_parser_state *cond
|
||||
= (struct cond_group_parser_state *)parser_push_state(
|
||||
ctx, IVY_AST_MATCH, 0);
|
||||
|
||||
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
|
||||
struct token_parse_result stmt_parse_if(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user