1 |
– |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Store variable definitions. |
6 |
|
* |
19 |
|
* 4/23/91 Added ':' assignment for constant expressions |
20 |
|
* |
21 |
|
* 8/7/91 Added optional context path to append to variable names |
22 |
+ |
* |
23 |
+ |
* 5/17/2001 Fixed clock counter wrapping behavior |
24 |
+ |
* |
25 |
+ |
* 2/19/03 Eliminated conditional compiles in favor of esupport extern. |
26 |
|
*/ |
27 |
|
|
28 |
+ |
#include "copyright.h" |
29 |
+ |
|
30 |
|
#include <stdio.h> |
31 |
|
|
32 |
+ |
#include <string.h> |
33 |
+ |
|
34 |
|
#include <ctype.h> |
35 |
|
|
36 |
|
#include "calcomp.h" |
43 |
|
|
44 |
|
#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) |
45 |
|
|
41 |
– |
extern char *ecalloc(), *emalloc(), *savestr(), *strcpy(); |
42 |
– |
|
46 |
|
static double dvalue(); |
47 |
|
|
48 |
+ |
#define MAXCLOCK (1L<<31) /* clock wrap value */ |
49 |
+ |
|
50 |
|
unsigned long eclock = 0; /* value storage timer */ |
51 |
|
|
52 |
< |
static char context[MAXWORD+1]; /* current context path */ |
52 |
> |
#define MAXCNTX 1023 /* maximum context length */ |
53 |
|
|
54 |
+ |
static char context[MAXCNTX+1]; /* current context path */ |
55 |
+ |
|
56 |
|
static VARDEF *hashtbl[NHASH]; /* definition list */ |
57 |
|
static int htndx; /* index for */ |
58 |
|
static VARDEF *htpos; /* ...dfirst() and */ |
52 |
– |
#ifdef OUTCHAN |
59 |
|
static EPNODE *ochpos; /* ...dnext */ |
60 |
|
static EPNODE *outchan; |
55 |
– |
#endif |
61 |
|
|
62 |
< |
#ifdef FUNCTION |
58 |
< |
EPNODE *curfunc; |
62 |
> |
EPNODE *curfunc = NULL; |
63 |
|
#define dname(ep) ((ep)->v.kid->type == SYM ? \ |
64 |
|
(ep)->v.kid->v.name : \ |
65 |
|
(ep)->v.kid->v.kid->v.name) |
62 |
– |
#else |
63 |
– |
#define dname(ep) ((ep)->v.kid->v.name) |
64 |
– |
#endif |
66 |
|
|
67 |
|
|
68 |
+ |
void |
69 |
|
fcompile(fname) /* get definitions from a file */ |
70 |
|
char *fname; |
71 |
|
{ |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
+ |
void |
90 |
|
scompile(str, fn, ln) /* get definitions from a string */ |
91 |
|
char *str; |
92 |
|
char *fn; |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
+ |
void |
120 |
|
varset(vname, assign, val) /* set a variable's value */ |
121 |
|
char *vname; |
122 |
|
int assign; |
151 |
|
} |
152 |
|
|
153 |
|
|
154 |
+ |
void |
155 |
|
dclear(name) /* delete variable definitions of name */ |
156 |
|
char *name; |
157 |
|
{ |
167 |
|
} |
168 |
|
|
169 |
|
|
170 |
+ |
void |
171 |
|
dremove(name) /* delete all definitions of name */ |
172 |
|
char *name; |
173 |
|
{ |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
+ |
int |
182 |
|
vardefined(name) /* return non-zero if variable defined */ |
183 |
|
char *name; |
184 |
|
{ |
205 |
|
cpp = context; /* start context with mark */ |
206 |
|
*cpp++ = CNTXMARK; |
207 |
|
do { /* carefully copy new context */ |
208 |
< |
if (cpp >= context+MAXWORD) |
208 |
> |
if (cpp >= context+MAXCNTX) |
209 |
|
break; /* just copy what we can */ |
210 |
|
if (isid(*ctx)) |
211 |
|
*cpp++ = *ctx++; |
225 |
|
char *ctx; |
226 |
|
{ |
227 |
|
extern char *strncpy(), *strcpy(); |
228 |
< |
char oldcontext[MAXWORD+1]; |
228 |
> |
char oldcontext[MAXCNTX+1]; |
229 |
|
register int n; |
230 |
|
|
231 |
|
strcpy(oldcontext, context); /* save old context */ |
232 |
|
setcontext(ctx); /* set new context */ |
233 |
|
n = strlen(context); /* tack on old */ |
234 |
< |
if (n+strlen(oldcontext) > MAXWORD) { |
235 |
< |
strncpy(context+n, oldcontext, MAXWORD-n); |
236 |
< |
context[MAXWORD] = '\0'; |
234 |
> |
if (n+strlen(oldcontext) > MAXCNTX) { |
235 |
> |
strncpy(context+n, oldcontext, MAXCNTX-n); |
236 |
> |
context[MAXCNTX] = '\0'; |
237 |
|
} else |
238 |
|
strcpy(context+n, oldcontext); |
239 |
|
return(context); |
303 |
|
} |
304 |
|
|
305 |
|
|
306 |
+ |
int |
307 |
|
incontext(qn) /* is qualified name in current context? */ |
308 |
|
register char *qn; |
309 |
|
{ |
315 |
|
} |
316 |
|
|
317 |
|
|
318 |
< |
#ifdef OUTCHAN |
318 |
> |
void |
319 |
|
chanout(cs) /* set output channels */ |
320 |
|
int (*cs)(); |
321 |
|
{ |
325 |
|
(*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling)); |
326 |
|
|
327 |
|
} |
320 |
– |
#endif |
328 |
|
|
329 |
|
|
330 |
+ |
void |
331 |
|
dcleanup(lvl) /* clear definitions (0->vars,1->output,2->consts) */ |
332 |
|
int lvl; |
333 |
|
{ |
342 |
|
dremove(vp->name); |
343 |
|
else |
344 |
|
dclear(vp->name); |
337 |
– |
#ifdef OUTCHAN |
345 |
|
if (lvl >= 1) { |
346 |
|
for (ep = outchan; ep != NULL; ep = ep->sibling) |
347 |
|
epfree(ep); |
348 |
|
outchan = NULL; |
349 |
|
} |
343 |
– |
#endif |
350 |
|
} |
351 |
|
|
352 |
|
|
390 |
|
return(vp); |
391 |
|
} |
392 |
|
vp = (VARDEF *)emalloc(sizeof(VARDEF)); |
387 |
– |
#ifdef FUNCTION |
393 |
|
vp->lib = liblookup(name); |
389 |
– |
#else |
390 |
– |
vp->lib = NULL; |
391 |
– |
#endif |
394 |
|
if (vp->lib == NULL) /* if name not in library */ |
395 |
|
name = qualname(name, 0); /* use fully qualified version */ |
396 |
|
hv = hash(name); |
403 |
|
} |
404 |
|
|
405 |
|
|
406 |
< |
#ifdef FUNCTION |
406 |
> |
void |
407 |
|
libupdate(fn) /* update library links */ |
408 |
|
char *fn; |
409 |
|
{ |
415 |
|
if (vp->lib != NULL || fn == NULL || !strcmp(fn, vp->name)) |
416 |
|
vp->lib = liblookup(vp->name); |
417 |
|
} |
416 |
– |
#endif |
418 |
|
|
419 |
|
|
420 |
+ |
void |
421 |
|
varfree(ln) /* release link to variable */ |
422 |
|
register VARDEF *ln; |
423 |
|
{ |
446 |
|
{ |
447 |
|
htndx = 0; |
448 |
|
htpos = NULL; |
447 |
– |
#ifdef OUTCHAN |
449 |
|
ochpos = outchan; |
449 |
– |
#endif |
450 |
|
return(dnext()); |
451 |
|
} |
452 |
|
|
468 |
|
return(ep); |
469 |
|
} |
470 |
|
} |
471 |
– |
#ifdef OUTCHAN |
471 |
|
if ((ep = ochpos) != NULL) |
472 |
|
ochpos = ep->sibling; |
473 |
|
return(ep); |
475 |
– |
#else |
476 |
– |
return(NULL); |
477 |
– |
#endif |
474 |
|
} |
475 |
|
|
476 |
|
|
490 |
|
} |
491 |
|
|
492 |
|
|
493 |
+ |
void |
494 |
|
dpush(nm, ep) /* push on a definition */ |
495 |
|
char *nm; |
496 |
|
register EPNODE *ep; |
503 |
|
} |
504 |
|
|
505 |
|
|
506 |
< |
#ifdef OUTCHAN |
506 |
> |
void |
507 |
|
addchan(sp) /* add an output channel assignment */ |
508 |
|
EPNODE *sp; |
509 |
|
{ |
531 |
|
sp->sibling = NULL; |
532 |
|
|
533 |
|
} |
537 |
– |
#endif |
534 |
|
|
535 |
|
|
536 |
+ |
void |
537 |
|
getstatement() /* get next statement */ |
538 |
|
{ |
539 |
|
register EPNODE *ep; |
544 |
|
scan(); |
545 |
|
return; |
546 |
|
} |
547 |
< |
#ifdef OUTCHAN |
548 |
< |
if (nextc == '$') { /* channel assignment */ |
547 |
> |
if (esupport&E_OUTCHAN && |
548 |
> |
nextc == '$') { /* channel assignment */ |
549 |
|
ep = getchan(); |
550 |
|
addchan(ep); |
551 |
< |
} else |
555 |
< |
#endif |
556 |
< |
{ /* ordinary definition */ |
551 |
> |
} else { /* ordinary definition */ |
552 |
|
ep = getdefn(); |
553 |
|
qname = qualname(dname(ep), 0); |
554 |
< |
#ifdef REDEFW |
560 |
< |
if ((vdef = varlookup(qname)) != NULL) |
554 |
> |
if (esupport&E_REDEFW && (vdef = varlookup(qname)) != NULL) |
555 |
|
if (vdef->def != NULL && epcmp(ep, vdef->def)) { |
556 |
|
wputs(qname); |
557 |
|
if (vdef->def->type == ':') |
558 |
|
wputs(": redefined constant expression\n"); |
559 |
|
else |
560 |
|
wputs(": redefined\n"); |
561 |
< |
} |
568 |
< |
#ifdef FUNCTION |
569 |
< |
else if (ep->v.kid->type == FUNC && vdef->lib != NULL) { |
561 |
> |
} else if (ep->v.kid->type == FUNC && vdef->lib != NULL) { |
562 |
|
wputs(qname); |
563 |
|
wputs(": definition hides library function\n"); |
564 |
|
} |
573 |
– |
#endif |
574 |
– |
#endif |
565 |
|
if (ep->type == ':') |
566 |
|
dremove(qname); |
567 |
|
else |
591 |
|
ep1->type = SYM; |
592 |
|
ep1->v.name = savestr(getname()); |
593 |
|
|
594 |
< |
#ifdef FUNCTION |
605 |
< |
if (nextc == '(') { |
594 |
> |
if (esupport&E_FUNCTION && nextc == '(') { |
595 |
|
ep2 = newnode(); |
596 |
|
ep2->type = FUNC; |
597 |
|
addekid(ep2, ep1); |
609 |
|
syntax("')' expected"); |
610 |
|
scan(); |
611 |
|
curfunc = ep1; |
612 |
< |
} else |
624 |
< |
curfunc = NULL; |
625 |
< |
#endif |
612 |
> |
} |
613 |
|
|
614 |
|
if (nextc != '=' && nextc != ':') |
615 |
|
syntax("'=' or ':' expected"); |
620 |
|
addekid(ep2, ep1); |
621 |
|
addekid(ep2, getE1()); |
622 |
|
|
623 |
< |
if ( |
637 |
< |
#ifdef FUNCTION |
638 |
< |
ep1->type == SYM && |
639 |
< |
#endif |
640 |
< |
ep1->sibling->type != NUM) { |
623 |
> |
if (ep1->type == SYM && ep1->sibling->type != NUM) { |
624 |
|
ep1 = newnode(); |
625 |
|
ep1->type = TICK; |
626 |
|
ep1->v.tick = 0; |
629 |
|
ep1->type = NUM; |
630 |
|
addekid(ep2, ep1); |
631 |
|
} |
632 |
+ |
curfunc = NULL; |
633 |
|
|
634 |
|
return(ep2); |
635 |
|
} |
636 |
|
|
637 |
|
|
654 |
– |
#ifdef OUTCHAN |
638 |
|
EPNODE * |
639 |
|
getchan() /* A -> $N = E1 */ |
640 |
|
{ |
659 |
|
|
660 |
|
return(ep2); |
661 |
|
} |
679 |
– |
#endif |
662 |
|
|
663 |
|
|
664 |
|
|
683 |
|
if (ep1->type == NUM) |
684 |
|
return(ep1->v.num); /* return if number */ |
685 |
|
ep2 = ep1->sibling; /* check time */ |
686 |
< |
if (ep2->v.tick == 0 || ep2->v.tick < eclock) { |
687 |
< |
ep2->v.tick = d->type == ':' ? ~0L : eclock; |
686 |
> |
if (eclock >= MAXCLOCK) |
687 |
> |
eclock = 1; /* wrap clock counter */ |
688 |
> |
if (ep2->v.tick < MAXCLOCK && |
689 |
> |
ep2->v.tick == 0 | ep2->v.tick != eclock) { |
690 |
> |
ep2->v.tick = d->type == ':' ? MAXCLOCK : eclock; |
691 |
|
ep2 = ep2->sibling; |
692 |
|
ep2->v.num = evalue(ep1); /* needs new value */ |
693 |
|
} else |