--- ray/src/common/caldefn.c 1992/09/21 12:01:44 2.4 +++ ray/src/common/caldefn.c 2003/02/25 02:47:21 2.13 @@ -1,9 +1,6 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: caldefn.c,v 2.13 2003/02/25 02:47:21 greg Exp $"; #endif - /* * Store variable definitions. * @@ -22,10 +19,18 @@ static char SCCSid[] = "$SunId$ LBL"; * 4/23/91 Added ':' assignment for constant expressions * * 8/7/91 Added optional context path to append to variable names + * + * 5/17/2001 Fixed clock counter wrapping behavior + * + * 2/19/03 Eliminated conditional compiles in favor of esupport extern. */ +#include "copyright.h" + #include +#include + #include #include "calcomp.h" @@ -34,36 +39,33 @@ static char SCCSid[] = "$SunId$ LBL"; #define NHASH 521 /* hash size (a prime!) */ #endif +#define hash(s) (shash(s)%NHASH) + #define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) -extern char *ecalloc(), *emalloc(), *savestr(), *strcpy(); +static double dvalue(); -static int hash(); +#define MAXCLOCK (1L<<31) /* clock wrap value */ -static double dvalue(); +unsigned long eclock = 0; /* value storage timer */ -long eclock = -1; /* value storage timer */ +#define MAXCNTX 1023 /* maximum context length */ -static char context[MAXWORD+1]; /* current context path */ +static char context[MAXCNTX+1]; /* current context path */ static VARDEF *hashtbl[NHASH]; /* definition list */ static int htndx; /* index for */ static VARDEF *htpos; /* ...dfirst() and */ -#ifdef OUTCHAN static EPNODE *ochpos; /* ...dnext */ static EPNODE *outchan; -#endif -#ifdef FUNCTION -EPNODE *curfunc; +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) -#endif +void fcompile(fname) /* get definitions from a file */ char *fname; { @@ -84,6 +86,7 @@ char *fname; } +void scompile(str, fn, ln) /* get definitions from a string */ char *str; char *fn; @@ -113,6 +116,7 @@ EPNODE *ep; } +void varset(vname, assign, val) /* set a variable's value */ char *vname; int assign; @@ -147,6 +151,7 @@ double val; } +void dclear(name) /* delete variable definitions of name */ char *name; { @@ -162,6 +167,7 @@ char *name; } +void dremove(name) /* delete all definitions of name */ char *name; { @@ -172,6 +178,7 @@ char *name; } +int vardefined(name) /* return non-zero if variable defined */ char *name; { @@ -189,15 +196,16 @@ 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) + cpp = context; /* start context with mark */ + *cpp++ = CNTXMARK; + do { /* carefully copy new context */ + if (cpp >= context+MAXCNTX) break; /* just copy what we can */ if (isid(*ctx)) *cpp++ = *ctx++; @@ -205,12 +213,51 @@ register char *ctx; *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[MAXCNTX+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) > MAXCNTX) { + strncpy(context+n, oldcontext, MAXCNTX-n); + context[MAXCNTX] = '\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; @@ -256,16 +303,19 @@ toolong: } +int 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 +void chanout(cs) /* set output channels */ int (*cs)(); { @@ -275,9 +325,9 @@ int (*cs)(); (*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling)); } -#endif +void dcleanup(lvl) /* clear definitions (0->vars,1->output,2->consts) */ int lvl; { @@ -287,18 +337,16 @@ 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 if (lvl >= 1) { for (ep = outchan; ep != NULL; ep = ep->sibling) epfree(ep); outchan = NULL; } -#endif } @@ -342,11 +390,7 @@ char *name; return(vp); } 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); @@ -359,7 +403,7 @@ char *name; } -#ifdef FUNCTION +void libupdate(fn) /* update library links */ char *fn; { @@ -371,9 +415,9 @@ char *fn; if (vp->lib != NULL || fn == NULL || !strcmp(fn, vp->name)) vp->lib = liblookup(vp->name); } -#endif +void varfree(ln) /* release link to variable */ register VARDEF *ln; { @@ -402,9 +446,7 @@ dfirst() /* return pointer to first definition */ { htndx = 0; htpos = NULL; -#ifdef OUTCHAN ochpos = outchan; -#endif return(dnext()); } @@ -426,13 +468,9 @@ dnext() /* return pointer to next definition */ return(ep); } } -#ifdef OUTCHAN if ((ep = ochpos) != NULL) ochpos = ep->sibling; return(ep); -#else - return(NULL); -#endif } @@ -452,6 +490,7 @@ char *name; } +void dpush(nm, ep) /* push on a definition */ char *nm; register EPNODE *ep; @@ -464,7 +503,7 @@ register EPNODE *ep; } -#ifdef OUTCHAN +void addchan(sp) /* add an output channel assignment */ EPNODE *sp; { @@ -492,9 +531,9 @@ EPNODE *sp; sp->sibling = NULL; } -#endif +void getstatement() /* get next statement */ { register EPNODE *ep; @@ -505,31 +544,24 @@ getstatement() /* get next statement */ scan(); return; } -#ifdef OUTCHAN - if (nextc == '$') { /* channel assignment */ + if (esupport&E_OUTCHAN && + nextc == '$') { /* channel assignment */ ep = getchan(); addchan(ep); - } else -#endif - { /* ordinary definition */ + } else { /* ordinary definition */ ep = getdefn(); qname = qualname(dname(ep), 0); -#ifdef REDEFW - if ((vdef = varlookup(qname)) != NULL) - if (vdef->def != NULL) { + if (esupport&E_REDEFW && (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) { + } else if (ep->v.kid->type == FUNC && vdef->lib != NULL) { wputs(qname); wputs(": definition hides library function\n"); } -#endif -#endif if (ep->type == ':') dremove(qname); else @@ -559,8 +591,7 @@ getdefn() /* A -> SYM = E1 */ ep1->type = SYM; ep1->v.name = savestr(getname()); -#ifdef FUNCTION - if (nextc == '(') { + if (esupport&E_FUNCTION && nextc == '(') { ep2 = newnode(); ep2->type = FUNC; addekid(ep2, ep1); @@ -578,9 +609,7 @@ getdefn() /* A -> SYM = E1 */ syntax("')' expected"); scan(); curfunc = ep1; - } else - curfunc = NULL; -#endif + } if (nextc != '=' && nextc != ':') syntax("'=' or ':' expected"); @@ -591,25 +620,21 @@ getdefn() /* A -> SYM = E1 */ addekid(ep2, ep1); addekid(ep2, getE1()); - if ( -#ifdef FUNCTION - ep1->type == SYM && -#endif - ep1->sibling->type != NUM) { + if (ep1->type == SYM && 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); } + curfunc = NULL; return(ep2); } -#ifdef OUTCHAN EPNODE * getchan() /* A -> $N = E1 */ { @@ -634,7 +659,6 @@ getchan() /* A -> $N = E1 */ return(ep2); } -#endif @@ -659,25 +683,15 @@ 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 (eclock >= MAXCLOCK) + eclock = 1; /* wrap clock counter */ + if (ep2->v.tick < MAXCLOCK && + ep2->v.tick == 0 | ep2->v.tick != eclock) { + ep2->v.tick = d->type == ':' ? MAXCLOCK : 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); }