lang: ast: implement parsing of inline and standalone if-else statements
This commit is contained in:
@@ -230,7 +230,7 @@ static bool op_node_is_complete(struct ivy_ast_op_node *node)
|
||||
}
|
||||
}
|
||||
|
||||
static enum ivy_status finalise_expr(
|
||||
enum ivy_status expr_finalise_arith(
|
||||
struct expr_parser_state *state, struct ivy_ast_node **expr_tree,
|
||||
enum ivy_operator_precedence minimum_precedence)
|
||||
{
|
||||
@@ -505,7 +505,7 @@ struct token_parse_result arith_parse_left_paren(
|
||||
|
||||
struct expr_parser_state *msg_expr
|
||||
= (struct expr_parser_state *)parser_push_state(
|
||||
ctx, IVY_AST_EXPR);
|
||||
ctx, IVY_AST_EXPR, 0);
|
||||
|
||||
msg_expr->s_msg = (struct ivy_ast_msg_node *)msg;
|
||||
msg_expr->s_sub_type = EXPR_SUBTYPE_COMPLEX_MSG;
|
||||
@@ -518,7 +518,8 @@ struct token_parse_result arith_parse_left_paren(
|
||||
/* a sub-expression surrounded by parentheses is parsed by creating
|
||||
* a sub-expression parser state */
|
||||
struct expr_parser_state *sub_expr
|
||||
= (struct expr_parser_state *)parser_push_state(ctx, IVY_AST_EXPR);
|
||||
= (struct expr_parser_state *)parser_push_state(
|
||||
ctx, IVY_AST_EXPR, 0);
|
||||
sub_expr->s_paren_depth = state->s_paren_depth + 1;
|
||||
sub_expr->s_subexpr_depth = state->s_subexpr_depth + 1;
|
||||
|
||||
@@ -543,7 +544,7 @@ static struct ivy_ast_selector_node *keyword_selector_from_label_list(b_queue *l
|
||||
return sel;
|
||||
}
|
||||
|
||||
static struct ivy_ast_cascade_node *finalise_cascade(struct expr_parser_state *state)
|
||||
static struct ivy_ast_cascade_node *expr_finalise_cascade(struct expr_parser_state *state)
|
||||
{
|
||||
struct ivy_ast_cascade_node *cascade
|
||||
= (struct ivy_ast_cascade_node *)ast_node_create(IVY_AST_CASCADE);
|
||||
@@ -562,7 +563,7 @@ static struct ivy_ast_cascade_node *finalise_cascade(struct expr_parser_state *s
|
||||
return cascade;
|
||||
}
|
||||
|
||||
static struct ivy_ast_msg_node *finalise_keyword_msg(struct expr_parser_state *state)
|
||||
static struct ivy_ast_msg_node *expr_finalise_keyword_msg(struct expr_parser_state *state)
|
||||
{
|
||||
struct ivy_ast_msg_node *msg
|
||||
= (struct ivy_ast_msg_node *)ast_node_create(IVY_AST_MSG);
|
||||
@@ -584,7 +585,7 @@ static struct ivy_ast_msg_node *finalise_keyword_msg(struct expr_parser_state *s
|
||||
return msg;
|
||||
}
|
||||
|
||||
static struct ivy_ast_msg_node *finalise_complex_msg(struct expr_parser_state *state)
|
||||
static struct ivy_ast_msg_node *expr_finalise_complex_msg(struct expr_parser_state *state)
|
||||
{
|
||||
struct ivy_ast_msg_node *msg = state->s_msg;
|
||||
if (!msg) {
|
||||
@@ -628,47 +629,7 @@ struct token_parse_result arith_parse_right_paren(
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
if (state->s_sub_type == EXPR_SUBTYPE_KEYWORD_MSG) {
|
||||
/* this is the end of a keyword-message */
|
||||
struct ivy_ast_msg_node *msg = finalise_keyword_msg(state);
|
||||
parser_replace_current_node(ctx, (struct ivy_ast_node *)msg);
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
|
||||
if (state->s_sub_type == EXPR_SUBTYPE_COMPLEX_MSG) {
|
||||
/* this is the end of a complex message */
|
||||
struct ivy_ast_msg_node *msg = finalise_complex_msg(state);
|
||||
parser_replace_current_node(ctx, (struct ivy_ast_node *)msg);
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
if (state->s_sub_type == EXPR_SUBTYPE_CASCADE) {
|
||||
/* this is the end of a cascade operation */
|
||||
struct ivy_ast_cascade_node *cascade = finalise_cascade(state);
|
||||
parser_replace_current_node(ctx, (struct ivy_ast_node *)cascade);
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
|
||||
/* this is the end of a regular parentheses-surrounded sub-expression. */
|
||||
struct ivy_ast_node *expr = NULL;
|
||||
enum ivy_status status
|
||||
= finalise_expr(state, &expr, IVY_PRECEDENCE_ASSIGN);
|
||||
if (status != IVY_OK) {
|
||||
return PARSE_RESULT(status, 0);
|
||||
}
|
||||
|
||||
parser_replace_current_node(ctx, expr);
|
||||
|
||||
int flags = 0;
|
||||
if (state->s_paren_depth == 0 && state->s_sub_type != EXPR_SUBTYPE_NONE) {
|
||||
flags = PARSE_REPEAT_TOKEN;
|
||||
}
|
||||
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return PARSE_RESULT(IVY_OK, flags);
|
||||
return expr_finalise_and_return(ctx, state);
|
||||
}
|
||||
|
||||
static enum ivy_status begin_cascade_operation(struct ivy_parser *ctx)
|
||||
@@ -680,12 +641,12 @@ static enum ivy_status begin_cascade_operation(struct ivy_parser *ctx)
|
||||
struct ivy_ast_msg_node *first_msg = NULL;
|
||||
|
||||
if (state->s_sub_type == EXPR_SUBTYPE_KEYWORD_MSG) {
|
||||
first_msg = finalise_keyword_msg(state);
|
||||
first_msg = expr_finalise_keyword_msg(state);
|
||||
parser_pop_state(ctx, 0);
|
||||
} else {
|
||||
struct ivy_ast_node *expr = NULL;
|
||||
enum ivy_status status
|
||||
= finalise_expr(state, &expr, IVY_PRECEDENCE_CASCADE);
|
||||
= expr_finalise_arith(state, &expr, IVY_PRECEDENCE_CASCADE);
|
||||
if (status != IVY_OK) {
|
||||
return status;
|
||||
}
|
||||
@@ -698,7 +659,8 @@ static enum ivy_status begin_cascade_operation(struct ivy_parser *ctx)
|
||||
}
|
||||
|
||||
struct expr_parser_state *cascade_expr
|
||||
= (struct expr_parser_state *)parser_push_state(ctx, IVY_AST_EXPR);
|
||||
= (struct expr_parser_state *)parser_push_state(
|
||||
ctx, IVY_AST_EXPR, 0);
|
||||
|
||||
if (!first_msg) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
@@ -714,7 +676,8 @@ static enum ivy_status begin_cascade_operation(struct ivy_parser *ctx)
|
||||
b_queue_push_back(&cascade_expr->s_cascade_msg, &first_msg->n_base.n_entry);
|
||||
|
||||
struct expr_parser_state *msg_expr
|
||||
= (struct expr_parser_state *)parser_push_state(ctx, IVY_AST_EXPR);
|
||||
= (struct expr_parser_state *)parser_push_state(
|
||||
ctx, IVY_AST_EXPR, 0);
|
||||
msg_expr->s_sub_type = EXPR_SUBTYPE_MSG;
|
||||
msg_expr->s_type = EXPR_TYPE_ARITH;
|
||||
msg_expr->s_subexpr_depth = state->s_subexpr_depth + 1;
|
||||
@@ -752,7 +715,7 @@ struct token_parse_result arith_parse_semicolon(
|
||||
/* finish parsing this expression and let the parent context handle the semicolon. */
|
||||
struct ivy_ast_node *expr = NULL;
|
||||
enum ivy_status status
|
||||
= finalise_expr(state, &expr, IVY_PRECEDENCE_ASSIGN);
|
||||
= expr_finalise_arith(state, &expr, IVY_PRECEDENCE_ASSIGN);
|
||||
if (status != IVY_OK) {
|
||||
return PARSE_RESULT(status, 0);
|
||||
}
|
||||
@@ -768,7 +731,7 @@ struct token_parse_result arith_parse_semicolon(
|
||||
/* this is another message in a cascade series. */
|
||||
struct expr_parser_state *msg_expr
|
||||
= (struct expr_parser_state *)parser_push_state(
|
||||
ctx, IVY_AST_EXPR);
|
||||
ctx, IVY_AST_EXPR, 0);
|
||||
|
||||
msg_expr->s_recipient = NULL;
|
||||
msg_expr->s_sub_type = EXPR_SUBTYPE_MSG;
|
||||
@@ -779,7 +742,7 @@ struct token_parse_result arith_parse_semicolon(
|
||||
}
|
||||
|
||||
if (state->s_sub_type == EXPR_SUBTYPE_KEYWORD_MSG && !state->s_recipient) {
|
||||
struct ivy_ast_msg_node *msg = finalise_keyword_msg(state);
|
||||
struct ivy_ast_msg_node *msg = expr_finalise_keyword_msg(state);
|
||||
if (!msg) {
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
@@ -794,6 +757,76 @@ struct token_parse_result arith_parse_semicolon(
|
||||
return PARSE_RESULT(status, 0);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
int flags = 0;
|
||||
if (state->s_subexpr_depth > 0) {
|
||||
flags = PARSE_REPEAT_TOKEN;
|
||||
}
|
||||
|
||||
if (state->s_sub_type == EXPR_SUBTYPE_MSG) {
|
||||
/* this is the end of a unary message (probably in a cascade operation). */
|
||||
struct ivy_ast_node *expr = NULL;
|
||||
enum ivy_status status = expr_finalise_arith(
|
||||
state, &expr, IVY_PRECEDENCE_CASCADE);
|
||||
if (status != IVY_OK) {
|
||||
return PARSE_RESULT(status, 0);
|
||||
}
|
||||
|
||||
*result = expr;
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
|
||||
if (state->s_sub_type == EXPR_SUBTYPE_CASCADE) {
|
||||
/* this is the end of a cascade operation */
|
||||
struct ivy_ast_cascade_node *cascade
|
||||
= expr_finalise_cascade(state);
|
||||
|
||||
*result = cascade;
|
||||
return PARSE_RESULT(IVY_OK, flags);
|
||||
}
|
||||
|
||||
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;
|
||||
return PARSE_RESULT(IVY_OK, flags);
|
||||
}
|
||||
|
||||
/* this is the end of a regular expression or sub-expression */
|
||||
struct ivy_ast_node *expr = NULL;
|
||||
enum ivy_status status
|
||||
= expr_finalise_arith(state, &expr, min_precedence);
|
||||
if (status != IVY_OK) {
|
||||
return PARSE_RESULT(status, 0);
|
||||
}
|
||||
|
||||
*result = expr;
|
||||
return PARSE_RESULT(IVY_OK, flags);
|
||||
}
|
||||
|
||||
struct token_parse_result expr_finalise_and_return(
|
||||
struct ivy_parser *ctx, struct expr_parser_state *state)
|
||||
{
|
||||
int flags = 0;
|
||||
if (state->s_subexpr_depth > 0) {
|
||||
flags = PARSE_REPEAT_TOKEN;
|
||||
}
|
||||
|
||||
struct ivy_ast_node *expr_node = NULL;
|
||||
struct token_parse_result result = expr_finalise(ctx, state, IVY_PRECEDENCE_ASSIGN, &expr_node);
|
||||
|
||||
if (result.r_status != IVY_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
parser_replace_current_node(ctx, expr_node);
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return result;
|
||||
}
|
||||
|
||||
struct token_parse_result arith_parse_dot(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
@@ -809,52 +842,7 @@ struct token_parse_result arith_parse_dot(
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
int flags = 0;
|
||||
if (state->s_subexpr_depth > 0) {
|
||||
flags = PARSE_REPEAT_TOKEN;
|
||||
}
|
||||
|
||||
if (state->s_sub_type == EXPR_SUBTYPE_MSG) {
|
||||
/* this is the end of a unary message (probably in a cascade operation). */
|
||||
struct ivy_ast_node *expr = NULL;
|
||||
enum ivy_status status
|
||||
= finalise_expr(state, &expr, IVY_PRECEDENCE_CASCADE);
|
||||
if (status != IVY_OK) {
|
||||
return PARSE_RESULT(status, 0);
|
||||
}
|
||||
|
||||
parser_replace_current_node(ctx, expr);
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
|
||||
if (state->s_sub_type == EXPR_SUBTYPE_CASCADE) {
|
||||
/* this is the end of a cascade operation */
|
||||
struct ivy_ast_cascade_node *cascade = finalise_cascade(state);
|
||||
parser_replace_current_node(ctx, (struct ivy_ast_node *)cascade);
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return PARSE_RESULT(IVY_OK, flags);
|
||||
}
|
||||
|
||||
if (state->s_sub_type == EXPR_SUBTYPE_KEYWORD_MSG) {
|
||||
/* this is the end of a keyword-message */
|
||||
struct ivy_ast_msg_node *msg = finalise_keyword_msg(state);
|
||||
parser_replace_current_node(ctx, (struct ivy_ast_node *)msg);
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return PARSE_RESULT(IVY_OK, flags);
|
||||
}
|
||||
|
||||
/* this is the end of a regular expression or sub-expression */
|
||||
struct ivy_ast_node *expr = NULL;
|
||||
enum ivy_status status
|
||||
= finalise_expr(state, &expr, IVY_PRECEDENCE_ASSIGN);
|
||||
if (status != IVY_OK) {
|
||||
return PARSE_RESULT(status, 0);
|
||||
}
|
||||
|
||||
parser_replace_current_node(ctx, expr);
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return PARSE_RESULT(IVY_OK, flags);
|
||||
return expr_finalise_and_return(ctx, state);
|
||||
}
|
||||
|
||||
struct token_parse_result arith_parse_label(
|
||||
@@ -874,7 +862,7 @@ struct token_parse_result arith_parse_label(
|
||||
* parent keyword-message parser context. */
|
||||
struct ivy_ast_node *expr = NULL;
|
||||
enum ivy_status status
|
||||
= finalise_expr(state, &expr, IVY_PRECEDENCE_ASSIGN);
|
||||
= expr_finalise_arith(state, &expr, IVY_PRECEDENCE_ASSIGN);
|
||||
if (status != IVY_OK) {
|
||||
return PARSE_RESULT(status, 0);
|
||||
}
|
||||
@@ -887,9 +875,20 @@ struct token_parse_result arith_parse_label(
|
||||
if (state->s_sub_type != EXPR_SUBTYPE_KEYWORD_MSG
|
||||
&& state->s_sub_type != EXPR_SUBTYPE_COMPLEX_MSG) {
|
||||
/* this is the beginning of a new keyword-message */
|
||||
struct expr_parser_state *msg_expr
|
||||
= (struct expr_parser_state *)parser_push_state(
|
||||
ctx, IVY_AST_EXPR);
|
||||
struct expr_parser_state *msg_expr;
|
||||
bool new_parser = true;
|
||||
|
||||
if (b_queue_empty(&state->s_operator_stack)
|
||||
&& b_queue_empty(&state->s_output_queue)) {
|
||||
new_parser = false;
|
||||
}
|
||||
|
||||
if (new_parser) {
|
||||
msg_expr = (struct expr_parser_state *)parser_push_state(
|
||||
ctx, IVY_AST_EXPR, 0);
|
||||
} else {
|
||||
msg_expr = state;
|
||||
}
|
||||
|
||||
/* the only operator with a lower precedence than
|
||||
* keyword-messages is assignment. everything in the preceding
|
||||
@@ -899,7 +898,7 @@ struct token_parse_result arith_parse_label(
|
||||
|
||||
if (state->s_sub_type != EXPR_SUBTYPE_MSG) {
|
||||
enum ivy_status status
|
||||
= finalise_expr(state, &expr, IVY_PRECEDENCE_KEYWORD_MSG);
|
||||
= expr_finalise_arith(state, &expr, IVY_PRECEDENCE_KEYWORD_MSG);
|
||||
if (status != IVY_OK) {
|
||||
return PARSE_RESULT(status, 0);
|
||||
}
|
||||
@@ -908,7 +907,7 @@ struct token_parse_result arith_parse_label(
|
||||
msg_expr->s_recipient = expr;
|
||||
msg_expr->s_sub_type = EXPR_SUBTYPE_KEYWORD_MSG;
|
||||
msg_expr->s_type = EXPR_TYPE_ARITH;
|
||||
msg_expr->s_subexpr_depth = state->s_subexpr_depth + 1;
|
||||
msg_expr->s_subexpr_depth = new_parser ? state->s_subexpr_depth + 1 : 0;
|
||||
state = msg_expr;
|
||||
}
|
||||
|
||||
@@ -921,7 +920,7 @@ struct token_parse_result arith_parse_label(
|
||||
|
||||
struct expr_parser_state *arg_expr
|
||||
= (struct expr_parser_state *)parser_push_state(
|
||||
ctx, IVY_AST_EXPR);
|
||||
ctx, IVY_AST_EXPR, 0);
|
||||
arg_expr->s_terminator = IVY_TOK_LABEL;
|
||||
arg_expr->s_sub_type = EXPR_SUBTYPE_KEYWORD_ARG;
|
||||
arg_expr->s_subexpr_depth = state->s_subexpr_depth + 1;
|
||||
@@ -958,4 +957,4 @@ enum ivy_status arith_add_child(
|
||||
}
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user