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 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 */ |
50 |
#ifdef OUTCHAN |
51 |
static EPNODE *ochpos; /* ...dnext */ |
52 |
static EPNODE *outchan; |
53 |
#endif |
54 |
|
55 |
#ifdef FUNCTION |
56 |
EPNODE *curfunc; |
57 |
#define dname(ep) ((ep)->v.kid->type == SYM ? \ |
58 |
(ep)->v.kid->v.name : \ |
59 |
(ep)->v.kid->v.kid->v.name) |
60 |
#else |
61 |
#define dname(ep) ((ep)->v.kid->v.name) |
62 |
#endif |
63 |
|
64 |
|
65 |
fcompile(fname) /* get definitions from a file */ |
66 |
char *fname; |
67 |
{ |
68 |
FILE *fp; |
69 |
|
70 |
if (fname == NULL) |
71 |
fp = stdin; |
72 |
else if ((fp = fopen(fname, "r")) == NULL) { |
73 |
eputs(fname); |
74 |
eputs(": cannot open\n"); |
75 |
quit(1); |
76 |
} |
77 |
initfile(fp, fname, 0); |
78 |
while (nextc != EOF) |
79 |
getstatement(); |
80 |
if (fname != NULL) |
81 |
fclose(fp); |
82 |
} |
83 |
|
84 |
|
85 |
scompile(str, fn, ln) /* get definitions from a string */ |
86 |
char *str; |
87 |
char *fn; |
88 |
int ln; |
89 |
{ |
90 |
initstr(str, fn, ln); |
91 |
while (nextc != EOF) |
92 |
getstatement(); |
93 |
} |
94 |
|
95 |
|
96 |
double |
97 |
varvalue(vname) /* return a variable's value */ |
98 |
char *vname; |
99 |
{ |
100 |
return(dvalue(vname, dlookup(vname))); |
101 |
} |
102 |
|
103 |
|
104 |
double |
105 |
evariable(ep) /* evaluate a variable */ |
106 |
EPNODE *ep; |
107 |
{ |
108 |
register VARDEF *dp = ep->v.ln; |
109 |
|
110 |
return(dvalue(dp->name, dp->def)); |
111 |
} |
112 |
|
113 |
|
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(qname)) != NULL && ep1->v.kid->type == SYM) { |
125 |
ep2 = ep1->v.kid->sibling; |
126 |
if (ep2->type == NUM) { |
127 |
ep2->v.num = val; |
128 |
ep1->type = assign; |
129 |
return; |
130 |
} |
131 |
} |
132 |
/* hand build definition */ |
133 |
ep1 = newnode(); |
134 |
ep1->type = assign; |
135 |
ep2 = newnode(); |
136 |
ep2->type = SYM; |
137 |
ep2->v.name = savestr(vname); |
138 |
addekid(ep1, ep2); |
139 |
ep2 = newnode(); |
140 |
ep2->type = NUM; |
141 |
ep2->v.num = val; |
142 |
addekid(ep1, ep2); |
143 |
dremove(qname); |
144 |
dpush(qname, ep1); |
145 |
} |
146 |
|
147 |
|
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 |
} |
171 |
|
172 |
|
173 |
vardefined(name) /* return non-zero if variable defined */ |
174 |
char *name; |
175 |
{ |
176 |
register EPNODE *dp; |
177 |
|
178 |
return((dp = dlookup(name)) != NULL && dp->v.kid->type == SYM); |
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(cs) /* set output channels */ |
275 |
int (*cs)(); |
276 |
{ |
277 |
register EPNODE *ep; |
278 |
|
279 |
for (ep = outchan; ep != NULL; ep = ep->sibling) |
280 |
(*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling)); |
281 |
|
282 |
} |
283 |
#endif |
284 |
|
285 |
|
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 |
/* if context is global, clear all */ |
293 |
for (i = 0; i < NHASH; i++) |
294 |
for (vp = hashtbl[i]; vp != NULL; vp = vp->next) |
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 |
if (lvl >= 1) { |
302 |
for (ep = outchan; ep != NULL; ep = ep->sibling) |
303 |
epfree(ep); |
304 |
outchan = NULL; |
305 |
} |
306 |
#endif |
307 |
} |
308 |
|
309 |
|
310 |
EPNODE * |
311 |
dlookup(name) /* look up a definition */ |
312 |
char *name; |
313 |
{ |
314 |
register VARDEF *vp; |
315 |
|
316 |
if ((vp = varlookup(name)) == NULL) |
317 |
return(NULL); |
318 |
return(vp->def); |
319 |
} |
320 |
|
321 |
|
322 |
VARDEF * |
323 |
varlookup(name) /* look up a variable */ |
324 |
char *name; |
325 |
{ |
326 |
int lvl = 0; |
327 |
register char *qname; |
328 |
register VARDEF *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 |
|
337 |
|
338 |
VARDEF * |
339 |
varinsert(name) /* get a link to a variable */ |
340 |
char *name; |
341 |
{ |
342 |
register VARDEF *vp; |
343 |
LIBR *libp; |
344 |
int hv; |
345 |
|
346 |
if ((vp = varlookup(name)) != NULL) { |
347 |
vp->nlinks++; |
348 |
return(vp); |
349 |
} |
350 |
#ifdef FUNCTION |
351 |
libp = liblookup(name); |
352 |
#else |
353 |
libp = NULL; |
354 |
#endif |
355 |
if (libp == NULL) /* if name not in library */ |
356 |
name = qualname(name, 0); /* use fully qualified version */ |
357 |
hv = hash(name); |
358 |
vp = (VARDEF *)emalloc(sizeof(VARDEF)); |
359 |
vp->name = savestr(name); |
360 |
vp->nlinks = 1; |
361 |
vp->def = NULL; |
362 |
vp->lib = libp; |
363 |
vp->next = hashtbl[hv]; |
364 |
hashtbl[hv] = vp; |
365 |
return(vp); |
366 |
} |
367 |
|
368 |
|
369 |
varfree(ln) /* release link to variable */ |
370 |
register VARDEF *ln; |
371 |
{ |
372 |
register VARDEF *vp; |
373 |
int hv; |
374 |
|
375 |
if (--ln->nlinks > 0) |
376 |
return; /* still active */ |
377 |
|
378 |
hv = hash(ln->name); |
379 |
vp = hashtbl[hv]; |
380 |
if (vp == ln) |
381 |
hashtbl[hv] = vp->next; |
382 |
else { |
383 |
while (vp->next != ln) /* must be in list */ |
384 |
vp = vp->next; |
385 |
vp->next = ln->next; |
386 |
} |
387 |
freestr(ln->name); |
388 |
efree((char *)ln); |
389 |
} |
390 |
|
391 |
|
392 |
EPNODE * |
393 |
dfirst() /* return pointer to first definition */ |
394 |
{ |
395 |
htndx = 0; |
396 |
htpos = NULL; |
397 |
#ifdef OUTCHAN |
398 |
ochpos = outchan; |
399 |
#endif |
400 |
return(dnext()); |
401 |
} |
402 |
|
403 |
|
404 |
EPNODE * |
405 |
dnext() /* return pointer to next definition */ |
406 |
{ |
407 |
register EPNODE *ep; |
408 |
|
409 |
while (htndx < NHASH) { |
410 |
if (htpos == NULL) |
411 |
htpos = hashtbl[htndx++]; |
412 |
while (htpos != NULL) { |
413 |
ep = htpos->def; |
414 |
htpos = htpos->next; |
415 |
if (ep != NULL) |
416 |
return(ep); |
417 |
} |
418 |
} |
419 |
#ifdef OUTCHAN |
420 |
if ((ep = ochpos) != NULL) |
421 |
ochpos = ep->sibling; |
422 |
return(ep); |
423 |
#else |
424 |
return(NULL); |
425 |
#endif |
426 |
} |
427 |
|
428 |
|
429 |
EPNODE * |
430 |
dpop(name) /* pop a definition */ |
431 |
char *name; |
432 |
{ |
433 |
register VARDEF *vp; |
434 |
register EPNODE *dp; |
435 |
|
436 |
if ((vp = varlookup(name)) == NULL || vp->def == NULL) |
437 |
return(NULL); |
438 |
dp = vp->def; |
439 |
vp->def = dp->sibling; |
440 |
varfree(vp); |
441 |
return(dp); |
442 |
} |
443 |
|
444 |
|
445 |
dpush(nm, ep) /* push on a definition */ |
446 |
char *nm; |
447 |
register EPNODE *ep; |
448 |
{ |
449 |
register VARDEF *vp; |
450 |
|
451 |
vp = varinsert(nm); |
452 |
ep->sibling = vp->def; |
453 |
vp->def = ep; |
454 |
} |
455 |
|
456 |
|
457 |
#ifdef OUTCHAN |
458 |
addchan(sp) /* add an output channel assignment */ |
459 |
EPNODE *sp; |
460 |
{ |
461 |
int ch = sp->v.kid->v.chan; |
462 |
register EPNODE *ep, *epl; |
463 |
|
464 |
for (epl = NULL, ep = outchan; ep != NULL; epl = ep, ep = ep->sibling) |
465 |
if (ep->v.kid->v.chan >= ch) { |
466 |
if (epl != NULL) |
467 |
epl->sibling = sp; |
468 |
else |
469 |
outchan = sp; |
470 |
if (ep->v.kid->v.chan > ch) |
471 |
sp->sibling = ep; |
472 |
else { |
473 |
sp->sibling = ep->sibling; |
474 |
epfree(ep); |
475 |
} |
476 |
return; |
477 |
} |
478 |
if (epl != NULL) |
479 |
epl->sibling = sp; |
480 |
else |
481 |
outchan = sp; |
482 |
sp->sibling = NULL; |
483 |
|
484 |
} |
485 |
#endif |
486 |
|
487 |
|
488 |
getstatement() /* get next statement */ |
489 |
{ |
490 |
register EPNODE *ep; |
491 |
char *qname; |
492 |
register VARDEF *vdef; |
493 |
|
494 |
if (nextc == ';') { /* empty statement */ |
495 |
scan(); |
496 |
return; |
497 |
} |
498 |
#ifdef OUTCHAN |
499 |
if (nextc == '$') { /* channel assignment */ |
500 |
ep = getchan(); |
501 |
addchan(ep); |
502 |
} else |
503 |
#endif |
504 |
{ /* ordinary definition */ |
505 |
ep = getdefn(); |
506 |
qname = qualname(dname(ep), 0); |
507 |
#ifdef REDEFW |
508 |
if ((vdef = varlookup(qname)) != NULL) |
509 |
if (vdef->def != NULL) { |
510 |
wputs(qname); |
511 |
if (vdef->def->type == ':') |
512 |
wputs(": redefined constant expression\n"); |
513 |
else |
514 |
wputs(": redefined\n"); |
515 |
} |
516 |
#ifdef FUNCTION |
517 |
else if (ep->v.kid->type == FUNC && vdef->lib != NULL) { |
518 |
wputs(qname); |
519 |
wputs(": definition hides library function\n"); |
520 |
} |
521 |
#endif |
522 |
#endif |
523 |
if (ep->type == ':') |
524 |
dremove(qname); |
525 |
else |
526 |
dclear(qname); |
527 |
dpush(qname, ep); |
528 |
} |
529 |
if (nextc != EOF) { |
530 |
if (nextc != ';') |
531 |
syntax("';' expected"); |
532 |
scan(); |
533 |
} |
534 |
} |
535 |
|
536 |
|
537 |
EPNODE * |
538 |
getdefn() /* A -> SYM = E1 */ |
539 |
/* SYM : E1 */ |
540 |
/* FUNC(SYM,..) = E1 */ |
541 |
/* FUNC(SYM,..) : E1 */ |
542 |
{ |
543 |
register EPNODE *ep1, *ep2; |
544 |
|
545 |
if (!isalpha(nextc)) |
546 |
syntax("illegal variable name"); |
547 |
|
548 |
ep1 = newnode(); |
549 |
ep1->type = SYM; |
550 |
ep1->v.name = savestr(getname()); |
551 |
|
552 |
#ifdef FUNCTION |
553 |
if (nextc == '(') { |
554 |
ep2 = newnode(); |
555 |
ep2->type = FUNC; |
556 |
addekid(ep2, ep1); |
557 |
ep1 = ep2; |
558 |
do { |
559 |
scan(); |
560 |
if (!isalpha(nextc)) |
561 |
syntax("illegal variable name"); |
562 |
ep2 = newnode(); |
563 |
ep2->type = SYM; |
564 |
ep2->v.name = savestr(getname()); |
565 |
addekid(ep1, ep2); |
566 |
} while (nextc == ','); |
567 |
if (nextc != ')') |
568 |
syntax("')' expected"); |
569 |
scan(); |
570 |
curfunc = ep1; |
571 |
} else |
572 |
curfunc = NULL; |
573 |
#endif |
574 |
|
575 |
if (nextc != '=' && nextc != ':') |
576 |
syntax("'=' or ':' expected"); |
577 |
|
578 |
ep2 = newnode(); |
579 |
ep2->type = nextc; |
580 |
scan(); |
581 |
addekid(ep2, ep1); |
582 |
addekid(ep2, getE1()); |
583 |
|
584 |
if ( |
585 |
#ifdef FUNCTION |
586 |
ep1->type == SYM && |
587 |
#endif |
588 |
ep1->sibling->type != NUM) { |
589 |
ep1 = newnode(); |
590 |
ep1->type = TICK; |
591 |
ep1->v.tick = -1; |
592 |
addekid(ep2, ep1); |
593 |
ep1 = newnode(); |
594 |
ep1->type = NUM; |
595 |
addekid(ep2, ep1); |
596 |
} |
597 |
|
598 |
return(ep2); |
599 |
} |
600 |
|
601 |
|
602 |
#ifdef OUTCHAN |
603 |
EPNODE * |
604 |
getchan() /* A -> $N = E1 */ |
605 |
{ |
606 |
register EPNODE *ep1, *ep2; |
607 |
|
608 |
if (nextc != '$') |
609 |
syntax("missing '$'"); |
610 |
scan(); |
611 |
|
612 |
ep1 = newnode(); |
613 |
ep1->type = CHAN; |
614 |
ep1->v.chan = getinum(); |
615 |
|
616 |
if (nextc != '=') |
617 |
syntax("'=' expected"); |
618 |
scan(); |
619 |
|
620 |
ep2 = newnode(); |
621 |
ep2->type = '='; |
622 |
addekid(ep2, ep1); |
623 |
addekid(ep2, getE1()); |
624 |
|
625 |
return(ep2); |
626 |
} |
627 |
#endif |
628 |
|
629 |
|
630 |
|
631 |
/* |
632 |
* The following routines are for internal use only: |
633 |
*/ |
634 |
|
635 |
|
636 |
static double |
637 |
dvalue(name, d) /* evaluate a variable */ |
638 |
char *name; |
639 |
EPNODE *d; |
640 |
{ |
641 |
register EPNODE *ep1, *ep2; |
642 |
|
643 |
if (d == NULL || d->v.kid->type != SYM) { |
644 |
eputs(name); |
645 |
eputs(": undefined variable\n"); |
646 |
quit(1); |
647 |
} |
648 |
ep1 = d->v.kid->sibling; /* get expression */ |
649 |
if (ep1->type == NUM) |
650 |
return(ep1->v.num); /* return if number */ |
651 |
ep2 = ep1->sibling; /* check time */ |
652 |
if (ep2->v.tick < 0 || ep2->v.tick < eclock) { |
653 |
ep2->v.tick = d->type == ':' ? 1L<<30 : eclock; |
654 |
ep2 = ep2->sibling; |
655 |
ep2->v.num = evalue(ep1); /* needs new value */ |
656 |
} else |
657 |
ep2 = ep2->sibling; /* else reuse old value */ |
658 |
|
659 |
return(ep2->v.num); |
660 |
} |
661 |
|
662 |
|
663 |
static int |
664 |
hash(s) /* hash a string */ |
665 |
register char *s; |
666 |
{ |
667 |
register int rval = 0; |
668 |
|
669 |
while (*s) |
670 |
rval += *s++; |
671 |
|
672 |
return(rval % NHASH); |
673 |
} |