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