* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
static char sccsid
[] = "@(#)symtab.c 5.1 (Berkeley) 6/6/85";
* Symbol table implementation.
* The symbol table structure is currently assumes no deletions.
#define MAXHASHSIZE 1009 /* largest allowable hash table */
* Macro to hash a string.
* The hash value is returned through the "h" parameter which should
* an unsigned integer. The other parameters are the symbol table, "st",
* and a pointer to the string to be hashed, "name".
#define hash(h, st, name) \
for (cp = name; *cp != '\0'; cp++) { \
* To create a symbol table, we allocate space for the symbols and
* for a hash table that's twice as big (+1 to make it odd).
st
->hshsize
= 2*size
+ 1;
if (st
->hshsize
> MAXHASHSIZE
) {
st
->hshsize
= MAXHASHSIZE
;
st
->symhsh
= alloc(st
->hshsize
, SYM
*);
st
->symarray
= alloc(st
->size
, SYM
);
for (i
= 0; i
< st
->hshsize
; i
++) {
* insert a symbol into a table
panic("tried to insert into NIL table");
if (st
->symindex
>= st
->size
) {
panic("too many symbols");
s
= &(st
->symarray
[st
->symindex
++]);
s
->next_sym
= st
->symhsh
[h
];
panic("tried to lookup in NIL table");
for (s
= st
->symhsh
[h
]; s
!= NIL
; s
= s
->next_sym
) {
if (strcmp(s
->symbol
, name
) == 0) {
* Dump out all the variables associated with the given
* procedure, function, or program at the given recursive level.
* This is quite inefficient. We traverse the entire symbol table
* each time we're called. The assumption is that this routine
* won't be called frequently enough to merit improved performance.
first
= symtab
->symarray
;
last
= first
+ symtab
->symindex
- 1;
for (s
= first
; s
<= last
; s
++) {
if (should_print(s
, f
)) {
* Create an alias for a command.
* We put it into the given table with block 1, which is how it
* is distinguished for printing purposes.
enter_alias(table
, new, old
)
if ((s
= st_lookup(table
, old
)) == NIL
) {
error("%s is not a known command", old
);
if (st_lookup(table
, new) != NIL
) {
error("cannot alias command names");
make_keyword(table
, new, s
->symvalue
.token
.toknum
);
t
= st_insert(table
, new);
t
->symvalue
.token
.toknum
= s
->symvalue
.token
.toknum
;
* Print out the currently active aliases.
* The kludge is that the type pointer for an alias points to the
* symbol it is aliased to.
s
= st_lookup(table
, name
);
error("\"%s\" is not an alias", name
);
printf("%s\n", s
->type
->symbol
);
last
= first
+ table
->symindex
- 1;
for (s
= first
; s
<= last
; s
++) {
printf("%s\t%s\n", s
->symbol
, s
->type
->symbol
);
* Find a named type that points to t; return NIL if there is none.
* This is necessary because of the way pi keeps symbols.
#define NSYMS_BACK 20 /* size of local context to try */
first
= symtab
->symarray
;
last
= first
+ symtab
->symindex
- 1;
if ((lower
= t
- NSYMS_BACK
) < first
) {
if ((s
= search(t
, lower
, last
)) == NIL
) {
s
= search(t
, first
, last
);
* Search the symbol table from first to last, looking for a
* named type pointing to the given type symbol.
LOCAL SYM
*search(t
, first
, last
)
register SYM
*first
, *last
;
for (s
= first
; s
<= last
; s
++) {
if (s
->class == TYPE
&& s
->type
== t
&& s
->symbol
!= NIL
) {