From e5c4f32126f287e75fc42fa3bd6fe3862e6f36b5 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Fri, 24 Nov 2023 21:54:31 +0100 Subject: [PATCH] refactor --- src/context.c | 23 +++++++++++++++++++++++ src/context.h | 2 ++ src/eval.c | 29 +++-------------------------- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/context.c b/src/context.c index 61d54f2..13e3e26 100644 --- a/src/context.c +++ b/src/context.c @@ -2530,3 +2530,26 @@ apfl_cfunc_run_deferred(apfl_ctx ctx, struct call_stack_entry *cse) deferred->cb(ctx, deferred->opaque); } } + +bool +apfl_call_stack_try_push(apfl_ctx ctx, struct call_stack_entry cse) +{ + return apfl_resizable_append( + ctx->gc.allocator, + sizeof(struct call_stack_entry), + (void**)&ctx->call_stack.items, + &ctx->call_stack.len, + &ctx->call_stack.cap, + &cse, + 1 + ); +} + +void +apfl_call_stack_push(apfl_ctx ctx, struct call_stack_entry cse) +{ + if (!apfl_call_stack_try_push(ctx, cse)) { + apfl_call_stack_entry_deinit(ctx->gc.allocator, &cse); + apfl_raise_alloc_error(ctx); + } +} diff --git a/src/context.h b/src/context.h index 30d86b4..26f8155 100644 --- a/src/context.h +++ b/src/context.h @@ -209,6 +209,8 @@ struct apfl_string *apfl_to_dynamic_string(apfl_ctx ctx, apfl_stackidx index); noreturn void apfl_raise_errorfmt(apfl_ctx ctx, const char *fmt, ...); struct call_stack_entry *apfl_call_stack_cur_entry(apfl_ctx); +bool apfl_call_stack_try_push(apfl_ctx ctx, struct call_stack_entry cse); +void apfl_call_stack_push(apfl_ctx ctx, struct call_stack_entry cse); struct scope *apfl_closure_scope_for_func(apfl_ctx, struct scopes); diff --git a/src/eval.c b/src/eval.c index fb42065..cb08372 100644 --- a/src/eval.c +++ b/src/eval.c @@ -334,29 +334,6 @@ func_set_name(apfl_ctx ctx, struct apfl_string *name) apfl_func_set_name(value.func, name); } -static bool -try_call_stack_push(apfl_ctx ctx, struct call_stack_entry cse) -{ - return apfl_resizable_append( - ctx->gc.allocator, - sizeof(struct call_stack_entry), - (void**)&ctx->call_stack.items, - &ctx->call_stack.len, - &ctx->call_stack.cap, - &cse, - 1 - ); -} - -static void -call_stack_push(apfl_ctx ctx, struct call_stack_entry cse) -{ - if (!try_call_stack_push(ctx, cse)) { - apfl_call_stack_entry_deinit(ctx->gc.allocator, &cse); - apfl_raise_alloc_error(ctx); - } -} - static void call_stack_drop(apfl_ctx ctx) { @@ -400,7 +377,7 @@ return_from_function(apfl_ctx ctx) static void prepare_call(apfl_ctx ctx, size_t tmproots, struct apfl_value args, struct call_stack_entry cse) { - call_stack_push(ctx, cse); + apfl_call_stack_push(ctx, cse); // Note: This pushes args on the stack of the newly created call stack apfl_stack_must_push(ctx, args); @@ -822,7 +799,7 @@ matcher_init_matching_inner(apfl_ctx ctx, struct matcher *matcher, struct scopes matcher_cse.matcher_state_stack_len = 1; matcher_cse.matcher_state_stack_cap = 1; - if (!try_call_stack_push(ctx, (struct call_stack_entry) { + if (!apfl_call_stack_try_push(ctx, (struct call_stack_entry) { .type = APFL_CSE_MATCHER, .stack = apfl_stack_new(), .matcher = matcher_cse, @@ -1455,7 +1432,7 @@ iterative_runner_eval_expr_inner(apfl_iterative_runner runner, struct apfl_expr apfl_raise_error_object(ctx, error); } - call_stack_push(ctx, (struct call_stack_entry) { + apfl_call_stack_push(ctx, (struct call_stack_entry) { .type = APFL_CSE_FUNCTION, .stack = apfl_stack_new(), .func = (struct func_call_stack_entry) {