1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
18 |
|
* 11/16/88 Added VARDEF structure for hard linking. |
19 |
|
* |
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 |
< |
extern double dvalue(); |
41 |
> |
static double dvalue(); |
42 |
|
|
43 |
|
long eclock = -1; /* value storage timer */ |
44 |
|
|
45 |
+ |
static char context[MAXWORD]; /* current context path */ |
46 |
+ |
|
47 |
|
static VARDEF *hashtbl[NHASH]; /* definition list */ |
48 |
|
static int htndx; /* index for */ |
49 |
|
static VARDEF *htpos; /* ...dfirst() and */ |
74 |
|
eputs(": cannot open\n"); |
75 |
|
quit(1); |
76 |
|
} |
77 |
< |
initfile(fname, fp); |
77 |
> |
initfile(fp, fname, 0); |
78 |
|
while (nextc != EOF) |
79 |
< |
loaddefn(); |
79 |
> |
getstatement(); |
80 |
|
if (fname != NULL) |
81 |
|
fclose(fp); |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
< |
scompile(file, str) /* get definitions from a string */ |
80 |
< |
char *file; |
85 |
> |
scompile(str, fn, ln) /* get definitions from a string */ |
86 |
|
char *str; |
87 |
+ |
char *fn; |
88 |
+ |
int ln; |
89 |
|
{ |
90 |
< |
initstr(file, str); |
90 |
> |
initstr(str, fn, ln); |
91 |
|
while (nextc != EOF) |
92 |
< |
loaddefn(); |
92 |
> |
getstatement(); |
93 |
|
} |
94 |
|
|
95 |
|
|
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
< |
varset(vname, val) /* set a variable's value */ |
114 |
> |
varset(vname, assign, val) /* set a variable's value */ |
115 |
|
char *vname; |
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; |
128 |
< |
ep2->sibling->v.tick = -1; |
128 |
> |
ep1->type = assign; |
129 |
|
return; |
130 |
|
} |
131 |
|
} |
132 |
|
/* hand build definition */ |
133 |
|
ep1 = newnode(); |
134 |
< |
ep1->type = '='; |
134 |
> |
ep1->type = assign; |
135 |
|
ep2 = newnode(); |
136 |
|
ep2->type = SYM; |
137 |
|
ep2->v.name = savestr(vname); |
140 |
|
ep2->type = NUM; |
141 |
|
ep2->v.num = val; |
142 |
|
addekid(ep1, ep2); |
143 |
< |
ep2 = newnode(); |
144 |
< |
ep2->type = TICK; |
134 |
< |
ep2->v.tick = -1; |
135 |
< |
addekid(ep1, ep2); |
136 |
< |
ep2 = newnode(); |
137 |
< |
ep2->type = NUM; |
138 |
< |
addekid(ep1, ep2); |
139 |
< |
dclear(vname); |
140 |
< |
dpush(ep1); |
143 |
> |
dremove(qname); |
144 |
> |
dpush(qname, ep1); |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
< |
dclear(name) /* delete all definitions of name */ |
148 |
> |
dclear(name) /* delete variable definitions of name */ |
149 |
|
char *name; |
150 |
|
{ |
151 |
|
register EPNODE *ep; |
152 |
|
|
153 |
+ |
while ((ep = dpop(name)) != NULL) { |
154 |
+ |
if (ep->type == ':') { |
155 |
+ |
dpush(name, ep); /* don't clear constants */ |
156 |
+ |
return; |
157 |
+ |
} |
158 |
+ |
epfree(ep); |
159 |
+ |
} |
160 |
+ |
} |
161 |
+ |
|
162 |
+ |
|
163 |
+ |
dremove(name) /* delete all definitions of name */ |
164 |
+ |
char *name; |
165 |
+ |
{ |
166 |
+ |
register EPNODE *ep; |
167 |
+ |
|
168 |
|
while ((ep = dpop(name)) != NULL) |
169 |
|
epfree(ep); |
170 |
|
} |
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-1) { |
199 |
+ |
*cpp = '\0'; |
200 |
+ |
wputs(context); |
201 |
+ |
wputs(": context path too long\n"); |
202 |
+ |
return(NULL); |
203 |
+ |
} |
204 |
+ |
if (isid(*ctx)) |
205 |
+ |
*cpp++ = *ctx++; |
206 |
+ |
else { |
207 |
+ |
*cpp++ = '_'; ctx++; |
208 |
+ |
} |
209 |
+ |
} while (*ctx); |
210 |
+ |
*cpp = '\0'; |
211 |
+ |
return(context); |
212 |
+ |
} |
213 |
+ |
|
214 |
+ |
|
215 |
+ |
char * |
216 |
+ |
qualname(nam, lvl) /* get qualified name */ |
217 |
+ |
register char *nam; |
218 |
+ |
int lvl; |
219 |
+ |
{ |
220 |
+ |
static char nambuf[MAXWORD]; |
221 |
+ |
register char *cp = nambuf, *cpp = context; |
222 |
+ |
/* check for explicit global */ |
223 |
+ |
if (*nam == CNTXMARK) |
224 |
+ |
return(lvl > 0 ? NULL : nam+1); |
225 |
+ |
/* check for repeat call */ |
226 |
+ |
if (nam == nambuf) |
227 |
+ |
return(lvl > 0 ? NULL : nambuf); |
228 |
+ |
/* copy name to static buffer */ |
229 |
+ |
while (*nam) { |
230 |
+ |
if (cp >= nambuf+MAXWORD-1) |
231 |
+ |
goto toolong; |
232 |
+ |
if ((*cp++ = *nam++) == CNTXMARK) |
233 |
+ |
cpp = NULL; /* flag a qualified name */ |
234 |
+ |
} |
235 |
+ |
if (cpp == NULL) { |
236 |
+ |
if (lvl > 0) |
237 |
+ |
return(NULL); /* no higher level */ |
238 |
+ |
if (cp[-1] == CNTXMARK) { |
239 |
+ |
cp--; cpp = context; /* current context explicitly */ |
240 |
+ |
} else |
241 |
+ |
cpp = ""; /* else fully qualified */ |
242 |
+ |
} else /* else skip the requested levels */ |
243 |
+ |
while (lvl-- > 0) { |
244 |
+ |
if (!*cpp) |
245 |
+ |
return(NULL); /* return NULL if past global level */ |
246 |
+ |
while (*++cpp && *cpp != CNTXMARK) |
247 |
+ |
; |
248 |
+ |
} |
249 |
+ |
while (*cpp) { /* copy context to static buffer */ |
250 |
+ |
if (cp >= nambuf+MAXWORD-1) |
251 |
+ |
goto toolong; |
252 |
+ |
*cp++ = *cpp++; |
253 |
+ |
} |
254 |
+ |
*cp = '\0'; |
255 |
+ |
return(nambuf); /* return qualified name */ |
256 |
+ |
toolong: |
257 |
+ |
*cp = '\0'; |
258 |
+ |
eputs(nambuf); |
259 |
+ |
eputs(": name too long\n"); |
260 |
+ |
quit(1); |
261 |
+ |
} |
262 |
+ |
|
263 |
+ |
|
264 |
+ |
incontext(qn) /* is qualified name in current context? */ |
265 |
+ |
register char *qn; |
266 |
+ |
{ |
267 |
+ |
while (*qn && *qn != CNTXMARK) /* find context mark */ |
268 |
+ |
; |
269 |
+ |
return(!strcmp(qn, context)); |
270 |
+ |
} |
271 |
+ |
|
272 |
+ |
|
273 |
|
#ifdef OUTCHAN |
274 |
< |
chanout() /* set output channels */ |
274 |
> |
chanout(cs) /* set output channels */ |
275 |
> |
int (*cs)(); |
276 |
|
{ |
277 |
|
register EPNODE *ep; |
278 |
|
|
279 |
|
for (ep = outchan; ep != NULL; ep = ep->sibling) |
280 |
< |
chanset(ep->v.kid->v.chan, evalue(ep->v.kid->sibling)); |
280 |
> |
(*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling)); |
281 |
|
|
282 |
|
} |
283 |
|
#endif |
284 |
|
|
285 |
|
|
286 |
< |
dclearall() /* clear all definitions */ |
286 |
> |
dcleanup(lvl) /* clear definitions (0->vars,1->output,2->consts) */ |
287 |
> |
int lvl; |
288 |
|
{ |
289 |
|
register int i; |
290 |
|
register VARDEF *vp; |
291 |
|
register EPNODE *ep; |
292 |
< |
|
292 |
> |
/* if context is global, clear all */ |
293 |
|
for (i = 0; i < NHASH; i++) |
294 |
|
for (vp = hashtbl[i]; vp != NULL; vp = vp->next) |
295 |
< |
dclear(vp->name); |
295 |
> |
if (!context[0] || incontext(vp->name)) |
296 |
> |
if (lvl >= 2) |
297 |
> |
dremove(vp->name); |
298 |
> |
else |
299 |
> |
dclear(vp->name); |
300 |
|
#ifdef OUTCHAN |
301 |
< |
for (ep = outchan; ep != NULL; ep = ep->sibling) |
302 |
< |
epfree(ep); |
303 |
< |
outchan = NULL; |
301 |
> |
if (lvl >= 1) { |
302 |
> |
for (ep = outchan; ep != NULL; ep = ep->sibling) |
303 |
> |
epfree(ep); |
304 |
> |
outchan = NULL; |
305 |
> |
} |
306 |
|
#endif |
307 |
|
} |
308 |
|
|
323 |
|
varlookup(name) /* look up a variable */ |
324 |
|
char *name; |
325 |
|
{ |
326 |
+ |
int lvl = 0; |
327 |
+ |
register char *qname; |
328 |
|
register VARDEF *vp; |
329 |
< |
|
330 |
< |
for (vp = hashtbl[hash(name)]; vp != NULL; vp = vp->next) |
331 |
< |
if (!strcmp(vp->name, name)) |
332 |
< |
return(vp); |
329 |
> |
/* find most qualified match */ |
330 |
> |
while ((qname = qualname(name, lvl++)) != NULL) |
331 |
> |
for (vp = hashtbl[hash(qname)]; vp != NULL; vp = vp->next) |
332 |
> |
if (!strcmp(vp->name, qname)) |
333 |
> |
return(vp); |
334 |
|
return(NULL); |
335 |
|
} |
336 |
|
|
342 |
|
register VARDEF *vp; |
343 |
|
int hv; |
344 |
|
|
345 |
+ |
if ((vp = varlookup(name)) != NULL) { |
346 |
+ |
vp->nlinks++; |
347 |
+ |
return(vp); |
348 |
+ |
} |
349 |
+ |
name = qualname(name, 0); /* use fully qualified name */ |
350 |
|
hv = hash(name); |
225 |
– |
for (vp = hashtbl[hv]; vp != NULL; vp = vp->next) |
226 |
– |
if (!strcmp(vp->name, name)) { |
227 |
– |
vp->nlinks++; |
228 |
– |
return(vp); |
229 |
– |
} |
351 |
|
vp = (VARDEF *)emalloc(sizeof(VARDEF)); |
352 |
|
vp->name = savestr(name); |
353 |
|
vp->nlinks = 1; |
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 |
+ |
char *qname; |
485 |
+ |
EPNODE *lastdef; |
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 (dlookup(dname(ep)) != NULL) { |
502 |
< |
dclear(dname(ep)); |
503 |
< |
wputs(dname(ep)); |
501 |
> |
if ((lastdef = dlookup(qname)) != NULL) { |
502 |
> |
wputs(qname); |
503 |
> |
if (lastdef->type == ':') |
504 |
> |
wputs(": redefined constant expression\n"); |
505 |
> |
else |
506 |
|
wputs(": redefined\n"); |
507 |
|
} |
508 |
< |
#else |
509 |
< |
dclear(dname(ep)); |
508 |
> |
#ifdef FUNCTION |
509 |
> |
else if (ep->v.kid->type == FUNC && liblookup(qname) != NULL) { |
510 |
> |
wputs(qname); |
511 |
> |
wputs(": definition hides library function\n"); |
512 |
> |
} |
513 |
|
#endif |
514 |
< |
dpush(ep); |
514 |
> |
#endif |
515 |
> |
if (ep->type == ':') |
516 |
> |
dremove(qname); |
517 |
> |
else |
518 |
> |
dclear(qname); |
519 |
> |
dpush(qname, ep); |
520 |
|
} |
521 |
|
if (nextc != EOF) { |
522 |
|
if (nextc != ';') |
528 |
|
|
529 |
|
EPNODE * |
530 |
|
getdefn() /* A -> SYM = E1 */ |
531 |
+ |
/* SYM : E1 */ |
532 |
|
/* FUNC(SYM,..) = E1 */ |
533 |
+ |
/* FUNC(SYM,..) : E1 */ |
534 |
|
{ |
535 |
|
register EPNODE *ep1, *ep2; |
536 |
|
|
564 |
|
curfunc = NULL; |
565 |
|
#endif |
566 |
|
|
567 |
< |
if (nextc != '=') |
568 |
< |
syntax("'=' expected"); |
432 |
< |
scan(); |
567 |
> |
if (nextc != '=' && nextc != ':') |
568 |
> |
syntax("'=' or ':' expected"); |
569 |
|
|
570 |
|
ep2 = newnode(); |
571 |
< |
ep2->type = '='; |
571 |
> |
ep2->type = nextc; |
572 |
> |
scan(); |
573 |
|
addekid(ep2, ep1); |
574 |
|
addekid(ep2, getE1()); |
575 |
|
|
576 |
+ |
if ( |
577 |
|
#ifdef FUNCTION |
578 |
< |
if (ep1->type == SYM) |
578 |
> |
ep1->type == SYM && |
579 |
|
#endif |
580 |
< |
{ |
580 |
> |
ep1->sibling->type != NUM) { |
581 |
|
ep1 = newnode(); |
582 |
|
ep1->type = TICK; |
583 |
|
ep1->v.tick = -1; |
638 |
|
quit(1); |
639 |
|
} |
640 |
|
ep1 = d->v.kid->sibling; /* get expression */ |
641 |
+ |
if (ep1->type == NUM) |
642 |
+ |
return(ep1->v.num); /* return if number */ |
643 |
|
ep2 = ep1->sibling; /* check time */ |
644 |
|
if (ep2->v.tick < 0 || ep2->v.tick < eclock) { |
645 |
< |
ep2->v.tick = eclock; |
645 |
> |
ep2->v.tick = d->type == ':' ? 1L<<30 : eclock; |
646 |
|
ep2 = ep2->sibling; |
647 |
< |
ep2->v.num = evalue(ep1); /* compute new value */ |
647 |
> |
ep2->v.num = evalue(ep1); /* needs new value */ |
648 |
|
} else |
649 |
< |
ep2 = ep2->sibling; /* reuse old value */ |
649 |
> |
ep2 = ep2->sibling; /* else reuse old value */ |
650 |
|
|
651 |
|
return(ep2->v.num); |
652 |
|
} |