From 70aab9998bf23b969aa00c993a141f28eb016842 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sun, 24 Nov 2024 11:46:42 +0000 Subject: [PATCH] lang: ast: add ast node size to ast_node_type --- lang/ast/ctx.c | 9 ++++----- lang/ast/ctx.h | 8 ++------ lang/ast/node.h | 1 + lang/ast/unit-package.c | 1 + lang/ast/unit.c | 5 +++-- 5 files changed, 11 insertions(+), 13 deletions(-) diff --git a/lang/ast/ctx.c b/lang/ast/ctx.c index 0b479ad..c807725 100644 --- a/lang/ast/ctx.c +++ b/lang/ast/ctx.c @@ -18,7 +18,7 @@ enum ivy_status ivy_parser_create(struct ivy_parser **parser) memset(out, 0x0, sizeof *out); - parser_push_state( out, IVY_AST_UNIT, struct ivy_ast_unit_node); + parser_push_state(out, IVY_AST_UNIT); *parser = out; return IVY_OK; @@ -64,9 +64,8 @@ struct parser_state *parser_get_state_generic(struct ivy_parser *parser) return state; } -struct parser_state *parser_push_state_generic( - struct ivy_parser *parser, enum ivy_ast_node_type type, - size_t node_size) +struct parser_state *parser_push_state( + struct ivy_parser *parser, enum ivy_ast_node_type type) { const struct ast_node_type *node_type = get_ast_node_type(type); if (!node_type) { @@ -87,7 +86,7 @@ struct parser_state *parser_push_state_generic( state->s_parent = current_state->s_node; } - state->s_node = ast_node_create_with_size(type, node_size); + state->s_node = ast_node_create_with_size(type, node_type->n_node_size); if (node_type->n_init_state) { node_type->n_init_state(state); diff --git a/lang/ast/ctx.h b/lang/ast/ctx.h index 87d21ff..6380490 100644 --- a/lang/ast/ctx.h +++ b/lang/ast/ctx.h @@ -5,9 +5,6 @@ #include #include -#define parser_push_state(parser, node_id, node_type) \ - (parser_push_state_generic( \ - parser, node_id, sizeof(node_type))) #define parser_get_state(parser, state_type) \ ((state_type *)parser_get_state_generic(parser)) @@ -28,9 +25,8 @@ enum pop_state_flags { STATE_ADD_NODE_TO_PARENT = 0x01u, }; -extern struct parser_state *parser_push_state_generic( - struct ivy_parser *parser, enum ivy_ast_node_type node_type, - size_t node_size); +extern struct parser_state *parser_push_state( + struct ivy_parser *parser, enum ivy_ast_node_type node_type); extern void parser_pop_state(struct ivy_parser *parser, enum pop_state_flags flags); extern struct parser_state *parser_get_state_generic(struct ivy_parser *parser); diff --git a/lang/ast/node.h b/lang/ast/node.h index 143541c..52ee06c 100644 --- a/lang/ast/node.h +++ b/lang/ast/node.h @@ -18,6 +18,7 @@ struct ast_node_type { void (*n_print)(struct ivy_ast_node *); void (*n_init_state)(struct parser_state *); size_t n_state_size; + size_t n_node_size; token_parse_function n_token_parsers[IVY_TOK_TYPE_COUNT]; token_parse_function n_keyword_parsers[IVY_KW_TYPE_COUNT]; diff --git a/lang/ast/unit-package.c b/lang/ast/unit-package.c index fd95c68..1b79dbe 100644 --- a/lang/ast/unit-package.c +++ b/lang/ast/unit-package.c @@ -63,6 +63,7 @@ struct ast_node_type unit_package_node_ops = { .n_print = print, .n_init_state = init_state, .n_state_size = sizeof(struct unit_package_parser_state), + .n_node_size = sizeof(struct ivy_ast_unit_package_node), .n_symbol_parsers = { [IVY_SYM_DOT] = parse_dot, }, diff --git a/lang/ast/unit.c b/lang/ast/unit.c index 0807b91..684ac73 100644 --- a/lang/ast/unit.c +++ b/lang/ast/unit.c @@ -1,3 +1,4 @@ +#include #include "ctx.h" #include "node.h" #include "unit-package.h" @@ -5,8 +6,7 @@ static enum ivy_status parse_package_keyword( struct ivy_parser *ctx, struct ivy_token *tok) { - parser_push_state( - ctx, IVY_AST_UNIT_PACKAGE, struct ivy_ast_unit_package_node); + parser_push_state(ctx, IVY_AST_UNIT_PACKAGE); return IVY_OK; } @@ -24,6 +24,7 @@ struct ast_node_type unit_node_ops = { .n_add_child = add_child, .n_print = print, .n_state_size = sizeof(struct parser_state), + .n_node_size = sizeof(struct ivy_ast_unit_node), .n_keyword_parsers = { [IVY_KW_PACKAGE] = parse_package_keyword, },