Commit | Line | Data |
---|---|---|
92ec8d3b KB |
1 | /* |
2 | * Copyright (c) 1989 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Robert Paul Corbett. | |
7 | * | |
8 | * Redistribution and use in source and binary forms are permitted | |
9 | * provided that the above copyright notice and this paragraph are | |
10 | * duplicated in all such forms and that any documentation, | |
11 | * advertising materials, and other materials related to such | |
12 | * distribution and use acknowledge that the software was developed | |
13 | * by the University of California, Berkeley. The name of the | |
14 | * University may not be used to endorse or promote products derived | |
15 | * from this software without specific prior written permission. | |
16 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
17 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
19 | */ | |
20 | ||
21 | #ifndef lint | |
22 | static char sccsid[] = "@(#)symtab.c 5.1 (Berkeley) %G%"; | |
23 | #endif /* not lint */ | |
24 | ||
25 | #include "defs.h" | |
26 | ||
27 | /* TABLE_SIZE is the number of entries in the symbol table. */ | |
28 | /* TABLE_SIZE must be a power of two. */ | |
29 | ||
30 | #define TABLE_SIZE 1024 | |
31 | ||
32 | ||
33 | bucket **symbol_table; | |
34 | bucket *first_symbol; | |
35 | bucket *last_symbol; | |
36 | ||
37 | ||
38 | int | |
39 | hash(name) | |
40 | char *name; | |
41 | { | |
42 | register char *s; | |
43 | register int c, k; | |
44 | ||
45 | assert(name && *name); | |
46 | s = name; | |
47 | k = *s; | |
48 | while (c = *++s) | |
49 | k = (31*k + c) & (TABLE_SIZE - 1); | |
50 | ||
51 | return (k); | |
52 | } | |
53 | ||
54 | ||
55 | bucket * | |
56 | make_bucket(name) | |
57 | char *name; | |
58 | { | |
59 | register bucket *bp; | |
60 | ||
61 | assert(name); | |
62 | bp = (bucket *) MALLOC(sizeof(bucket)); | |
63 | if (bp == 0) no_space(); | |
64 | bp->link = 0; | |
65 | bp->next = 0; | |
66 | bp->name = MALLOC(strlen(name) + 1); | |
67 | bp->tag = 0; | |
68 | bp->value = UNDEFINED; | |
69 | bp->index = 0; | |
70 | bp->prec = 0; | |
71 | bp-> class = UNKNOWN; | |
72 | bp->assoc = TOKEN; | |
73 | ||
74 | if (bp->name == 0) no_space(); | |
75 | strcpy(bp->name, name); | |
76 | ||
77 | return (bp); | |
78 | } | |
79 | ||
80 | ||
81 | bucket * | |
82 | lookup(name) | |
83 | char *name; | |
84 | { | |
85 | register bucket *bp, **bpp; | |
86 | ||
87 | bpp = symbol_table + hash(name); | |
88 | bp = *bpp; | |
89 | ||
90 | while (bp) | |
91 | { | |
92 | if (strcmp(name, bp->name) == 0) return (bp); | |
93 | bpp = &bp->link; | |
94 | bp = *bpp; | |
95 | } | |
96 | ||
97 | *bpp = bp = make_bucket(name); | |
98 | last_symbol->next = bp; | |
99 | last_symbol = bp; | |
100 | ||
101 | return (bp); | |
102 | } | |
103 | ||
104 | ||
105 | create_symbol_table() | |
106 | { | |
107 | register int i; | |
108 | register bucket *bp; | |
109 | ||
110 | symbol_table = (bucket **) MALLOC(TABLE_SIZE*sizeof(bucket)); | |
111 | for (i = 0; i < TABLE_SIZE; i++) | |
112 | symbol_table[i] = 0; | |
113 | ||
114 | bp = make_bucket("error"); | |
115 | bp->index = 1; | |
116 | bp->class = TERM; | |
117 | ||
118 | first_symbol = bp; | |
119 | last_symbol = bp; | |
120 | symbol_table[hash("error")] = bp; | |
121 | } | |
122 | ||
123 | ||
124 | free_symbol_table() | |
125 | { | |
126 | FREE(symbol_table); | |
127 | symbol_table = 0; | |
128 | } | |
129 | ||
130 | ||
131 | free_symbols() | |
132 | { | |
133 | register bucket *p, *q; | |
134 | ||
135 | for (p = first_symbol; p; p = q) | |
136 | { | |
137 | q = p->next; | |
138 | FREE(p); | |
139 | } | |
140 | } |