--- ray/src/common/caldefn.c 2003/02/25 02:47:21 2.13 +++ ray/src/common/caldefn.c 2014/07/24 15:41:13 2.26 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: caldefn.c,v 2.13 2003/02/25 02:47:21 greg Exp $"; +static const char RCSid[] = "$Id: caldefn.c,v 2.26 2014/07/24 15:41:13 greg Exp $"; #endif /* * Store variable definitions. @@ -28,11 +28,12 @@ static const char RCSid[] = "$Id: caldefn.c,v 2.13 200 #include "copyright.h" #include - #include - #include +#include "rterror.h" +#include "rtio.h" +#include "rtmisc.h" #include "calcomp.h" #ifndef NHASH @@ -43,7 +44,7 @@ static const char RCSid[] = "$Id: caldefn.c,v 2.13 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 */ @@ -66,8 +67,9 @@ EPNODE *curfunc = NULL; void -fcompile(fname) /* get definitions from a file */ -char *fname; +fcompile( /* get definitions from a file */ + char *fname +) { FILE *fp; @@ -87,10 +89,11 @@ char *fname; void -scompile(str, fn, ln) /* get definitions from a string */ -char *str; -char *fn; -int ln; +scompile( /* get definitions from a string */ + char *str, + char *fn, + int ln +) { initstr(str, fn, ln); while (nextc != EOF) @@ -99,35 +102,39 @@ int ln; double -varvalue(vname) /* return a variable's value */ -char *vname; +varvalue( /* return a variable's value */ + char *vname +) { return(dvalue(vname, dlookup(vname))); } double -evariable(ep) /* evaluate a variable */ -EPNODE *ep; +evariable( /* evaluate a variable */ + EPNODE *ep +) { - register VARDEF *dp = ep->v.ln; + VARDEF *dp = ep->v.ln; return(dvalue(dp->name, dp->def)); } void -varset(vname, assign, val) /* set a variable's value */ -char *vname; -int assign; -double val; +varset( /* set a variable's value */ + char *vname, + int assign, + double val +) { 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; @@ -135,6 +142,13 @@ double val; 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; @@ -146,16 +160,20 @@ double val; ep2->type = NUM; ep2->v.num = val; addekid(ep1, ep2); - dremove(qname); + if (assign == ':') + dremove(qname); + else + dclear(qname); dpush(qname, ep1); } void -dclear(name) /* delete variable definitions of name */ -char *name; +dclear( /* delete variable definitions of name */ + char *name +) { - register EPNODE *ep; + EPNODE *ep; while ((ep = dpop(name)) != NULL) { if (ep->type == ':') { @@ -168,10 +186,11 @@ char *name; void -dremove(name) /* delete all definitions of name */ -char *name; +dremove( /* delete all definitions of name */ + char *name +) { - register EPNODE *ep; + EPNODE *ep; while ((ep = dpop(name)) != NULL) epfree(ep); @@ -179,20 +198,22 @@ char *name; int -vardefined(name) /* return non-zero if variable defined */ -char *name; +vardefined( /* return non-zero if variable defined */ + char *name +) { - register EPNODE *dp; + EPNODE *dp; return((dp = dlookup(name)) != NULL && dp->v.kid->type == SYM); } char * -setcontext(ctx) /* set a new context path */ -register char *ctx; +setcontext( /* set a new context path */ + char *ctx +) { - register char *cpp; + char *cpp; if (ctx == NULL) return(context); /* just asking */ @@ -221,12 +242,12 @@ register char *ctx; char * -pushcontext(ctx) /* push on another context */ -char *ctx; +pushcontext( /* push on another context */ + char *ctx +) { - extern char *strncpy(), *strcpy(); char oldcontext[MAXCNTX+1]; - register int n; + int n; strcpy(oldcontext, context); /* save old context */ setcontext(ctx); /* set new context */ @@ -241,9 +262,9 @@ char *ctx; char * -popcontext() /* pop off top context */ +popcontext(void) /* pop off top context */ { - register char *cp1, *cp2; + char *cp1, *cp2; if (!context[0]) /* nothing left to pop */ return(context); @@ -251,19 +272,20 @@ popcontext() /* pop off top context */ while (*++cp2 && *cp2 != CNTXMARK) ; cp1 = context; /* copy tail to front */ - while (*cp1++ = *cp2++) + while ( (*cp1++ = *cp2++) ) ; return(context); } char * -qualname(nam, lvl) /* get qualified name */ -register char *nam; -int lvl; +qualname( /* get qualified name */ + 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 */ @@ -274,7 +296,7 @@ int lvl; return(lvl > 0 ? NULL : nam); /* copy name to static buffer */ while (*nam) { - if (cp >= nambuf+MAXWORD) + if (cp >= nambuf+RMAXWORD) goto toolong; *cp++ = *nam++; } @@ -293,7 +315,7 @@ int lvl; ; } while (*cpp) { /* copy context to static buffer */ - if (cp >= nambuf+MAXWORD) + if (cp >= nambuf+RMAXWORD) goto toolong; *cp++ = *cpp++; } @@ -304,8 +326,9 @@ toolong: int -incontext(qn) /* is qualified name in current context? */ -register char *qn; +incontext( /* is qualified name in current context? */ + char *qn +) { if (!context[0]) /* global context accepts all */ return(1); @@ -316,10 +339,11 @@ register char *qn; void -chanout(cs) /* set output channels */ -int (*cs)(); +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)); @@ -328,20 +352,22 @@ int (*cs)(); void -dcleanup(lvl) /* clear definitions (0->vars,1->output,2->consts) */ -int lvl; +dcleanup( /* clear definitions (0->vars,1->output,2->consts) */ + 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); @@ -351,10 +377,11 @@ int lvl; EPNODE * -dlookup(name) /* look up a definition */ -char *name; +dlookup( /* look up a definition */ + char *name +) { - register VARDEF *vp; + VARDEF *vp; if ((vp = varlookup(name)) == NULL) return(NULL); @@ -363,12 +390,13 @@ char *name; VARDEF * -varlookup(name) /* look up a variable */ -char *name; +varlookup( /* look up a variable */ + char *name +) { 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) @@ -379,10 +407,11 @@ char *name; VARDEF * -varinsert(name) /* get a link to a variable */ -char *name; +varinsert( /* get a link to a variable */ + char *name +) { - register VARDEF *vp; + VARDEF *vp; int hv; if ((vp = varlookup(name)) != NULL) { @@ -404,11 +433,12 @@ char *name; void -libupdate(fn) /* update library links */ -char *fn; +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) @@ -418,10 +448,11 @@ char *fn; void -varfree(ln) /* release link to variable */ -register VARDEF *ln; +varfree( /* release link to variable */ + VARDEF *ln +) { - register VARDEF *vp; + VARDEF *vp; int hv; if (--ln->nlinks > 0) @@ -442,7 +473,7 @@ register VARDEF *ln; EPNODE * -dfirst() /* return pointer to first definition */ +dfirst(void) /* return pointer to first definition */ { htndx = 0; htpos = NULL; @@ -452,10 +483,10 @@ dfirst() /* return pointer to first definition */ EPNODE * -dnext() /* return pointer to next definition */ +dnext(void) /* return pointer to next definition */ { - register EPNODE *ep; - register char *nm; + EPNODE *ep; + char *nm; while (htndx < NHASH) { if (htpos == NULL) @@ -475,11 +506,12 @@ dnext() /* return pointer to next definition */ EPNODE * -dpop(name) /* pop a definition */ -char *name; +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); @@ -491,11 +523,12 @@ char *name; void -dpush(nm, ep) /* push on a definition */ -char *nm; -register EPNODE *ep; +dpush( /* push on a definition */ + char *nm, + EPNODE *ep +) { - register VARDEF *vp; + VARDEF *vp; vp = varinsert(nm); ep->sibling = vp->def; @@ -504,11 +537,12 @@ register EPNODE *ep; void -addchan(sp) /* add an output channel assignment */ -EPNODE *sp; +addchan( /* add an output channel assignment */ + EPNODE *sp +) { 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) { @@ -534,11 +568,11 @@ EPNODE *sp; void -getstatement() /* get next statement */ +getstatement(void) /* get next statement */ { - register EPNODE *ep; + EPNODE *ep; char *qname; - register VARDEF *vdef; + VARDEF *vdef; if (nextc == ';') { /* empty statement */ scan(); @@ -551,7 +585,7 @@ getstatement() /* 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 == ':') @@ -562,6 +596,7 @@ getstatement() /* get next statement */ wputs(qname); wputs(": definition hides library function\n"); } + } if (ep->type == ':') dremove(qname); else @@ -577,12 +612,13 @@ getstatement() /* get next statement */ EPNODE * -getdefn() /* A -> SYM = E1 */ - /* SYM : E1 */ - /* FUNC(SYM,..) = E1 */ - /* FUNC(SYM,..) : E1 */ +getdefn(void) + /* A -> SYM = E1 */ + /* SYM : E1 */ + /* FUNC(SYM,..) = E1 */ + /* FUNC(SYM,..) : E1 */ { - register EPNODE *ep1, *ep2; + EPNODE *ep1, *ep2; if (!isalpha(nextc) && nextc != CNTXMARK) syntax("illegal variable name"); @@ -599,7 +635,7 @@ getdefn() /* A -> SYM = E1 */ do { scan(); if (!isalpha(nextc)) - syntax("illegal variable name"); + syntax("illegal parameter name"); ep2 = newnode(); ep2->type = SYM; ep2->v.name = savestr(getname()); @@ -622,7 +658,7 @@ getdefn() /* A -> SYM = E1 */ 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(); @@ -636,9 +672,9 @@ getdefn() /* A -> SYM = E1 */ EPNODE * -getchan() /* A -> $N = E1 */ +getchan(void) /* A -> $N = E1 */ { - register EPNODE *ep1, *ep2; + EPNODE *ep1, *ep2; if (nextc != '$') syntax("missing '$'"); @@ -667,12 +703,10 @@ getchan() /* A -> $N = E1 */ */ -static double -dvalue(name, d) /* 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); @@ -686,7 +720,7 @@ EPNODE *d; 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 */