summaryrefslogtreecommitdiff
path: root/Scope.c
diff options
context:
space:
mode:
Diffstat (limited to 'Scope.c')
-rw-r--r--Scope.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/Scope.c b/Scope.c
new file mode 100644
index 0000000..3df00b9
--- /dev/null
+++ b/Scope.c
@@ -0,0 +1,36 @@
+#include "Scope.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <xxhash.h>
+
+Scope *scope_create(const Scope *parent)
+{
+ Scope *const scope = malloc(sizeof(Scope));
+ if (scope == NULL) {
+ perror("scope_create");
+ return NULL;
+ }
+ scope->used = 0;
+ scope->parent = parent;
+ return scope;
+}
+
+int scope_insert(Scope *scope, const char *word, int value)
+{
+ if (scope->used >= SCOPE_WORD_MAX) {
+ fprintf(stderr, "scope full\n");
+ return 1;
+ }
+ const XXH32_hash_t hash = XXH32(word, strlen(word), 0);
+ for (int i = 0; i < scope->used; i++)
+ if (scope->words_hash[i] == hash) {
+ fprintf(stderr, "shadowed word\n");
+ return 1;
+ }
+ scope->words[scope->used] = word;
+ scope->values[scope->used] = value;
+ scope->words_hash[scope->used] = hash;
+ scope->used += 1;
+ return 0;
+}