--- ray/src/common/caldefn.c 2003/06/07 12:50:20 2.16 +++ ray/src/common/caldefn.c 2022/03/12 15:50:13 2.30 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: caldefn.c,v 2.16 2003/06/07 12:50:20 schorsch Exp $"; +static const char RCSid[] = "$Id: caldefn.c,v 2.30 2022/03/12 15:50:13 greg Exp $"; #endif /* * Store variable definitions. @@ -27,12 +27,11 @@ static const char RCSid[] = "$Id: caldefn.c,v 2.16 200 #include "copyright.h" -#include - -#include - #include +#include "rterror.h" +#include "rtio.h" +#include "rtmisc.h" #include "calcomp.h" #ifndef NHASH @@ -43,7 +42,7 @@ static const char RCSid[] = "$Id: caldefn.c,v 2.16 200 #define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) -static double dvalue(); +static double dvalue(char *name, EPNODE *d); #define MAXCLOCK (1L<<31) /* clock wrap value */ @@ -114,7 +113,7 @@ evariable( /* evaluate a variable */ EPNODE *ep ) { - register VARDEF *dp = ep->v.ln; + VARDEF *dp = ep->v.ln; return(dvalue(dp->name, dp->def)); } @@ -128,11 +127,12 @@ varset( /* set a variable's value */ ) { char *qname; - register EPNODE *ep1, *ep2; + EPNODE *ep1, *ep2; /* get qualified name */ qname = qualname(vname, 0); /* check for quick set */ - if ((ep1 = dlookup(qname)) != NULL && ep1->v.kid->type == SYM) { + if ((ep1 = dlookup(qname)) != NULL && ep1->v.kid->type == SYM && + (ep1->type == ':') <= (assign == ':')) { ep2 = ep1->v.kid->sibling; if (ep2->type == NUM) { ep2->v.num = val; @@ -140,6 +140,13 @@ varset( /* set a variable's value */ return; } } + if (ep1 != NULL && esupport&E_REDEFW) { + wputs(qname); + if (ep1->type == ':') + wputs(": reset constant expression\n"); + else + wputs(": reset expression\n"); + } /* hand build definition */ ep1 = newnode(); ep1->type = assign; @@ -151,7 +158,10 @@ varset( /* set a variable's value */ ep2->type = NUM; ep2->v.num = val; addekid(ep1, ep2); - dremove(qname); + if (assign == ':') + dremove(qname); + else + dclear(qname); dpush(qname, ep1); } @@ -161,7 +171,7 @@ dclear( /* delete variable definitions of name */ char *name ) { - register EPNODE *ep; + EPNODE *ep; while ((ep = dpop(name)) != NULL) { if (ep->type == ':') { @@ -178,7 +188,7 @@ dremove( /* delete all definitions of name */ char *name ) { - register EPNODE *ep; + EPNODE *ep; while ((ep = dpop(name)) != NULL) epfree(ep); @@ -186,22 +196,24 @@ dremove( /* delete all definitions of name */ int -vardefined( /* return non-zero if variable defined */ +vardefined( /* return '=' or ':' if variable/constant defined */ char *name ) { - register EPNODE *dp; + EPNODE *dp; - return((dp = dlookup(name)) != NULL && dp->v.kid->type == SYM); + if ((dp = dlookup(name)) == NULL || dp->v.kid->type != SYM) + return(0); + return(dp->type); } char * setcontext( /* set a new context path */ - register char *ctx + char *ctx ) { - register char *cpp; + char *cpp; if (ctx == NULL) return(context); /* just asking */ @@ -235,7 +247,7 @@ pushcontext( /* push on another context */ ) { char oldcontext[MAXCNTX+1]; - register int n; + int n; strcpy(oldcontext, context); /* save old context */ setcontext(ctx); /* set new context */ @@ -252,7 +264,7 @@ pushcontext( /* push on another context */ char * popcontext(void) /* pop off top context */ { - register char *cp1, *cp2; + char *cp1, *cp2; if (!context[0]) /* nothing left to pop */ return(context); @@ -260,7 +272,7 @@ popcontext(void) /* pop off top context */ while (*++cp2 && *cp2 != CNTXMARK) ; cp1 = context; /* copy tail to front */ - while (*cp1++ = *cp2++) + while ( (*cp1++ = *cp2++) ) ; return(context); } @@ -268,12 +280,12 @@ popcontext(void) /* pop off top context */ char * qualname( /* get qualified name */ - register char *nam, + char *nam, int lvl ) { - static char nambuf[MAXWORD+1]; - register char *cp = nambuf, *cpp; + static char nambuf[RMAXWORD+1]; + char *cp = nambuf, *cpp; /* check for explicit local */ if (*nam == CNTXMARK) if (lvl > 0) /* only action is to refuse search */ @@ -284,7 +296,7 @@ qualname( /* get qualified name */ return(lvl > 0 ? NULL : nam); /* copy name to static buffer */ while (*nam) { - if (cp >= nambuf+MAXWORD) + if (cp >= nambuf+RMAXWORD) goto toolong; *cp++ = *nam++; } @@ -303,7 +315,7 @@ qualname( /* get qualified name */ ; } while (*cpp) { /* copy context to static buffer */ - if (cp >= nambuf+MAXWORD) + if (cp >= nambuf+RMAXWORD) goto toolong; *cp++ = *cpp++; } @@ -315,7 +327,7 @@ toolong: int incontext( /* is qualified name in current context? */ - register char *qn + char *qn ) { if (!context[0]) /* global context accepts all */ @@ -331,7 +343,7 @@ chanout( /* set output channels */ void (*cs)(int n, double v) ) { - register EPNODE *ep; + EPNODE *ep; for (ep = outchan; ep != NULL; ep = ep->sibling) (*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling)); @@ -344,17 +356,18 @@ dcleanup( /* clear definitions (0->vars,1->output,2-> int lvl ) { - register int i; - register VARDEF *vp; - register EPNODE *ep; + int i; + VARDEF *vp; + EPNODE *ep; /* if context is global, clear all */ for (i = 0; i < NHASH; i++) for (vp = hashtbl[i]; vp != NULL; vp = vp->next) - if (incontext(vp->name)) + if (incontext(vp->name)) { if (lvl >= 2) dremove(vp->name); else dclear(vp->name); + } if (lvl >= 1) { for (ep = outchan; ep != NULL; ep = ep->sibling) epfree(ep); @@ -368,7 +381,7 @@ dlookup( /* look up a definition */ char *name ) { - register VARDEF *vp; + VARDEF *vp; if ((vp = varlookup(name)) == NULL) return(NULL); @@ -382,8 +395,8 @@ varlookup( /* look up a variable */ ) { int lvl = 0; - register char *qname; - register VARDEF *vp; + char *qname; + VARDEF *vp; /* find most qualified match */ while ((qname = qualname(name, lvl++)) != NULL) for (vp = hashtbl[hash(qname)]; vp != NULL; vp = vp->next) @@ -398,7 +411,7 @@ varinsert( /* get a link to a variable */ char *name ) { - register VARDEF *vp; + VARDEF *vp; int hv; if ((vp = varlookup(name)) != NULL) { @@ -424,8 +437,8 @@ libupdate( /* update library links */ char *fn ) { - register int i; - register VARDEF *vp; + int i; + VARDEF *vp; /* if fn is NULL then relink all */ for (i = 0; i < NHASH; i++) for (vp = hashtbl[i]; vp != NULL; vp = vp->next) @@ -436,10 +449,10 @@ libupdate( /* update library links */ void varfree( /* release link to variable */ - register VARDEF *ln + VARDEF *ln ) { - register VARDEF *vp; + VARDEF *vp; int hv; if (--ln->nlinks > 0) @@ -472,8 +485,8 @@ dfirst(void) /* return pointer to first definition * EPNODE * dnext(void) /* return pointer to next definition */ { - register EPNODE *ep; - register char *nm; + EPNODE *ep; + char *nm; while (htndx < NHASH) { if (htpos == NULL) @@ -497,8 +510,8 @@ dpop( /* pop a definition */ char *name ) { - register VARDEF *vp; - register EPNODE *dp; + VARDEF *vp; + EPNODE *dp; if ((vp = varlookup(name)) == NULL || vp->def == NULL) return(NULL); @@ -512,10 +525,10 @@ dpop( /* pop a definition */ void dpush( /* push on a definition */ char *nm, - register EPNODE *ep + EPNODE *ep ) { - register VARDEF *vp; + VARDEF *vp; vp = varinsert(nm); ep->sibling = vp->def; @@ -529,7 +542,7 @@ addchan( /* add an output channel assignment */ ) { int ch = sp->v.kid->v.chan; - register EPNODE *ep, *epl; + EPNODE *ep, *epl; for (epl = NULL, ep = outchan; ep != NULL; epl = ep, ep = ep->sibling) if (ep->v.kid->v.chan >= ch) { @@ -557,9 +570,9 @@ addchan( /* add an output channel assignment */ void getstatement(void) /* get next statement */ { - register EPNODE *ep; + EPNODE *ep; char *qname; - register VARDEF *vdef; + VARDEF *vdef; if (nextc == ';') { /* empty statement */ scan(); @@ -572,7 +585,7 @@ getstatement(void) /* get next statement */ } else { /* ordinary definition */ ep = getdefn(); qname = qualname(dname(ep), 0); - if (esupport&E_REDEFW && (vdef = varlookup(qname)) != NULL) + if (esupport&E_REDEFW && (vdef = varlookup(qname)) != NULL) { if (vdef->def != NULL && epcmp(ep, vdef->def)) { wputs(qname); if (vdef->def->type == ':') @@ -583,6 +596,7 @@ getstatement(void) /* get next statement */ wputs(qname); wputs(": definition hides library function\n"); } + } if (ep->type == ':') dremove(qname); else @@ -604,7 +618,7 @@ getdefn(void) /* FUNC(SYM,..) = E1 */ /* FUNC(SYM,..) : E1 */ { - register EPNODE *ep1, *ep2; + EPNODE *ep1, *ep2; if (!isalpha(nextc) && nextc != CNTXMARK) syntax("illegal variable name"); @@ -621,10 +635,12 @@ getdefn(void) do { scan(); if (!isalpha(nextc)) - syntax("illegal variable name"); + syntax("illegal parameter name"); ep2 = newnode(); ep2->type = SYM; ep2->v.name = savestr(getname()); + if (strchr(ep2->v.name, CNTXMARK) != NULL) + syntax("illegal parameter name"); addekid(ep1, ep2); } while (nextc == ','); if (nextc != ')') @@ -644,7 +660,7 @@ getdefn(void) if (ep1->type == SYM && ep1->sibling->type != NUM) { ep1 = newnode(); - ep1->type = TICK; + ep1->type = CLKT; ep1->v.tick = 0; addekid(ep2, ep1); ep1 = newnode(); @@ -660,7 +676,7 @@ getdefn(void) EPNODE * getchan(void) /* A -> $N = E1 */ { - register EPNODE *ep1, *ep2; + EPNODE *ep1, *ep2; if (nextc != '$') syntax("missing '$'"); @@ -689,13 +705,10 @@ getchan(void) /* A -> $N = E1 */ */ -static double -dvalue( /* evaluate a variable */ -char *name, -EPNODE *d -) +static double /* evaluate a variable */ +dvalue(char *name, EPNODE *d) { - register EPNODE *ep1, *ep2; + EPNODE *ep1, *ep2; if (d == NULL || d->v.kid->type != SYM) { eputs(name); @@ -705,11 +718,15 @@ EPNODE *d ep1 = d->v.kid->sibling; /* get expression */ if (ep1->type == NUM) return(ep1->v.num); /* return if number */ + if (esupport&E_RCONST && d->type == ':') { + wputs(name); + wputs(": assigned non-constant value\n"); + } ep2 = ep1->sibling; /* check time */ if (eclock >= MAXCLOCK) eclock = 1; /* wrap clock counter */ if (ep2->v.tick < MAXCLOCK && - ep2->v.tick == 0 | ep2->v.tick != eclock) { + (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 */