From bd5e52424130c988715db454a87959e7dd0e504a Mon Sep 17 00:00:00 2001 From: Max Wash Date: Fri, 6 Dec 2024 20:24:08 +0000 Subject: [PATCH] lang: ast: replace ast node print callback with to_string --- lang/ast/atom.c | 8 ++++---- lang/ast/class.c | 6 +++--- lang/ast/double.c | 7 ++++--- lang/ast/ident.c | 8 ++++---- lang/ast/int.c | 8 ++++---- lang/ast/node.c | 24 ++++++++---------------- lang/ast/node.h | 8 ++++---- lang/ast/op.c | 8 ++++---- lang/ast/property.c | 14 +++++++------- lang/ast/selector.c | 22 +++++++++++----------- lang/ast/string.c | 8 ++++++++ lang/ast/unit-import.c | 11 +++++------ lang/ast/unit-package.c | 10 ++++------ lang/include/ivy/lang/ast.h | 3 ++- 14 files changed, 72 insertions(+), 73 deletions(-) diff --git a/lang/ast/atom.c b/lang/ast/atom.c index a4f9d6d..eb0e401 100644 --- a/lang/ast/atom.c +++ b/lang/ast/atom.c @@ -1,18 +1,18 @@ #include "ctx.h" #include "node.h" -#include +#include -static void print(struct ivy_ast_node *node) +static void to_string(struct ivy_ast_node *node, b_string *str) { struct ivy_ast_atom_node *v = (struct ivy_ast_atom_node *)node; - printf("%s (%s)\n", ivy_ast_node_type_to_string(node->n_type), + b_string_append_cstrf(str, "%s (%s)", ivy_ast_node_type_to_string(node->n_type), v->n_content->t_str); } struct ast_node_type atom_node_ops = { - .n_print = print, + .n_to_string = to_string, .n_state_size = sizeof(struct parser_state), .n_node_size = sizeof(struct ivy_ast_atom_node), }; diff --git a/lang/ast/class.c b/lang/ast/class.c index fa4d5da..32de36a 100644 --- a/lang/ast/class.c +++ b/lang/ast/class.c @@ -142,11 +142,11 @@ static enum ivy_status add_child( return IVY_OK; } -static void print(struct ivy_ast_node *node) +static void to_string(struct ivy_ast_node *node, b_string *str) { struct ivy_ast_class_node *c = (struct ivy_ast_class_node *)node; - printf("%s(%s)\n", ivy_ast_node_type_to_string(node->n_type), + b_string_append_cstrf(str, "%s (%s)", ivy_ast_node_type_to_string(node->n_type), c->n_ident->t_str); } @@ -177,7 +177,7 @@ static void collect_children( struct ast_node_type class_node_ops = { .n_add_child = add_child, - .n_print = print, + .n_to_string = to_string, .n_init_state = init_state, .n_collect_children = collect_children, .n_state_size = sizeof(struct class_parser_state), diff --git a/lang/ast/double.c b/lang/ast/double.c index 12627b8..6d88dc4 100644 --- a/lang/ast/double.c +++ b/lang/ast/double.c @@ -1,17 +1,18 @@ #include "ctx.h" #include "node.h" -#include +#include -static void print(struct ivy_ast_node *node) +static void to_string(struct ivy_ast_node *node, b_string *str) { struct ivy_ast_double_node *v = (struct ivy_ast_double_node *)node; - printf("%s (%.2lf)\n", ivy_ast_node_type_to_string(node->n_type), + b_string_append_cstr(str, "%s (%.2lf)", ivy_ast_node_type_to_string(node->n_type), v->n_value->t_double); } struct ast_node_type double_node_ops = { + .n_to_string = to_string, .n_state_size = sizeof(struct parser_state), .n_node_size = sizeof(struct ivy_ast_double_node), }; diff --git a/lang/ast/ident.c b/lang/ast/ident.c index a51335b..2ed12c3 100644 --- a/lang/ast/ident.c +++ b/lang/ast/ident.c @@ -1,18 +1,18 @@ #include "ctx.h" #include "node.h" -#include +#include -static void print(struct ivy_ast_node *node) +static void to_string(struct ivy_ast_node *node, b_string *str) { struct ivy_ast_ident_node *ident = (struct ivy_ast_ident_node *)node; - printf("%s (%s)\n", ivy_ast_node_type_to_string(node->n_type), + b_string_append_cstrf(str, "%s (%s)", ivy_ast_node_type_to_string(node->n_type), ident->n_content->t_str); } struct ast_node_type ident_node_ops = { - .n_print = print, + .n_to_string = to_string, .n_state_size = sizeof(struct parser_state), .n_node_size = sizeof(struct ivy_ast_ident_node), }; diff --git a/lang/ast/int.c b/lang/ast/int.c index 76c978f..3849d35 100644 --- a/lang/ast/int.c +++ b/lang/ast/int.c @@ -1,18 +1,18 @@ #include "ctx.h" #include "node.h" -#include +#include -static void print(struct ivy_ast_node *node) +static void to_string(struct ivy_ast_node *node, b_string *str) { struct ivy_ast_int_node *v = (struct ivy_ast_int_node *)node; - printf("%s (%llu)\n", ivy_ast_node_type_to_string(node->n_type), + b_string_append_cstrf(str, "%s (%llu)", ivy_ast_node_type_to_string(node->n_type), v->n_value->t_int); } struct ast_node_type int_node_ops = { - .n_print = print, + .n_to_string = to_string, .n_state_size = sizeof(struct parser_state), .n_node_size = sizeof(struct ivy_ast_int_node), }; diff --git a/lang/ast/node.c b/lang/ast/node.c index b908349..6b5c346 100644 --- a/lang/ast/node.c +++ b/lang/ast/node.c @@ -192,27 +192,19 @@ struct ivy_ast_node *ast_node_create(enum ivy_ast_node_type type) return node; } -static enum ivy_status node_print( - struct ivy_ast_node *node, struct ivy_ast_node_iterator *it) + +void ivy_ast_node_to_string(struct ivy_ast_node *node, struct b_string *out) { - for (unsigned int i = 0; i < node->n_it.it_depth; i++) { - fputs(" ", stdout); + const struct ast_node_type *type_info = get_ast_node_type(node->n_type); + if (!type_info) { + return; } - const struct ast_node_type *type = get_ast_node_type(node->n_type); - if (type && type->n_print) { - type->n_print(node); + if (type_info->n_to_string) { + type_info->n_to_string(node, out); } else { - printf("%s\n", ivy_ast_node_type_to_string(node->n_type)); + b_string_append_cstr(out, ivy_ast_node_type_to_string(node->n_type)); } - - return IVY_OK; -} - -void ivy_ast_node_print(struct ivy_ast_node *node) -{ - struct ivy_ast_node_iterator it = {0}; - ivy_ast_node_iterate(node, &it, node_print); } void ivy_ast_node_destroy(struct ivy_ast_node *node) diff --git a/lang/ast/node.h b/lang/ast/node.h index abd98b4..2571cd0 100644 --- a/lang/ast/node.h +++ b/lang/ast/node.h @@ -45,11 +45,11 @@ typedef struct token_parse_result (*token_parse_function)( struct ast_node_type { enum ivy_status (*n_add_child)( - struct parser_state*, struct ivy_ast_node*); - void (*n_print)(struct ivy_ast_node*); - void (*n_init_state)(struct ivy_parser*, struct parser_state*, uintptr_t); + struct parser_state*, struct ivy_ast_node *); + void (*n_to_string)(struct ivy_ast_node *, struct b_string *); + void (*n_init_state)(struct ivy_parser *, struct parser_state *, uintptr_t); void (*n_collect_children)( - struct ivy_ast_node*, struct ivy_ast_node_iterator*); + struct ivy_ast_node*, struct ivy_ast_node_iterator *); size_t n_state_size; size_t n_node_size; diff --git a/lang/ast/op.c b/lang/ast/op.c index d78ec5b..f1b487c 100644 --- a/lang/ast/op.c +++ b/lang/ast/op.c @@ -1,13 +1,13 @@ #include "ctx.h" #include "node.h" #include "iterate.h" -#include +#include -static void print(struct ivy_ast_node *node) +static void to_string(struct ivy_ast_node *node, b_string *str) { struct ivy_ast_op_node *op = (struct ivy_ast_op_node *)node; - printf("%s (%s)\n", ivy_ast_node_type_to_string(node->n_type), ivy_operator_id_to_string(op->n_op->op_id)); + b_string_append_cstrf(str, "%s (%s)", ivy_ast_node_type_to_string(node->n_type), ivy_operator_id_to_string(op->n_op->op_id)); } static void collect_children( @@ -25,7 +25,7 @@ static void collect_children( } struct ast_node_type op_node_ops = { - .n_print = print, + .n_to_string = to_string, .n_collect_children = collect_children, .n_state_size = sizeof(struct parser_state), .n_node_size = sizeof(struct ivy_ast_op_node), diff --git a/lang/ast/property.c b/lang/ast/property.c index 90cdc95..1b3afb3 100644 --- a/lang/ast/property.c +++ b/lang/ast/property.c @@ -349,29 +349,29 @@ static void collect_children( } } -static void print(struct ivy_ast_node *node) +static void to_string(struct ivy_ast_node *node, b_string *str) { struct ivy_ast_property_node *prop = (struct ivy_ast_property_node *)node; - printf("%s (%s) [", ivy_ast_node_type_to_string(node->n_type), prop->n_ident->t_str); + b_string_append_cstrf(str, "%s (%s) [", ivy_ast_node_type_to_string(node->n_type), prop->n_ident->t_str); if (prop->n_flags & IVY_AST_PROPERTY_GET) { - puts("get"); + b_string_append_cstr(str, "get"); } if ((prop->n_flags & IVY_AST_PROPERTY_GET) && prop->n_flags & IVY_AST_PROPERTY_SET) { - puts(", "); + b_string_append_cstr(str, ", "); } if (prop->n_flags & IVY_AST_PROPERTY_SET) { - puts("set"); + b_string_append_cstr(str, "set"); } - puts("]\n"); + b_string_append_cstr(str, "]"); } struct ast_node_type property_node_ops = { - .n_print = print, + .n_to_string = to_string, .n_add_child = add_child, .n_init_state = init_state, .n_collect_children = collect_children, diff --git a/lang/ast/selector.c b/lang/ast/selector.c index 596d56d..d7dff99 100644 --- a/lang/ast/selector.c +++ b/lang/ast/selector.c @@ -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 = { diff --git a/lang/ast/string.c b/lang/ast/string.c index 0151465..e1910f4 100644 --- a/lang/ast/string.c +++ b/lang/ast/string.c @@ -1,6 +1,13 @@ #include "ctx.h" #include "expr/expr.h" #include "node.h" +#include + +static void to_string(struct ivy_ast_node *node, b_string *str) +{ + struct ivy_ast_string_node *s = (struct ivy_ast_string_node *)node; + b_string_append_cstrf(str, "%s (\"%s\")", ivy_ast_node_type_to_string(node->n_type), s->n_value->t_str); +} struct token_parse_result parse_string( struct ivy_parser *ctx, struct ivy_token *tok) @@ -63,6 +70,7 @@ 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), }; diff --git a/lang/ast/unit-import.c b/lang/ast/unit-import.c index 6452131..5eab680 100644 --- a/lang/ast/unit-import.c +++ b/lang/ast/unit-import.c @@ -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), diff --git a/lang/ast/unit-package.c b/lang/ast/unit-package.c index 1e4ba79..314807e 100644 --- a/lang/ast/unit-package.c +++ b/lang/ast/unit-package.c @@ -57,11 +57,11 @@ 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_package_node *unit_package = (struct ivy_ast_unit_package_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_package->n_ident) { @@ -75,9 +75,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 +86,7 @@ static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_ } struct ast_node_type unit_package_node_ops = { - .n_print = print, + .n_to_string = to_string, .n_init_state = init_state, .n_state_size = sizeof(struct unit_package_parser_state), .n_node_size = sizeof(struct ivy_ast_unit_package_node), diff --git a/lang/include/ivy/lang/ast.h b/lang/include/ivy/lang/ast.h index 4904d59..ecc6f5b 100644 --- a/lang/include/ivy/lang/ast.h +++ b/lang/include/ivy/lang/ast.h @@ -9,6 +9,7 @@ struct ivy_token; struct ivy_parser; +struct b_string; enum ivy_ast_node_type { IVY_AST_NONE = 0x00, @@ -293,7 +294,7 @@ IVY_API enum ivy_status ivy_parser_push_token( IVY_API enum ivy_status ivy_ast_node_iterate( struct ivy_ast_node *node, struct ivy_ast_node_iterator *it, ivy_ast_node_iteration_callback callback); -IVY_API void ivy_ast_node_print(struct ivy_ast_node *node); +IVY_API void ivy_ast_node_to_string(struct ivy_ast_node *node, struct b_string *out); IVY_API void ivy_ast_node_destroy(struct ivy_ast_node *node); IVY_API const char *ivy_ast_node_type_to_string(enum ivy_ast_node_type v);