lang: ast: implement for-loop parsing

This commit is contained in:
2024-12-06 10:02:31 +00:00
parent f3cd89c72a
commit d88d58be70
7 changed files with 347 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
#include "../node.h"
#include "../block.h"
#include "../debug.h"
#include "../node.h"
#include "expr.h"
#include <blue/object/string.h>
@@ -481,6 +482,40 @@ struct token_parse_result arith_parse_in(
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
struct token_parse_result arith_parse_do(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct expr_parser_state *state
= parser_get_state(ctx, struct expr_parser_state);
bool terminator = false;
if (state->s_terminator == IVY_KW_DO) {
terminator = true;
}
if (state->s_sub_type == EXPR_SUBTYPE_KEYWORD_MSG
|| state->s_sub_type == EXPR_SUBTYPE_KEYWORD_ARG) {
terminator = true;
}
if (terminator) {
state->s_prev_token = IVY_KW_DO;
struct token_parse_result result
= expr_finalise_and_return(ctx, state);
result.r_flags = PARSE_REPEAT_TOKEN;
return result;
}
/* next component will be a block. */
struct block_parser_state *block
= (struct block_parser_state *)parser_push_state(
ctx, IVY_AST_BLOCK, 0);
/* set the sub-expression depth to be non-zero so the expression parser doesn't consume the expression separator. */
state->s_prev_token = IVY_KW_DO;
return PARSE_RESULT(IVY_OK, 0);
}
struct token_parse_result arith_parse_ident(
struct ivy_parser *ctx, struct ivy_token *tok)
{

View File

@@ -40,15 +40,16 @@ struct ast_node_type expr_node_ops = {
},
.n_keyword_parsers = {
/* statement keywords */
KW_PARSER(FOR, stmt_parse_for),
KW_PARSER(WHILE, stmt_parse_while),
KW_PARSER(MATCH, stmt_parse_match),
KW_PARSER(IF, stmt_parse_if),
KW_PARSER(THEN, stmt_parse_end),
KW_PARSER(ELSE, stmt_parse_end),
KW_PARSER(DO, stmt_parse_end),
KW_PARSER(END, stmt_parse_end),
/* operator keywords */
/* operator/block keywords */
KW_PARSER(IN, arith_parse_in),
KW_PARSER(DO, arith_parse_do),
}
};

View File

@@ -128,9 +128,13 @@ 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);
extern struct token_parse_result arith_parse_do(
struct ivy_parser *ctx, struct ivy_token *tok);
/* statement parser callbacks */
extern struct token_parse_result stmt_parse_for(
struct ivy_parser *ctx, struct ivy_token *tok);
extern struct token_parse_result stmt_parse_while(
struct ivy_parser *ctx, struct ivy_token *tok);
extern struct token_parse_result stmt_parse_match(

View File

@@ -6,6 +6,42 @@
#include <ivy/lang/operator.h>
#include <stdio.h>
struct token_parse_result stmt_parse_for(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct expr_parser_state *state
= parser_get_state(ctx, struct expr_parser_state);
if (state->s_sub_type == EXPR_SUBTYPE_KEYWORD_ARG) {
/* keyword messages have a higher precedence than inline
* conditionals, so treat this as a statement terminator. */
struct token_parse_result result
= expr_finalise_and_return(ctx, state);
result.r_flags |= PARSE_REPEAT_TOKEN;
return result;
}
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_FOR;
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. */
parser_push_state(ctx, IVY_AST_FOR_LOOP, (uintptr_t)expr);
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
}
struct token_parse_result stmt_parse_while(
struct ivy_parser *ctx, struct ivy_token *tok)
{