all repos — hex @ 9cda0007e098d5a6930dac0650b69dff5a6ec9d7

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

Add symbol table copy functionality and update test scripts
h3rald h3rald@h3rald.com
Wed, 02 Apr 2025 11:04:20 +0200
commit

9cda0007e098d5a6930dac0650b69dff5a6ec9d7

parent

1a16cf41337cf5121d17867f9ac9319404d8fdf8

5 files changed, 46 insertions(+), 1 deletions(-)

jump to
M MakefileMakefile

@@ -31,6 +31,7 @@ rm -f hex.js

rm -f hex.wasm test: hex + ./hex -b lib/utils.hex ./hex -b scripts/test.hex ./hex scripts/test.hbx
M scripts/test.hexscripts/test.hex

@@ -1,6 +1,6 @@

#!/usr/bin/env hex -"lib/utils.hex" read ! +"lib/utils.hbx" read ! ; --- Globals
M src/hex.hsrc/hex.h

@@ -400,6 +400,7 @@ int hex_symboltable_get_index(hex_context_t *ctx, const char *symbol);

char *hex_symboltable_get_value(hex_context_t *ctx, uint16_t index); int hex_decode_bytecode_symboltable(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t count); uint8_t *hex_encode_bytecode_symboltable(hex_context_t *ctx, size_t *out_size); +hex_symbol_table_t *hex_symboltable_copy(hex_context_t *ctx); // REPL and initialization void hex_register_symbols(hex_context_t *ctx);
M src/symbols.csrc/symbols.c

@@ -276,7 +276,11 @@ for (size_t i = 0; i < item->quotation_size; i++)

{ bytecode[i] = (uint8_t)item->data.quotation_value[i]->data.int_value; } + // Copy the current symbol table before evaluating the bytecode + hex_symbol_table_t *symbol_table_copy = hex_symboltable_copy(ctx); int result = hex_interpret_bytecode(ctx, bytecode, item->quotation_size, "<eval>"); + // Restore the original symbol table + ctx->symbol_table = symbol_table_copy; return result; } else
M src/symboltable.csrc/symboltable.c

@@ -104,3 +104,42 @@

*out_size = total_size; return bytecode; } + +hex_symbol_table_t *hex_symboltable_copy(hex_context_t *ctx) +{ + hex_symbol_table_t *original = ctx->symbol_table; + hex_symbol_table_t *copy = malloc(sizeof(hex_symbol_table_t)); + if (copy == NULL) + { + hex_error(ctx, "[symbol table copy] Memory allocation failed"); + return NULL; + } + + copy->symbols = malloc(sizeof(char *) * HEX_MAX_USER_SYMBOLS); + if (copy->symbols == NULL) + { + hex_error(ctx, "[symbol table copy] Memory allocation failed for symbols array"); + free(copy); + return NULL; + } + + copy->count = original->count; + for (uint16_t i = 0; i < original->count; ++i) + { + copy->symbols[i] = strdup(original->symbols[i]); + if (copy->symbols[i] == NULL) + { + hex_error(ctx, "[symbol table copy] Memory allocation failed for symbol"); + // Free already allocated symbols + for (uint16_t j = 0; j < i; ++j) + { + free(copy->symbols[j]); + } + free(copy->symbols); + free(copy); + return NULL; + } + } + + return copy; +}