From 26b368a512051ace2986d082ce2698c37db7abee Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sat, 30 May 2026 19:52:24 +0100 Subject: [PATCH] compile: fix memory leaks --- bshell.runtime/compile/compile.c | 10 +++++++ bshell.runtime/compile/expression.c | 1 + bshell.runtime/compile/function.c | 39 +++++++++++++++++++++++++ bshell.runtime/compile/state.c | 8 ++--- bshell.runtime/include/bshell/compile.h | 4 +++ 5 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 bshell.runtime/compile/function.c diff --git a/bshell.runtime/compile/compile.c b/bshell.runtime/compile/compile.c index 9a8e219..808a679 100644 --- a/bshell.runtime/compile/compile.c +++ b/bshell.runtime/compile/compile.c @@ -32,6 +32,16 @@ enum bshell_status bshell_compile( } status = compile_resolve_labels(&ctx); + + while (!fx_queue_empty(&ctx.c_state)) { + compile_pop_state(&ctx); + } + + while (!fx_queue_empty(&ctx.c_stack)) { + struct compile_value *value = compile_pop_value(&ctx); + compile_value_destroy(value); + } + if (status != BSHELL_SUCCESS) { return status; } diff --git a/bshell.runtime/compile/expression.c b/bshell.runtime/compile/expression.c index cbcc7f3..e405806 100644 --- a/bshell.runtime/compile/expression.c +++ b/bshell.runtime/compile/expression.c @@ -72,5 +72,6 @@ enum bshell_status compile_expression( compile_value_destroy(result); } + compile_pop_state(ctx); return BSHELL_SUCCESS; } diff --git a/bshell.runtime/compile/function.c b/bshell.runtime/compile/function.c new file mode 100644 index 0000000..fc3483c --- /dev/null +++ b/bshell.runtime/compile/function.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +enum bshell_status bshell_compile_function( + struct bshell_ast_node *src, + bshell_function **out) +{ + struct bshell_func_ast_node *func_ast + = (struct bshell_func_ast_node *)src; + bshell_function *func + = bshell_function_create(func_ast->n_name->tok_str); + if (!func) { + return BSHELL_ERR_NO_MEMORY; + } + + fx_queue_entry *cur = fx_queue_first(&func_ast->n_params); + while (cur) { + struct bshell_var_ast_node *var_ast = fx_unbox( + struct bshell_var_ast_node, + cur, + n_base.n_entry); + bshell_function_add_positional_parameter( + func, + var_ast->n_ident->tok_str); + cur = fx_queue_next(cur); + } + + bshell_scriptblock *body = bshell_function_get_body(func); + enum bshell_status status = bshell_compile(func_ast->n_body, body); + if (status != BSHELL_SUCCESS) { + bshell_function_unref(func); + return status; + } + + *out = func; + return BSHELL_SUCCESS; +} diff --git a/bshell.runtime/compile/state.c b/bshell.runtime/compile/state.c index f40d07a..39ff112 100644 --- a/bshell.runtime/compile/state.c +++ b/bshell.runtime/compile/state.c @@ -14,8 +14,8 @@ struct compile_state *compile_push_state( return NULL; } - const struct compile_state_type *state_type = state_types - [state_type_id]; + const struct compile_state_type *state_type + = state_types[state_type_id]; if (!state_type) { return NULL; } @@ -43,10 +43,6 @@ void compile_pop_state(struct compile_ctx *ctx) return; } - if (!fx_queue_prev(&state->s_entry)) { - return; - } - if (state->s_type->s_compile_end) { state->s_type->s_compile_end(ctx); } diff --git a/bshell.runtime/include/bshell/compile.h b/bshell.runtime/include/bshell/compile.h index 4c20b65..d60dfe7 100644 --- a/bshell.runtime/include/bshell/compile.h +++ b/bshell.runtime/include/bshell/compile.h @@ -1,6 +1,7 @@ #ifndef BSHELL_COMPILE_H_ #define BSHELL_COMPILE_H_ +#include #include struct bshell_ast_node; @@ -8,5 +9,8 @@ struct bshell_ast_node; extern enum bshell_status bshell_compile( struct bshell_ast_node *src, bshell_scriptblock *dest); +extern enum bshell_status bshell_compile_function( + struct bshell_ast_node *src, + bshell_function **out); #endif