--- ray/src/common/caldefn.c 1991/08/08 12:12:16 1.14 +++ ray/src/common/caldefn.c 1997/01/08 17:10:24 2.10 @@ -1,4 +1,4 @@ -/* Copyright (c) 1991 Regents of the University of California */ +/* Copyright (c) 1992 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -30,35 +30,37 @@ static char SCCSid[] = "$SunId$ LBL"; #include "calcomp.h" -#ifndef NHASH -#define NHASH 521 /* hash size (a prime!) */ +#ifndef NHASH +#define NHASH 521 /* hash size (a prime!) */ #endif -#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) +#define hash(s) (shash(s)%NHASH) -extern char *ecalloc(), *savestr(), *strcpy(); +#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) +extern char *ecalloc(), *emalloc(), *savestr(), *strcpy(); + static double dvalue(); -long eclock = -1; /* value storage timer */ +unsigned long eclock = 0; /* value storage timer */ -static char context[MAXWORD]; /* current context path */ +static char context[MAXWORD+1]; /* current context path */ static VARDEF *hashtbl[NHASH]; /* definition list */ static int htndx; /* index for */ static VARDEF *htpos; /* ...dfirst() and */ -#ifdef OUTCHAN +#ifdef OUTCHAN static EPNODE *ochpos; /* ...dnext */ static EPNODE *outchan; #endif -#ifdef FUNCTION -EPNODE *curfunc; -#define dname(ep) ((ep)->v.kid->type == SYM ? \ +#ifdef FUNCTION +EPNODE *curfunc = NULL; +#define dname(ep) ((ep)->v.kid->type == SYM ? \ (ep)->v.kid->v.name : \ (ep)->v.kid->v.kid->v.name) #else -#define dname(ep) ((ep)->v.kid->v.name) +#define dname(ep) ((ep)->v.kid->v.name) #endif @@ -103,7 +105,7 @@ char *vname; double evariable(ep) /* evaluate a variable */ -EPNODE *ep; +EPNODE *ep; { register VARDEF *dp = ep->v.ln; @@ -114,7 +116,7 @@ EPNODE *ep; varset(vname, assign, val) /* set a variable's value */ char *vname; int assign; -double val; +double val; { char *qname; register EPNODE *ep1, *ep2; @@ -187,87 +189,125 @@ register char *ctx; if (ctx == NULL) return(context); /* just asking */ + while (*ctx == CNTXMARK) + ctx++; /* skip past marks */ if (!*ctx) { - context[0] = '\0'; /* clear context */ + context[0] = '\0'; /* empty means clear context */ return(context); } - cpp = context; /* else copy it (carefully!) */ - if (*ctx != CNTXMARK) - *cpp++ = CNTXMARK; /* make sure there's a mark */ - do { - if (cpp >= context+MAXWORD-1) { - *cpp = '\0'; - wputs(context); - wputs(": context path too long\n"); - return(NULL); - } + cpp = context; /* start context with mark */ + *cpp++ = CNTXMARK; + do { /* carefully copy new context */ + if (cpp >= context+MAXWORD) + break; /* just copy what we can */ if (isid(*ctx)) *cpp++ = *ctx++; else { *cpp++ = '_'; ctx++; } } while (*ctx); + while (cpp[-1] == CNTXMARK) /* cannot end in context mark */ + cpp--; *cpp = '\0'; return(context); } char * +pushcontext(ctx) /* push on another context */ +char *ctx; +{ + extern char *strncpy(), *strcpy(); + char oldcontext[MAXWORD+1]; + register int n; + + strcpy(oldcontext, context); /* save old context */ + setcontext(ctx); /* set new context */ + n = strlen(context); /* tack on old */ + if (n+strlen(oldcontext) > MAXWORD) { + strncpy(context+n, oldcontext, MAXWORD-n); + context[MAXWORD] = '\0'; + } else + strcpy(context+n, oldcontext); + return(context); +} + + +char * +popcontext() /* pop off top context */ +{ + register char *cp1, *cp2; + + if (!context[0]) /* nothing left to pop */ + return(context); + cp2 = context; /* find mark */ + while (*++cp2 && *cp2 != CNTXMARK) + ; + cp1 = context; /* copy tail to front */ + while (*cp1++ = *cp2++) + ; + return(context); +} + + +char * qualname(nam, lvl) /* get qualified name */ register char *nam; int lvl; { - static char nambuf[MAXWORD]; - register char *cp = nambuf, *cpp = context; - /* check for repeat call */ - if (nam == nambuf) - return(lvl > 0 ? NULL : nambuf); + static char nambuf[MAXWORD+1]; + register char *cp = nambuf, *cpp; + /* check for explicit local */ + if (*nam == CNTXMARK) + if (lvl > 0) /* only action is to refuse search */ + return(NULL); + else + nam++; + else if (nam == nambuf) /* check for repeat call */ + return(lvl > 0 ? NULL : nam); /* copy name to static buffer */ while (*nam) { - if (cp >= nambuf+MAXWORD-1) + if (cp >= nambuf+MAXWORD) goto toolong; - if ((*cp++ = *nam++) == CNTXMARK) - cpp = NULL; /* flag a qualified name */ + *cp++ = *nam++; } - if (cpp == NULL) { + /* check for explicit global */ + if (cp > nambuf && cp[-1] == CNTXMARK) { if (lvl > 0) - return(NULL); /* no higher level */ - if (cp[-1] == CNTXMARK) { - cp--; cpp = context; /* current context explicitly */ - } else - cpp = ""; /* else fully qualified */ - } else /* else skip the requested levels */ - while (lvl-- > 0) { - if (!*cpp) - return(NULL); /* return NULL if past global level */ - while (*++cpp && *cpp != CNTXMARK) - ; - } + return(NULL); + *--cp = '\0'; + return(nambuf); /* already qualified */ + } + cpp = context; /* else skip the requested levels */ + while (lvl-- > 0) { + if (!*cpp) + return(NULL); /* return NULL if past global level */ + while (*++cpp && *cpp != CNTXMARK) + ; + } while (*cpp) { /* copy context to static buffer */ - if (cp >= nambuf+MAXWORD-1) + if (cp >= nambuf+MAXWORD) goto toolong; *cp++ = *cpp++; } - *cp = '\0'; - return(nambuf); /* return qualified name */ toolong: *cp = '\0'; - eputs(nambuf); - eputs(": name too long\n"); - quit(1); + return(nambuf); /* return qualified name */ } incontext(qn) /* is qualified name in current context? */ register char *qn; { + if (!context[0]) /* global context accepts all */ + return(1); while (*qn && *qn != CNTXMARK) /* find context mark */ - ; + qn++; return(!strcmp(qn, context)); } -#ifdef OUTCHAN +#ifdef OUTCHAN chanout(cs) /* set output channels */ int (*cs)(); { @@ -289,12 +329,12 @@ int lvl; /* if context is global, clear all */ for (i = 0; i < NHASH; i++) for (vp = hashtbl[i]; vp != NULL; vp = vp->next) - if (!context[0] || incontext(vp->name)) + if (incontext(vp->name)) if (lvl >= 2) dremove(vp->name); else dclear(vp->name); -#ifdef OUTCHAN +#ifdef OUTCHAN if (lvl >= 1) { for (ep = outchan; ep != NULL; ep = ep->sibling) epfree(ep); @@ -311,7 +351,7 @@ char *name; register VARDEF *vp; if ((vp = varlookup(name)) == NULL) - return(NULL); + return(NULL); return(vp->def); } @@ -320,10 +360,10 @@ VARDEF * varlookup(name) /* look up a variable */ char *name; { - int lvl = 0; + int lvl = 0; register char *qname; register VARDEF *vp; - /* find most qualified match */ + /* find most qualified match */ while ((qname = qualname(name, lvl++)) != NULL) for (vp = hashtbl[hash(qname)]; vp != NULL; vp = vp->next) if (!strcmp(vp->name, qname)) @@ -337,42 +377,62 @@ varinsert(name) /* get a link to a variable */ char *name; { register VARDEF *vp; - int hv; + int hv; if ((vp = varlookup(name)) != NULL) { vp->nlinks++; return(vp); } - name = qualname(name, 0); /* use fully qualified name */ - hv = hash(name); vp = (VARDEF *)emalloc(sizeof(VARDEF)); +#ifdef FUNCTION + vp->lib = liblookup(name); +#else + vp->lib = NULL; +#endif + if (vp->lib == NULL) /* if name not in library */ + name = qualname(name, 0); /* use fully qualified version */ + hv = hash(name); vp->name = savestr(name); vp->nlinks = 1; vp->def = NULL; - vp->lib = NULL; vp->next = hashtbl[hv]; hashtbl[hv] = vp; return(vp); } +#ifdef FUNCTION +libupdate(fn) /* update library links */ +char *fn; +{ + register int i; + register VARDEF *vp; + /* if fn is NULL then relink all */ + for (i = 0; i < NHASH; i++) + for (vp = hashtbl[i]; vp != NULL; vp = vp->next) + if (vp->lib != NULL || fn == NULL || !strcmp(fn, vp->name)) + vp->lib = liblookup(vp->name); +} +#endif + + varfree(ln) /* release link to variable */ -register VARDEF *ln; +register VARDEF *ln; { register VARDEF *vp; - int hv; + int hv; if (--ln->nlinks > 0) - return; /* still active */ + return; /* still active */ hv = hash(ln->name); vp = hashtbl[hv]; if (vp == ln) - hashtbl[hv] = vp->next; + hashtbl[hv] = vp->next; else { - while (vp->next != ln) /* must be in list */ - vp = vp->next; - vp->next = ln->next; + while (vp->next != ln) /* must be in list */ + vp = vp->next; + vp->next = ln->next; } freestr(ln->name); efree((char *)ln); @@ -384,7 +444,7 @@ dfirst() /* return pointer to first definition */ { htndx = 0; htpos = NULL; -#ifdef OUTCHAN +#ifdef OUTCHAN ochpos = outchan; #endif return(dnext()); @@ -395,20 +455,22 @@ EPNODE * dnext() /* return pointer to next definition */ { register EPNODE *ep; + register char *nm; while (htndx < NHASH) { - if (htpos == NULL) - htpos = hashtbl[htndx++]; - while (htpos != NULL) { - ep = htpos->def; - htpos = htpos->next; - if (ep != NULL) - return(ep); - } + if (htpos == NULL) + htpos = hashtbl[htndx++]; + while (htpos != NULL) { + ep = htpos->def; + nm = htpos->name; + htpos = htpos->next; + if (ep != NULL && incontext(nm)) + return(ep); + } } -#ifdef OUTCHAN +#ifdef OUTCHAN if ((ep = ochpos) != NULL) - ochpos = ep->sibling; + ochpos = ep->sibling; return(ep); #else return(NULL); @@ -424,7 +486,7 @@ char *name; register EPNODE *dp; if ((vp = varlookup(name)) == NULL || vp->def == NULL) - return(NULL); + return(NULL); dp = vp->def; vp->def = dp->sibling; varfree(vp); @@ -434,7 +496,7 @@ char *name; dpush(nm, ep) /* push on a definition */ char *nm; -register EPNODE *ep; +register EPNODE *ep; { register VARDEF *vp; @@ -444,11 +506,11 @@ register EPNODE *ep; } -#ifdef OUTCHAN +#ifdef OUTCHAN addchan(sp) /* add an output channel assignment */ -EPNODE *sp; +EPNODE *sp; { - int ch = sp->v.kid->v.chan; + int ch = sp->v.kid->v.chan; register EPNODE *ep, *epl; for (epl = NULL, ep = outchan; ep != NULL; epl = ep, ep = ep->sibling) @@ -479,13 +541,13 @@ getstatement() /* get next statement */ { register EPNODE *ep; char *qname; - EPNODE *lastdef; + register VARDEF *vdef; if (nextc == ';') { /* empty statement */ scan(); return; } -#ifdef OUTCHAN +#ifdef OUTCHAN if (nextc == '$') { /* channel assignment */ ep = getchan(); addchan(ep); @@ -494,19 +556,20 @@ getstatement() /* get next statement */ { /* ordinary definition */ ep = getdefn(); qname = qualname(dname(ep), 0); -#ifdef REDEFW - if ((lastdef = dlookup(qname)) != NULL) { - wputs(qname); - if (lastdef->type == ':') - wputs(": redefined constant expression\n"); - else - wputs(": redefined\n"); - } -#ifdef FUNCTION - else if (ep->v.kid->type == FUNC && liblookup(qname) != NULL) { - wputs(qname); - wputs(": definition hides library function\n"); - } +#ifdef REDEFW + if ((vdef = varlookup(qname)) != NULL) + if (vdef->def != NULL && epcmp(ep, vdef->def)) { + wputs(qname); + if (vdef->def->type == ':') + wputs(": redefined constant expression\n"); + else + wputs(": redefined\n"); + } +#ifdef FUNCTION + else if (ep->v.kid->type == FUNC && vdef->lib != NULL) { + wputs(qname); + wputs(": definition hides library function\n"); + } #endif #endif if (ep->type == ':') @@ -526,19 +589,19 @@ getstatement() /* get next statement */ EPNODE * getdefn() /* A -> SYM = E1 */ /* SYM : E1 */ - /* FUNC(SYM,..) = E1 */ + /* FUNC(SYM,..) = E1 */ /* FUNC(SYM,..) : E1 */ { register EPNODE *ep1, *ep2; - if (!isalpha(nextc)) + if (!isalpha(nextc) && nextc != CNTXMARK) syntax("illegal variable name"); ep1 = newnode(); ep1->type = SYM; ep1->v.name = savestr(getname()); -#ifdef FUNCTION +#ifdef FUNCTION if (nextc == '(') { ep2 = newnode(); ep2->type = FUNC; @@ -557,8 +620,7 @@ getdefn() /* A -> SYM = E1 */ syntax("')' expected"); scan(); curfunc = ep1; - } else - curfunc = NULL; + } #endif if (nextc != '=' && nextc != ':') @@ -571,24 +633,28 @@ getdefn() /* A -> SYM = E1 */ addekid(ep2, getE1()); if ( -#ifdef FUNCTION - ep1->type == SYM && +#ifdef FUNCTION + ep1->type == SYM && #endif ep1->sibling->type != NUM) { ep1 = newnode(); ep1->type = TICK; - ep1->v.tick = -1; + ep1->v.tick = 0; addekid(ep2, ep1); ep1 = newnode(); ep1->type = NUM; addekid(ep2, ep1); } +#ifdef FUNCTION + curfunc = NULL; +#endif + return(ep2); } -#ifdef OUTCHAN +#ifdef OUTCHAN EPNODE * getchan() /* A -> $N = E1 */ { @@ -625,7 +691,7 @@ getchan() /* A -> $N = E1 */ static double dvalue(name, d) /* evaluate a variable */ char *name; -EPNODE *d; +EPNODE *d; { register EPNODE *ep1, *ep2; @@ -638,25 +704,12 @@ EPNODE *d; if (ep1->type == NUM) return(ep1->v.num); /* return if number */ ep2 = ep1->sibling; /* check time */ - if (ep2->v.tick < 0 || ep2->v.tick < eclock) { - ep2->v.tick = d->type == ':' ? 1L<<30 : eclock; + if (ep2->v.tick == 0 || ep2->v.tick < eclock) { + ep2->v.tick = d->type == ':' ? ~0L : eclock; ep2 = ep2->sibling; ep2->v.num = evalue(ep1); /* needs new value */ } else ep2 = ep2->sibling; /* else reuse old value */ return(ep2->v.num); -} - - -static int -hash(s) /* hash a string */ -register char *s; -{ - register int rval = 0; - - while (*s) - rval += *s++; - - return(rval % NHASH); }