lang: replace stack-based parser with a recursive parser

This commit is contained in:
2026-04-06 18:25:43 +01:00
parent 3a549aa6df
commit b904813cef
77 changed files with 2984 additions and 6499 deletions
-64
View File
@@ -1,5 +1,3 @@
#include "ctx.h"
#include "expr/expr.h"
#include "iterate.h"
#include "node.h"
@@ -13,57 +11,6 @@ static void to_string(struct ivy_ast_node *node, fx_string *str)
s->n_value->t_str);
}
struct token_parse_result parse_string(struct ivy_parser *ctx, struct ivy_token *tok)
{
struct parser_state *state = parser_get_state_generic(ctx);
struct ivy_ast_fstring_node *parent
= (struct ivy_ast_fstring_node *)state->s_node;
struct ivy_ast_string_node *child
= (struct ivy_ast_string_node *)ast_node_create(IVY_AST_STRING);
if (!child) {
return PARSE_RESULT(IVY_ERR_NO_MEMORY, 0);
}
child->n_value = tok;
fx_queue_push_back(&parent->n_value, &child->n_base.n_entry);
return PARSE_RESULT(IVY_OK, 0);
}
struct token_parse_result parse_left_brace(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct expr_parser_state *expr
= (struct expr_parser_state *)parser_push_state(
ctx, IVY_AST_EXPR, 0);
expr_add_terminator(expr, IVY_SYM_RIGHT_BRACE);
return PARSE_RESULT(IVY_OK, 0);
}
struct token_parse_result parse_right_brace(
struct ivy_parser *ctx, struct ivy_token *tok)
{
return PARSE_RESULT(IVY_OK, 0);
}
struct token_parse_result parse_str_end(
struct ivy_parser *ctx, struct ivy_token *tok)
{
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status add_child(
struct parser_state *parent, struct ivy_ast_node *child)
{
struct ivy_ast_fstring_node *str
= (struct ivy_ast_fstring_node *)parent->s_node;
fx_queue_push_back(&str->n_value, &child->n_entry);
return IVY_OK;
}
static void collect_children(
struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator)
{
@@ -79,21 +26,10 @@ static void collect_children(
struct ast_node_type string_node_ops = {
.n_to_string = to_string,
.n_state_size = sizeof(struct parser_state),
.n_node_size = sizeof(struct ivy_ast_string_node),
};
struct ast_node_type fstring_node_ops = {
.n_add_child = add_child,
.n_collect_children = collect_children,
.n_state_size = sizeof(struct parser_state),
.n_node_size = sizeof(struct ivy_ast_fstring_node),
.n_token_parsers = {
TOK_PARSER(STRING, parse_string),
TOK_PARSER(STR_END, parse_str_end),
},
.n_symbol_parsers = {
SYM_PARSER(LEFT_BRACE, parse_left_brace),
SYM_PARSER(RIGHT_BRACE, parse_right_brace),
},
};