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" |
39 |
|
#define NHASH 521 /* hash size (a prime!) */ |
40 |
|
#endif |
41 |
|
|
42 |
+ |
#define hash(s) (shash(s)%NHASH) |
43 |
+ |
|
44 |
|
#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) |
45 |
|
|
46 |
< |
extern char *ecalloc(), *emalloc(), *savestr(), *strcpy(); |
46 |
> |
static double dvalue(); |
47 |
|
|
48 |
< |
static int hash(); |
48 |
> |
#define MAXCLOCK (1L<<31) /* clock wrap value */ |
49 |
|
|
50 |
< |
static double dvalue(); |
50 |
> |
unsigned long eclock = 0; /* value storage timer */ |
51 |
|
|
52 |
< |
long eclock = -1; /* value storage timer */ |
52 |
> |
#define MAXCNTX 1023 /* maximum context length */ |
53 |
|
|
54 |
< |
static char context[MAXWORD+1]; /* current context path */ |
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 |
|
{ |
196 |
|
|
197 |
|
if (ctx == NULL) |
198 |
|
return(context); /* just asking */ |
199 |
+ |
while (*ctx == CNTXMARK) |
200 |
+ |
ctx++; /* skip past marks */ |
201 |
|
if (!*ctx) { |
202 |
< |
context[0] = '\0'; /* clear context */ |
202 |
> |
context[0] = '\0'; /* empty means clear context */ |
203 |
|
return(context); |
204 |
|
} |
205 |
< |
cpp = context; /* else copy it (carefully!) */ |
206 |
< |
if (*ctx != CNTXMARK) |
207 |
< |
*cpp++ = CNTXMARK; /* make sure there's a mark */ |
208 |
< |
do { |
200 |
< |
if (cpp >= context+MAXWORD) |
205 |
> |
cpp = context; /* start context with mark */ |
206 |
> |
*cpp++ = CNTXMARK; |
207 |
> |
do { /* carefully copy new context */ |
208 |
> |
if (cpp >= context+MAXCNTX) |
209 |
|
break; /* just copy what we can */ |
210 |
|
if (isid(*ctx)) |
211 |
|
*cpp++ = *ctx++; |
213 |
|
*cpp++ = '_'; ctx++; |
214 |
|
} |
215 |
|
} while (*ctx); |
216 |
+ |
while (cpp[-1] == CNTXMARK) /* cannot end in context mark */ |
217 |
+ |
cpp--; |
218 |
|
*cpp = '\0'; |
219 |
|
return(context); |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
char * |
224 |
+ |
pushcontext(ctx) /* push on another context */ |
225 |
+ |
char *ctx; |
226 |
+ |
{ |
227 |
+ |
char oldcontext[MAXCNTX+1]; |
228 |
+ |
register int n; |
229 |
+ |
|
230 |
+ |
strcpy(oldcontext, context); /* save old context */ |
231 |
+ |
setcontext(ctx); /* set new context */ |
232 |
+ |
n = strlen(context); /* tack on old */ |
233 |
+ |
if (n+strlen(oldcontext) > MAXCNTX) { |
234 |
+ |
strncpy(context+n, oldcontext, MAXCNTX-n); |
235 |
+ |
context[MAXCNTX] = '\0'; |
236 |
+ |
} else |
237 |
+ |
strcpy(context+n, oldcontext); |
238 |
+ |
return(context); |
239 |
+ |
} |
240 |
+ |
|
241 |
+ |
|
242 |
+ |
char * |
243 |
+ |
popcontext() /* pop off top context */ |
244 |
+ |
{ |
245 |
+ |
register char *cp1, *cp2; |
246 |
+ |
|
247 |
+ |
if (!context[0]) /* nothing left to pop */ |
248 |
+ |
return(context); |
249 |
+ |
cp2 = context; /* find mark */ |
250 |
+ |
while (*++cp2 && *cp2 != CNTXMARK) |
251 |
+ |
; |
252 |
+ |
cp1 = context; /* copy tail to front */ |
253 |
+ |
while (*cp1++ = *cp2++) |
254 |
+ |
; |
255 |
+ |
return(context); |
256 |
+ |
} |
257 |
+ |
|
258 |
+ |
|
259 |
+ |
char * |
260 |
|
qualname(nam, lvl) /* get qualified name */ |
261 |
|
register char *nam; |
262 |
|
int lvl; |
302 |
|
} |
303 |
|
|
304 |
|
|
305 |
+ |
int |
306 |
|
incontext(qn) /* is qualified name in current context? */ |
307 |
|
register char *qn; |
308 |
|
{ |
309 |
+ |
if (!context[0]) /* global context accepts all */ |
310 |
+ |
return(1); |
311 |
|
while (*qn && *qn != CNTXMARK) /* find context mark */ |
312 |
|
qn++; |
313 |
|
return(!strcmp(qn, context)); |
314 |
|
} |
315 |
|
|
316 |
|
|
317 |
< |
#ifdef OUTCHAN |
317 |
> |
void |
318 |
|
chanout(cs) /* set output channels */ |
319 |
< |
int (*cs)(); |
319 |
> |
void (*cs)(); |
320 |
|
{ |
321 |
|
register EPNODE *ep; |
322 |
|
|
324 |
|
(*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling)); |
325 |
|
|
326 |
|
} |
278 |
– |
#endif |
327 |
|
|
328 |
|
|
329 |
+ |
void |
330 |
|
dcleanup(lvl) /* clear definitions (0->vars,1->output,2->consts) */ |
331 |
|
int lvl; |
332 |
|
{ |
336 |
|
/* if context is global, clear all */ |
337 |
|
for (i = 0; i < NHASH; i++) |
338 |
|
for (vp = hashtbl[i]; vp != NULL; vp = vp->next) |
339 |
< |
if (!context[0] || incontext(vp->name)) |
339 |
> |
if (incontext(vp->name)) |
340 |
|
if (lvl >= 2) |
341 |
|
dremove(vp->name); |
342 |
|
else |
343 |
|
dclear(vp->name); |
295 |
– |
#ifdef OUTCHAN |
344 |
|
if (lvl >= 1) { |
345 |
|
for (ep = outchan; ep != NULL; ep = ep->sibling) |
346 |
|
epfree(ep); |
347 |
|
outchan = NULL; |
348 |
|
} |
301 |
– |
#endif |
349 |
|
} |
350 |
|
|
351 |
|
|
389 |
|
return(vp); |
390 |
|
} |
391 |
|
vp = (VARDEF *)emalloc(sizeof(VARDEF)); |
345 |
– |
#ifdef FUNCTION |
392 |
|
vp->lib = liblookup(name); |
347 |
– |
#else |
348 |
– |
vp->lib = NULL; |
349 |
– |
#endif |
393 |
|
if (vp->lib == NULL) /* if name not in library */ |
394 |
|
name = qualname(name, 0); /* use fully qualified version */ |
395 |
|
hv = hash(name); |
402 |
|
} |
403 |
|
|
404 |
|
|
405 |
< |
#ifdef FUNCTION |
405 |
> |
void |
406 |
|
libupdate(fn) /* update library links */ |
407 |
|
char *fn; |
408 |
|
{ |
414 |
|
if (vp->lib != NULL || fn == NULL || !strcmp(fn, vp->name)) |
415 |
|
vp->lib = liblookup(vp->name); |
416 |
|
} |
374 |
– |
#endif |
417 |
|
|
418 |
|
|
419 |
+ |
void |
420 |
|
varfree(ln) /* release link to variable */ |
421 |
|
register VARDEF *ln; |
422 |
|
{ |
445 |
|
{ |
446 |
|
htndx = 0; |
447 |
|
htpos = NULL; |
405 |
– |
#ifdef OUTCHAN |
448 |
|
ochpos = outchan; |
407 |
– |
#endif |
449 |
|
return(dnext()); |
450 |
|
} |
451 |
|
|
467 |
|
return(ep); |
468 |
|
} |
469 |
|
} |
429 |
– |
#ifdef OUTCHAN |
470 |
|
if ((ep = ochpos) != NULL) |
471 |
|
ochpos = ep->sibling; |
472 |
|
return(ep); |
433 |
– |
#else |
434 |
– |
return(NULL); |
435 |
– |
#endif |
473 |
|
} |
474 |
|
|
475 |
|
|
489 |
|
} |
490 |
|
|
491 |
|
|
492 |
+ |
void |
493 |
|
dpush(nm, ep) /* push on a definition */ |
494 |
|
char *nm; |
495 |
|
register EPNODE *ep; |
502 |
|
} |
503 |
|
|
504 |
|
|
505 |
< |
#ifdef OUTCHAN |
505 |
> |
void |
506 |
|
addchan(sp) /* add an output channel assignment */ |
507 |
|
EPNODE *sp; |
508 |
|
{ |
530 |
|
sp->sibling = NULL; |
531 |
|
|
532 |
|
} |
495 |
– |
#endif |
533 |
|
|
534 |
|
|
535 |
+ |
void |
536 |
|
getstatement() /* get next statement */ |
537 |
|
{ |
538 |
|
register EPNODE *ep; |
543 |
|
scan(); |
544 |
|
return; |
545 |
|
} |
546 |
< |
#ifdef OUTCHAN |
547 |
< |
if (nextc == '$') { /* channel assignment */ |
546 |
> |
if (esupport&E_OUTCHAN && |
547 |
> |
nextc == '$') { /* channel assignment */ |
548 |
|
ep = getchan(); |
549 |
|
addchan(ep); |
550 |
< |
} else |
513 |
< |
#endif |
514 |
< |
{ /* ordinary definition */ |
550 |
> |
} else { /* ordinary definition */ |
551 |
|
ep = getdefn(); |
552 |
|
qname = qualname(dname(ep), 0); |
553 |
< |
#ifdef REDEFW |
554 |
< |
if ((vdef = varlookup(qname)) != NULL) |
519 |
< |
if (vdef->def != NULL) { |
553 |
> |
if (esupport&E_REDEFW && (vdef = varlookup(qname)) != NULL) |
554 |
> |
if (vdef->def != NULL && epcmp(ep, vdef->def)) { |
555 |
|
wputs(qname); |
556 |
|
if (vdef->def->type == ':') |
557 |
|
wputs(": redefined constant expression\n"); |
558 |
|
else |
559 |
|
wputs(": redefined\n"); |
560 |
< |
} |
526 |
< |
#ifdef FUNCTION |
527 |
< |
else if (ep->v.kid->type == FUNC && vdef->lib != NULL) { |
560 |
> |
} else if (ep->v.kid->type == FUNC && vdef->lib != NULL) { |
561 |
|
wputs(qname); |
562 |
|
wputs(": definition hides library function\n"); |
563 |
|
} |
531 |
– |
#endif |
532 |
– |
#endif |
564 |
|
if (ep->type == ':') |
565 |
|
dremove(qname); |
566 |
|
else |
590 |
|
ep1->type = SYM; |
591 |
|
ep1->v.name = savestr(getname()); |
592 |
|
|
593 |
< |
#ifdef FUNCTION |
563 |
< |
if (nextc == '(') { |
593 |
> |
if (esupport&E_FUNCTION && nextc == '(') { |
594 |
|
ep2 = newnode(); |
595 |
|
ep2->type = FUNC; |
596 |
|
addekid(ep2, ep1); |
608 |
|
syntax("')' expected"); |
609 |
|
scan(); |
610 |
|
curfunc = ep1; |
611 |
< |
} else |
582 |
< |
curfunc = NULL; |
583 |
< |
#endif |
611 |
> |
} |
612 |
|
|
613 |
|
if (nextc != '=' && nextc != ':') |
614 |
|
syntax("'=' or ':' expected"); |
619 |
|
addekid(ep2, ep1); |
620 |
|
addekid(ep2, getE1()); |
621 |
|
|
622 |
< |
if ( |
595 |
< |
#ifdef FUNCTION |
596 |
< |
ep1->type == SYM && |
597 |
< |
#endif |
598 |
< |
ep1->sibling->type != NUM) { |
622 |
> |
if (ep1->type == SYM && ep1->sibling->type != NUM) { |
623 |
|
ep1 = newnode(); |
624 |
|
ep1->type = TICK; |
625 |
< |
ep1->v.tick = -1; |
625 |
> |
ep1->v.tick = 0; |
626 |
|
addekid(ep2, ep1); |
627 |
|
ep1 = newnode(); |
628 |
|
ep1->type = NUM; |
629 |
|
addekid(ep2, ep1); |
630 |
|
} |
631 |
+ |
curfunc = NULL; |
632 |
|
|
633 |
|
return(ep2); |
634 |
|
} |
635 |
|
|
636 |
|
|
612 |
– |
#ifdef OUTCHAN |
637 |
|
EPNODE * |
638 |
|
getchan() /* A -> $N = E1 */ |
639 |
|
{ |
658 |
|
|
659 |
|
return(ep2); |
660 |
|
} |
637 |
– |
#endif |
661 |
|
|
662 |
|
|
663 |
|
|
682 |
|
if (ep1->type == NUM) |
683 |
|
return(ep1->v.num); /* return if number */ |
684 |
|
ep2 = ep1->sibling; /* check time */ |
685 |
< |
if (ep2->v.tick < 0 || ep2->v.tick < eclock) { |
686 |
< |
ep2->v.tick = d->type == ':' ? 1L<<30 : eclock; |
685 |
> |
if (eclock >= MAXCLOCK) |
686 |
> |
eclock = 1; /* wrap clock counter */ |
687 |
> |
if (ep2->v.tick < MAXCLOCK && |
688 |
> |
ep2->v.tick == 0 | ep2->v.tick != eclock) { |
689 |
> |
ep2->v.tick = d->type == ':' ? MAXCLOCK : eclock; |
690 |
|
ep2 = ep2->sibling; |
691 |
|
ep2->v.num = evalue(ep1); /* needs new value */ |
692 |
|
} else |
693 |
|
ep2 = ep2->sibling; /* else reuse old value */ |
694 |
|
|
695 |
|
return(ep2->v.num); |
670 |
– |
} |
671 |
– |
|
672 |
– |
|
673 |
– |
static int |
674 |
– |
hash(s) /* hash a string */ |
675 |
– |
register char *s; |
676 |
– |
{ |
677 |
– |
register int rval = 0; |
678 |
– |
|
679 |
– |
while (*s) |
680 |
– |
rval += *s++; |
681 |
– |
|
682 |
– |
return(rval % NHASH); |
696 |
|
} |