mkfs: move directory entry manipulation
[minix.git] / external / bsd / byacc / dist / symtab.c
blobd1aac162a64f31a571cf78c6a157dcb7e4ac02d3
1 /* $NetBSD: symtab.c,v 1.6 2011/09/10 21:29:04 christos Exp $ */
2 /* Id: symtab.c,v 1.9 2010/11/24 15:12:29 tom Exp */
4 #include "defs.h"
6 #include <sys/cdefs.h>
7 __RCSID("$NetBSD: symtab.c,v 1.6 2011/09/10 21:29:04 christos Exp $");
9 /* TABLE_SIZE is the number of entries in the symbol table. */
10 /* TABLE_SIZE must be a power of two. */
12 #define TABLE_SIZE 1024
14 static bucket **symbol_table = 0;
15 bucket *first_symbol;
16 bucket *last_symbol;
18 static int
19 hash(const char *name)
21 const char *s;
22 int c, k;
24 assert(name && *name);
25 s = name;
26 k = *s;
27 while ((c = *++s) != 0)
28 k = (31 * k + c) & (TABLE_SIZE - 1);
30 return (k);
33 bucket *
34 make_bucket(const char *name)
36 bucket *bp;
38 assert(name != 0);
40 bp = (bucket *)MALLOC(sizeof(bucket));
41 NO_SPACE(bp);
43 bp->link = 0;
44 bp->next = 0;
46 bp->name = MALLOC(strlen(name) + 1);
47 NO_SPACE(bp->name);
49 bp->tag = 0;
50 bp->value = UNDEFINED;
51 bp->index = 0;
52 bp->prec = 0;
53 bp->class = UNKNOWN;
54 bp->assoc = TOKEN;
55 strcpy(bp->name, name);
57 return (bp);
60 bucket *
61 lookup(const char *name)
63 bucket *bp, **bpp;
65 bpp = symbol_table + hash(name);
66 bp = *bpp;
68 while (bp)
70 if (strcmp(name, bp->name) == 0)
71 return (bp);
72 bpp = &bp->link;
73 bp = *bpp;
76 *bpp = bp = make_bucket(name);
77 last_symbol->next = bp;
78 last_symbol = bp;
80 return (bp);
83 void
84 create_symbol_table(void)
86 int i;
87 bucket *bp;
89 symbol_table = (bucket **)MALLOC(TABLE_SIZE * sizeof(bucket *));
90 NO_SPACE(symbol_table);
92 for (i = 0; i < TABLE_SIZE; i++)
93 symbol_table[i] = 0;
95 bp = make_bucket("error");
96 bp->index = 1;
97 bp->class = TERM;
99 first_symbol = bp;
100 last_symbol = bp;
101 symbol_table[hash("error")] = bp;
104 void
105 free_symbol_table(void)
107 FREE(symbol_table);
108 symbol_table = 0;
111 void
112 free_symbols(void)
114 bucket *p, *q;
116 for (p = first_symbol; p; p = q)
118 q = p->next;
119 FREE(p);