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

@@ -1,18 +1,18 @@
#include "ctx.h"
#include "node.h"
#include <stdio.h>
#include <blue/object/string.h>
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),
};

View File

@@ -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),

View File

@@ -1,17 +1,18 @@
#include "ctx.h"
#include "node.h"
#include <stdio.h>
#include <blue/object/string.h>
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),
};

View File

@@ -1,18 +1,18 @@
#include "ctx.h"
#include "node.h"
#include <stdio.h>
#include <blue/object/string.h>
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),
};

View File

@@ -1,18 +1,18 @@
#include "ctx.h"
#include "node.h"
#include <stdio.h>
#include <blue/object/string.h>
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),
};

View File

@@ -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)

View File

@@ -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;

View File

@@ -1,13 +1,13 @@
#include "ctx.h"
#include "node.h"
#include "iterate.h"
#include <stdio.h>
#include <blue/object/string.h>
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),

View File

@@ -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,

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 = {

View File

@@ -1,6 +1,13 @@
#include "ctx.h"
#include "expr/expr.h"
#include "node.h"
#include <blue/object/string.h>
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),
};

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),

View File

@@ -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),