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