diff --git a/lang/main.c b/lang/main.c index 4a398c0..a82a01b 100644 --- a/lang/main.c +++ b/lang/main.c @@ -80,9 +80,6 @@ } int lang_main(void) { - init_symbols(); - init_symbols(); - init_symbols(); printf("Hello world!\n"); printf("symb %i\n", find_symbol("a")); printf("symb %i\n", find_symbol("b")); diff --git a/lang/symbol.c b/lang/symbol.c index c7471e0..9e42ce4 100644 --- a/lang/symbol.c +++ b/lang/symbol.c @@ -2,51 +2,17 @@ // Copyright (c) 2023 John Watts and the LuminaSensum contributors #include "symbol.h" -#include "error.h" -#include -#include +#include -#define SYMBOL_LEN 32 -#define SYMBOLS_MAX 32 - -typedef struct { - char name[SYMBOL_LEN]; - symbol sym; -} symbol_mapping; - -symbol_mapping symbols[SYMBOLS_MAX]; -mtx_t symbols_lock; - -void init_symbols(void) { - int err = mtx_init(&symbols_lock, mtx_plain); - abort_if(err != thrd_success, "unable to init symbols mutex"); - memset(symbols, 0, sizeof(symbols)); -} - -symbol find_symbol(char *name) { - int err = mtx_lock(&symbols_lock); - abort_if(err != thrd_success, "unable to lock symbols mutex"); - for (int i = 0; i < SYMBOLS_MAX; ++i) { - symbol_mapping *curr_sym = &symbols[i]; - char *curr_name = curr_sym->name; - if (curr_name[0] == 0) { - strncpy(curr_name, name, SYMBOL_LEN); - curr_sym->sym = i; - err = mtx_unlock(&symbols_lock); - abort_if(err != thrd_success, - "unable to unlock symbols mutex"); - return curr_sym->sym; - } else { - int cmp = strncmp(curr_name, name, SYMBOL_LEN); - if (cmp == 0) { - err = mtx_unlock(&symbols_lock); - abort_if(err != thrd_success, - "unable to unlock symbols mutex"); - return curr_sym->sym; - } - } +// FNV-1a Algorithm from http://www.isthe.com/chongo/tech/comp/fnv/ +// Thanks for releasing it to the public domain :) +uint32_t fnv32a_str(unsigned char *str) { + uint32_t hash = (uint32_t)0x811c9dc5; + while (*str) { + hash ^= (uint32_t)*str++; + hash *= (uint32_t)0x01000193; } - err = mtx_unlock(&symbols_lock); - abort_if(err != thrd_success, "unable to unlock symbols mutex"); - abort_msg("ran out of symbols"); + return hash; } + +symbol find_symbol(char *name) { return fnv32a_str((unsigned char *)name); } diff --git a/lang/symbol.h b/lang/symbol.h index ca5de6e..291a7e7 100644 --- a/lang/symbol.h +++ b/lang/symbol.h @@ -7,9 +7,6 @@ // Symbols are integers with unique values typedef int symbol; -// Clears all symbols -void init_symbols(void); - // Find a unique value associated with the symbol's name symbol find_symbol(char *name);