compile: fix memory leaks

This commit is contained in:
2026-05-30 19:52:24 +01:00
parent a30e76e525
commit 26b368a512
5 changed files with 56 additions and 6 deletions
+10
View File
@@ -32,6 +32,16 @@ enum bshell_status bshell_compile(
} }
status = compile_resolve_labels(&ctx); 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) { if (status != BSHELL_SUCCESS) {
return status; return status;
} }
+1
View File
@@ -72,5 +72,6 @@ enum bshell_status compile_expression(
compile_value_destroy(result); compile_value_destroy(result);
} }
compile_pop_state(ctx);
return BSHELL_SUCCESS; return BSHELL_SUCCESS;
} }
+39
View File
@@ -0,0 +1,39 @@
#include <bshell/ast.h>
#include <bshell/compile.h>
#include <bshell/parse/token.h>
#include <bshell/status.h>
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;
}
+2 -6
View File
@@ -14,8 +14,8 @@ struct compile_state *compile_push_state(
return NULL; return NULL;
} }
const struct compile_state_type *state_type = state_types const struct compile_state_type *state_type
[state_type_id]; = state_types[state_type_id];
if (!state_type) { if (!state_type) {
return NULL; return NULL;
} }
@@ -43,10 +43,6 @@ void compile_pop_state(struct compile_ctx *ctx)
return; return;
} }
if (!fx_queue_prev(&state->s_entry)) {
return;
}
if (state->s_type->s_compile_end) { if (state->s_type->s_compile_end) {
state->s_type->s_compile_end(ctx); state->s_type->s_compile_end(ctx);
} }
+4
View File
@@ -1,6 +1,7 @@
#ifndef BSHELL_COMPILE_H_ #ifndef BSHELL_COMPILE_H_
#define BSHELL_COMPILE_H_ #define BSHELL_COMPILE_H_
#include <bshell/command/function.h>
#include <bshell/runtime/script-block.h> #include <bshell/runtime/script-block.h>
struct bshell_ast_node; struct bshell_ast_node;
@@ -8,5 +9,8 @@ struct bshell_ast_node;
extern enum bshell_status bshell_compile( extern enum bshell_status bshell_compile(
struct bshell_ast_node *src, struct bshell_ast_node *src,
bshell_scriptblock *dest); bshell_scriptblock *dest);
extern enum bshell_status bshell_compile_function(
struct bshell_ast_node *src,
bshell_function **out);
#endif #endif