--- ray/src/common/caldefn.c 2003/07/27 22:12:01 2.20 +++ ray/src/common/caldefn.c 2023/02/07 20:28:16 2.34 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: caldefn.c,v 2.20 2003/07/27 22:12:01 schorsch Exp $"; +static const char RCSid[] = "$Id: caldefn.c,v 2.34 2023/02/07 20:28:16 greg Exp $"; #endif /* * Store variable definitions. @@ -27,11 +27,11 @@ static const char RCSid[] = "$Id: caldefn.c,v 2.20 200 #include "copyright.h" -#include -#include #include #include "rterror.h" +#include "rtio.h" +#include "rtmisc.h" #include "calcomp.h" #ifndef NHASH @@ -78,11 +78,18 @@ fcompile( /* get definitions from a file */ eputs(": cannot open\n"); quit(1); } - initfile(fp, fname, 0); +#ifdef getc_unlocked /* avoid stupid semaphores */ + flockfile(fp); +#endif + initfile(fp, fname, 0); while (nextc != EOF) getstatement(); if (fname != NULL) fclose(fp); +#ifdef getc_unlocked + else + funlockfile(fp); +#endif } @@ -113,7 +120,7 @@ evariable( /* evaluate a variable */ EPNODE *ep ) { - register VARDEF *dp = ep->v.ln; + VARDEF *dp = ep->v.ln; return(dvalue(dp->name, dp->def)); } @@ -127,11 +134,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; @@ -139,6 +147,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; @@ -150,7 +165,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); } @@ -160,7 +178,7 @@ dclear( /* delete variable definitions of name */ char *name ) { - register EPNODE *ep; + EPNODE *ep; while ((ep = dpop(name)) != NULL) { if (ep->type == ':') { @@ -177,7 +195,7 @@ dremove( /* delete all definitions of name */ char *name ) { - register EPNODE *ep; + EPNODE *ep; while ((ep = dpop(name)) != NULL) epfree(ep); @@ -185,22 +203,25 @@ 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 = dlookup(name); - return((dp = dlookup(name)) != NULL && dp->v.kid->type == SYM); + if (dp == NULL || dp->v.kid->type != SYM) + return(0); + + return(dp->type); } char * -setcontext( /* set a new context path */ - register char *ctx +calcontext( /* set a new context path */ + char *ctx ) { - register char *cpp; + char *cpp; if (ctx == NULL) return(context); /* just asking */ @@ -234,10 +255,10 @@ 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 */ + calcontext(ctx); /* set new context */ n = strlen(context); /* tack on old */ if (n+strlen(oldcontext) > MAXCNTX) { strncpy(context+n, oldcontext, MAXCNTX-n); @@ -251,7 +272,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); @@ -267,12 +288,12 @@ popcontext(void) /* pop off top context */ char * qualname( /* get qualified name */ - register char *nam, + char *nam, int lvl ) { static char nambuf[RMAXWORD+1]; - register char *cp = nambuf, *cpp; + char *cp = nambuf, *cpp; /* check for explicit local */ if (*nam == CNTXMARK) if (lvl > 0) /* only action is to refuse search */ @@ -314,7 +335,7 @@ toolong: int incontext( /* is qualified name in current context? */ - register char *qn + char *qn ) { if (!context[0]) /* global context accepts all */ @@ -330,7 +351,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)); @@ -343,9 +364,9 @@ 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) @@ -368,7 +389,7 @@ dlookup( /* look up a definition */ char *name ) { - register VARDEF *vp; + VARDEF *vp; if ((vp = varlookup(name)) == NULL) return(NULL); @@ -382,8 +403,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 +419,7 @@ varinsert( /* get a link to a variable */ char *name ) { - register VARDEF *vp; + VARDEF *vp; int hv; if ((vp = varlookup(name)) != NULL) { @@ -424,22 +445,22 @@ 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) - if (vp->lib != NULL || fn == NULL || !strcmp(fn, vp->name)) + if ((vp->lib != NULL) | (fn == NULL) || !strcmp(fn, vp->name)) vp->lib = liblookup(vp->name); } void varfree( /* release link to variable */ - register VARDEF *ln + VARDEF *ln ) { - register VARDEF *vp; + VARDEF *vp; int hv; if (--ln->nlinks > 0) @@ -472,8 +493,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 +518,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 +533,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 +550,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 +578,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(); @@ -605,9 +626,9 @@ getdefn(void) /* FUNC(SYM,..) = E1 */ /* FUNC(SYM,..) : E1 */ { - register EPNODE *ep1, *ep2; + EPNODE *ep1, *ep2; - if (!isalpha(nextc) && nextc != CNTXMARK) + if (!isalpha(nextc) & (nextc != CNTXMARK)) syntax("illegal variable name"); ep1 = newnode(); @@ -622,10 +643,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 != ')') @@ -634,7 +657,7 @@ getdefn(void) curfunc = ep1; } - if (nextc != '=' && nextc != ':') + if ((nextc != '=') & (nextc != ':')) syntax("'=' or ':' expected"); ep2 = newnode(); @@ -645,7 +668,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(); @@ -661,7 +684,7 @@ getdefn(void) EPNODE * getchan(void) /* A -> $N = E1 */ { - register EPNODE *ep1, *ep2; + EPNODE *ep1, *ep2; if (nextc != '$') syntax("missing '$'"); @@ -691,9 +714,9 @@ getchan(void) /* A -> $N = E1 */ static double /* evaluate a variable */ -dvalue(char *name, EPNODE *d) +dvalue(char *name, EPNODE *d) { - register EPNODE *ep1, *ep2; + EPNODE *ep1, *ep2; if (d == NULL || d->v.kid->type != SYM) { eputs(name); @@ -703,6 +726,10 @@ dvalue(char *name, 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 */