2 /********************************************
4 copyright 1991, Michael D. Brennan
6 This is a source file for mawk, an implementation of
7 the AWK programming language.
9 Mawk is distributed without warranty under the terms of
10 the GNU General Public License, version 2, 1991.
11 ********************************************/
15 * Revision 1.3 1994/10/08 19:15:43 mike
18 * Revision 1.2 1993/07/16 00:17:35 mike
21 * Revision 1.1.1.1 1993/07/03 18:58:14 mike
24 * Revision 5.1 1991/12/05 07:56:05 brennan
41 register unsigned h = 0 ;
43 while (*s) h += h + *s++ ;
53 static HASHNODE *PROTO(delete, (char *)) ;
55 static HASHNODE *hash_table[HASH_PRIME] ;
58 insert a string in the symbol table.
59 Caller knows the symbol is not there
60 -- used for initialization
67 register HASHNODE *p = ZMALLOC(HASHNODE) ;
70 p->link = hash_table[h = hash(s) % HASH_PRIME] ;
76 /* Find s in the symbol table,
77 if not there insert it, s must be dup'ed */
83 register HASHNODE *p ;
87 p = hash_table[h = hash(s) % HASH_PRIME] ;
93 p = ZMALLOC(HASHNODE) ;
94 p->symtab.type = ST_NONE ;
95 p->symtab.name = strcpy(zmalloc(strlen(s) + 1), s) ;
99 if (strcmp(p->symtab.name, s) == 0) /* found */
101 if (!q) /* already at the front */
103 else /* delete from the list */
105 q->link = p->link ; break ;
109 q = p ; p = p->link ;
111 /* put p on front of the list */
112 p->link = hash_table[h] ;
118 /* remove a node from the hash table
119 return a ptr to the node */
121 static unsigned last_hash ;
127 register HASHNODE *p ;
128 HASHNODE *q = (HASHNODE *) 0 ;
131 p = hash_table[last_hash = h = hash(s) % HASH_PRIME] ;
134 if (strcmp(p->symtab.name, s) == 0) /* found */
136 if (q) q->link = p->link ;
137 else hash_table[h] = p->link ;
142 q = p ; p = p->link ;
146 #ifdef DEBUG /* we should not ever get here */
149 return (HASHNODE *) 0 ;
152 /* when processing user functions, global ids which are
153 replaced by local ids are saved on this list */
155 static HASHNODE *save_list ;
157 /* store a global id on the save list,
158 return a ptr to the local symtab */
167 q = ZMALLOC(HASHNODE) ;
168 q->symtab.type = ST_LOCAL_NONE ;
169 q->symtab.name = p->symtab.name ;
170 /* put q in the hash table */
171 q->link = hash_table[h = last_hash] ;
175 p->link = save_list ; save_list = p ;
180 /* restore all global indentifiers */
184 register HASHNODE *p, *q ;
185 register unsigned h ;
187 q = save_list ; save_list = (HASHNODE *) 0 ;
190 p = q ; q = q->link ;
191 zfree(delete(p->symtab.name), sizeof(HASHNODE)) ;
192 p->link = hash_table[h = last_hash] ;
198 /* search the symbol table backwards for the
199 disassembler. This is slow -- so what
204 reverse_find(type, ptr)
210 static char uk[] = "unknown" ;
220 cp = *(CELL **) ptr ;
224 array = *(ARRAY *) ptr ;
231 for (i = 0; i < HASH_PRIME; i++)
236 if (p->symtab.type == type)
242 if (cp == p->symtab.stval.cp)
243 return p->symtab.name ;
247 if (array == p->symtab.stval.array)
248 return p->symtab.name ;