1 /* $Id: symtab.c,v 1.7 2008/11/24 21:30:35 tom Exp $ */
5 /* TABLE_SIZE is the number of entries in the symbol table. */
6 /* TABLE_SIZE must be a power of two. */
8 #define TABLE_SIZE 1024
10 static bucket **symbol_table = 0;
15 hash(const char *name)
20 assert(name && *name);
23 while ((c = *++s) != 0)
24 k = (31 * k + c) & (TABLE_SIZE - 1);
30 make_bucket(const char *name)
35 bp = (bucket *)MALLOC(sizeof(bucket));
40 bp->name = MALLOC(strlen(name) + 1);
44 bp->value = UNDEFINED;
52 strcpy(bp->name, name);
58 lookup(const char *name)
62 bpp = symbol_table + hash(name);
67 if (strcmp(name, bp->name) == 0)
73 *bpp = bp = make_bucket(name);
74 last_symbol->next = bp;
81 create_symbol_table(void)
86 symbol_table = (bucket **)MALLOC(TABLE_SIZE * sizeof(bucket *));
87 if (symbol_table == 0)
89 for (i = 0; i < TABLE_SIZE; i++)
92 bp = make_bucket("error");
98 symbol_table[hash("error")] = bp;
102 free_symbol_table(void)
113 for (p = first_symbol; p; p = q)