all repos — hex @ ce0744f885a7d17bbef5ff428e3964be81071917

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

Improved debug messages.
h3rald h3rald@h3rald.com
Fri, 20 Dec 2024 13:31:37 +0100
commit

ce0744f885a7d17bbef5ff428e3964be81071917

parent

e5cbcde17790686e02e40daa8fb0039a939fcb5b

4 files changed, 57 insertions(+), 51 deletions(-)

jump to
M src/hex.hsrc/hex.h

@@ -294,6 +294,7 @@ void hex_rpad(const char *str, int total_length);

void hex_lpad(const char *str, int total_length); void hex_encode_length(uint8_t **bytecode, size_t *size, size_t length); int hex_is_binary(const uint8_t *data, size_t size); +void hex_print_string(FILE *stream, char *value); char *hex_bytes_to_string(const uint8_t *bytes, size_t size); char *hex_process_string(const char *value); size_t hex_min_bytes_to_encode_integer(int32_t value);
M src/symboltable.csrc/symboltable.c

@@ -59,7 +59,6 @@ table->count = 0;

for (size_t i = 0; i < total; i++) { - hex_debug(ctx, "Decoding symbol %zu", i); size_t len = (size_t)(*bytecode)[0]; (*bytecode)++; *size -= 1;
M src/utils.csrc/utils.c

@@ -171,6 +171,45 @@ }

