all repos — hex @ 8fc84ab1c3ad36b61d5bdf8121d5e965cc603958

A tiny, minimalist, slightly-esoteric concatenative programming lannguage.

Refactoring stack trace.
h3rald h3rald@h3rald.com
Wed, 27 Nov 2024 11:08:34 +0100
commit

8fc84ab1c3ad36b61d5bdf8121d5e965cc603958

parent

57cfc9998a910ea642ef67f86839e4adce972afd

2 files changed, 14 insertions(+), 16 deletions(-)

jump to
M hex.chex.c

@@ -197,7 +197,7 @@ }

else if (element.type == HEX_TYPE_NATIVE_SYMBOL) { hex_debug_element(ctx, "CALL", element); - add_to_stack_trace(element.token); + add_to_stack_trace(ctx, element.token); result = element.data.fnValue(ctx); } else

@@ -295,7 +295,7 @@ }

int hex_push_symbol(hex_context_t *ctx, hex_token_t *token) { - add_to_stack_trace(token); + add_to_stack_trace(ctx, token); hex_item_t value; if (hex_get_symbol(ctx, token->value, &value)) {

@@ -748,24 +748,22 @@ ////////////////////////////////////////

// Stack trace implementation // //////////////////////////////////////// -hex_stack_trace_t stackTrace = {.start = 0, .size = 0}; - // Add an entry to the circular stack trace -void add_to_stack_trace(hex_token_t *token) +void add_to_stack_trace(hex_context_t *ctx, hex_token_t *token) { - int index = (stackTrace.start + stackTrace.size) % HEX_STACK_TRACE_SIZE; + int index = (ctx->stack_trace.start + ctx->stack_trace.size) % HEX_STACK_TRACE_SIZE; - if (stackTrace.size < HEX_STACK_TRACE_SIZE) + if (ctx->stack_trace.size < HEX_STACK_TRACE_SIZE) { // Buffer is not full; add element - stackTrace.entries[index] = *token; - stackTrace.size++; + ctx->stack_trace.entries[index] = *token; + ctx->stack_trace.size++; } else { // Buffer is full; overwrite the oldest element - stackTrace.entries[index] = *token; - stackTrace.start = (stackTrace.start + 1) % HEX_STACK_TRACE_SIZE; + ctx->stack_trace.entries[index] = *token; + ctx->stack_trace.start = (ctx->stack_trace.start + 1) % HEX_STACK_TRACE_SIZE; } }

@@ -778,10 +776,10 @@ return;

} fprintf(stderr, "[stack trace] (most recent symbol first):\n"); - for (int i = 0; i < stackTrace.size; i++) + for (int i = 0; i < ctx->stack_trace.size; i++) { - int index = (stackTrace.start + stackTrace.size - 1 - i) % HEX_STACK_TRACE_SIZE; - hex_token_t token = stackTrace.entries[index]; + int index = (ctx->stack_trace.start + ctx->stack_trace.size - 1 - i) % HEX_STACK_TRACE_SIZE; + hex_token_t token = ctx->stack_trace.entries[index]; fprintf(stderr, " %s (%s:%d:%d)\n", token.value, token.position.filename, token.position.line, token.position.column); } }

@@ -3731,7 +3729,7 @@ }

if (token != NULL && token->type == HEX_TOKEN_INVALID) { token->position.filename = strdup(filename); - add_to_stack_trace(token); + add_to_stack_trace(ctx, token); print_stack_trace(ctx); return 1; }
M hex.hhex.h

@@ -136,7 +136,7 @@ void hex_error(hex_context_t *ctx, const char *format, ...);

void hex_debug(hex_context_t *ctx, char *format, ...); void hex_debug_element(hex_context_t *ctx, const char *message, hex_item_t element); void hex_print_element(FILE *stream, hex_item_t element); -void add_to_stack_trace(hex_token_t *token); +void add_to_stack_trace(hex_context_t *ctx, hex_token_t *token); char *hex_type(hex_item_type_t type); int hex_push(hex_context_t *ctx, hex_item_t element);