all repos — hex @ 289940d9ae7c2ac09f3f93242e2892529b68c927

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

Fixes & refactoring.
h3rald h3rald@h3rald.com
Thu, 28 Nov 2024 07:35:27 +0100
commit

289940d9ae7c2ac09f3f93242e2892529b68c927

parent

053f32d65041538437f6f088933d079e9c294289

2 files changed, 18 insertions(+), 104 deletions(-)

jump to
M hex.chex.c

@@ -5,72 +5,6 @@ #define POP(ctx, x) hex_item_t x = hex_pop(ctx)

#define FREE(ctx, x) hex_free_element(ctx, x) #define PUSH(ctx, x) hex_push(ctx, x) -static char *HEX_NATIVE_SYMBOLS_LIST[] = { - "store", - "free", - "type", - "i", - "eval", - "puts", - "warn", - "print", - "gets", - "+", - "-", - "*", - "/", - "%", - "&", - "|", - "^", - "~", - "<<", - ">>", - "int", - "str", - "dec", - "hex", - "==", - "!=", - ">", - "<", - ">=", - "<=", - "and", - "or", - "not", - "xor", - "cat", - "slice", - "len", - "get", - "insert", - "index", - "join", - "split", - "replace", - "read", - "write", - "append", - "args", - "exit", - "exec", - "run", - "if", - "when", - "while", - "each", - "error", - "try", - "q", - "map", - "filter", - "swap", - "dup", - "stack", - "clear", - "pop"}; - //////////////////////////////////////// // Registry Implementation // ////////////////////////////////////////

@@ -428,20 +362,6 @@ ////////////////////////////////////////

// Help System // //////////////////////////////////////// -// Hash function (simple djb2 hash) - -unsigned int hex_doc_hash(const char *str) -{ - unsigned long hash = 5381; - int c; - while ((c = *str++)) - { - hash = ((hash << 5) + hash) + c; // hash * 33 + c - } - 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, .input = input, .output = output};

@@ -450,24 +370,17 @@ dict->entries[dict->size] = doc;

dict->size++; } -// Lookup a DocEntry by name -hex_doc_entry_t *hex_get_doc(hex_doc_dictionary_t *dict, const char *name) +int hex_get_doc(hex_doc_dictionary_t *docs, const char *key, hex_doc_entry_t *result) { - unsigned int index = hex_doc_hash(name); - unsigned int start_index = index; // Save the starting index for detection of loops - while (dict->entries[index].name != NULL) + for (int i = 0; i < docs->size; i++) { - if (strcmp(dict->entries[index].name, name) == 0) - { - return &dict->entries[index]; - } - index = (index + 1) % HEX_NATIVE_SYMBOLS; - if (index == start_index) + if (strcmp(docs->entries[i].name, key) == 0) { - break; // Avoid infinite loops + *result = docs->entries[i]; + return 1; } } - return NULL; // Not found + return 0; } void hex_create_docs(hex_doc_dictionary_t *docs)

@@ -697,7 +610,7 @@ int len = ptr - start;

token->value = (char *)malloc(len + 1); strncpy(token->value, start, len); token->value[len] = '\0'; - if (hex_valid_native_symbol(token->value) || hex_valid_user_symbol(ctx, token->value)) + if (hex_valid_native_symbol(ctx, token->value) || hex_valid_user_symbol(ctx, token->value)) { token->type = HEX_TOKEN_SYMBOL; }

@@ -723,11 +636,12 @@ free(token);

} } -int hex_valid_native_symbol(const char *symbol) +int hex_valid_native_symbol(hex_context_t *ctx, const char *symbol) { - for (size_t i = 0; i < sizeof(HEX_NATIVE_SYMBOLS_LIST) / sizeof(HEX_NATIVE_SYMBOLS_LIST[0]); i++) + hex_doc_entry_t doc; + for (size_t i = 0; i < HEX_NATIVE_SYMBOLS; i++) { - if (strcmp(symbol, HEX_NATIVE_SYMBOLS_LIST[i]) == 0) + if (hex_get_doc(&ctx->docs, symbol, &doc)) { return 1; }

@@ -792,7 +706,7 @@ element->data.strValue = strdup(processedStr);

} else if (token->type == HEX_TOKEN_SYMBOL) { - if (hex_valid_native_symbol(token->value)) + if (hex_valid_native_symbol(ctx, token->value)) { element->type = HEX_TYPE_NATIVE_SYMBOL; hex_item_t value;

@@ -4035,11 +3949,12 @@ {

if (help) { // Lookup symbol - hex_doc_entry_t *doc = hex_get_doc(&ctx.docs, arg); + hex_doc_entry_t doc; + hex_get_doc(&ctx.docs, arg, &doc); help = 0; - if (doc) + if (doc.name != NULL) { - hex_print_doc(doc); + hex_print_doc(&doc); return 0; } else
M hex.hhex.h

@@ -138,9 +138,8 @@ char **argv;

} hex_context_t; // Help System -unsigned int hex_doc_hash(const char *str); 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); +int hex_get_doc(hex_doc_dictionary_t *docs, const char *key, hex_doc_entry_t *result); 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);

@@ -152,7 +151,7 @@ void hex_free_list(hex_context_t *ctx, hex_item_t **quotation, int size);

// Symbol management int hex_valid_user_symbol(hex_context_t *ctx, const char *symbol); -int hex_valid_native_symbol(const char *symbol); +int hex_valid_native_symbol(hex_context_t *ctx, const char *symbol); int hex_set_symbol(hex_context_t *ctx, const char *key, hex_item_t value, int native); void hex_set_native_symbol(hex_context_t *ctx, const char *name, int (*func)()); int hex_get_symbol(hex_context_t *ctx, const char *key, hex_item_t *result);