return 0; } +void hex_print_string(FILE *stream, char *value) +{ + fprintf(stream, "\""); + for (char *c = value; *c != '\0'; c++) + { + switch (*c) + { + case '\n': + fprintf(stream, "\\n"); + break; + case '\t': + fprintf(stream, "\\t"); + break; + case '\r': + fprintf(stream, "\\r"); + break; + case '\b': + fprintf(stream, "\\b"); + break; + case '\f': + fprintf(stream, "\\f"); + break; + case '\v': + fprintf(stream, "\\v"); + break; + case '\\': + fprintf(stream, "\\\\"); + break; + case '\"': + fprintf(stream, "\\\""); + break; + default: + fputc(*c, stream); + break; + } + } + fprintf(stream, "\""); +} + void hex_print_item(FILE *stream, hex_item_t item) { switch (item.type)

@@ -180,41 +219,7 @@ fprintf(stream, "0x%x", item.data.int_value);

break; case HEX_TYPE_STRING: - fprintf(stream, "\""); - for (char *c = item.data.str_value; *c != '\0'; c++) - { - switch (*c) - { - case '\n': - fprintf(stream, "\\n"); - break; - case '\t': - fprintf(stream, "\\t"); - break; - case '\r': - fprintf(stream, "\\r"); - break; - case '\b': - fprintf(stream, "\\b"); - break; - case '\f': - fprintf(stream, "\\f"); - break; - case '\v': - fprintf(stream, "\\v"); - break; - case '\\': - fprintf(stream, "\\\\"); - break; - case '\"': - fprintf(stream, "\\\""); - break; - default: - fputc(*c, stream); - break; - } - } - fprintf(stream, "\""); + hex_print_string(stream, item.data.str_value); break; case HEX_TYPE_USER_SYMBOL:
M src/vm.csrc/vm.c

@@ -8,7 +8,7 @@ ////////////////////////////////////////

int hex_bytecode_integer(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t *capacity, int32_t value) { - hex_debug(ctx, "PUSHIN: %d", value); + hex_debug(ctx, "PUSHIN[01]: 0x%X", value); // Check if we need to resize the buffer (size + int32_t size + opcode (1) + max encoded length (4)) if (*size + sizeof(int32_t) + 1 + 4 > *capacity) {

@@ -64,7 +64,7 @@ {

hex_error(ctx, "[add bytecode string] Memory allocation failed"); return 1; } - hex_debug(ctx, "PUSHST: \"%s\"", str); + hex_debug(ctx, "PUSHST[02]: \"%s\"", str); size_t len = strlen(value); // Check if we need to resize the buffer (size + strlen + opcode (1) + max encoded length (4)) if (*size + len + 1 + 4 > *capacity)

@@ -100,7 +100,6 @@ int hex_bytecode_symbol(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t *capacity, const char *value)

{ if (hex_valid_native_symbol(ctx, value)) { - hex_debug(ctx, "NATSYM: %s", value); // Check if we need to resize the buffer (size + opcode (1)) if (*size + 1 > *capacity) {

@@ -113,7 +112,9 @@ return 1;

} *bytecode = new_bytecode; } - (*bytecode)[*size] = hex_symbol_to_opcode(value); + uint8_t opcode = hex_symbol_to_opcode(value); + hex_debug(ctx, "NATSYM[%02X]: %s", opcode, value); + (*bytecode)[*size] = opcode; *size += 1; // opcode } else

@@ -121,7 +122,7 @@ {

// Add to symbol table hex_symboltable_set(ctx, value); int index = hex_symboltable_get_index(ctx, value); - hex_debug(ctx, "LOOKUP: #%d -> %s", index, value); + hex_debug(ctx, "LOOKUP[00]: %02X -> %s", index, value); // Check if we need to resize the buffer (size + 1 opcode + 2 max index) if (*size + 1 + 2 > *capacity) {

@@ -163,7 +164,7 @@ *size += 1; // opcode

hex_encode_length(bytecode, size, *n_items); memcpy(&(*bytecode)[*size], *output, *output_size); *size += *output_size; - hex_debug(ctx, "PUSHQT: <end> (items: %d)", *n_items); + hex_debug(ctx, "PUSHQT[03]: <end> (items: %d)", *n_items); return 0; }

@@ -199,7 +200,7 @@ {

size_t n_items = 0; uint8_t *quotation_bytecode = NULL; size_t quotation_size = 0; - hex_debug(ctx, "PUSHQT: <start>"); + hex_debug(ctx, "PUSHQT[03]: <start>"); if (hex_generate_quotation_bytecode(ctx, &input, &quotation_bytecode, &quotation_size, &n_items, position) != 0) { hex_error(ctx, "[generate quotation bytecode] Failed to generate quotation bytecode (main)");

@@ -257,7 +258,7 @@ {

size_t n_items = 0; uint8_t *quotation_bytecode = NULL; size_t quotation_size = 0; - hex_debug(ctx, "PUSHQT: <start>"); + hex_debug(ctx, "PUSHQT[03]: <start>"); if (hex_generate_quotation_bytecode(ctx, input, &quotation_bytecode, &quotation_size, &n_items, position) != 0) { hex_error(ctx, "[generate quotation bytecode] Failed to generate quotation bytecode");

@@ -335,7 +336,7 @@

*bytecode += length; *size -= length; - hex_debug(ctx, ">> PUSHIN: 0x%X", value); + hex_debug(ctx, ">> PUSHIN[01]: 0x%X", value); hex_item_t item = hex_integer_item(ctx, value); *result = item; return 0;

@@ -385,7 +386,7 @@ {

hex_error(ctx, "[interpret bytecode string] Memory allocation failed"); return 1; } - hex_debug(ctx, ">> PUSHST: \"%s\"", str); + hex_debug(ctx, ">> PUSHST[02]: \"%s\"", str); return 0; }

@@ -418,7 +419,7 @@ hex_error(ctx, "(%d,%d) Unable to reference native symbol: %s (bytecode)", token->position.line, token->position.column, token->value);

hex_free_token(token); return 1; } - hex_debug(ctx, ">> NATSYM: 0x%02X (%s)", opcode, symbol); + hex_debug(ctx, ">> NATSYM[%02X]: %s", opcode, symbol); *result = item; return 0; }

@@ -463,7 +464,7 @@ hex_item_t item;

item.type = HEX_TYPE_USER_SYMBOL; item.token = token; - hex_debug(ctx, ">> LOOKUP: #%zu -> %s", index, value); + hex_debug(ctx, ">> LOOKUP[00]: %02X -> %s", index, value); *result = item; return 0; }

@@ -487,7 +488,7 @@ (*bytecode)++;

(*size)--; } while (**bytecode & 0x80); - hex_debug(ctx, ">> PUSHQT: <start> (items: %zu)", n_items); + hex_debug(ctx, ">> PUSHQT[03]: <start> (items: %zu)", n_items); hex_item_t **items = (hex_item_t **)malloc(n_items * sizeof(hex_item_t)); if (!items)

@@ -548,7 +549,7 @@ result->type = HEX_TYPE_QUOTATION;

result->data.quotation_value = items; result->quotation_size = n_items; - hex_debug(ctx, ">> PUSHQT: <end> (items: %zu)", n_items); + hex_debug(ctx, ">> PUSHQT[03]: <end> (items: %zu)", n_items); return 0; }

@@ -564,7 +565,7 @@ return 1;

} memcpy(header, bytecode, 8); int symbol_table_size = hex_validate_header(header); - hex_debug(ctx, "hex executable file - version: %d - symbols: %d", header[4], symbol_table_size); + hex_debug(ctx, "[Hex Bytecode eXecutable File - version: %d - symbols: %d]", header[4], symbol_table_size); if (symbol_table_size < 0) { hex_error(ctx, "[interpret bytecode header] Invalid bytecode header");