--- ray/src/common/calexpr.c 1991/08/14 08:18:14 1.15 +++ ray/src/common/calexpr.c 2008/05/01 16:42:05 2.32 @@ -1,9 +1,6 @@ -/* Copyright (c) 1991 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: calexpr.c,v 2.32 2008/05/01 16:42:05 greg Exp $"; #endif - /* * Compute data values using expression parser * @@ -16,57 +13,53 @@ static char SCCSid[] = "$SunId$ LBL"; * 1/29/87 Made variables conditional (VARIABLE) * * 5/19/88 Added constant subexpression elimination (RCONST) + * + * 2/19/03 Eliminated conditional compiles in favor of esupport extern. */ -#include +#include "copyright.h" +#include +#include #include - #include +#include +#include +#include "rtmisc.h" +#include "rtio.h" +#include "rterror.h" #include "calcomp.h" -#define MAXLINE 256 /* maximum line length */ +#define MAXLINE 256 /* maximum line length */ -#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) +#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) -#define isdecimal(c) (isdigit(c) || (c) == '.') +#define isdecimal(c) (isdigit(c) || (c) == '.') -extern double atof(), pow(); -extern char *fgets(), *savestr(); -extern char *emalloc(), *ecalloc(); -extern EPNODE *curfunc; -extern double efunc(), evariable(); -static double euminus(), echannel(), eargument(), enumber(); -static double eadd(), esubtr(), emult(), edivi(), epow(); -static double ebotch(); -extern int errno; +static double euminus(EPNODE *), eargument(EPNODE *), enumber(EPNODE *); +static double echannel(EPNODE *); +static double eadd(EPNODE *), esubtr(EPNODE *), + emult(EPNODE *), edivi(EPNODE *), + epow(EPNODE *); +static double ebotch(EPNODE *); +unsigned int esupport = /* what to support */ + E_VARIABLE | E_FUNCTION ; + +int eofc = 0; /* optional end-of-file character */ int nextc; /* lookahead character */ -double (*eoper[])() = { /* expression operations */ +double (*eoper[])(EPNODE *) = { /* expression operations */ ebotch, -#ifdef VARIABLE evariable, -#else - ebotch, -#endif enumber, euminus, -#ifdef INCHAN echannel, -#else - ebotch, -#endif -#ifdef FUNCTION efunc, eargument, -#else ebotch, ebotch, -#endif - ebotch, - ebotch, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, emult, @@ -92,15 +85,14 @@ static int linepos; /* position in buffer */ EPNODE * -eparse(expr) /* parse an expression string */ -char *expr; +eparse( /* parse an expression string */ + char *expr +) { EPNODE *ep; initstr(expr, NULL, 0); -#if defined(VARIABLE) && defined(FUNCTION) curfunc = NULL; -#endif ep = getE1(); if (nextc != EOF) syntax("unexpected character"); @@ -109,8 +101,9 @@ char *expr; double -eval(expr) /* evaluate an expression string */ -char *expr; +eval( /* evaluate an expression string */ + char *expr +) { register EPNODE *ep; double rval; @@ -122,18 +115,68 @@ char *expr; } -epfree(epar) /* free a parse tree */ -register EPNODE *epar; +int +epcmp( /* compare two expressions for equivalence */ + register EPNODE *ep1, + register EPNODE *ep2 +) { + double d; + + if (ep1->type != ep2->type) + return(1); + + switch (ep1->type) { + + case VAR: + return(ep1->v.ln != ep2->v.ln); + + case NUM: + if (ep2->v.num == 0) + return(ep1->v.num != 0); + d = ep1->v.num / ep2->v.num; + return((d > 1.000000000001) | (d < 0.999999999999)); + + case CHAN: + case ARG: + return(ep1->v.chan != ep2->v.chan); + + case '=': + case ':': + return(epcmp(ep1->v.kid->sibling, ep2->v.kid->sibling)); + + case CLKT: + case SYM: /* should never get this one */ + return(0); + + default: + ep1 = ep1->v.kid; + ep2 = ep2->v.kid; + while (ep1 != NULL) { + if (ep2 == NULL) + return(1); + if (epcmp(ep1, ep2)) + return(1); + ep1 = ep1->sibling; + ep2 = ep2->sibling; + } + return(ep2 != NULL); + } +} + + +void +epfree( /* free a parse tree */ + register EPNODE *epar +) +{ register EPNODE *ep; switch (epar->type) { -#if defined(VARIABLE) || defined(FUNCTION) case VAR: varfree(epar->v.ln); break; -#endif case SYM: freestr(epar->v.name); @@ -142,12 +185,14 @@ register EPNODE *epar; case NUM: case CHAN: case ARG: - case TICK: + case CLKT: break; default: - for (ep = epar->v.kid; ep != NULL; ep = ep->sibling) + while ((ep = epar->v.kid) != NULL) { + epar->v.kid = ep->sibling; epfree(ep); + } break; } @@ -156,43 +201,44 @@ register EPNODE *epar; } /* the following used to be a switch */ -#ifdef FUNCTION static double -eargument(ep) -EPNODE *ep; +eargument( + EPNODE *ep +) { return(argument(ep->v.chan)); } -#endif static double -enumber(ep) -EPNODE *ep; +enumber( + EPNODE *ep +) { return(ep->v.num); } static double -euminus(ep) -EPNODE *ep; +euminus( + EPNODE *ep +) { register EPNODE *ep1 = ep->v.kid; return(-evalue(ep1)); } -#ifdef INCHAN static double -echannel(ep) -EPNODE *ep; +echannel( + EPNODE *ep +) { return(chanvalue(ep->v.chan)); } -#endif static double -eadd(ep) -EPNODE *ep; +eadd( + EPNODE *ep +) { register EPNODE *ep1 = ep->v.kid; @@ -200,8 +246,9 @@ EPNODE *ep; } static double -esubtr(ep) -EPNODE *ep; +esubtr( + EPNODE *ep +) { register EPNODE *ep1 = ep->v.kid; @@ -209,8 +256,9 @@ EPNODE *ep; } static double -emult(ep) -EPNODE *ep; +emult( + EPNODE *ep +) { register EPNODE *ep1 = ep->v.kid; @@ -218,8 +266,9 @@ EPNODE *ep; } static double -edivi(ep) -EPNODE *ep; +edivi( + EPNODE *ep +) { register EPNODE *ep1 = ep->v.kid; double d; @@ -234,21 +283,25 @@ EPNODE *ep; } static double -epow(ep) -EPNODE *ep; +epow( + EPNODE *ep +) { register EPNODE *ep1 = ep->v.kid; double d; - int lasterrno; + int lasterrno; lasterrno = errno; errno = 0; d = pow(evalue(ep1), evalue(ep1->sibling)); -#ifdef IEEE - if (!finite(d)) - errno = EDOM; +#ifdef isnan + if (errno == 0) + if (isnan(d)) + errno = EDOM; + else if (isinf(d)) + errno = ERANGE; #endif - if (errno) { + if (errno == EDOM || errno == ERANGE) { wputs("Illegal power\n"); return(0.0); } @@ -257,18 +310,21 @@ EPNODE *ep; } static double -ebotch(ep) -EPNODE *ep; +ebotch( + EPNODE *ep +) { eputs("Bad expression!\n"); quit(1); + return 0.0; /* pro forma return */ } EPNODE * -ekid(ep, n) /* return pointer to a node's nth kid */ -register EPNODE *ep; -register int n; +ekid( /* return pointer to a node's nth kid */ + register EPNODE *ep, + register int n +) { for (ep = ep->v.kid; ep != NULL; ep = ep->sibling) @@ -280,8 +336,9 @@ register int n; int -nekids(ep) /* return # of kids for node ep */ -register EPNODE *ep; +nekids( /* return # of kids for node ep */ + register EPNODE *ep +) { register int n = 0; @@ -292,12 +349,14 @@ register EPNODE *ep; } -initfile(fp, fn, ln) /* prepare input file */ -FILE *fp; -char *fn; -int ln; +void +initfile( /* prepare input file */ + FILE *fp, + char *fn, + int ln +) { - static char inpbuf[MAXLINE]; + static char inpbuf[MAXLINE]; infp = fp; linbuf = inpbuf; @@ -309,10 +368,12 @@ int ln; } -initstr(s, fn, ln) /* prepare input string */ -char *s; -char *fn; -int ln; +void +initstr( /* prepare input string */ + char *s, + char *fn, + int ln +) { infp = NULL; infile = fn; @@ -323,11 +384,13 @@ int ln; } -getscanpos(fnp, lnp, spp, fpp) /* return current scan position */ -char **fnp; -int *lnp; -char **spp; -FILE **fpp; +void +getscanpos( /* return current scan position */ + char **fnp, + int *lnp, + char **spp, + FILE **fpp +) { if (fnp != NULL) *fnp = infile; if (lnp != NULL) *lnp = lineno; @@ -337,7 +400,7 @@ FILE **fpp; int -scan() /* scan next character, return literal next */ +scan(void) /* scan next character, return literal next */ { register int lnext = 0; @@ -354,6 +417,10 @@ scan() /* scan next character, return literal next nextc = linbuf[linepos++]; if (!lnext) lnext = nextc; + if (nextc == eofc) { + nextc = EOF; + break; + } if (nextc == '{') { scan(); while (nextc != '}') @@ -369,12 +436,13 @@ scan() /* scan next character, return literal next char * -ltoa(l) /* convert long to ascii */ -long l; +long2ascii( /* convert long to ascii */ + long l +) { - static char buf[16]; + static char buf[16]; register char *cp; - int neg = 0; + int neg = 0; if (l == 0) return("0"); @@ -394,8 +462,10 @@ long l; } -syntax(err) /* report syntax error and quit */ -char *err; +void +syntax( /* report syntax error and quit */ + char *err +) { register int i; @@ -403,9 +473,9 @@ char *err; if (infile != NULL) eputs(infile); if (lineno != 0) { eputs(infile != NULL ? ", line " : "line "); - eputs(ltoa((long)lineno)); + eputs(long2ascii((long)lineno)); } - eputs(": syntax error:\n"); + eputs(":\n"); } eputs(linbuf); if (linbuf[strlen(linbuf)-1] != '\n') @@ -419,9 +489,11 @@ char *err; } -addekid(ep, ekid) /* add a child to ep */ -register EPNODE *ep; -EPNODE *ekid; +void +addekid( /* add a child to ep */ + register EPNODE *ep, + EPNODE *ekid +) { if (ep->v.kid == NULL) ep->v.kid = ekid; @@ -435,13 +507,13 @@ EPNODE *ekid; char * -getname() /* scan an identifier */ +getname(void) /* scan an identifier */ { - static char str[MAXWORD+1]; + static char str[RMAXWORD+1]; register int i, lnext; lnext = nextc; - for (i = 0; i < MAXWORD && isid(lnext); i++, lnext = scan()) + for (i = 0; i < RMAXWORD && isid(lnext); i++, lnext = scan()) str[i] = lnext; str[i] = '\0'; while (isid(lnext)) /* skip rest of name */ @@ -452,7 +524,7 @@ getname() /* scan an identifier */ int -getinum() /* scan a positive integer */ +getinum(void) /* scan a positive integer */ { register int n, lnext; @@ -467,33 +539,37 @@ getinum() /* scan a positive integer */ double -getnum() /* scan a positive float */ +getnum(void) /* scan a positive float */ { register int i, lnext; - char str[MAXWORD+1]; + char str[RMAXWORD+1]; i = 0; lnext = nextc; - while (isdigit(lnext) && i < MAXWORD) { + while (isdigit(lnext) && i < RMAXWORD) { str[i++] = lnext; lnext = scan(); } - if (lnext == '.' && i < MAXWORD) { - str[i++] = lnext; - lnext = scan(); - while (isdigit(lnext) && i < MAXWORD) { + if (lnext == '.' && i < RMAXWORD) { + str[i++] = lnext; + lnext = scan(); + if (i == 1 && !isdigit(lnext)) + syntax("badly formed number"); + while (isdigit(lnext) && i < RMAXWORD) { str[i++] = lnext; lnext = scan(); } } - if ((lnext == 'e' || lnext == 'E') && i < MAXWORD) { - str[i++] = lnext; - lnext = scan(); - if ((lnext == '-' || lnext == '+') && i < MAXWORD) { + if ((lnext == 'e') | (lnext == 'E') && i < RMAXWORD) { + str[i++] = lnext; + lnext = scan(); + if ((lnext == '-') | (lnext == '+') && i < RMAXWORD) { str[i++] = lnext; lnext = scan(); } - while (isdigit(lnext) && i < MAXWORD) { + if (!isdigit(lnext)) + syntax("missing exponent"); + while (isdigit(lnext) && i < RMAXWORD) { str[i++] = lnext; lnext = scan(); } @@ -505,8 +581,8 @@ getnum() /* scan a positive float */ EPNODE * -getE1() /* E1 -> E1 ADDOP E2 */ - /* E2 */ +getE1(void) /* E1 -> E1 ADDOP E2 */ + /* E2 */ { register EPNODE *ep1, *ep2; @@ -517,10 +593,9 @@ getE1() /* E1 -> E1 ADDOP E2 */ scan(); addekid(ep2, ep1); addekid(ep2, getE2()); -#ifdef RCONST - if (ep1->type == NUM && ep1->sibling->type == NUM) + if (esupport&E_RCONST && + ep1->type == NUM && ep1->sibling->type == NUM) ep2 = rconst(ep2); -#endif ep1 = ep2; } return(ep1); @@ -528,8 +603,8 @@ getE1() /* E1 -> E1 ADDOP E2 */ EPNODE * -getE2() /* E2 -> E2 MULOP E3 */ - /* E3 */ +getE2(void) /* E2 -> E2 MULOP E3 */ + /* E3 */ { register EPNODE *ep1, *ep2; @@ -540,10 +615,9 @@ getE2() /* E2 -> E2 MULOP E3 */ scan(); addekid(ep2, ep1); addekid(ep2, getE3()); -#ifdef RCONST - if (ep1->type == NUM && ep1->sibling->type == NUM) + if (esupport&E_RCONST && + ep1->type == NUM && ep1->sibling->type == NUM) ep2 = rconst(ep2); -#endif ep1 = ep2; } return(ep1); @@ -551,8 +625,8 @@ getE2() /* E2 -> E2 MULOP E3 */ EPNODE * -getE3() /* E3 -> E4 ^ E3 */ - /* E4 */ +getE3(void) /* E3 -> E4 ^ E3 */ + /* E4 */ { register EPNODE *ep1, *ep2; @@ -563,10 +637,9 @@ getE3() /* E3 -> E4 ^ E3 */ scan(); addekid(ep2, ep1); addekid(ep2, getE3()); -#ifdef RCONST - if (ep1->type == NUM && ep1->sibling->type == NUM) + if (esupport&E_RCONST && + ep1->type == NUM && ep1->sibling->type == NUM) ep2 = rconst(ep2); -#endif return(ep2); } return(ep1); @@ -574,8 +647,8 @@ getE3() /* E3 -> E4 ^ E3 */ EPNODE * -getE4() /* E4 -> ADDOP E5 */ - /* E5 */ +getE4(void) /* E4 -> ADDOP E5 */ + /* E5 */ { register EPNODE *ep1, *ep2; @@ -586,6 +659,10 @@ getE4() /* E4 -> ADDOP E5 */ ep2->v.num = -ep2->v.num; return(ep2); } + if (ep2->type == UMINUS) { /* don't generate -(-E5) */ + efree((char *)ep2); + return(ep2->v.kid); + } ep1 = newnode(); ep1->type = UMINUS; addekid(ep1, ep2); @@ -598,98 +675,87 @@ getE4() /* E4 -> ADDOP E5 */ EPNODE * -getE5() /* E5 -> (E1) */ - /* VAR */ - /* NUM */ - /* $N */ - /* FUNC(E1,..) */ - /* ARG */ +getE5(void) /* E5 -> (E1) */ + /* VAR */ + /* NUM */ + /* $N */ + /* FUNC(E1,..) */ + /* ARG */ { - int i; - char *nam; - register EPNODE *ep1, *ep2; + int i; + char *nam; + register EPNODE *ep1, *ep2; - if (nextc == '(') { - scan(); - ep1 = getE1(); - if (nextc != ')') - syntax("')' expected"); - scan(); - return(ep1); - } + if (nextc == '(') { + scan(); + ep1 = getE1(); + if (nextc != ')') + syntax("')' expected"); + scan(); + return(ep1); + } -#ifdef INCHAN - if (nextc == '$') { - scan(); - ep1 = newnode(); - ep1->type = CHAN; - ep1->v.chan = getinum(); - return(ep1); - } -#endif + if (esupport&E_INCHAN && nextc == '$') { + scan(); + ep1 = newnode(); + ep1->type = CHAN; + ep1->v.chan = getinum(); + return(ep1); + } -#if defined(VARIABLE) || defined(FUNCTION) - if (isalpha(nextc) || nextc == CNTXMARK) { - nam = getname(); -#if defined(VARIABLE) && defined(FUNCTION) - ep1 = NULL; - if (curfunc != NULL) - for (i = 1, ep2 = curfunc->v.kid->sibling; - ep2 != NULL; i++, ep2 = ep2->sibling) - if (!strcmp(ep2->v.name, nam)) { - ep1 = newnode(); - ep1->type = ARG; - ep1->v.chan = i; - break; + if (esupport&(E_VARIABLE|E_FUNCTION) && + (isalpha(nextc) || nextc == CNTXMARK)) { + nam = getname(); + ep1 = NULL; + if ((esupport&(E_VARIABLE|E_FUNCTION)) == (E_VARIABLE|E_FUNCTION) + && curfunc != NULL) + for (i = 1, ep2 = curfunc->v.kid->sibling; + ep2 != NULL; i++, ep2 = ep2->sibling) + if (!strcmp(ep2->v.name, nam)) { + ep1 = newnode(); + ep1->type = ARG; + ep1->v.chan = i; + break; + } + if (ep1 == NULL) { + ep1 = newnode(); + ep1->type = VAR; + ep1->v.ln = varinsert(nam); } - if (ep1 == NULL) -#endif - { - ep1 = newnode(); - ep1->type = VAR; - ep1->v.ln = varinsert(nam); + if (esupport&E_FUNCTION && nextc == '(') { + ep2 = newnode(); + ep2->type = FUNC; + addekid(ep2, ep1); + ep1 = ep2; + do { + scan(); + addekid(ep1, getE1()); + } while (nextc == ','); + if (nextc != ')') + syntax("')' expected"); + scan(); + } else if (!(esupport&E_VARIABLE)) + syntax("'(' expected"); + if (esupport&E_RCONST && isconstvar(ep1)) + ep1 = rconst(ep1); + return(ep1); } -#ifdef FUNCTION - if (nextc == '(') { - ep2 = newnode(); - ep2->type = FUNC; - addekid(ep2, ep1); - ep1 = ep2; - do { - scan(); - addekid(ep1, getE1()); - } while (nextc == ','); - if (nextc != ')') - syntax("')' expected"); - scan(); - } -#ifndef VARIABLE - else - syntax("'(' expected"); -#endif -#endif -#ifdef RCONST - if (isconstvar(ep1)) - ep1 = rconst(ep1); -#endif - return(ep1); - } -#endif - if (isdecimal(nextc)) { - ep1 = newnode(); - ep1->type = NUM; - ep1->v.num = getnum(); - return(ep1); - } - syntax("unexpected character"); + if (isdecimal(nextc)) { + ep1 = newnode(); + ep1->type = NUM; + ep1->v.num = getnum(); + return(ep1); + } + syntax("unexpected character"); + return NULL; /* pro forma return */ } -#ifdef RCONST EPNODE * -rconst(epar) /* reduce a constant expression */ -register EPNODE *epar; +rconst( /* reduce a constant expression */ + register EPNODE *epar +) { register EPNODE *ep; @@ -697,22 +763,22 @@ register EPNODE *epar; ep->type = NUM; errno = 0; ep->v.num = evalue(epar); - if (errno) - syntax("bad constant expression"); + if (errno == EDOM || errno == ERANGE) + syntax("bad constant expression"); epfree(epar); return(ep); } -isconstvar(ep) /* is ep linked to a constant expression? */ -register EPNODE *ep; +int +isconstvar( /* is ep linked to a constant expression? */ + register EPNODE *ep +) { -#ifdef VARIABLE register EPNODE *ep1; -#ifdef FUNCTION - if (ep->type == FUNC) { + if (esupport&E_FUNCTION && ep->type == FUNC) { if (!isconstfun(ep->v.kid)) return(0); for (ep1 = ep->v.kid->sibling; ep1 != NULL; ep1 = ep1->sibling) @@ -720,40 +786,34 @@ register EPNODE *ep; return(0); return(1); } -#endif if (ep->type != VAR) return(0); ep1 = ep->v.ln->def; if (ep1 == NULL || ep1->type != ':') return(0); -#ifdef FUNCTION - if (ep1->v.kid->type != SYM) + if (esupport&E_FUNCTION && ep1->v.kid->type != SYM) return(0); -#endif return(1); -#else - return(ep->type == FUNC); -#endif } -#if defined(FUNCTION) && defined(VARIABLE) -isconstfun(ep) /* is ep linked to a constant function? */ -register EPNODE *ep; +int +isconstfun( /* is ep linked to a constant function? */ + register EPNODE *ep +) { register EPNODE *dp; register LIBR *lp; if (ep->type != VAR) return(0); - dp = ep->v.ln->def; - if (dp != NULL && dp->type != ':') - return(0); - if ((dp == NULL || dp->v.kid->type != FUNC) - && ((lp = liblookup(ep->v.ln->name)) == NULL - || lp->atyp != ':')) - return(0); - return(1); + if ((dp = ep->v.ln->def) != NULL) { + if (dp->v.kid->type == FUNC) + return(dp->type == ':'); + else + return(0); /* don't identify masked library functions */ + } + if ((lp = ep->v.ln->lib) != NULL) + return(lp->atyp == ':'); + return(0); } -#endif -#endif