20 |
|
* 5/31/90 Added conditional compile (REDEFW) for redefinition warning. |
21 |
|
* |
22 |
|
* 4/23/91 Added ':' assignment for constant expressions |
23 |
+ |
* |
24 |
+ |
* 8/7/91 Added optional context path to append to variable names |
25 |
|
*/ |
26 |
|
|
27 |
|
#include <stdio.h> |
36 |
|
|
37 |
|
#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) |
38 |
|
|
39 |
< |
extern char *ecalloc(), *savestr(); |
39 |
> |
extern char *ecalloc(), *savestr(), *strcpy(); |
40 |
|
|
41 |
|
static double dvalue(); |
42 |
|
|
43 |
|
long eclock = -1; /* value storage timer */ |
44 |
|
|
45 |
+ |
static char context[MAXWORD+1]; /* current context path */ |
46 |
+ |
|
47 |
|
static VARDEF *hashtbl[NHASH]; /* definition list */ |
48 |
|
static int htndx; /* index for */ |
49 |
|
static VARDEF *htpos; /* ...dfirst() and */ |
76 |
|
} |
77 |
|
initfile(fp, fname, 0); |
78 |
|
while (nextc != EOF) |
79 |
< |
loaddefn(); |
79 |
> |
getstatement(); |
80 |
|
if (fname != NULL) |
81 |
|
fclose(fp); |
82 |
|
} |
89 |
|
{ |
90 |
|
initstr(str, fn, ln); |
91 |
|
while (nextc != EOF) |
92 |
< |
loaddefn(); |
92 |
> |
getstatement(); |
93 |
|
} |
94 |
|
|
95 |
|
|
116 |
|
int assign; |
117 |
|
double val; |
118 |
|
{ |
119 |
+ |
char *qname; |
120 |
|
register EPNODE *ep1, *ep2; |
121 |
+ |
/* get qualified name */ |
122 |
+ |
qname = qualname(vname, 0); |
123 |
|
/* check for quick set */ |
124 |
< |
if ((ep1 = dlookup(vname)) != NULL && ep1->v.kid->type == SYM) { |
124 |
> |
if ((ep1 = dlookup(qname)) != NULL && ep1->v.kid->type == SYM) { |
125 |
|
ep2 = ep1->v.kid->sibling; |
126 |
|
if (ep2->type == NUM) { |
127 |
|
ep2->v.num = val; |
140 |
|
ep2->type = NUM; |
141 |
|
ep2->v.num = val; |
142 |
|
addekid(ep1, ep2); |
143 |
< |
dremove(vname); |
144 |
< |
dpush(ep1); |
143 |
> |
dremove(qname); |
144 |
> |
dpush(qname, ep1); |
145 |
|
} |
146 |
|
|
147 |
|
|
152 |
|
|
153 |
|
while ((ep = dpop(name)) != NULL) { |
154 |
|
if (ep->type == ':') { |
155 |
< |
dpush(ep); /* don't clear constants */ |
155 |
> |
dpush(name, ep); /* don't clear constants */ |
156 |
|
return; |
157 |
|
} |
158 |
|
epfree(ep); |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
+ |
char * |
183 |
+ |
setcontext(ctx) /* set a new context path */ |
184 |
+ |
register char *ctx; |
185 |
+ |
{ |
186 |
+ |
register char *cpp; |
187 |
+ |
|
188 |
+ |
if (ctx == NULL) |
189 |
+ |
return(context); /* just asking */ |
190 |
+ |
if (!*ctx) { |
191 |
+ |
context[0] = '\0'; /* clear context */ |
192 |
+ |
return(context); |
193 |
+ |
} |
194 |
+ |
cpp = context; /* else copy it (carefully!) */ |
195 |
+ |
if (*ctx != CNTXMARK) |
196 |
+ |
*cpp++ = CNTXMARK; /* make sure there's a mark */ |
197 |
+ |
do { |
198 |
+ |
if (cpp >= context+MAXWORD) |
199 |
+ |
break; /* just copy what we can */ |
200 |
+ |
if (isid(*ctx)) |
201 |
+ |
*cpp++ = *ctx++; |
202 |
+ |
else { |
203 |
+ |
*cpp++ = '_'; ctx++; |
204 |
+ |
} |
205 |
+ |
} while (*ctx); |
206 |
+ |
*cpp = '\0'; |
207 |
+ |
return(context); |
208 |
+ |
} |
209 |
+ |
|
210 |
+ |
|
211 |
+ |
char * |
212 |
+ |
qualname(nam, lvl) /* get qualified name */ |
213 |
+ |
register char *nam; |
214 |
+ |
int lvl; |
215 |
+ |
{ |
216 |
+ |
static char nambuf[MAXWORD+1]; |
217 |
+ |
register char *cp = nambuf, *cpp; |
218 |
+ |
/* check for explicit local */ |
219 |
+ |
if (*nam == CNTXMARK) |
220 |
+ |
if (lvl > 0) /* only action is to refuse search */ |
221 |
+ |
return(NULL); |
222 |
+ |
else |
223 |
+ |
nam++; |
224 |
+ |
else if (nam == nambuf) /* check for repeat call */ |
225 |
+ |
return(lvl > 0 ? NULL : nam); |
226 |
+ |
/* copy name to static buffer */ |
227 |
+ |
while (*nam) { |
228 |
+ |
if (cp >= nambuf+MAXWORD) |
229 |
+ |
goto toolong; |
230 |
+ |
*cp++ = *nam++; |
231 |
+ |
} |
232 |
+ |
/* check for explicit global */ |
233 |
+ |
if (cp > nambuf && cp[-1] == CNTXMARK) { |
234 |
+ |
if (lvl > 0) |
235 |
+ |
return(NULL); |
236 |
+ |
*--cp = '\0'; |
237 |
+ |
return(nambuf); /* already qualified */ |
238 |
+ |
} |
239 |
+ |
cpp = context; /* else skip the requested levels */ |
240 |
+ |
while (lvl-- > 0) { |
241 |
+ |
if (!*cpp) |
242 |
+ |
return(NULL); /* return NULL if past global level */ |
243 |
+ |
while (*++cpp && *cpp != CNTXMARK) |
244 |
+ |
; |
245 |
+ |
} |
246 |
+ |
while (*cpp) { /* copy context to static buffer */ |
247 |
+ |
if (cp >= nambuf+MAXWORD) |
248 |
+ |
goto toolong; |
249 |
+ |
*cp++ = *cpp++; |
250 |
+ |
} |
251 |
+ |
toolong: |
252 |
+ |
*cp = '\0'; |
253 |
+ |
return(nambuf); /* return qualified name */ |
254 |
+ |
} |
255 |
+ |
|
256 |
+ |
|
257 |
+ |
incontext(qn) /* is qualified name in current context? */ |
258 |
+ |
register char *qn; |
259 |
+ |
{ |
260 |
+ |
while (*qn && *qn != CNTXMARK) /* find context mark */ |
261 |
+ |
; |
262 |
+ |
return(!strcmp(qn, context)); |
263 |
+ |
} |
264 |
+ |
|
265 |
+ |
|
266 |
|
#ifdef OUTCHAN |
267 |
|
chanout(cs) /* set output channels */ |
268 |
|
int (*cs)(); |
276 |
|
#endif |
277 |
|
|
278 |
|
|
279 |
< |
dcleanup(lvl) /* clear definitions (0->vars,1->consts,2->output) */ |
279 |
> |
dcleanup(lvl) /* clear definitions (0->vars,1->output,2->consts) */ |
280 |
|
int lvl; |
281 |
|
{ |
282 |
|
register int i; |
283 |
|
register VARDEF *vp; |
284 |
|
register EPNODE *ep; |
285 |
< |
|
285 |
> |
/* if context is global, clear all */ |
286 |
|
for (i = 0; i < NHASH; i++) |
287 |
|
for (vp = hashtbl[i]; vp != NULL; vp = vp->next) |
288 |
< |
if (lvl >= 1) |
289 |
< |
dremove(vp->name); |
290 |
< |
else |
291 |
< |
dclear(vp->name); |
288 |
> |
if (!context[0] || incontext(vp->name)) |
289 |
> |
if (lvl >= 2) |
290 |
> |
dremove(vp->name); |
291 |
> |
else |
292 |
> |
dclear(vp->name); |
293 |
|
#ifdef OUTCHAN |
294 |
< |
if (lvl >= 2) { |
294 |
> |
if (lvl >= 1) { |
295 |
|
for (ep = outchan; ep != NULL; ep = ep->sibling) |
296 |
|
epfree(ep); |
297 |
|
outchan = NULL; |
316 |
|
varlookup(name) /* look up a variable */ |
317 |
|
char *name; |
318 |
|
{ |
319 |
+ |
int lvl = 0; |
320 |
+ |
register char *qname; |
321 |
|
register VARDEF *vp; |
322 |
< |
|
323 |
< |
for (vp = hashtbl[hash(name)]; vp != NULL; vp = vp->next) |
324 |
< |
if (!strcmp(vp->name, name)) |
325 |
< |
return(vp); |
322 |
> |
/* find most qualified match */ |
323 |
> |
while ((qname = qualname(name, lvl++)) != NULL) |
324 |
> |
for (vp = hashtbl[hash(qname)]; vp != NULL; vp = vp->next) |
325 |
> |
if (!strcmp(vp->name, qname)) |
326 |
> |
return(vp); |
327 |
|
return(NULL); |
328 |
|
} |
329 |
|
|
333 |
|
char *name; |
334 |
|
{ |
335 |
|
register VARDEF *vp; |
336 |
+ |
LIBR *libp; |
337 |
|
int hv; |
338 |
|
|
339 |
+ |
if ((vp = varlookup(name)) != NULL) { |
340 |
+ |
vp->nlinks++; |
341 |
+ |
return(vp); |
342 |
+ |
} |
343 |
+ |
#ifdef FUNCTION |
344 |
+ |
libp = liblookup(name); |
345 |
+ |
#else |
346 |
+ |
libp = NULL; |
347 |
+ |
#endif |
348 |
+ |
if (libp == NULL) /* if name not in library */ |
349 |
+ |
name = qualname(name, 0); /* use fully qualified version */ |
350 |
|
hv = hash(name); |
244 |
– |
for (vp = hashtbl[hv]; vp != NULL; vp = vp->next) |
245 |
– |
if (!strcmp(vp->name, name)) { |
246 |
– |
vp->nlinks++; |
247 |
– |
return(vp); |
248 |
– |
} |
351 |
|
vp = (VARDEF *)emalloc(sizeof(VARDEF)); |
352 |
|
vp->name = savestr(name); |
353 |
|
vp->nlinks = 1; |
354 |
|
vp->def = NULL; |
355 |
< |
vp->lib = NULL; |
355 |
> |
vp->lib = libp; |
356 |
|
vp->next = hashtbl[hv]; |
357 |
|
hashtbl[hv] = vp; |
358 |
|
return(vp); |
435 |
|
} |
436 |
|
|
437 |
|
|
438 |
< |
dpush(ep) /* push on a definition */ |
438 |
> |
dpush(nm, ep) /* push on a definition */ |
439 |
> |
char *nm; |
440 |
|
register EPNODE *ep; |
441 |
|
{ |
442 |
|
register VARDEF *vp; |
443 |
|
|
444 |
< |
vp = varinsert(dname(ep)); |
444 |
> |
vp = varinsert(nm); |
445 |
|
ep->sibling = vp->def; |
446 |
|
vp->def = ep; |
447 |
|
} |
478 |
|
#endif |
479 |
|
|
480 |
|
|
481 |
< |
loaddefn() /* load next definition */ |
481 |
> |
getstatement() /* get next statement */ |
482 |
|
{ |
483 |
|
register EPNODE *ep; |
484 |
< |
EPNODE *lastdef; |
484 |
> |
char *qname; |
485 |
> |
register VARDEF *vdef; |
486 |
|
|
487 |
|
if (nextc == ';') { /* empty statement */ |
488 |
|
scan(); |
496 |
|
#endif |
497 |
|
{ /* ordinary definition */ |
498 |
|
ep = getdefn(); |
499 |
+ |
qname = qualname(dname(ep), 0); |
500 |
|
#ifdef REDEFW |
501 |
< |
if ((lastdef = dlookup(dname(ep))) != NULL) { |
502 |
< |
wputs(dname(ep)); |
503 |
< |
if (lastdef->type == ':') |
504 |
< |
wputs(": redefined constant expression\n"); |
505 |
< |
else |
506 |
< |
wputs(": redefined\n"); |
507 |
< |
} |
501 |
> |
if ((vdef = varlookup(qname)) != NULL) |
502 |
> |
if (vdef->def != NULL) { |
503 |
> |
wputs(qname); |
504 |
> |
if (vdef->def->type == ':') |
505 |
> |
wputs(": redefined constant expression\n"); |
506 |
> |
else |
507 |
> |
wputs(": redefined\n"); |
508 |
> |
} |
509 |
|
#ifdef FUNCTION |
510 |
< |
else if (ep->v.kid->type == FUNC && |
511 |
< |
liblookup(ep->v.kid->v.kid->v.name) != NULL) { |
512 |
< |
wputs(ep->v.kid->v.kid->v.name); |
513 |
< |
wputs(": definition hides library function\n"); |
408 |
< |
} |
510 |
> |
else if (ep->v.kid->type == FUNC && vdef->lib != NULL) { |
511 |
> |
wputs(qname); |
512 |
> |
wputs(": definition hides library function\n"); |
513 |
> |
} |
514 |
|
#endif |
515 |
|
#endif |
516 |
|
if (ep->type == ':') |
517 |
< |
dremove(dname(ep)); |
517 |
> |
dremove(qname); |
518 |
|
else |
519 |
< |
dclear(dname(ep)); |
520 |
< |
dpush(ep); |
519 |
> |
dclear(qname); |
520 |
> |
dpush(qname, ep); |
521 |
|
} |
522 |
|
if (nextc != EOF) { |
523 |
|
if (nextc != ';') |
535 |
|
{ |
536 |
|
register EPNODE *ep1, *ep2; |
537 |
|
|
538 |
< |
if (!isalpha(nextc)) |
538 |
> |
if (!isalpha(nextc) && nextc != CNTXMARK) |
539 |
|
syntax("illegal variable name"); |
540 |
|
|
541 |
|
ep1 = newnode(); |