ast: implement cleanup callbacks

This commit is contained in:
2026-05-30 19:50:13 +01:00
parent f2e1d89313
commit fec3be2140
8 changed files with 82 additions and 33 deletions
+8 -18
View File
@@ -154,10 +154,8 @@ struct bshell_ast_node *bshell_ast_iterator_dequeue(
return NULL; return NULL;
} }
struct bshell_ast_node *node = fx_unbox( struct bshell_ast_node *node
struct bshell_ast_node, = fx_unbox(struct bshell_ast_node, cur, n_it.e_entry);
cur,
n_it.e_entry);
const struct bshell_ast_node_definition *def const struct bshell_ast_node_definition *def
= bshell_ast_node_definitions[node->n_type]; = bshell_ast_node_definitions[node->n_type];
@@ -178,10 +176,8 @@ void bshell_ast_iterator_enqueue(
fx_queue_entry *cur = fx_queue_first(&it->it_queue); fx_queue_entry *cur = fx_queue_first(&it->it_queue);
if (cur) { if (cur) {
struct bshell_ast_node *cur_node = fx_unbox( struct bshell_ast_node *cur_node
struct bshell_ast_node, = fx_unbox(struct bshell_ast_node, cur, n_it.e_entry);
cur,
n_it.e_entry);
new_depth = cur_node->n_it.e_depth + 1; new_depth = cur_node->n_it.e_depth + 1;
} }
@@ -222,11 +218,8 @@ enum bshell_status bshell_ast_node_iterate(
return BSHELL_ERR_INTERNAL_FAILURE; return BSHELL_ERR_INTERNAL_FAILURE;
} }
struct bshell_ast_iterate_result result = callback( struct bshell_ast_iterate_result result
node, = callback(node, BSHELL_AST_ITERATION_PRE, it, arg);
BSHELL_AST_ITERATION_PRE,
it,
arg);
if (result.r_status != BSHELL_SUCCESS if (result.r_status != BSHELL_SUCCESS
|| result.r_flags & BSHELL_AST_ITERATE_STOP) { || result.r_flags & BSHELL_AST_ITERATE_STOP) {
return result.r_status; return result.r_status;
@@ -258,11 +251,8 @@ enum bshell_status bshell_ast_node_iterate(
return BSHELL_ERR_INTERNAL_FAILURE; return BSHELL_ERR_INTERNAL_FAILURE;
} }
struct bshell_ast_iterate_result result = callback( struct bshell_ast_iterate_result result
node, = callback(node, BSHELL_AST_ITERATION_POST, it, arg);
BSHELL_AST_ITERATION_POST,
it,
arg);
if (result.r_status != BSHELL_SUCCESS if (result.r_status != BSHELL_SUCCESS
|| result.r_flags & BSHELL_AST_ITERATE_STOP) { || result.r_flags & BSHELL_AST_ITERATE_STOP) {
return result.r_status; return result.r_status;
+11 -2
View File
@@ -4,13 +4,22 @@
static void to_string(const struct bshell_ast_node *node, fx_bstr *out) static void to_string(const struct bshell_ast_node *node, fx_bstr *out)
{ {
struct bshell_double_ast_node *i = (struct bshell_double_ast_node *) struct bshell_double_ast_node *i
node; = (struct bshell_double_ast_node *)node;
fx_value_to_string(&i->n_value->tok_number, (fx_stream *)out, NULL); fx_value_to_string(&i->n_value->tok_number, (fx_stream *)out, NULL);
} }
static enum bshell_status cleanup(struct bshell_ast_node *node)
{
struct bshell_double_ast_node *d
= (struct bshell_double_ast_node *)node;
bshell_lex_token_destroy(d->n_value);
return BSHELL_SUCCESS;
}
struct bshell_ast_node_definition double_bshell_ast_node = { struct bshell_ast_node_definition double_bshell_ast_node = {
.def_id = BSHELL_AST_DOUBLE, .def_id = BSHELL_AST_DOUBLE,
.def_node_size = sizeof(struct bshell_double_ast_node), .def_node_size = sizeof(struct bshell_double_ast_node),
.def_to_string = to_string, .def_to_string = to_string,
.def_cleanup = cleanup,
}; };
+12 -6
View File
@@ -9,10 +9,8 @@ static enum bshell_status collect_children(
fx_queue_entry *cur = fx_queue_first(&func->n_params); fx_queue_entry *cur = fx_queue_first(&func->n_params);
while (cur) { while (cur) {
struct bshell_ast_node *child = fx_unbox( struct bshell_ast_node *child
struct bshell_ast_node, = fx_unbox(struct bshell_ast_node, cur, n_entry);
cur,
n_entry);
bshell_ast_iterator_enqueue(it, child); bshell_ast_iterator_enqueue(it, child);
cur = fx_queue_next(cur); cur = fx_queue_next(cur);
} }
@@ -26,14 +24,22 @@ static enum bshell_status collect_children(
static void to_string(const struct bshell_ast_node *node, fx_bstr *out) static void to_string(const struct bshell_ast_node *node, fx_bstr *out)
{ {
const struct bshell_func_ast_node *func = (const struct const struct bshell_func_ast_node *func
bshell_func_ast_node *)node; = (const struct bshell_func_ast_node *)node;
fx_bstr_write_fmt(out, NULL, "%s", func->n_name->tok_str); fx_bstr_write_fmt(out, NULL, "%s", func->n_name->tok_str);
} }
static enum bshell_status cleanup(struct bshell_ast_node *node)
{
struct bshell_func_ast_node *d = (struct bshell_func_ast_node *)node;
bshell_lex_token_destroy(d->n_name);
return BSHELL_SUCCESS;
}
struct bshell_ast_node_definition func_bshell_ast_node = { struct bshell_ast_node_definition func_bshell_ast_node = {
.def_id = BSHELL_AST_FUNC, .def_id = BSHELL_AST_FUNC,
.def_node_size = sizeof(struct bshell_func_ast_node), .def_node_size = sizeof(struct bshell_func_ast_node),
.def_collect_children = collect_children, .def_collect_children = collect_children,
.def_to_string = to_string, .def_to_string = to_string,
.def_cleanup = cleanup,
}; };
+8
View File
@@ -8,8 +8,16 @@ static void to_string(const struct bshell_ast_node *node, fx_bstr *out)
fx_value_to_string(&i->n_value->tok_number, (fx_stream *)out, NULL); fx_value_to_string(&i->n_value->tok_number, (fx_stream *)out, NULL);
} }
static enum bshell_status cleanup(struct bshell_ast_node *node)
{
struct bshell_int_ast_node *i = (struct bshell_int_ast_node *)node;
bshell_lex_token_destroy(i->n_value);
return BSHELL_SUCCESS;
}
struct bshell_ast_node_definition int_bshell_ast_node = { struct bshell_ast_node_definition int_bshell_ast_node = {
.def_id = BSHELL_AST_INT, .def_id = BSHELL_AST_INT,
.def_node_size = sizeof(struct bshell_int_ast_node), .def_node_size = sizeof(struct bshell_int_ast_node),
.def_to_string = to_string, .def_to_string = to_string,
.def_cleanup = cleanup,
}; };
+12
View File
@@ -1,4 +1,5 @@
#include <bshell/ast.h> #include <bshell/ast.h>
#include <bshell/parse/lex.h>
static enum bshell_status collect_children( static enum bshell_status collect_children(
struct bshell_ast_node *node, struct bshell_ast_node *node,
@@ -41,9 +42,20 @@ static void to_string(const struct bshell_ast_node *node, fx_bstr *out)
} }
} }
static enum bshell_status cleanup(struct bshell_ast_node *node)
{
struct bshell_redirection_ast_node *v
= (struct bshell_redirection_ast_node *)node;
if (v->n_out_tok) {
bshell_lex_token_destroy(v->n_out_tok);
}
return BSHELL_SUCCESS;
}
struct bshell_ast_node_definition redirection_bshell_ast_node = { struct bshell_ast_node_definition redirection_bshell_ast_node = {
.def_id = BSHELL_AST_REDIRECTION, .def_id = BSHELL_AST_REDIRECTION,
.def_node_size = sizeof(struct bshell_redirection_ast_node), .def_node_size = sizeof(struct bshell_redirection_ast_node),
.def_collect_children = collect_children, .def_collect_children = collect_children,
.def_to_string = to_string, .def_to_string = to_string,
.def_cleanup = cleanup,
}; };
+11 -3
View File
@@ -3,14 +3,22 @@
static void to_string(const struct bshell_ast_node *node, fx_bstr *out) static void to_string(const struct bshell_ast_node *node, fx_bstr *out)
{ {
const struct bshell_string_ast_node *string = (const struct const struct bshell_string_ast_node *string
bshell_string_ast_node *) = (const struct bshell_string_ast_node *)node;
node;
fx_bstr_write_fmt(out, NULL, "%s", string->n_value->tok_str); fx_bstr_write_fmt(out, NULL, "%s", string->n_value->tok_str);
} }
static enum bshell_status cleanup(struct bshell_ast_node *node)
{
struct bshell_string_ast_node *string
= (struct bshell_string_ast_node *)node;
bshell_lex_token_destroy(string->n_value);
return BSHELL_SUCCESS;
}
struct bshell_ast_node_definition string_bshell_ast_node = { struct bshell_ast_node_definition string_bshell_ast_node = {
.def_id = BSHELL_AST_STRING, .def_id = BSHELL_AST_STRING,
.def_node_size = sizeof(struct bshell_string_ast_node), .def_node_size = sizeof(struct bshell_string_ast_node),
.def_to_string = to_string, .def_to_string = to_string,
.def_cleanup = cleanup,
}; };
+10 -2
View File
@@ -3,13 +3,21 @@
static void to_string(const struct bshell_ast_node *node, fx_bstr *out) static void to_string(const struct bshell_ast_node *node, fx_bstr *out)
{ {
const struct bshell_var_ast_node *var = (const struct const struct bshell_var_ast_node *var
bshell_var_ast_node *)node; = (const struct bshell_var_ast_node *)node;
fx_bstr_write_fmt(out, NULL, "%s", var->n_ident->tok_str); fx_bstr_write_fmt(out, NULL, "%s", var->n_ident->tok_str);
} }
static enum bshell_status cleanup(struct bshell_ast_node *node)
{
struct bshell_var_ast_node *v = (struct bshell_var_ast_node *)node;
bshell_lex_token_destroy(v->n_ident);
return BSHELL_SUCCESS;
}
struct bshell_ast_node_definition var_bshell_ast_node = { struct bshell_ast_node_definition var_bshell_ast_node = {
.def_id = BSHELL_AST_VAR, .def_id = BSHELL_AST_VAR,
.def_node_size = sizeof(struct bshell_var_ast_node), .def_node_size = sizeof(struct bshell_var_ast_node),
.def_to_string = to_string, .def_to_string = to_string,
.def_cleanup = cleanup,
}; };
+10 -2
View File
@@ -3,13 +3,21 @@
static void to_string(const struct bshell_ast_node *node, fx_bstr *out) static void to_string(const struct bshell_ast_node *node, fx_bstr *out)
{ {
const struct bshell_word_ast_node *word = (const struct const struct bshell_word_ast_node *word
bshell_word_ast_node *)node; = (const struct bshell_word_ast_node *)node;
fx_bstr_write_fmt(out, NULL, "%s", word->n_value->tok_str); fx_bstr_write_fmt(out, NULL, "%s", word->n_value->tok_str);
} }
static enum bshell_status cleanup(struct bshell_ast_node *node)
{
struct bshell_word_ast_node *w = (struct bshell_word_ast_node *)node;
bshell_lex_token_destroy(w->n_value);
return BSHELL_SUCCESS;
}
struct bshell_ast_node_definition word_bshell_ast_node = { struct bshell_ast_node_definition word_bshell_ast_node = {
.def_id = BSHELL_AST_WORD, .def_id = BSHELL_AST_WORD,
.def_node_size = sizeof(struct bshell_word_ast_node), .def_node_size = sizeof(struct bshell_word_ast_node),
.def_to_string = to_string, .def_to_string = to_string,
.def_cleanup = cleanup,
}; };