lang: ast: replace ast node print callback with to_string

This commit is contained in:
2024-12-06 20:24:08 +00:00
parent ae15f228d3
commit bd5e524241
14 changed files with 72 additions and 73 deletions

View File

@@ -57,11 +57,12 @@ static struct token_parse_result parse_linefeed(
return PARSE_RESULT(IVY_OK, 0);
}
static void print(struct ivy_ast_node *node)
static void to_string(struct ivy_ast_node *node, b_string *str)
{
struct ivy_ast_unit_import_node *unit_import
= (struct ivy_ast_unit_import_node *)node;
b_string *str = b_string_create();
b_string_append_cstr(str, ivy_ast_node_type_to_string(node->n_type));
b_queue_iterator it = {0};
b_queue_foreach (&it, &unit_import->n_ident) {
@@ -75,9 +76,7 @@ static void print(struct ivy_ast_node *node)
b_string_append_cstr(str, tok->t_str);
}
printf("%s(%s)\n", ivy_ast_node_type_to_string(node->n_type),
b_string_ptr(str));
b_string_release(str);
b_string_append_cstr(str, ")");
}
static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_t arg)
@@ -88,7 +87,7 @@ static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_
}
struct ast_node_type unit_import_node_ops = {
.n_print = print,
.n_to_string = to_string,
.n_init_state = init_state,
.n_state_size = sizeof(struct unit_import_parser_state),
.n_node_size = sizeof(struct ivy_ast_unit_import_node),