cpp: handle nested macro calls like M(M(1))
[neatcc.git] / tab.c
blob80ff035133574d8aa2015d26b5e9d0d0ede6c8cc
1 #include <string.h>
2 #include "tab.h"
4 static int hash(char *s)
6 unsigned h = 0x12345678;
7 while (*s) {
8 h ^= (h >> ((h & 0xf) + 1));
9 h += *s++;
10 h ^= (h << ((h & 0xf) + 5));
12 h &= (TABITEMS - 1);
13 return h ? h : 1;
16 void tab_add(struct tab *t, char *s)
18 int h = hash(s);
19 int i = t->n++;
20 if (!i)
21 i = t->n++;
22 t->next[i] = t->head[h];
23 t->head[h] = i;
24 t->data[i] = s;
27 char *tab_get(struct tab *t, char *s)
29 int h = t->head[hash(s)];
30 while (h && t->data[h]) {
31 if (!strcmp(s, t->data[h]))
32 return t->data[h];
33 h = t->next[h];
35 return NULL;
38 void tab_del(struct tab *t, char *s)
40 int h = hash(s);
41 int prev = -1;
42 while (h && t->data[h]) {
43 if (!strcmp(s, t->data[h])) {
44 if (prev)
45 t->next[prev] = t->next[h];
46 else
47 t->head[h] = t->next[h];
48 t->next[h] = 0;
49 t->data[h] = NULL;
50 return;
52 prev = h;
53 h = t->next[h];