This repository has been archived by the owner on Jun 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymtab.c
62 lines (53 loc) · 1.41 KB
/
symtab.c
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// TODO: generalize this code a bit so it can be used for more than
// just the sym table (i.e. allow full hash tables in the lang itself)
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include "symtab.h"
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
extern sym_table *symtab;
// TODO: test that this works
char *lstr_to_cstr(lstr *l) {
char *c = malloc(l->n + 1);
memcpy(c, l + offsetof(lstr, str), l->n);
c[l->n] = 0;
return c;
}
// TODO: test that this works
lstr *cstr_to_lstr(char *c) {
size_t s = strlen(c);
lstr *l = malloc(sizeof(short) + sizeof(char) * s);
return l;
}
sym_table st_create(unsigned short size) {
sym_table t;
t.size = size;
t.table = calloc(size, sizeof(bucket));
return t;
}
unsigned int st_hash(lstr *k) {
// djb2
unsigned long hash = 5381;
for(int i = 0; i < k->n; i++)
hash = ((hash << 5) + hash) + k->str[i];
return hash % symtab->size;
}
void* st_get_or_set(lstr *k) {
int bin = st_hash(k);
bucket *b = &(symtab->table[bin]);
if(b->vals == NULL) {
b->vals = NIL;
}
for(cons *c = b->vals; c != NIL; c = c->cdr) {
lstr *l = ((lstr *) c->car);
if(strncmp(l->str, k->str, MIN(l->n, k->n)) == 0)
return (void *) c->car;
}
cons *n = malloc(sizeof(cons));
n->car = k;
n->cdr = b->vals;
b->vals = n;
return n->car;
}