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

@@ -220,18 +220,18 @@ static struct token_parse_result parse_other(
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
static void print(struct ivy_ast_node *node)
static void to_string(struct ivy_ast_node *node, b_string *str)
{
struct ivy_ast_selector_node *sel = (struct ivy_ast_selector_node *)node;
printf("%s [", ivy_ast_node_type_to_string(node->n_type));
b_string_append_cstrf(str, "%s [", ivy_ast_node_type_to_string(node->n_type));
switch (sel->n_recipient) {
case IVY_SELECTOR_RECIPIENT_CLASS:
fputc('+', stdout);
b_string_append_cstr(str, "+");
break;
case IVY_SELECTOR_RECIPIENT_OBJECT:
fputc('-', stdout);
b_string_append_cstr(str, "-");
break;
default:
/* this will occur if the selector is being used to send a
@@ -241,11 +241,11 @@ static void print(struct ivy_ast_node *node)
}
if (sel->n_msg_name) {
printf("%s", sel->n_msg_name->t_str);
b_string_append_cstr(str, sel->n_msg_name->t_str);
}
if (sel->n_msg_name && !b_queue_empty(&sel->n_arg_labels)) {
fputc('(', stdout);
b_string_append_cstr(str, "(");
}
b_queue_iterator label_it = {0};
@@ -258,7 +258,7 @@ static void print(struct ivy_ast_node *node)
while (b_queue_iterator_is_valid(&label_it)
&& b_queue_iterator_is_valid(&name_it)) {
if (i > 0) {
fputc(' ', stdout);
b_string_append_cstr(str, " ");
}
struct ivy_token *label
@@ -267,7 +267,7 @@ static void print(struct ivy_ast_node *node)
= b_unbox(struct ivy_token, name_it.entry, t_entry);
if (label && name) {
printf("%s:%s", label->t_str, name->t_str);
b_string_append_cstrf(str, "%s:%s", label->t_str, name->t_str);
}
i++;
@@ -276,10 +276,10 @@ static void print(struct ivy_ast_node *node)
}
if (sel->n_msg_name && !b_queue_empty(&sel->n_arg_labels)) {
fputc(')', stdout);
b_string_append_cstr(str, ")");
}
fputs("]\n", stdout);
b_string_append_cstr(str, "]");
}
static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_t arg)
@@ -291,7 +291,7 @@ static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_
struct ast_node_type selector_node_ops = {
.n_init_state = init_state,
.n_print = print,
.n_to_string = to_string,
.n_state_size = sizeof(struct selector_parser_state),
.n_node_size = sizeof(struct ivy_ast_selector_node),
.n_token_parsers = {