1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Store variable definitions. |
9 |
* |
10 |
* 7/1/85 Greg Ward |
11 |
* |
12 |
* 11/11/85 Added conditional compiles (OUTCHAN) for control output. |
13 |
* |
14 |
* 4/2/86 Added conditional compiles for function definitions (FUNCTION). |
15 |
* |
16 |
* 1/15/88 Added clock for caching of variable values. |
17 |
* |
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> |
28 |
|
29 |
#include <ctype.h> |
30 |
|
31 |
#include "calcomp.h" |
32 |
|
33 |
#ifndef NHASH |
34 |
#define NHASH 521 /* hash size (a prime!) */ |
35 |
#endif |
36 |
|
37 |
#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) |
38 |
|
39 |
extern char *ecalloc(), *savestr(), *strcpy(); |
40 |
|
41 |
static int hash(); |
42 |
|
43 |
static double dvalue(); |
44 |
|
45 |
long eclock = -1; /* value storage timer */ |
46 |
|
47 |
static char context[MAXWORD+1]; /* current context path */ |
48 |
|
49 |
static VARDEF *hashtbl[NHASH]; /* definition list */ |
50 |
static int htndx; /* index for */ |
51 |
static VARDEF *htpos; /* ...dfirst() and */ |
52 |
#ifdef OUTCHAN |
53 |
static EPNODE *ochpos; /* ...dnext */ |
54 |
static EPNODE *outchan; |
55 |
#endif |
56 |
|
57 |
#ifdef FUNCTION |
58 |
EPNODE *curfunc; |
59 |
#define dname(ep) ((ep)->v.kid->type == SYM ? \ |
60 |
(ep)->v.kid->v.name : \ |
61 |
(ep)->v.kid->v.kid->v.name) |
62 |
#else |
63 |
#define dname(ep) ((ep)->v.kid->v.name) |
64 |
#endif |
65 |
|
66 |
|
67 |
fcompile(fname) /* get definitions from a file */ |
68 |
char *fname; |
69 |
{ |
70 |
FILE *fp; |
71 |
|
72 |
if (fname == NULL) |
73 |
fp = stdin; |
74 |
else if ((fp = fopen(fname, "r")) == NULL) { |
75 |
eputs(fname); |
76 |
eputs(": cannot open\n"); |
77 |
quit(1); |
78 |
} |
79 |
initfile(fp, fname, 0); |
80 |
while (nextc != EOF) |
81 |
getstatement(); |
82 |
if (fname != NULL) |
83 |
fclose(fp); |
84 |
} |
85 |
|
86 |
|
87 |
scompile(str, fn, ln) /* get definitions from a string */ |
88 |
char *str; |
89 |
char *fn; |
90 |
int ln; |
91 |
{ |
92 |
initstr(str, fn, ln); |
93 |
while (nextc != EOF) |
94 |
getstatement(); |
95 |
} |
96 |
|
97 |
|
98 |
double |
99 |
varvalue(vname) /* return a variable's value */ |
100 |
char *vname; |
101 |
{ |
102 |
return(dvalue(vname, dlookup(vname))); |
103 |
} |
104 |
|
105 |
|
106 |
double |
107 |
evariable(ep) /* evaluate a variable */ |
108 |
EPNODE *ep; |
109 |
{ |
110 |
register VARDEF *dp = ep->v.ln; |
111 |
|
112 |
return(dvalue(dp->name, dp->def)); |
113 |
} |
114 |
|
115 |
|
116 |
varset(vname, assign, val) /* set a variable's value */ |
117 |
char *vname; |
118 |
int assign; |
119 |
double val; |
120 |
{ |
121 |
char *qname; |
122 |
register EPNODE *ep1, *ep2; |
123 |
/* get qualified name */ |
124 |
qname = qualname(vname, 0); |
125 |
/* check for quick set */ |
126 |
if ((ep1 = dlookup(qname)) != NULL && ep1->v.kid->type == SYM) { |
127 |
ep2 = ep1->v.kid->sibling; |
128 |
if (ep2->type == NUM) { |
129 |
ep2->v.num = val; |
130 |
ep1->type = assign; |
131 |
return; |
132 |
} |
133 |
} |
134 |
/* hand build definition */ |
135 |
ep1 = newnode(); |
136 |
ep1->type = assign; |
137 |
ep2 = newnode(); |
138 |
ep2->type = SYM; |
139 |
ep2->v.name = savestr(vname); |
140 |
addekid(ep1, ep2); |
141 |
ep2 = newnode(); |
142 |
ep2->type = NUM; |
143 |
ep2->v.num = val; |
144 |
addekid(ep1, ep2); |
145 |
dremove(qname); |
146 |
dpush(qname, ep1); |
147 |
} |
148 |
|
149 |
|
150 |
dclear(name) /* delete variable definitions of name */ |
151 |
char *name; |
152 |
{ |
153 |
register EPNODE *ep; |
154 |
|
155 |
while ((ep = dpop(name)) != NULL) { |
156 |
if (ep->type == ':') { |
157 |
dpush(name, ep); /* don't clear constants */ |
158 |
return; |
159 |
} |
160 |
epfree(ep); |
161 |
} |
162 |
} |
163 |
|
164 |
|
165 |
dremove(name) /* delete all definitions of name */ |
166 |
char *name; |
167 |
{ |
168 |
register EPNODE *ep; |
169 |
|
170 |
while ((ep = dpop(name)) != NULL) |
171 |
epfree(ep); |
172 |
} |
173 |
|
174 |
|
175 |
vardefined(name) /* return non-zero if variable defined */ |
176 |
char *name; |
177 |
{ |
178 |
register EPNODE *dp; |
179 |
|
180 |
return((dp = dlookup(name)) != NULL && dp->v.kid->type == SYM); |
181 |
} |
182 |
|
183 |
|
184 |
char * |
185 |
setcontext(ctx) /* set a new context path */ |
186 |
register char *ctx; |
187 |
{ |
188 |
register char *cpp; |
189 |
|
190 |
if (ctx == NULL) |
191 |
return(context); /* just asking */ |
192 |
if (!*ctx) { |
193 |
context[0] = '\0'; /* clear context */ |
194 |
return(context); |
195 |
} |
196 |
cpp = context; /* else copy it (carefully!) */ |
197 |
if (*ctx != CNTXMARK) |
198 |
*cpp++ = CNTXMARK; /* make sure there's a mark */ |
199 |
do { |
200 |
if (cpp >= context+MAXWORD) |
201 |
break; /* just copy what we can */ |
202 |
if (isid(*ctx)) |
203 |
*cpp++ = *ctx++; |
204 |
else { |
205 |
*cpp++ = '_'; ctx++; |
206 |
} |
207 |
} while (*ctx); |
208 |
*cpp = '\0'; |
209 |
return(context); |
210 |
} |
211 |
|
212 |
|
213 |
char * |
214 |
qualname(nam, lvl) /* get qualified name */ |
215 |
register char *nam; |
216 |
int lvl; |
217 |
{ |
218 |
static char nambuf[MAXWORD+1]; |
219 |
register char *cp = nambuf, *cpp; |
220 |
/* check for explicit local */ |
221 |
if (*nam == CNTXMARK) |
222 |
if (lvl > 0) /* only action is to refuse search */ |
223 |
return(NULL); |
224 |
else |
225 |
nam++; |
226 |
else if (nam == nambuf) /* check for repeat call */ |
227 |
return(lvl > 0 ? NULL : nam); |
228 |
/* copy name to static buffer */ |
229 |
while (*nam) { |
230 |
if (cp >= nambuf+MAXWORD) |
231 |
goto toolong; |
232 |
*cp++ = *nam++; |
233 |
} |
234 |
/* check for explicit global */ |
235 |
if (cp > nambuf && cp[-1] == CNTXMARK) { |
236 |
if (lvl > 0) |
237 |
return(NULL); |
238 |
*--cp = '\0'; |
239 |
return(nambuf); /* already qualified */ |
240 |
} |
241 |
cpp = context; /* else skip the requested levels */ |
242 |
while (lvl-- > 0) { |
243 |
if (!*cpp) |
244 |
return(NULL); /* return NULL if past global level */ |
245 |
while (*++cpp && *cpp != CNTXMARK) |
246 |
; |
247 |
} |
248 |
while (*cpp) { /* copy context to static buffer */ |
249 |
if (cp >= nambuf+MAXWORD) |
250 |
goto toolong; |
251 |
*cp++ = *cpp++; |
252 |
} |
253 |
toolong: |
254 |
*cp = '\0'; |
255 |
return(nambuf); /* return qualified name */ |
256 |
} |
257 |
|
258 |
|
259 |
incontext(qn) /* is qualified name in current context? */ |
260 |
register char *qn; |
261 |
{ |
262 |
while (*qn && *qn != CNTXMARK) /* find context mark */ |
263 |
qn++; |
264 |
return(!strcmp(qn, context)); |
265 |
} |
266 |
|
267 |
|
268 |
#ifdef OUTCHAN |
269 |
chanout(cs) /* set output channels */ |
270 |
int (*cs)(); |
271 |
{ |
272 |
register EPNODE *ep; |
273 |
|
274 |
for (ep = outchan; ep != NULL; ep = ep->sibling) |
275 |
(*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling)); |
276 |
|
277 |
} |
278 |
#endif |
279 |
|
280 |
|
281 |
dcleanup(lvl) /* clear definitions (0->vars,1->output,2->consts) */ |
282 |
int lvl; |
283 |
{ |
284 |
register int i; |
285 |
register VARDEF *vp; |
286 |
register EPNODE *ep; |
287 |
/* if context is global, clear all */ |
288 |
for (i = 0; i < NHASH; i++) |
289 |
for (vp = hashtbl[i]; vp != NULL; vp = vp->next) |
290 |
if (!context[0] || incontext(vp->name)) |
291 |
if (lvl >= 2) |
292 |
dremove(vp->name); |
293 |
else |
294 |
dclear(vp->name); |
295 |
#ifdef OUTCHAN |
296 |
if (lvl >= 1) { |
297 |
for (ep = outchan; ep != NULL; ep = ep->sibling) |
298 |
epfree(ep); |
299 |
outchan = NULL; |
300 |
} |
301 |
#endif |
302 |
} |
303 |
|
304 |
|
305 |
EPNODE * |
306 |
dlookup(name) /* look up a definition */ |
307 |
char *name; |
308 |
{ |
309 |
register VARDEF *vp; |
310 |
|
311 |
if ((vp = varlookup(name)) == NULL) |
312 |
return(NULL); |
313 |
return(vp->def); |
314 |
} |
315 |
|
316 |
|
317 |
VARDEF * |
318 |
varlookup(name) /* look up a variable */ |
319 |
char *name; |
320 |
{ |
321 |
int lvl = 0; |
322 |
register char *qname; |
323 |
register VARDEF *vp; |
324 |
/* find most qualified match */ |
325 |
while ((qname = qualname(name, lvl++)) != NULL) |
326 |
for (vp = hashtbl[hash(qname)]; vp != NULL; vp = vp->next) |
327 |
if (!strcmp(vp->name, qname)) |
328 |
return(vp); |
329 |
return(NULL); |
330 |
} |
331 |
|
332 |
|
333 |
VARDEF * |
334 |
varinsert(name) /* get a link to a variable */ |
335 |
char *name; |
336 |
{ |
337 |
register VARDEF *vp; |
338 |
LIBR *libp; |
339 |
int hv; |
340 |
|
341 |
if ((vp = varlookup(name)) != NULL) { |
342 |
vp->nlinks++; |
343 |
return(vp); |
344 |
} |
345 |
#ifdef FUNCTION |
346 |
libp = liblookup(name); |
347 |
#else |
348 |
libp = NULL; |
349 |
#endif |
350 |
if (libp == NULL) /* if name not in library */ |
351 |
name = qualname(name, 0); /* use fully qualified version */ |
352 |
hv = hash(name); |
353 |
vp = (VARDEF *)emalloc(sizeof(VARDEF)); |
354 |
vp->name = savestr(name); |
355 |
vp->nlinks = 1; |
356 |
vp->def = NULL; |
357 |
vp->lib = libp; |
358 |
vp->next = hashtbl[hv]; |
359 |
hashtbl[hv] = vp; |
360 |
return(vp); |
361 |
} |
362 |
|
363 |
|
364 |
varfree(ln) /* release link to variable */ |
365 |
register VARDEF *ln; |
366 |
{ |
367 |
register VARDEF *vp; |
368 |
int hv; |
369 |
|
370 |
if (--ln->nlinks > 0) |
371 |
return; /* still active */ |
372 |
|
373 |
hv = hash(ln->name); |
374 |
vp = hashtbl[hv]; |
375 |
if (vp == ln) |
376 |
hashtbl[hv] = vp->next; |
377 |
else { |
378 |
while (vp->next != ln) /* must be in list */ |
379 |
vp = vp->next; |
380 |
vp->next = ln->next; |
381 |
} |
382 |
freestr(ln->name); |
383 |
efree((char *)ln); |
384 |
} |
385 |
|
386 |
|
387 |
EPNODE * |
388 |
dfirst() /* return pointer to first definition */ |
389 |
{ |
390 |
htndx = 0; |
391 |
htpos = NULL; |
392 |
#ifdef OUTCHAN |
393 |
ochpos = outchan; |
394 |
#endif |
395 |
return(dnext()); |
396 |
} |
397 |
|
398 |
|
399 |
EPNODE * |
400 |
dnext() /* return pointer to next definition */ |
401 |
{ |
402 |
register EPNODE *ep; |
403 |
register char *nm; |
404 |
|
405 |
while (htndx < NHASH) { |
406 |
if (htpos == NULL) |
407 |
htpos = hashtbl[htndx++]; |
408 |
while (htpos != NULL) { |
409 |
ep = htpos->def; |
410 |
nm = htpos->name; |
411 |
htpos = htpos->next; |
412 |
if (ep != NULL && incontext(nm)) |
413 |
return(ep); |
414 |
} |
415 |
} |
416 |
#ifdef OUTCHAN |
417 |
if ((ep = ochpos) != NULL) |
418 |
ochpos = ep->sibling; |
419 |
return(ep); |
420 |
#else |
421 |
return(NULL); |
422 |
#endif |
423 |
} |
424 |
|
425 |
|
426 |
EPNODE * |
427 |
dpop(name) /* pop a definition */ |
428 |
char *name; |
429 |
{ |
430 |
register VARDEF *vp; |
431 |
register EPNODE *dp; |
432 |
|
433 |
if ((vp = varlookup(name)) == NULL || vp->def == NULL) |
434 |
return(NULL); |
435 |
dp = vp->def; |
436 |
vp->def = dp->sibling; |
437 |
varfree(vp); |
438 |
return(dp); |
439 |
} |
440 |
|
441 |
|
442 |
dpush(nm, ep) /* push on a definition */ |
443 |
char *nm; |
444 |
register EPNODE *ep; |
445 |
{ |
446 |
register VARDEF *vp; |
447 |
|
448 |
vp = varinsert(nm); |
449 |
ep->sibling = vp->def; |
450 |
vp->def = ep; |
451 |
} |
452 |
|
453 |
|
454 |
#ifdef OUTCHAN |
455 |
addchan(sp) /* add an output channel assignment */ |
456 |
EPNODE *sp; |
457 |
{ |
458 |
int ch = sp->v.kid->v.chan; |
459 |
register EPNODE *ep, *epl; |
460 |
|
461 |
for (epl = NULL, ep = outchan; ep != NULL; epl = ep, ep = ep->sibling) |
462 |
if (ep->v.kid->v.chan >= ch) { |
463 |
if (epl != NULL) |
464 |
epl->sibling = sp; |
465 |
else |
466 |
outchan = sp; |
467 |
if (ep->v.kid->v.chan > ch) |
468 |
sp->sibling = ep; |
469 |
else { |
470 |
sp->sibling = ep->sibling; |
471 |
epfree(ep); |
472 |
} |
473 |
return; |
474 |
} |
475 |
if (epl != NULL) |
476 |
epl->sibling = sp; |
477 |
else |
478 |
outchan = sp; |
479 |
sp->sibling = NULL; |
480 |
|
481 |
} |
482 |
#endif |
483 |
|
484 |
|
485 |
getstatement() /* get next statement */ |
486 |
{ |
487 |
register EPNODE *ep; |
488 |
char *qname; |
489 |
register VARDEF *vdef; |
490 |
|
491 |
if (nextc == ';') { /* empty statement */ |
492 |
scan(); |
493 |
return; |
494 |
} |
495 |
#ifdef OUTCHAN |
496 |
if (nextc == '$') { /* channel assignment */ |
497 |
ep = getchan(); |
498 |
addchan(ep); |
499 |
} else |
500 |
#endif |
501 |
{ /* ordinary definition */ |
502 |
ep = getdefn(); |
503 |
qname = qualname(dname(ep), 0); |
504 |
#ifdef REDEFW |
505 |
if ((vdef = varlookup(qname)) != NULL) |
506 |
if (vdef->def != NULL) { |
507 |
wputs(qname); |
508 |
if (vdef->def->type == ':') |
509 |
wputs(": redefined constant expression\n"); |
510 |
else |
511 |
wputs(": redefined\n"); |
512 |
} |
513 |
#ifdef FUNCTION |
514 |
else if (ep->v.kid->type == FUNC && vdef->lib != NULL) { |
515 |
wputs(qname); |
516 |
wputs(": definition hides library function\n"); |
517 |
} |
518 |
#endif |
519 |
#endif |
520 |
if (ep->type == ':') |
521 |
dremove(qname); |
522 |
else |
523 |
dclear(qname); |
524 |
dpush(qname, ep); |
525 |
} |
526 |
if (nextc != EOF) { |
527 |
if (nextc != ';') |
528 |
syntax("';' expected"); |
529 |
scan(); |
530 |
} |
531 |
} |
532 |
|
533 |
|
534 |
EPNODE * |
535 |
getdefn() /* A -> SYM = E1 */ |
536 |
/* SYM : E1 */ |
537 |
/* FUNC(SYM,..) = E1 */ |
538 |
/* FUNC(SYM,..) : E1 */ |
539 |
{ |
540 |
register EPNODE *ep1, *ep2; |
541 |
|
542 |
if (!isalpha(nextc) && nextc != CNTXMARK) |
543 |
syntax("illegal variable name"); |
544 |
|
545 |
ep1 = newnode(); |
546 |
ep1->type = SYM; |
547 |
ep1->v.name = savestr(getname()); |
548 |
|
549 |
#ifdef FUNCTION |
550 |
if (nextc == '(') { |
551 |
ep2 = newnode(); |
552 |
ep2->type = FUNC; |
553 |
addekid(ep2, ep1); |
554 |
ep1 = ep2; |
555 |
do { |
556 |
scan(); |
557 |
if (!isalpha(nextc)) |
558 |
syntax("illegal variable name"); |
559 |
ep2 = newnode(); |
560 |
ep2->type = SYM; |
561 |
ep2->v.name = savestr(getname()); |
562 |
addekid(ep1, ep2); |
563 |
} while (nextc == ','); |
564 |
if (nextc != ')') |
565 |
syntax("')' expected"); |
566 |
scan(); |
567 |
curfunc = ep1; |
568 |
} else |
569 |
curfunc = NULL; |
570 |
#endif |
571 |
|
572 |
if (nextc != '=' && nextc != ':') |
573 |
syntax("'=' or ':' expected"); |
574 |
|
575 |
ep2 = newnode(); |
576 |
ep2->type = nextc; |
577 |
scan(); |
578 |
addekid(ep2, ep1); |
579 |
addekid(ep2, getE1()); |
580 |
|
581 |
if ( |
582 |
#ifdef FUNCTION |
583 |
ep1->type == SYM && |
584 |
#endif |
585 |
ep1->sibling->type != NUM) { |
586 |
ep1 = newnode(); |
587 |
ep1->type = TICK; |
588 |
ep1->v.tick = -1; |
589 |
addekid(ep2, ep1); |
590 |
ep1 = newnode(); |
591 |
ep1->type = NUM; |
592 |
addekid(ep2, ep1); |
593 |
} |
594 |
|
595 |
return(ep2); |
596 |
} |
597 |
|
598 |
|
599 |
#ifdef OUTCHAN |
600 |
EPNODE * |
601 |
getchan() /* A -> $N = E1 */ |
602 |
{ |
603 |
register EPNODE *ep1, *ep2; |
604 |
|
605 |
if (nextc != '$') |
606 |
syntax("missing '$'"); |
607 |
scan(); |
608 |
|
609 |
ep1 = newnode(); |
610 |
ep1->type = CHAN; |
611 |
ep1->v.chan = getinum(); |
612 |
|
613 |
if (nextc != '=') |
614 |
syntax("'=' expected"); |
615 |
scan(); |
616 |
|
617 |
ep2 = newnode(); |
618 |
ep2->type = '='; |
619 |
addekid(ep2, ep1); |
620 |
addekid(ep2, getE1()); |
621 |
|
622 |
return(ep2); |
623 |
} |
624 |
#endif |
625 |
|
626 |
|
627 |
|
628 |
/* |
629 |
* The following routines are for internal use only: |
630 |
*/ |
631 |
|
632 |
|
633 |
static double |
634 |
dvalue(name, d) /* evaluate a variable */ |
635 |
char *name; |
636 |
EPNODE *d; |
637 |
{ |
638 |
register EPNODE *ep1, *ep2; |
639 |
|
640 |
if (d == NULL || d->v.kid->type != SYM) { |
641 |
eputs(name); |
642 |
eputs(": undefined variable\n"); |
643 |
quit(1); |
644 |
} |
645 |
ep1 = d->v.kid->sibling; /* get expression */ |
646 |
if (ep1->type == NUM) |
647 |
return(ep1->v.num); /* return if number */ |
648 |
ep2 = ep1->sibling; /* check time */ |
649 |
if (ep2->v.tick < 0 || ep2->v.tick < eclock) { |
650 |
ep2->v.tick = d->type == ':' ? 1L<<30 : eclock; |
651 |
ep2 = ep2->sibling; |
652 |
ep2->v.num = evalue(ep1); /* needs new value */ |
653 |
} else |
654 |
ep2 = ep2->sibling; /* else reuse old value */ |
655 |
|
656 |
return(ep2->v.num); |
657 |
} |
658 |
|
659 |
|
660 |
static int |
661 |
hash(s) /* hash a string */ |
662 |
register char *s; |
663 |
{ |
664 |
register int rval = 0; |
665 |
|
666 |
while (*s) |
667 |
rval += *s++; |
668 |
|
669 |
return(rval % NHASH); |
670 |
} |