Fixes.
h3rald h3rald@h3rald.com
Thu, 28 Nov 2024 07:22:17 +0100
M
hex.c
→
hex.c
@@ -429,6 +429,7 @@ // Help System //
//////////////////////////////////////// // Hash function (simple djb2 hash) + unsigned int hex_doc_hash(const char *str) { unsigned long hash = 5381;@@ -437,20 +438,16 @@ while ((c = *str++))
{ hash = ((hash << 5) + hash) + c; // hash * 33 + c } - return hash % HEX_NATIVE_SYMBOLS; + return hash & (HEX_NATIVE_SYMBOLS - 1); // Use bitwise AND for better distribution } // Insert a DocEntry into the hash table void hex_doc(hex_doc_dictionary_t *dict, const char *name, const char *description, const char *input, const char *output) { - hex_doc_entry_t doc = {.name = name, .description = description, .signature.input = input, .signature.output = output}; - unsigned int index = hex_doc_hash(doc.name); - // Handle collisions using linear probing - while (dict->entries[index].name != NULL) - { - index = (index + 1) % HEX_NATIVE_SYMBOLS; - } - dict->entries[index] = doc; + hex_doc_entry_t doc = {.name = name, .description = description, .input = input, .output = output}; + // No overflow check as the dictionary is fixed size + dict->entries[dict->size] = doc; + dict->size++; } // Lookup a DocEntry by name@@ -473,74 +470,72 @@ }
return NULL; // Not found } -hex_doc_dictionary_t hex_create_docs() +void hex_create_docs(hex_doc_dictionary_t *docs) { - hex_doc_dictionary_t docs; - hex_doc(&docs, "!=", "-", "-", "-"); - hex_doc(&docs, "%%", "-", "-", "-"); - hex_doc(&docs, "&", "-", "-", "-"); - hex_doc(&docs, "*", "-", "-", "-"); - hex_doc(&docs, "+", "-", "-", "-"); - hex_doc(&docs, "-", "-", "-", "-"); - hex_doc(&docs, "/", "-", "-", "-"); - hex_doc(&docs, "<", "-", "-", "-"); - hex_doc(&docs, "<<", "-", "-", "-"); - hex_doc(&docs, "<=", "-", "-", "-"); - hex_doc(&docs, "==", "-", "-", "-"); - hex_doc(&docs, ">", "-", "-", "-"); - hex_doc(&docs, ">=", "-", "-", "-"); - hex_doc(&docs, ">>", "-", "-", "-"); - hex_doc(&docs, "^", "-", "-", "-"); - hex_doc(&docs, "and", "-", "-", "-"); - hex_doc(&docs, "append", "-", "-", "-"); - hex_doc(&docs, "args", "-", "-", "-"); - hex_doc(&docs, "cat", "-", "-", "-"); - hex_doc(&docs, "clear", "-", "-", "-"); - hex_doc(&docs, "dec", "-", "-", "-"); - hex_doc(&docs, "dup", "-", "-", "-"); - hex_doc(&docs, "each", "-", "-", "-"); - hex_doc(&docs, "error", "-", "-", "-"); - hex_doc(&docs, "eval", "-", "-", "-"); - hex_doc(&docs, "exec", "-", "-", "-"); - hex_doc(&docs, "exit", "-", "-", "-"); - hex_doc(&docs, "filter", "-", "-", "-"); - hex_doc(&docs, "free", "-", "-", "-"); - hex_doc(&docs, "get", "-", "-", "-"); - hex_doc(&docs, "gets", "-", "-", "-"); - hex_doc(&docs, "hex", "-", "-", "-"); - hex_doc(&docs, "i", "-", "-", "-"); - hex_doc(&docs, "if", "-", "-", "-"); - hex_doc(&docs, "index", "-", "-", "-"); - hex_doc(&docs, "insert", "-", "-", "-"); - hex_doc(&docs, "int", "-", "-", "-"); - hex_doc(&docs, "join", "-", "-", "-"); - hex_doc(&docs, "len", "-", "-", "-"); - hex_doc(&docs, "map", "-", "-", "-"); - hex_doc(&docs, "not", "-", "-", "-"); - hex_doc(&docs, "or", "-", "-", "-"); - hex_doc(&docs, "pop", "-", "-", "-"); - hex_doc(&docs, "print", "-", "-", "-"); - hex_doc(&docs, "puts", "-", "-", "-"); - hex_doc(&docs, "q", "-", "-", "-"); - hex_doc(&docs, "read", "-", "-", "-"); - hex_doc(&docs, "replace", "-", "-", "-"); - hex_doc(&docs, "run", "-", "-", "-"); - hex_doc(&docs, "slice", "-", "-", "-"); - hex_doc(&docs, "split", "-", "-", "-"); - hex_doc(&docs, "stack", "-", "-", "-"); - hex_doc(&docs, "store", "-", "-", "-"); - hex_doc(&docs, "str", "-", "-", "-"); - hex_doc(&docs, "swap", "-", "-", "-"); - hex_doc(&docs, "try", "-", "-", "-"); - hex_doc(&docs, "type", "-", "-", "-"); - hex_doc(&docs, "warn", "-", "-", "-"); - hex_doc(&docs, "when", "-", "-", "-"); - hex_doc(&docs, "while", "-", "-", "-"); - hex_doc(&docs, "write", "-", "-", "-"); - hex_doc(&docs, "xor", "-", "-", "-"); - hex_doc(&docs, "|", "-", "-", "-"); - hex_doc(&docs, "~", "-", "-", "-"); - return docs; + hex_doc(docs, "!=", "-", "-", "-"); + hex_doc(docs, "%%", "-", "-", "-"); + hex_doc(docs, "&", "-", "-", "-"); + hex_doc(docs, "*", "-", "-", "-"); + hex_doc(docs, "+", "-", "-", "-"); + hex_doc(docs, "-", "-", "-", "-"); + hex_doc(docs, "/", "-", "-", "-"); + hex_doc(docs, "<", "-", "-", "-"); + hex_doc(docs, "<<", "-", "-", "-"); + hex_doc(docs, "<=", "-", "-", "-"); + hex_doc(docs, "==", "-", "-", "-"); + hex_doc(docs, ">", "-", "-", "-"); + hex_doc(docs, ">=", "-", "-", "-"); + hex_doc(docs, ">>", "-", "-", "-"); + hex_doc(docs, "^", "-", "-", "-"); + hex_doc(docs, "and", "-", "-", "-"); + hex_doc(docs, "append", "-", "-", "-"); + hex_doc(docs, "args", "-", "-", "-"); + hex_doc(docs, "cat", "-", "-", "-"); + hex_doc(docs, "clear", "-", "-", "-"); + hex_doc(docs, "dec", "-", "-", "-"); + hex_doc(docs, "dup", "-", "-", "-"); + hex_doc(docs, "each", "-", "-", "-"); + hex_doc(docs, "error", "-", "-", "-"); + hex_doc(docs, "eval", "-", "-", "-"); + hex_doc(docs, "exec", "-", "-", "-"); + hex_doc(docs, "exit", "-", "-", "-"); + hex_doc(docs, "filter", "-", "-", "-"); + hex_doc(docs, "free", "-", "-", "-"); + hex_doc(docs, "get", "-", "-", "-"); + hex_doc(docs, "gets", "-", "-", "-"); + hex_doc(docs, "hex", "-", "-", "-"); + hex_doc(docs, "i", "-", "-", "-"); + hex_doc(docs, "if", "-", "-", "-"); + hex_doc(docs, "index", "-", "-", "-"); + hex_doc(docs, "insert", "-", "-", "-"); + hex_doc(docs, "int", "-", "-", "-"); + hex_doc(docs, "join", "-", "-", "-"); + hex_doc(docs, "len", "-", "-", "-"); + hex_doc(docs, "map", "-", "-", "-"); + hex_doc(docs, "not", "-", "-", "-"); + hex_doc(docs, "or", "-", "-", "-"); + hex_doc(docs, "pop", "-", "-", "-"); + hex_doc(docs, "print", "-", "-", "-"); + hex_doc(docs, "puts", "-", "-", "-"); + hex_doc(docs, "q", "-", "-", "-"); + hex_doc(docs, "read", "-", "-", "-"); + hex_doc(docs, "replace", "-", "-", "-"); + hex_doc(docs, "run", "-", "-", "-"); + hex_doc(docs, "slice", "-", "-", "-"); + hex_doc(docs, "split", "-", "-", "-"); + hex_doc(docs, "stack", "-", "-", "-"); + hex_doc(docs, "store", "-", "-", "-"); + hex_doc(docs, "str", "-", "-", "-"); + hex_doc(docs, "swap", "-", "-", "-"); + hex_doc(docs, "try", "-", "-", "-"); + hex_doc(docs, "type", "-", "-", "-"); + hex_doc(docs, "warn", "-", "-", "-"); + hex_doc(docs, "when", "-", "-", "-"); + hex_doc(docs, "while", "-", "-", "-"); + hex_doc(docs, "write", "-", "-", "-"); + hex_doc(docs, "xor", "-", "-", "-"); + hex_doc(docs, "|", "-", "-", "-"); + hex_doc(docs, "~", "-", "-", "-"); } ////////////////////////////////////////@@ -3782,6 +3777,7 @@ hex_context_t context;
context.argc = 0; context.argv = NULL; context.registry.size = 0; + context.docs.size = 0; context.stack.top = -1; context.stack_trace.start = 0; context.stack_trace.size = 0;@@ -3993,7 +3989,7 @@
void hex_print_doc(hex_doc_entry_t *doc) { printf(" Symbol '%s'\n", doc->name); - printf(" %s => %s\n", doc->signature.input, doc->signature.output); + printf(" %s => %s\n", doc->input, doc->output); printf(" %s\n", doc->description); }@@ -4012,11 +4008,11 @@ ctx.argc = argc;
ctx.argv = argv; hex_register_symbols(&ctx); - hex_doc_dictionary_t docs = hex_create_docs(); + hex_create_docs(&ctx.docs); + int help = 0; if (argc > 1) { - int help = 0; for (int i = 1; i < argc; i++) { char *arg = strdup(argv[i]);@@ -4039,7 +4035,7 @@ {
if (help) { // Lookup symbol - hex_doc_entry_t *doc = hex_get_doc(&docs, arg); + hex_doc_entry_t *doc = hex_get_doc(&ctx.docs, arg); help = 0; if (doc) {@@ -4068,7 +4064,7 @@ }
if (help) { // TODO print help - hex_print_help(&docs); + hex_print_help(&ctx.docs); return 0; } }
M
hex.h
→
hex.h
@@ -104,6 +104,20 @@ hex_registry_entry_t entries[HEX_REGISTRY_SIZE];
int size; } hex_registry_t; +typedef struct hex_doc_entry_t +{ + const char *name; + const char *description; + const char *input; + const char *output; +} hex_doc_entry_t; + +typedef struct hex_doc_dictionary_t +{ + hex_doc_entry_t entries[64]; + int size; +} hex_doc_dictionary_t; + typedef struct hex_settings_t { int debugging_enabled;@@ -117,34 +131,17 @@ hex_stack_t stack;
hex_registry_t registry; hex_stack_trace_t stack_trace; hex_settings_t settings; + hex_doc_dictionary_t docs; char error[256]; int argc; char **argv; } hex_context_t; -typedef struct hex_signature_t -{ - const char *input; - const char *output; -} hex_signature_t; - -typedef struct hex_doc_entry_t -{ - const char *name; - const char *description; - hex_signature_t signature; -} hex_doc_entry_t; - -typedef struct hex_doc_dictionary_t -{ - hex_doc_entry_t entries[64]; -} hex_doc_dictionary_t; - // Help System unsigned int hex_doc_hash(const char *str); -void hex_doc(hex_doc_dictionary_t *dict, const char *name, const char *description, const char *input, const char *output); -hex_doc_entry_t *hex_get_doc(hex_doc_dictionary_t *dict, const char *symbol); -hex_doc_dictionary_t hex_create_docs(); +void hex_doc(hex_doc_dictionary_t *docs, const char *name, const char *description, const char *input, const char *output); +hex_doc_entry_t *hex_get_doc(hex_doc_dictionary_t *docs, const char *symbol); +void hex_create_docs(hex_doc_dictionary_t *docs); void hex_print_help(hex_doc_dictionary_t *docs); void hex_print_doc(hex_doc_entry_t *doc);