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 |
|
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 */ |
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 |
|
|
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; |
135 |
< |
ep2->v.tick = -1; |
136 |
< |
addekid(ep1, ep2); |
137 |
< |
ep2 = newnode(); |
138 |
< |
ep2->type = NUM; |
139 |
< |
addekid(ep1, ep2); |
140 |
< |
dclear(vname); |
141 |
< |
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 repeat call */ |
223 |
+ |
if (nam == nambuf) |
224 |
+ |
return(lvl > 0 ? NULL : nambuf); |
225 |
+ |
/* copy name to static buffer */ |
226 |
+ |
while (*nam) { |
227 |
+ |
if (cp >= nambuf+MAXWORD-1) |
228 |
+ |
goto toolong; |
229 |
+ |
if ((*cp++ = *nam++) == CNTXMARK) |
230 |
+ |
cpp = NULL; /* flag a qualified name */ |
231 |
+ |
} |
232 |
+ |
if (cpp == NULL) { |
233 |
+ |
if (lvl > 0) |
234 |
+ |
return(NULL); /* no higher level */ |
235 |
+ |
if (cp[-1] == CNTXMARK) { |
236 |
+ |
cp--; cpp = context; /* current context explicitly */ |
237 |
+ |
} else |
238 |
+ |
cpp = ""; /* else fully qualified */ |
239 |
+ |
} else /* 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-1) |
248 |
+ |
goto toolong; |
249 |
+ |
*cp++ = *cpp++; |
250 |
+ |
} |
251 |
+ |
*cp = '\0'; |
252 |
+ |
return(nambuf); /* return qualified name */ |
253 |
+ |
toolong: |
254 |
+ |
*cp = '\0'; |
255 |
+ |
wputs(nambuf); |
256 |
+ |
wputs(": name too long\n"); |
257 |
+ |
return(NULL); |
258 |
+ |
} |
259 |
+ |
|
260 |
+ |
|
261 |
|
#ifdef OUTCHAN |
262 |
< |
chanout() /* set output channels */ |
262 |
> |
chanout(cs) /* set output channels */ |
263 |
> |
int (*cs)(); |
264 |
|
{ |
265 |
|
register EPNODE *ep; |
266 |
|
|
267 |
|
for (ep = outchan; ep != NULL; ep = ep->sibling) |
268 |
< |
chanset(ep->v.kid->v.chan, evalue(ep->v.kid->sibling)); |
268 |
> |
(*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling)); |
269 |
|
|
270 |
|
} |
271 |
|
#endif |
272 |
|
|
273 |
|
|
274 |
< |
dclearall() /* clear all definitions */ |
274 |
> |
dcleanup(lvl) /* clear definitions (0->vars,1->output,2->consts) */ |
275 |
> |
int lvl; |
276 |
|
{ |
277 |
|
register int i; |
278 |
|
register VARDEF *vp; |
280 |
|
|
281 |
|
for (i = 0; i < NHASH; i++) |
282 |
|
for (vp = hashtbl[i]; vp != NULL; vp = vp->next) |
283 |
< |
dclear(vp->name); |
283 |
> |
if (lvl >= 2) |
284 |
> |
dremove(vp->name); |
285 |
> |
else |
286 |
> |
dclear(vp->name); |
287 |
|
#ifdef OUTCHAN |
288 |
< |
for (ep = outchan; ep != NULL; ep = ep->sibling) |
289 |
< |
epfree(ep); |
290 |
< |
outchan = NULL; |
288 |
> |
if (lvl >= 1) { |
289 |
> |
for (ep = outchan; ep != NULL; ep = ep->sibling) |
290 |
> |
epfree(ep); |
291 |
> |
outchan = NULL; |
292 |
> |
} |
293 |
|
#endif |
294 |
|
} |
295 |
|
|
310 |
|
varlookup(name) /* look up a variable */ |
311 |
|
char *name; |
312 |
|
{ |
313 |
+ |
int lvl = 0; |
314 |
+ |
register char *qname; |
315 |
|
register VARDEF *vp; |
316 |
< |
|
317 |
< |
for (vp = hashtbl[hash(name)]; vp != NULL; vp = vp->next) |
318 |
< |
if (!strcmp(vp->name, name)) |
319 |
< |
return(vp); |
316 |
> |
/* find most qualified match */ |
317 |
> |
while ((qname = qualname(name, lvl++)) != NULL) |
318 |
> |
for (vp = hashtbl[hash(qname)]; vp != NULL; vp = vp->next) |
319 |
> |
if (!strcmp(vp->name, qname)) |
320 |
> |
return(vp); |
321 |
|
return(NULL); |
322 |
|
} |
323 |
|
|
329 |
|
register VARDEF *vp; |
330 |
|
int hv; |
331 |
|
|
332 |
+ |
if ((vp = varlookup(name)) != NULL) { |
333 |
+ |
vp->nlinks++; |
334 |
+ |
return(vp); |
335 |
+ |
} |
336 |
+ |
name = qualname(name, 0); /* use fully qualified name */ |
337 |
|
hv = hash(name); |
226 |
– |
for (vp = hashtbl[hv]; vp != NULL; vp = vp->next) |
227 |
– |
if (!strcmp(vp->name, name)) { |
228 |
– |
vp->nlinks++; |
229 |
– |
return(vp); |
230 |
– |
} |
338 |
|
vp = (VARDEF *)emalloc(sizeof(VARDEF)); |
339 |
|
vp->name = savestr(name); |
340 |
|
vp->nlinks = 1; |
422 |
|
} |
423 |
|
|
424 |
|
|
425 |
< |
dpush(ep) /* push on a definition */ |
425 |
> |
dpush(nm, ep) /* push on a definition */ |
426 |
> |
char *nm; |
427 |
|
register EPNODE *ep; |
428 |
|
{ |
429 |
|
register VARDEF *vp; |
430 |
|
|
431 |
< |
vp = varinsert(dname(ep)); |
431 |
> |
vp = varinsert(nm); |
432 |
|
ep->sibling = vp->def; |
433 |
|
vp->def = ep; |
434 |
|
} |
465 |
|
#endif |
466 |
|
|
467 |
|
|
468 |
< |
loaddefn() /* load next definition */ |
468 |
> |
getstatement() /* get next statement */ |
469 |
|
{ |
470 |
|
register EPNODE *ep; |
471 |
+ |
char *qname; |
472 |
+ |
EPNODE *lastdef; |
473 |
|
|
474 |
|
if (nextc == ';') { /* empty statement */ |
475 |
|
scan(); |
483 |
|
#endif |
484 |
|
{ /* ordinary definition */ |
485 |
|
ep = getdefn(); |
486 |
+ |
qname = qualname(dname(ep), 0); |
487 |
|
#ifdef REDEFW |
488 |
< |
if (dlookup(dname(ep)) != NULL) { |
489 |
< |
dclear(dname(ep)); |
490 |
< |
wputs(dname(ep)); |
488 |
> |
if ((lastdef = dlookup(qname)) != NULL) { |
489 |
> |
wputs(qname); |
490 |
> |
if (lastdef->type == ':') |
491 |
> |
wputs(": redefined constant expression\n"); |
492 |
> |
else |
493 |
|
wputs(": redefined\n"); |
494 |
|
} |
495 |
< |
#else |
496 |
< |
dclear(dname(ep)); |
495 |
> |
#ifdef FUNCTION |
496 |
> |
else if (ep->v.kid->type == FUNC && liblookup(qname) != NULL) { |
497 |
> |
wputs(qname); |
498 |
> |
wputs(": definition hides library function\n"); |
499 |
> |
} |
500 |
|
#endif |
501 |
< |
dpush(ep); |
501 |
> |
#endif |
502 |
> |
if (ep->type == ':') |
503 |
> |
dremove(qname); |
504 |
> |
else |
505 |
> |
dclear(qname); |
506 |
> |
dpush(qname, ep); |
507 |
|
} |
508 |
|
if (nextc != EOF) { |
509 |
|
if (nextc != ';') |
515 |
|
|
516 |
|
EPNODE * |
517 |
|
getdefn() /* A -> SYM = E1 */ |
518 |
+ |
/* SYM : E1 */ |
519 |
|
/* FUNC(SYM,..) = E1 */ |
520 |
+ |
/* FUNC(SYM,..) : E1 */ |
521 |
|
{ |
522 |
|
register EPNODE *ep1, *ep2; |
523 |
|
|
551 |
|
curfunc = NULL; |
552 |
|
#endif |
553 |
|
|
554 |
< |
if (nextc != '=') |
555 |
< |
syntax("'=' expected"); |
433 |
< |
scan(); |
554 |
> |
if (nextc != '=' && nextc != ':') |
555 |
> |
syntax("'=' or ':' expected"); |
556 |
|
|
557 |
|
ep2 = newnode(); |
558 |
< |
ep2->type = '='; |
558 |
> |
ep2->type = nextc; |
559 |
> |
scan(); |
560 |
|
addekid(ep2, ep1); |
561 |
|
addekid(ep2, getE1()); |
562 |
|
|
563 |
+ |
if ( |
564 |
|
#ifdef FUNCTION |
565 |
< |
if (ep1->type == SYM) |
565 |
> |
ep1->type == SYM && |
566 |
|
#endif |
567 |
< |
{ |
567 |
> |
ep1->sibling->type != NUM) { |
568 |
|
ep1 = newnode(); |
569 |
|
ep1->type = TICK; |
570 |
|
ep1->v.tick = -1; |
625 |
|
quit(1); |
626 |
|
} |
627 |
|
ep1 = d->v.kid->sibling; /* get expression */ |
628 |
+ |
if (ep1->type == NUM) |
629 |
+ |
return(ep1->v.num); /* return if number */ |
630 |
|
ep2 = ep1->sibling; /* check time */ |
631 |
|
if (ep2->v.tick < 0 || ep2->v.tick < eclock) { |
632 |
< |
ep2->v.tick = eclock; |
632 |
> |
ep2->v.tick = d->type == ':' ? 1L<<30 : eclock; |
633 |
|
ep2 = ep2->sibling; |
634 |
< |
ep2->v.num = evalue(ep1); /* compute new value */ |
634 |
> |
ep2->v.num = evalue(ep1); /* needs new value */ |
635 |
|
} else |
636 |
< |
ep2 = ep2->sibling; /* reuse old value */ |
636 |
> |
ep2 = ep2->sibling; /* else reuse old value */ |
637 |
|
|
638 |
|
return(ep2->v.num); |
639 |
|
} |