xref: /original-bsd/usr.bin/pascal/src/lookup.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)lookup.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "whoami.h"
13 #include "0.h"
14 
15 struct nl *disptab[077+1];
16 
17 /*
18  * Lookup is called to
19  * find a symbol in the
20  * block structure symbol
21  * table and returns a pointer to
22  * its namelist entry.
23  */
24 struct nl *
25 lookup(s)
26 	register char *s;
27 {
28 	register struct nl *p;
29 	register struct udinfo;
30 
31 	if (s == NIL) {
32 		nocascade();
33 		return (NLNIL);
34 	}
35 	p = lookup1(s);
36 	if (p == NLNIL) {
37 		derror("%s is undefined", s);
38 		return (NLNIL);
39 	}
40 	if (p->class == FVAR) {
41 		p = p->chain;
42 		bn--;
43 	}
44 	return (p);
45 }
46 
47 #ifndef PI0
48 int	flagwas;
49 #endif
50 /*
51  * Lookup1 is an internal lookup.
52  * It is not an error to call lookup1
53  * if the symbol is not defined.  Also
54  * lookup1 will return FVARs while
55  * lookup never will, thus asgnop
56  * calls it when it thinks you are
57  * assigning to the function variable.
58  */
59 
60 struct nl *
61 lookup1(s)
62 	register char *s;
63 {
64 	register struct nl *p;
65 #ifndef PI0
66 	register struct nl *q;
67 #endif
68 	register int i;
69 
70 	if (s == NIL)
71 		return (NLNIL);
72 	bn = cbn;
73 #ifndef PI0
74 	/*
75 	 * We first check the field names
76 	 * of the currently active with
77 	 * statements (expensive since they
78 	 * are not hashed).
79 	 */
80 	for (p = withlist; p != NLNIL; p = p->nl_next) {
81 		q = p->type;
82 		if (q == NLNIL)
83 			continue;
84 		if (reclook(q, s) != NIL)
85 			/*
86 			 * Return the WITHPTR, lvalue understands.
87 			 */
88 			return (p);
89 	}
90 #endif
91 	/*
92 	 * Symbol table is a 64 way hash
93 	 * on the low bits of the character
94 	 * pointer value. (Simple, but effective)
95 	 */
96 	i = (int) s & 077;
97 	for (p = disptab[i]; p != NLNIL; p = p->nl_next)
98 		if (p->symbol == s && p->class != FIELD && p->class != BADUSE) {
99 			bn = (p->nl_block & 037);
100 #ifndef PI0
101 			flagwas = p->nl_flags;
102 			p->nl_flags |= NUSED;
103 #endif
104 			return (p);
105 		}
106 	return (NLNIL);
107 }
108 
109 #ifndef PI01
110 nlfund(sp)
111 	char *sp;
112 {
113 	register struct nl *p;
114 	register int i;
115 
116 	i = (int) sp & 077;
117 	for (p = disptab[i]; p != NLNIL; p = p->nl_next)
118 	if (p->symbol == sp && (p->nl_block & 037) == 0)
119 		return (nloff(p));
120 	return (0);
121 }
122 #endif
123