1 /* $Id: symtab.c,v 1.11 2014/03/26 00:17:09 Tom.Shields 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
)
36 bp
= TMALLOC(bucket
, 1);
42 bp
->name
= TMALLOC(char, strlen(name
) + 1);
46 bp
->value
= UNDEFINED
;
57 strcpy(bp
->name
, name
);
63 lookup(const char *name
)
67 bpp
= symbol_table
+ hash(name
);
72 if (strcmp(name
, bp
->name
) == 0)
78 *bpp
= bp
= make_bucket(name
);
79 last_symbol
->next
= bp
;
86 create_symbol_table(void)
91 symbol_table
= TMALLOC(bucket
*, TABLE_SIZE
);
92 NO_SPACE(symbol_table
);
94 for (i
= 0; i
< TABLE_SIZE
; i
++)
97 bp
= make_bucket("error");
103 symbol_table
[hash("error")] = bp
;
107 free_symbol_table(void)
118 for (p
= first_symbol
; p
; p
= q
)