From 5f8462fe34f8c6979b0d305f611820245cc31cf7 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Thu, 10 Feb 2022 22:39:39 +0100 Subject: [PATCH] Add apfl_stackidx typedef This makes it clear that a value of that type is meant as an index into the stack. --- src/apfl.h | 4 +++- src/eval.c | 16 ++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/apfl.h b/src/apfl.h index c4ca0a0..f4974fb 100644 --- a/src/apfl.h +++ b/src/apfl.h @@ -565,6 +565,8 @@ struct apfl_expr apfl_parser_get_expr(apfl_parser_ptr); struct apfl_ctx_data; typedef struct apfl_ctx_data *apfl_ctx; +typedef int apfl_stackidx; + enum apfl_result { APFL_RESULT_OK, // Evaluation succeeded, value is on the stack @@ -581,7 +583,7 @@ void apfl_ctx_destroy(apfl_ctx); enum apfl_result apfl_eval(apfl_ctx, struct apfl_expr); -void apfl_debug_print_val(apfl_ctx, int, FILE *); +void apfl_debug_print_val(apfl_ctx, apfl_stackidx, FILE *); #ifdef __cplusplus } diff --git a/src/eval.c b/src/eval.c index bd2a266..55186cd 100644 --- a/src/eval.c +++ b/src/eval.c @@ -230,7 +230,7 @@ stack_push(apfl_ctx ctx, struct apfl_value value) } static bool -stack_check_index(apfl_ctx ctx, int *index) +stack_check_index(apfl_ctx ctx, apfl_stackidx *index) { if (*index < 0) { if ((size_t)-*index > ctx->stack_len) { @@ -247,7 +247,7 @@ stack_check_index(apfl_ctx ctx, int *index) } static bool -stack_pop(apfl_ctx ctx, struct apfl_value *value, int index) +stack_pop(apfl_ctx ctx, struct apfl_value *value, apfl_stackidx index) { if (!stack_check_index(ctx, &index)) { return false; @@ -271,7 +271,7 @@ stack_pop(apfl_ctx ctx, struct apfl_value *value, int index) } static struct apfl_value -stack_must_pop(apfl_ctx ctx, int index) +stack_must_pop(apfl_ctx ctx, apfl_stackidx index) { struct apfl_value value; assert(stack_pop(ctx, &value, index)); @@ -279,7 +279,7 @@ stack_must_pop(apfl_ctx ctx, int index) } static bool -stack_get(apfl_ctx ctx, struct apfl_value *value, int index) +stack_get(apfl_ctx ctx, struct apfl_value *value, apfl_stackidx index) { if (!stack_check_index(ctx, &index)) { return false; @@ -291,7 +291,7 @@ stack_get(apfl_ctx ctx, struct apfl_value *value, int index) } static struct apfl_value -stack_must_get(apfl_ctx ctx, int index) +stack_must_get(apfl_ctx ctx, apfl_stackidx index) { struct apfl_value value; assert(stack_get(ctx, &value, index)); @@ -299,7 +299,7 @@ stack_must_get(apfl_ctx ctx, int index) } static bool -stack_drop(apfl_ctx ctx, int index) +stack_drop(apfl_ctx ctx, apfl_stackidx index) { struct apfl_value value; if (!stack_pop(ctx, &value, index)) { @@ -310,7 +310,7 @@ stack_drop(apfl_ctx ctx, int index) } static void -stack_must_drop(apfl_ctx ctx, int index) +stack_must_drop(apfl_ctx ctx, apfl_stackidx index) { assert(stack_drop(ctx, index)); } @@ -1420,7 +1420,7 @@ apfl_eval(apfl_ctx ctx, struct apfl_expr expr) } void -apfl_debug_print_val(apfl_ctx ctx, int index, FILE *f) +apfl_debug_print_val(apfl_ctx ctx, apfl_stackidx index, FILE *f) { struct apfl_value value; if (!stack_pop(ctx, &value, index)) {