lang: ast: implement caret (return) parsing
This commit is contained in:
@@ -813,10 +813,22 @@ struct token_parse_result arith_parse_semicolon(
|
||||
}
|
||||
|
||||
enum ivy_status status = begin_cascade_operation(ctx);
|
||||
|
||||
|
||||
return PARSE_RESULT(status, 0);
|
||||
}
|
||||
|
||||
static struct ivy_ast_node *create_return_node(struct ivy_ast_node *val)
|
||||
{
|
||||
struct ivy_ast_return_node *ret
|
||||
= (struct ivy_ast_return_node *)ast_node_create(IVY_AST_RETURN);
|
||||
if (!ret) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret->n_val = val;
|
||||
return (struct ivy_ast_node *)ret;
|
||||
}
|
||||
|
||||
struct token_parse_result expr_finalise(
|
||||
struct ivy_parser *ctx, struct expr_parser_state *state,
|
||||
enum ivy_operator_precedence min_precedence, struct ivy_ast_node **result)
|
||||
@@ -840,6 +852,10 @@ struct token_parse_result expr_finalise(
|
||||
return PARSE_RESULT(status, 0);
|
||||
}
|
||||
|
||||
if (state->s_return && expr) {
|
||||
expr = create_return_node(expr);
|
||||
}
|
||||
|
||||
*result = expr;
|
||||
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
@@ -849,6 +865,11 @@ struct token_parse_result expr_finalise(
|
||||
struct ivy_ast_cascade_node *cascade
|
||||
= expr_finalise_cascade(state);
|
||||
|
||||
if (state->s_return && cascade) {
|
||||
cascade = (struct ivy_ast_cascade_node *)create_return_node(
|
||||
(struct ivy_ast_node *)cascade);
|
||||
}
|
||||
|
||||
*result = (struct ivy_ast_node *)cascade;
|
||||
return PARSE_RESULT(IVY_OK, flags);
|
||||
}
|
||||
@@ -856,6 +877,12 @@ struct token_parse_result expr_finalise(
|
||||
if (state->s_sub_type == EXPR_SUBTYPE_COMPLEX_MSG) {
|
||||
/* this is the end of a complex message */
|
||||
struct ivy_ast_msg_node *msg = expr_finalise_complex_msg(state);
|
||||
|
||||
if (state->s_return && msg) {
|
||||
msg = (struct ivy_ast_msg_node *)create_return_node(
|
||||
(struct ivy_ast_node *)msg);
|
||||
}
|
||||
|
||||
*result = (struct ivy_ast_node *)msg;
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
@@ -863,6 +890,12 @@ struct token_parse_result expr_finalise(
|
||||
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);
|
||||
|
||||
if (state->s_return && msg) {
|
||||
msg = (struct ivy_ast_msg_node *)create_return_node(
|
||||
(struct ivy_ast_node *)msg);
|
||||
}
|
||||
|
||||
*result = (struct ivy_ast_node *)msg;
|
||||
return PARSE_RESULT(IVY_OK, flags);
|
||||
}
|
||||
@@ -874,6 +907,10 @@ struct token_parse_result expr_finalise(
|
||||
return PARSE_RESULT(status, 0);
|
||||
}
|
||||
|
||||
if (expr && state->s_return) {
|
||||
expr = create_return_node(expr);
|
||||
}
|
||||
|
||||
*result = expr;
|
||||
return PARSE_RESULT(IVY_OK, flags);
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ struct ast_node_type expr_node_ops = {
|
||||
SYM_PARSER(RIGHT_PAREN, arith_parse_right_paren),
|
||||
SYM_PARSER(SEMICOLON, arith_parse_semicolon),
|
||||
SYM_PARSER(UNDERSCORE, arith_parse_operand),
|
||||
SYM_PARSER(CARET, arith_parse_caret),
|
||||
SYM_PARSER(COMMA, arith_parse_comma),
|
||||
SYM_PARSER(DOT, arith_parse_dot),
|
||||
SYM_PARSER(EQUAL_RIGHT_ANGLE, arith_parse_equal_right_angle),
|
||||
|
||||
@@ -47,6 +47,9 @@ struct expr_parser_state {
|
||||
enum expr_type s_type;
|
||||
enum expr_subtype s_sub_type;
|
||||
|
||||
/* this is a return statement (prefixed with a caret) */
|
||||
bool s_return;
|
||||
|
||||
/* for arithmetic expressions, this records whether the previous
|
||||
* component (either a token or parenthesised group of tokens) is an
|
||||
* operator, operand, or message */
|
||||
@@ -117,6 +120,8 @@ 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_caret(
|
||||
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(
|
||||
|
||||
Reference in New Issue
Block a user