lang: add missing includes; misc formatting cleanup

This commit is contained in:
2024-12-06 09:53:09 +00:00
parent 5fe3231c9e
commit d1855afc05
9 changed files with 131 additions and 88 deletions

View File

@@ -1,8 +1,7 @@
#include "ctx.h" #include "ctx.h"
#include "iterate.h"
#include "node.h" #include "node.h"
#include <stdio.h>
static void collect_children( static void collect_children(
struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator) struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator)
{ {

View File

@@ -1,5 +1,6 @@
#include "ctx.h" #include "ctx.h"
#include "../debug.h"
#include "node.h" #include "node.h"
#include <blue/core/queue.h> #include <blue/core/queue.h>
@@ -16,7 +17,8 @@ static void print_state_stack(struct ivy_parser *parser)
b_queue_foreach (&it, &parser->p_state) { b_queue_foreach (&it, &parser->p_state) {
struct parser_state *state struct parser_state *state
= b_unbox(struct parser_state, it.entry, s_entry); = b_unbox(struct parser_state, it.entry, s_entry);
debug_printf(" %s\n", debug_printf(
" %s\n",
ivy_ast_node_type_to_string(state->s_node->n_type)); ivy_ast_node_type_to_string(state->s_node->n_type));
} }
} }
@@ -97,7 +99,8 @@ struct parser_state *parser_get_state_generic(struct ivy_parser *parser)
return state; return state;
} }
struct parser_state *parser_get_parent_state_generic(struct ivy_parser *parser, enum ivy_ast_node_type type) struct parser_state *parser_get_parent_state_generic(
struct ivy_parser *parser, enum ivy_ast_node_type type)
{ {
b_queue_entry *entry = b_queue_last(&parser->p_state); b_queue_entry *entry = b_queue_last(&parser->p_state);
if (!entry) { if (!entry) {

View File

@@ -51,7 +51,8 @@ static void print_operand(struct ivy_ast_node *node)
} }
} }
enum ivy_status 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) { switch (tok->t_type) {
case IVY_TOK_IDENT: { case IVY_TOK_IDENT: {
@@ -493,7 +494,6 @@ struct token_parse_result arith_parse_ident(
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
} }
result = arith_parse_operator(ctx, tok); result = arith_parse_operator(ctx, tok);
state->s_prev_component = EXPR_CMP_MSG; state->s_prev_component = EXPR_CMP_MSG;
return result; return result;
@@ -576,7 +576,8 @@ static struct ivy_ast_selector_node *keyword_selector_from_label_list(b_queue *l
return sel; return sel;
} }
static struct ivy_ast_cascade_node *expr_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 *cascade
= (struct ivy_ast_cascade_node *)ast_node_create(IVY_AST_CASCADE); = (struct ivy_ast_cascade_node *)ast_node_create(IVY_AST_CASCADE);
@@ -595,7 +596,8 @@ static struct ivy_ast_cascade_node *expr_finalise_cascade(struct expr_parser_sta
return cascade; return cascade;
} }
static struct ivy_ast_msg_node *expr_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 *msg
= (struct ivy_ast_msg_node *)ast_node_create(IVY_AST_MSG); = (struct ivy_ast_msg_node *)ast_node_create(IVY_AST_MSG);
@@ -617,7 +619,8 @@ static struct ivy_ast_msg_node *expr_finalise_keyword_msg(struct expr_parser_sta
return msg; return msg;
} }
static struct ivy_ast_msg_node *expr_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; struct ivy_ast_msg_node *msg = state->s_msg;
if (!msg) { if (!msg) {
@@ -667,7 +670,8 @@ struct token_parse_result arith_parse_right_paren(
static enum ivy_status begin_cascade_operation(struct ivy_parser *ctx) static enum ivy_status begin_cascade_operation(struct ivy_parser *ctx)
{ {
struct expr_parser_state *state = parser_get_state(ctx, struct expr_parser_state); struct expr_parser_state *state
= parser_get_state(ctx, struct expr_parser_state);
/* this is the beginning of a cascade operation */ /* this is the beginning of a cascade operation */
@@ -678,16 +682,19 @@ static enum ivy_status begin_cascade_operation(struct ivy_parser *ctx)
parser_pop_state(ctx, 0); parser_pop_state(ctx, 0);
} else { } else {
/* we need to find who the first message in the cascade is being sent to. */ /* we need to find who the first message in the cascade is being sent to. */
enum ivy_operator_precedence min_precedence = IVY_PRECEDENCE_CASCADE; enum ivy_operator_precedence min_precedence
= IVY_PRECEDENCE_CASCADE;
struct ivy_ast_node *prev = b_unbox( struct ivy_ast_node *prev = b_unbox(
struct ivy_ast_node, struct ivy_ast_node,
b_queue_last(&state->s_operator_stack), n_entry); b_queue_last(&state->s_operator_stack), n_entry);
if (prev && prev->n_type == IVY_AST_MSG) { if (prev && prev->n_type == IVY_AST_MSG) {
/* unary complex messages (which will be found on the operator stack) have a very high /* unary complex messages (which will be found on the
* precedence (much higher than most arithmetic operators), so the recipient will be much * operator stack) have a very high precedence (much
* narrower. this also means that any subsequent messages in the cascade inherit this high * higher than most arithmetic operators), so the
* precedence, regardless of their type. */ * recipient will be much narrower. this also means that
* any subsequent messages in the cascade inherit this
* high precedence, regardless of their type. */
min_precedence = IVY_PRECEDENCE_UNARY_MSG; min_precedence = IVY_PRECEDENCE_UNARY_MSG;
} }
@@ -699,10 +706,11 @@ static enum ivy_status begin_cascade_operation(struct ivy_parser *ctx)
} }
if (expr->n_type != IVY_AST_MSG) { if (expr->n_type != IVY_AST_MSG) {
/* the recipient and first message of the cascade operation is ambiguous due /* the recipient and first message of the cascade
* to operator precedence. this is usually resolved by adding parentheses * operation is ambiguous due to operator precedence.
* (either around the recipient or around the cascade) to make the recipient * this is usually resolved by adding parentheses
* and first message clear. */ * (either around the recipient or around the cascade)
* to make the recipient and first message clear. */
return IVY_ERR_BAD_SYNTAX; return IVY_ERR_BAD_SYNTAX;
} }
@@ -765,8 +773,8 @@ struct token_parse_result arith_parse_semicolon(
if (end_subexpr) { if (end_subexpr) {
/* finish parsing this expression and let the parent context handle the semicolon. */ /* finish parsing this expression and let the parent context handle the semicolon. */
struct ivy_ast_node *expr = NULL; struct ivy_ast_node *expr = NULL;
enum ivy_status status enum ivy_status status = expr_finalise_arith(
= expr_finalise_arith(state, &expr, IVY_PRECEDENCE_ASSIGN); state, &expr, IVY_PRECEDENCE_ASSIGN);
if (status != IVY_OK) { if (status != IVY_OK) {
return PARSE_RESULT(status, 0); return PARSE_RESULT(status, 0);
} }
@@ -776,7 +784,8 @@ struct token_parse_result arith_parse_semicolon(
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN); return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
} }
struct expr_parser_state *parent_state = parser_get_parent_state(ctx, IVY_AST_EXPR, struct expr_parser_state); struct expr_parser_state *parent_state = parser_get_parent_state(
ctx, IVY_AST_EXPR, struct expr_parser_state);
if (state->s_sub_type == EXPR_SUBTYPE_CASCADE) { if (state->s_sub_type == EXPR_SUBTYPE_CASCADE) {
/* this is another message in a cascade series. */ /* this is another message in a cascade series. */
@@ -845,7 +854,7 @@ struct token_parse_result expr_finalise(
} }
if (state->s_sub_type == EXPR_SUBTYPE_COMPLEX_MSG) { if (state->s_sub_type == EXPR_SUBTYPE_COMPLEX_MSG) {
/* this is the end of a keyword-message */ /* this is the end of a complex message */
struct ivy_ast_msg_node *msg = expr_finalise_complex_msg(state); struct ivy_ast_msg_node *msg = expr_finalise_complex_msg(state);
*result = (struct ivy_ast_node *)msg; *result = (struct ivy_ast_node *)msg;
return PARSE_RESULT(IVY_OK, 0); return PARSE_RESULT(IVY_OK, 0);
@@ -860,8 +869,7 @@ struct token_parse_result expr_finalise(
/* this is the end of a regular expression or sub-expression */ /* this is the end of a regular expression or sub-expression */
struct ivy_ast_node *expr = NULL; struct ivy_ast_node *expr = NULL;
enum ivy_status status enum ivy_status status = expr_finalise_arith(state, &expr, min_precedence);
= expr_finalise_arith(state, &expr, min_precedence);
if (status != IVY_OK) { if (status != IVY_OK) {
return PARSE_RESULT(status, 0); return PARSE_RESULT(status, 0);
} }
@@ -874,7 +882,8 @@ struct token_parse_result expr_finalise_and_return(
struct ivy_parser *ctx, struct expr_parser_state *state) struct ivy_parser *ctx, struct expr_parser_state *state)
{ {
struct ivy_ast_node *expr_node = NULL; struct ivy_ast_node *expr_node = NULL;
struct token_parse_result result = expr_finalise(ctx, state, IVY_PRECEDENCE_ASSIGN, &expr_node); struct token_parse_result result
= expr_finalise(ctx, state, IVY_PRECEDENCE_ASSIGN, &expr_node);
if (result.r_status != IVY_OK) { if (result.r_status != IVY_OK) {
return result; return result;
@@ -904,6 +913,27 @@ struct token_parse_result arith_parse_dot(
return expr_finalise_and_return(ctx, state); return expr_finalise_and_return(ctx, state);
} }
struct token_parse_result arith_parse_caret(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct expr_parser_state *state
= parser_get_state(ctx, struct expr_parser_state);
/* caret must be used at the beginning of a top-level expression */
if (state->s_subexpr_depth > 0 || state->s_paren_depth > 0) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
if (!b_queue_empty(&state->s_operator_stack)
|| !b_queue_empty(&state->s_output_queue)) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
state->s_prev_token = IVY_SYM_CARET;
state->s_return = true;
return PARSE_RESULT(IVY_OK, 0);
}
struct token_parse_result arith_parse_comma( struct token_parse_result arith_parse_comma(
struct ivy_parser *ctx, struct ivy_token *tok) struct ivy_parser *ctx, struct ivy_token *tok)
{ {
@@ -914,7 +944,9 @@ struct token_parse_result arith_parse_comma(
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); 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)) { if (state->s_paren_depth > 0
&& (state->s_prev_component == EXPR_CMP_OPERAND
|| state->s_prev_component == EXPR_CMP_MSG)) {
/* tuple. */ /* tuple. */
return PARSE_RESULT(IVY_ERR_NOT_SUPPORTED, 0); return PARSE_RESULT(IVY_ERR_NOT_SUPPORTED, 0);
} }
@@ -952,8 +984,8 @@ struct token_parse_result arith_parse_label(
* next argument. terminate here and propagate this label to the * next argument. terminate here and propagate this label to the
* parent keyword-message parser context. */ * parent keyword-message parser context. */
struct ivy_ast_node *expr = NULL; struct ivy_ast_node *expr = NULL;
enum ivy_status status enum ivy_status status = expr_finalise_arith(
= expr_finalise_arith(state, &expr, IVY_PRECEDENCE_ASSIGN); state, &expr, IVY_PRECEDENCE_ASSIGN);
if (status != IVY_OK) { if (status != IVY_OK) {
return PARSE_RESULT(status, 0); return PARSE_RESULT(status, 0);
} }
@@ -988,8 +1020,8 @@ struct token_parse_result arith_parse_label(
struct ivy_ast_node *expr = NULL; struct ivy_ast_node *expr = NULL;
if (state->s_sub_type != EXPR_SUBTYPE_MSG) { if (state->s_sub_type != EXPR_SUBTYPE_MSG) {
enum ivy_status status enum ivy_status status = expr_finalise_arith(
= expr_finalise_arith(state, &expr, IVY_PRECEDENCE_KEYWORD_MSG); state, &expr, IVY_PRECEDENCE_KEYWORD_MSG);
if (status != IVY_OK) { if (status != IVY_OK) {
return PARSE_RESULT(status, 0); return PARSE_RESULT(status, 0);
} }
@@ -1033,7 +1065,8 @@ enum ivy_status arith_add_child(
if (state->s_sub_type == EXPR_SUBTYPE_CASCADE) { if (state->s_sub_type == EXPR_SUBTYPE_CASCADE) {
/* treat the child node as a cascaded message */ /* treat the child node as a cascaded message */
b_queue_push_back(&state->s_cascade_msg, &child->n_entry); b_queue_push_back(&state->s_cascade_msg, &child->n_entry);
} else if (state->s_sub_type == EXPR_SUBTYPE_KEYWORD_MSG } else if (
state->s_sub_type == EXPR_SUBTYPE_KEYWORD_MSG
|| state->s_sub_type == EXPR_SUBTYPE_COMPLEX_MSG) { || state->s_sub_type == EXPR_SUBTYPE_COMPLEX_MSG) {
/* treat the child node as a keyword-message argument */ /* treat the child node as a keyword-message argument */
b_queue_push_back(&state->s_args, &child->n_entry); b_queue_push_back(&state->s_args, &child->n_entry);

View File

@@ -13,8 +13,8 @@ struct token_parse_result stmt_parse_while(
= parser_get_state(ctx, struct expr_parser_state); = parser_get_state(ctx, struct expr_parser_state);
if (state->s_sub_type == EXPR_SUBTYPE_KEYWORD_ARG) { if (state->s_sub_type == EXPR_SUBTYPE_KEYWORD_ARG) {
/* keyword messages have a higher precedence than inline conditionals, so /* keyword messages have a higher precedence than inline
* treat this as a statement terminator. */ * conditionals, so treat this as a statement terminator. */
struct token_parse_result result struct token_parse_result result
= expr_finalise_and_return(ctx, state); = expr_finalise_and_return(ctx, state);
result.r_flags |= PARSE_REPEAT_TOKEN; result.r_flags |= PARSE_REPEAT_TOKEN;
@@ -30,7 +30,8 @@ struct token_parse_result stmt_parse_while(
state->s_prev_token = IVY_KW_WHILE; state->s_prev_token = IVY_KW_WHILE;
if (b_queue_empty(&state->s_operator_stack) && b_queue_empty(&state->s_output_queue)) { if (b_queue_empty(&state->s_operator_stack)
&& b_queue_empty(&state->s_output_queue)) {
parser_pop_state(ctx, 0); parser_pop_state(ctx, 0);
} }
@@ -47,7 +48,8 @@ struct token_parse_result stmt_parse_match(
struct expr_parser_state *state struct expr_parser_state *state
= parser_get_state(ctx, struct expr_parser_state); = parser_get_state(ctx, struct expr_parser_state);
if (state->s_prev_component == EXPR_CMP_OPERAND || state->s_prev_component == EXPR_CMP_MSG) { if (state->s_prev_component == EXPR_CMP_OPERAND
|| state->s_prev_component == EXPR_CMP_MSG) {
/* match statements are operands. */ /* match statements are operands. */
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
} }
@@ -61,7 +63,8 @@ struct token_parse_result stmt_parse_match(
state->s_prev_token = IVY_KW_MATCH; state->s_prev_token = IVY_KW_MATCH;
if (b_queue_empty(&state->s_operator_stack) && b_queue_empty(&state->s_output_queue)) { if (b_queue_empty(&state->s_operator_stack)
&& b_queue_empty(&state->s_output_queue)) {
parser_pop_state(ctx, 0); parser_pop_state(ctx, 0);
} }
@@ -81,8 +84,8 @@ struct token_parse_result stmt_parse_if(
= parser_get_state(ctx, struct expr_parser_state); = parser_get_state(ctx, struct expr_parser_state);
if (state->s_sub_type == EXPR_SUBTYPE_KEYWORD_ARG) { if (state->s_sub_type == EXPR_SUBTYPE_KEYWORD_ARG) {
/* keyword messages have a higher precedence than inline conditionals, so /* keyword messages have a higher precedence than inline
* treat this as a statement terminator. */ * conditionals, so treat this as a statement terminator. */
struct token_parse_result result struct token_parse_result result
= expr_finalise_and_return(ctx, state); = expr_finalise_and_return(ctx, state);
result.r_flags |= PARSE_REPEAT_TOKEN; result.r_flags |= PARSE_REPEAT_TOKEN;
@@ -98,7 +101,8 @@ struct token_parse_result stmt_parse_if(
state->s_prev_token = IVY_KW_IF; state->s_prev_token = IVY_KW_IF;
if (b_queue_empty(&state->s_operator_stack) && b_queue_empty(&state->s_output_queue)) { if (b_queue_empty(&state->s_operator_stack)
&& b_queue_empty(&state->s_output_queue)) {
parser_pop_state(ctx, 0); parser_pop_state(ctx, 0);
} }

View File

@@ -1,6 +1,6 @@
#include "block.h" #include "block.h"
#include "iterate.h"
#include "expr/expr.h" #include "expr/expr.h"
#include "iterate.h"
struct match_parser_state { struct match_parser_state {
struct parser_state s_base; struct parser_state s_base;
@@ -16,14 +16,11 @@ struct match_parser_state {
static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_t arg) static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_t arg)
{ {
struct match_parser_state *state struct match_parser_state *state = (struct match_parser_state *)sp;
= (struct match_parser_state *)sp;
state->s_prev_node = (struct ivy_ast_node *)arg; state->s_prev_node = (struct ivy_ast_node *)arg;
} }
struct token_parse_result parse_match( struct token_parse_result parse_match(struct ivy_parser *ctx, struct ivy_token *tok)
struct ivy_parser *ctx,
struct ivy_token *tok)
{ {
struct match_parser_state *state struct match_parser_state *state
= parser_get_state(ctx, struct match_parser_state); = parser_get_state(ctx, struct match_parser_state);
@@ -32,7 +29,8 @@ struct token_parse_result parse_match(
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
} }
state->s_cur_branch = (struct ivy_ast_cond_node *)ast_node_create(IVY_AST_COND); state->s_cur_branch
= (struct ivy_ast_cond_node *)ast_node_create(IVY_AST_COND);
if (!state->s_cur_branch) { if (!state->s_cur_branch) {
return PARSE_RESULT(IVY_ERR_NO_MEMORY, 0); return PARSE_RESULT(IVY_ERR_NO_MEMORY, 0);
} }
@@ -119,7 +117,8 @@ static struct token_parse_result parse_arrow(
struct match_parser_state *state struct match_parser_state *state
= parser_get_state(ctx, struct match_parser_state); = parser_get_state(ctx, struct match_parser_state);
if (state->s_prev_token != IVY_KW_IN && state->s_prev_token != IVY_SYM_COMMA) { if (state->s_prev_token != IVY_KW_IN
&& state->s_prev_token != IVY_SYM_COMMA) {
/* this token can only appear after the `in` keyword and an expression. */ /* this token can only appear after the `in` keyword and an expression. */
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
} }
@@ -171,7 +170,8 @@ static struct token_parse_result parse_comma(
return PARSE_RESULT(IVY_OK, 0); return PARSE_RESULT(IVY_OK, 0);
} }
static struct token_parse_result parse_end(struct ivy_parser *ctx, struct ivy_token *tok) static struct token_parse_result parse_end(
struct ivy_parser *ctx, struct ivy_token *tok)
{ {
struct match_parser_state *state struct match_parser_state *state
= parser_get_state(ctx, struct match_parser_state); = parser_get_state(ctx, struct match_parser_state);
@@ -202,8 +202,7 @@ static struct token_parse_result parse_end(struct ivy_parser *ctx, struct ivy_to
static enum ivy_status add_child( static enum ivy_status add_child(
struct parser_state *parent, struct ivy_ast_node *child) struct parser_state *parent, struct ivy_ast_node *child)
{ {
struct match_parser_state *state struct match_parser_state *state = (struct match_parser_state *)parent;
= (struct match_parser_state *)parent;
if (state->s_prev_node) { if (state->s_prev_node) {
return IVY_ERR_BAD_SYNTAX; return IVY_ERR_BAD_SYNTAX;
@@ -216,14 +215,14 @@ static enum ivy_status add_child(
static void match_collect_children( static void match_collect_children(
struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator) struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator)
{ {
struct ivy_ast_match_node *match struct ivy_ast_match_node *match = (struct ivy_ast_match_node *)node;
= (struct ivy_ast_match_node *)node;
ast_node_iterator_enqueue_node(iterator, node, match->n_cond); ast_node_iterator_enqueue_node(iterator, node, match->n_cond);
b_queue_iterator it = {0}; b_queue_iterator it = {0};
b_queue_foreach (&it, &match->n_branches) { b_queue_foreach (&it, &match->n_branches) {
struct ivy_ast_node *branch = b_unbox(struct ivy_ast_node, it.entry, n_entry); struct ivy_ast_node *branch
= b_unbox(struct ivy_ast_node, it.entry, n_entry);
ast_node_iterator_enqueue_node(iterator, node, branch); ast_node_iterator_enqueue_node(iterator, node, branch);
} }
} }

View File

@@ -98,7 +98,7 @@ static enum ivy_status add_child(
return IVY_OK; return IVY_OK;
} }
static void init_state(struct ivy_parser *ctx, struct parser_state *sp) static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_t arg)
{ {
struct msgh_parser_state *state = (struct msgh_parser_state *)sp; struct msgh_parser_state *state = (struct msgh_parser_state *)sp;
state->s_oneline = false; state->s_oneline = false;

View File

@@ -1,6 +1,6 @@
#include "block.h" #include "block.h"
#include "iterate.h"
#include "expr/expr.h" #include "expr/expr.h"
#include "iterate.h"
struct while_parser_state { struct while_parser_state {
struct parser_state s_base; struct parser_state s_base;
@@ -14,14 +14,11 @@ struct while_parser_state {
static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_t arg) static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_t arg)
{ {
struct while_parser_state *state struct while_parser_state *state = (struct while_parser_state *)sp;
= (struct while_parser_state *)sp;
state->s_prev_node = (struct ivy_ast_node *)arg; state->s_prev_node = (struct ivy_ast_node *)arg;
} }
struct token_parse_result parse_while( struct token_parse_result parse_while(struct ivy_parser *ctx, struct ivy_token *tok)
struct ivy_parser* ctx,
struct ivy_token* tok)
{ {
struct while_parser_state *state struct while_parser_state *state
= parser_get_state(ctx, struct while_parser_state); = parser_get_state(ctx, struct while_parser_state);
@@ -41,11 +38,13 @@ struct token_parse_result parse_while(
state->s_prev_token = IVY_KW_WHILE; state->s_prev_token = IVY_KW_WHILE;
expr->s_subexpr_depth = 1; expr->s_subexpr_depth = 1;
expr->s_terminator = IVY_KW_DO;
return PARSE_RESULT(IVY_OK, 0); return PARSE_RESULT(IVY_OK, 0);
} }
static struct token_parse_result parse_do(struct ivy_parser *ctx, struct ivy_token *tok) static struct token_parse_result parse_do(
struct ivy_parser *ctx, struct ivy_token *tok)
{ {
struct while_parser_state *state struct while_parser_state *state
= parser_get_state(ctx, struct while_parser_state); = parser_get_state(ctx, struct while_parser_state);
@@ -77,7 +76,8 @@ static struct token_parse_result parse_do(struct ivy_parser *ctx, struct ivy_tok
return PARSE_RESULT(IVY_OK, 0); return PARSE_RESULT(IVY_OK, 0);
} }
static struct token_parse_result parse_expr_begin(struct ivy_parser* ctx, struct ivy_token* tok) static struct token_parse_result parse_expr_begin(
struct ivy_parser *ctx, struct ivy_token *tok)
{ {
struct while_parser_state *state struct while_parser_state *state
= parser_get_state(ctx, struct while_parser_state); = parser_get_state(ctx, struct while_parser_state);
@@ -87,7 +87,9 @@ static struct token_parse_result parse_expr_begin(struct ivy_parser* ctx, struct
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); 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); struct block_parser_state *block
= (struct block_parser_state *)parser_push_state(
ctx, IVY_AST_BLOCK, 0);
block->s_terminator = IVY_KW_END; block->s_terminator = IVY_KW_END;
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN); return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
} }
@@ -116,7 +118,6 @@ static enum ivy_status finalise_while_loop(struct while_parser_state *state)
return IVY_ERR_BAD_SYNTAX; return IVY_ERR_BAD_SYNTAX;
} }
loop->n_cond = state->s_cond; loop->n_cond = state->s_cond;
loop->n_body = state->s_prev_node; loop->n_body = state->s_prev_node;
@@ -146,7 +147,8 @@ static struct token_parse_result parse_punct_terminator(
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN); return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
} }
static struct token_parse_result parse_end(struct ivy_parser *ctx, struct ivy_token *tok) static struct token_parse_result parse_end(
struct ivy_parser *ctx, struct ivy_token *tok)
{ {
struct while_parser_state *state struct while_parser_state *state
= parser_get_state(ctx, struct while_parser_state); = parser_get_state(ctx, struct while_parser_state);
@@ -168,8 +170,7 @@ static struct token_parse_result parse_end(struct ivy_parser *ctx, struct ivy_to
static enum ivy_status add_child( static enum ivy_status add_child(
struct parser_state *parent, struct ivy_ast_node *child) struct parser_state *parent, struct ivy_ast_node *child)
{ {
struct while_parser_state *state struct while_parser_state *state = (struct while_parser_state *)parent;
= (struct while_parser_state *)parent;
if (state->s_prev_node) { if (state->s_prev_node) {
return IVY_ERR_BAD_SYNTAX; return IVY_ERR_BAD_SYNTAX;
@@ -182,7 +183,8 @@ static enum ivy_status add_child(
static void collect_children( static void collect_children(
struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator) struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator)
{ {
struct ivy_ast_while_loop_node *loop = (struct ivy_ast_while_loop_node *)node; struct ivy_ast_while_loop_node *loop
= (struct ivy_ast_while_loop_node *)node;
if (loop->n_cond) { if (loop->n_cond) {
ast_node_iterator_enqueue_node(iterator, node, loop->n_cond); ast_node_iterator_enqueue_node(iterator, node, loop->n_cond);

View File

@@ -3,6 +3,9 @@
#include <stdio.h> #include <stdio.h>
/* uncomment this to enable super-verbose debugging output */
// #define IVY_LANG_DEBUG
#if defined(IVY_LANG_DEBUG) #if defined(IVY_LANG_DEBUG)
#define debug_printf(...) printf(__VA_ARGS__) #define debug_printf(...) printf(__VA_ARGS__)
#else #else

View File