aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2025-07-08 19:14:02 +0300
committerkartofen <mladenovnasko0@gmail.com>2025-07-08 19:14:02 +0300
commite911e95f697b0eb48ed4e68cb2586ffb0dc11341 (patch)
tree3981038900cff77a0d3b0aaf739cc22025816810
parentd028cc9c04cf46256166434bdea68d5f5c6d310f (diff)
move things around
-rwxr-xr-xbuild.sh2
-rw-r--r--util/dict.c (renamed from dict.c)67
-rw-r--r--util/dict.h33
3 files changed, 69 insertions, 33 deletions
diff --git a/build.sh b/build.sh
index 9f1899b..8150542 100755
--- a/build.sh
+++ b/build.sh
@@ -27,7 +27,7 @@ function leak
log valgrind --leak-check=full --show-leak-kinds=all -s bin/$1 $2
}
-# cc dict -D_DICT_STANDALONE
+# cc util/dict -D_DICT_STANDALONE
# leak dict
# cc demos/lexer -D_LEXER_STANDALONE
diff --git a/dict.c b/util/dict.c
index 45ddf38..56ed6c9 100644
--- a/dict.c
+++ b/util/dict.c
@@ -3,28 +3,19 @@
#include <stdint.h>
#include <string.h>
-extern const struct string_token {
- char *s;
- int t;
-} strings[];
-extern const size_t nstrings;
-
-extern const uint8_t char_to_bit[];
-
-struct level {
- uint64_t bit_mask;
- uint64_t *token_masks;
-};
-
-#define MAPPED_CHARS 32
-static struct level start_level = {0};
-static struct level *bit_to_ptr[MAPPED_CHARS] = {0};
-static size_t num_levels;
+#include "util/dict.h"
#define CHAR_TO_PTR(c) (bit_to_ptr[char_to_bit[c]])
#define popcount(x) (__builtin_popcount(x))
-int dict_compile(void)
+#define start_level d->start_level
+#define bit_to_ptr d->bit_to_ptr
+#define num_levels d->num_levels
+#define strings d->strings
+#define nstrings d->nstrings
+#define char_to_bit d->char_to_bit
+
+int dict_compile(struct dict *d)
{
// max number of levels
for(size_t i = 0; i < nstrings; i++)
@@ -78,7 +69,7 @@ int dict_compile(void)
return 0;
}
-void dict_print(void)
+void dict_print(struct dict *d)
{
for(size_t i = 0; i < 256; i++)
for(size_t j = 0; j < num_levels; j++)
@@ -101,7 +92,7 @@ void dict_print(void)
printf(" %32s\n", "zyxwvutsrqponmlkjihgfedcbaE ");
}
-void dict_free(void)
+void dict_free(struct dict *d)
{
free(start_level.token_masks);
for(size_t i = 0; i < MAPPED_CHARS; i++) {
@@ -113,10 +104,10 @@ void dict_free(void)
}
}
-int dict_check(char *string)
+int dict_check(struct dict *d, char *string)
{
uint64_t token_mask = ~(uint64_t)0;
-
+
for(size_t i = 0; i < strlen(string) + 1; i++) {
struct level *l = (i == 0)
? &start_level
@@ -129,11 +120,18 @@ int dict_check(char *string)
uint64_t idx = popcount(l->bit_mask & ((1 << bit) - 1));
token_mask &= l->token_masks[idx];
}
-
+
if(token_mask) return __builtin_ctz(token_mask);
else return -1;
}
+#undef start_level
+#undef bit_to_ptr
+#undef num_levels
+#undef strings
+#undef nstrings
+#undef char_to_bit
+
#ifdef _DICT_STANDALONE
#define TOKENS(X) \
@@ -155,7 +153,7 @@ enum token {
const char * const token_to_string[] = {
TOKENS(TOKEN_STRING)
};
-const struct string_token strings[] = {
+struct string_token *strings = (struct string_token[]){
{"test", TOKEN_TEST},
{"retarded", TOKEN_RETARDED},
{"wow", TOKEN_WOW},
@@ -174,15 +172,20 @@ const uint8_t char_to_bit[256] = {
int main(void)
{
- dict_compile();
-
+ struct dict d = {0};
+ d.strings = strings;
+ d.nstrings = nstrings;
+ d.char_to_bit = char_to_bit;
+
+ dict_compile(&d);
+
int t;
- if((t = dict_check("tits")) >= 0) printf("%s\n", token_to_string[t]);
- if((t = dict_check("retarded")) >= 0) printf("%s\n", token_to_string[t]);
- if((t = dict_check("test2")) >= 0) printf("%s\n", token_to_string[t]);
- if((t = dict_check("tes")) >= 0) printf("%s\n", token_to_string[t]);
-
- dict_free();
+ if((t = dict_check(&d, "tits")) >= 0) printf("%s\n", token_to_string[t]);
+ if((t = dict_check(&d, "retarded")) >= 0) printf("%s\n", token_to_string[t]);
+ if((t = dict_check(&d, "test2")) >= 0) printf("%s\n", token_to_string[t]);
+ if((t = dict_check(&d, "tes")) >= 0) printf("%s\n", token_to_string[t]);
+
+ dict_free(&d);
}
#endif
diff --git a/util/dict.h b/util/dict.h
new file mode 100644
index 0000000..ecaf53a
--- /dev/null
+++ b/util/dict.h
@@ -0,0 +1,33 @@
+#ifndef DICT_H
+#define DICT_H
+
+struct level {
+ uint64_t bit_mask;
+ uint64_t *token_masks;
+};
+
+#ifndef MAPPED_CHARS
+#define MAPPED_CHARS 32
+#endif
+
+struct dict {
+ // parameters for compilation
+ struct string_token {
+ char *s;
+ int t;
+ } *strings;
+ size_t nstrings;
+ uint8_t *char_to_bit;
+
+ // result of compilation
+ struct level start_level;
+ struct level *bit_to_ptr[MAPPED_CHARS];
+ size_t num_levels;
+};
+
+int dict_compile(struct dict *d);
+void dict_free(struct dict *d);
+void dict_print(struct dict *d);
+int dict_check(struct dict *d, char *string);
+
+#endif