build/ccw_gen: stat the file in C instead of shelling out
[hvf.git] / build / byacc / symtab.c
blob1327eaffc33d6cb507fa8c2b0df4b033cfacae7f
1 /* $Id: symtab.c,v 1.9 2010/11/24 15:12:29 tom Exp $ */
3 #include "defs.h"
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;
11 bucket *first_symbol;
12 bucket *last_symbol;
14 static int
15 hash(const char *name)
17 const char *s;
18 int c, k;
20 assert(name && *name);
21 s = name;
22 k = *s;
23 while ((c = *++s) != 0)
24 k = (31 * k + c) & (TABLE_SIZE - 1);
26 return (k);
29 bucket *
30 make_bucket(const char *name)
32 bucket *bp;
34 assert(name != 0);
36 bp = (bucket *)MALLOC(sizeof(bucket));
37 NO_SPACE(bp);
39 bp->link = 0;
40 bp->next = 0;
42 bp->name = MALLOC(strlen(name) + 1);
43 NO_SPACE(bp->name);
45 bp->tag = 0;
46 bp->value = UNDEFINED;
47 bp->index = 0;
48 bp->prec = 0;
49 bp->class = UNKNOWN;
50 bp->assoc = TOKEN;
51 strcpy(bp->name, name);
53 return (bp);
56 bucket *
57 lookup(const char *name)
59 bucket *bp, **bpp;
61 bpp = symbol_table + hash(name);
62 bp = *bpp;
64 while (bp)
66 if (strcmp(name, bp->name) == 0)
67 return (bp);
68 bpp = &bp->link;
69 bp = *bpp;
72 *bpp = bp = make_bucket(name);
73 last_symbol->next = bp;
74 last_symbol = bp;
76 return (bp);
79 void
80 create_symbol_table(void)
82 int i;
83 bucket *bp;
85 symbol_table = (bucket **)MALLOC(TABLE_SIZE * sizeof(bucket *));
86 NO_SPACE(symbol_table);
88 for (i = 0; i < TABLE_SIZE; i++)
89 symbol_table[i] = 0;
91 bp = make_bucket("error");
92 bp->index = 1;
93 bp->class = TERM;
95 first_symbol = bp;
96 last_symbol = bp;
97 symbol_table[hash("error")] = bp;
100 void
101 free_symbol_table(void)
103 FREE(symbol_table);
104 symbol_table = 0;
107 void
108 free_symbols(void)
110 bucket *p, *q;
112 for (p = first_symbol; p; p = q)
114 q = p->next;
115 FREE(p);