Compiling (not working).
h3rald h3rald@h3rald.com
Tue, 31 Dec 2024 06:34:51 +0000
4 files changed,
33 insertions(+),
42 deletions(-)
M
src/hex.h
→
src/hex.h
@@ -84,12 +84,6 @@ hex_token_t *token; // Token containing stack information (valid for HEX_TYPE_NATIVE_SYMBOL and HEX_TYPE_USER_SYMBOL)
size_t quotation_size; // Size of the quotation (valid for HEX_TYPE_QUOTATION) } hex_item_t; -typedef struct hex_registry_entry -{ - char *key; - hex_item_t *value; -} hex_registry_entry_t; - typedef struct hex_stack_trace_t { hex_token_t **entries;@@ -104,11 +98,20 @@ int top;
size_t capacity; } hex_stack_t; +typedef struct hex_registry_entry_t +{ + char *key; + hex_item_t *value; + struct hex_registry_entry_t *next; // For collision resolution (chaining) +} hex_registry_entry_t; + typedef struct hex_registry_t { - hex_registry_entry_t **entries; - size_t size; + hex_registry_entry_t **buckets; // Array of bucket pointers + size_t bucket_count; // Number of buckets + size_t size; // Number of stored entries } hex_registry_t; + typedef struct hex_doc_entry_t {
M
src/interpreter.c
→
src/interpreter.c
@@ -11,9 +11,7 @@ {
hex_context_t *context = malloc(sizeof(hex_context_t)); context->argc = 0; context->argv = NULL; - context->registry = malloc(sizeof(hex_registry_t)); - context->registry->entries = malloc(HEX_REGISTRY_SIZE * sizeof(hex_registry_entry_t *)); - context->registry->size = 0; + context->registry = hex_registry_create(); context->docs = malloc(sizeof(hex_doc_dictionary_t)); context->docs->entries = malloc(HEX_NATIVE_SYMBOLS * sizeof(hex_doc_entry_t)); context->docs->size = 0;
M
src/registry.c
→
src/registry.c
@@ -43,7 +43,8 @@ {
hex_registry_t *registry = ctx->registry; - hex_registry_entry_t **new_buckets = calloc(registry->bucket_count * 2, sizeof(hex_registry_entry_t *)); + int new_bucket_count = registry->bucket_count * 2; + hex_registry_entry_t **new_buckets = calloc(new_bucket_count, sizeof(hex_registry_entry_t *)); if (new_buckets == NULL) { return 1; // Memory allocation failed@@ -110,11 +111,11 @@ {
hex_error(ctx, "Invalid symbol: %s", symbol); return 0; } - /*if (strlen(symbol) > HEX_MAX_SYMBOL_LENGTH) + if (strlen(symbol) > HEX_MAX_SYMBOL_LENGTH) { - hex_error(ctx, "Symbol name too long: %s", symbol); + hex_error(ctx, "[check valid symbol] Symbol name too long: %s", symbol); return 0; - }*/ + } for (size_t j = 1; j < strlen(symbol); j++) { if (!isalnum(symbol[j]) && symbol[j] != '_' && symbol[j] != '-')@@ -131,13 +132,13 @@ {
hex_registry_t *registry = ctx->registry; if (!native && hex_valid_user_symbol(ctx, key) == 0) { - hex_error(ctx, "[set symbol] Invalid user symbol %s", name); + hex_error(ctx, "[set symbol] Invalid user symbol %s", key); return 1; } if ((float)registry->size / registry->bucket_count > 0.75) { - hex_registry_resize(registry, registry->bucket_count * 2); + hex_registry_resize(ctx); } size_t bucket_index = hash_function(key, registry->bucket_count);@@ -201,7 +202,7 @@ int hex_get_symbol(hex_context_t *ctx, const char *key, hex_item_t *result)
{ hex_registry_t *registry = ctx->registry; hex_debug(ctx, "LOOK: %s", key); - size_t bucket_index = hash_function(key) % registry->bucket_count; + size_t bucket_index = hash_function(key, registry->bucket_count); hex_registry_entry_t *entry = registry->buckets[bucket_index]; while (entry != NULL)@@ -209,7 +210,7 @@ {
if (strcmp(entry->key, key) == 0) { // Key found, copy the value to result - *result = hex_copy_item(ctx, entry->value); // Copy the value structure + result = hex_copy_item(ctx, entry->value); // Copy the value structure if (result == NULL) { hex_error(ctx, "[get symbol] Failed to copy item for key: %s", key);
M
src/symbols.c
→
src/symbols.c
@@ -145,28 +145,12 @@ hex_error(ctx, "[symbol #] Cannot free native symbol '%s'", item->data.str_value);
HEX_FREE(ctx, item); return 1; } - for (size_t i = 0; i < ctx->registry->size; i++) + if (hex_delete_symbol(ctx, item->data.str_value) != 0) { - if (strcmp(ctx->registry->entries[i]->key, item->data.str_value) == 0) - { - // Free the memory associated with this entry - free(ctx->registry->entries[i]->key); - HEX_FREE(ctx, ctx->registry->entries[i]->value); - // Shift entries down to fill the gap - for (size_t j = i; j < ctx->registry->size - 1; j++) - { - ctx->registry->entries[j] = ctx->registry->entries[j + 1]; - } - // Clear the last entry to avoid dangling pointers - ctx->registry->entries[ctx->registry->size - 1] = NULL; - // Decrement the registry size - ctx->registry->size--; - // Free the `item` as it is no longer needed - HEX_FREE(ctx, item); - return 0; // Exit after removing the entry - } + hex_error(ctx, "[symbol #] Symbol not found: %s", item->data.str_value); + HEX_FREE(ctx, item); + return 1; } - HEX_FREE(ctx, item); return 0; }@@ -178,16 +162,19 @@ {
hex_error(ctx, "[symbol symbols] Memory allocation failed"); return 1; } - for (size_t i = 0; i < ctx->registry->size; i++) + for (size_t i = 0; i < ctx->registry->bucket_count; i++) + { + hex_registry_entry_t *entry = ctx->registry->buckets[i]; + while (entry != NULL) + { - char *id = malloc(strlen(ctx->registry->entries[i]->key) + 1); + char *id = strdup(entry->key); if (!id) { hex_error(ctx, "[symbol symbols] Memory allocation failed"); hex_free_list(ctx, quotation, i); return 1; } - strcpy(id, ctx->registry->entries[i]->key); quotation[i] = (hex_item_t *)malloc(sizeof(hex_item_t)); if (!quotation[i]) {@@ -196,6 +183,8 @@ hex_free_list(ctx, quotation, i);
return 1; } *quotation[i] = *hex_string_item(ctx, id); + entry = entry->next; + } } if (hex_push_quotation(ctx, quotation, ctx->registry->size) != 0) {