summaryrefslogtreecommitdiff
path: root/Scope.c
blob: 3df00b949ce9ff9b57d44bfd7ca4200f11b3388e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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;
}