src/error.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
#ifndef HEX_H #include "hex.h" #endif //////////////////////////////////////// // Error & Debugging // //////////////////////////////////////// void hex_error(hex_context_t *ctx, const char *format, ...) { va_list args; va_start(args, format); vsnprintf(ctx->error, sizeof(ctx->error), format, args); if (ctx->settings.errors_enabled) /// FC { fprintf(stderr, "ERROR: "); fprintf(stderr, "%s\n", ctx->error); } va_end(args); } void hex_debug(hex_context_t *ctx, const char *format, ...) { if (ctx->settings.debugging_enabled) { va_list args; va_start(args, format); fprintf(stdout, "*** "); vfprintf(stdout, format, args); fprintf(stdout, "\n"); va_end(args); } } char *hex_type(hex_item_type_t type) { switch (type) { case HEX_TYPE_INTEGER: return "integer"; case HEX_TYPE_STRING: return "string"; case HEX_TYPE_QUOTATION: return "quotation"; case HEX_TYPE_NATIVE_SYMBOL: return "native-symbol"; case HEX_TYPE_USER_SYMBOL: return "user-symbol"; case HEX_TYPE_INVALID: return "invalid"; default: return "unknown"; } } void hex_debug_item(hex_context_t *ctx, const char *message, hex_item_t item) { if (ctx->settings.debugging_enabled) { fprintf(stdout, "*** %s: ", message); hex_print_item(stdout, item); fprintf(stdout, "\n"); } } //////////////////////////////////////// // Stack trace implementation // //////////////////////////////////////// // Add an entry to the circular stack trace void add_to_stack_trace(hex_context_t *ctx, hex_token_t *token) { int index = (ctx->stack_trace.start + ctx->stack_trace.size) % HEX_STACK_TRACE_SIZE; if (ctx->stack_trace.size < HEX_STACK_TRACE_SIZE) { // Buffer is not full; add item ctx->stack_trace.entries[index] = *token; ctx->stack_trace.size++; } else { // Buffer is full; overwrite the oldest item ctx->stack_trace.entries[index] = *token; ctx->stack_trace.start = (ctx->stack_trace.start + 1) % HEX_STACK_TRACE_SIZE; } } // Print the stack trace void print_stack_trace(hex_context_t *ctx) { if (!ctx->settings.stack_trace_enabled || !ctx->settings.errors_enabled || ctx->stack_trace.size <= 0) { return; } fprintf(stderr, "[stack trace] (most recent symbol first):\n"); for (size_t i = 0; i < ctx->stack_trace.size; i++) { 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); } } |