--- ray/src/common/calexpr.c 2003/06/07 12:50:20 2.22 +++ ray/src/common/calexpr.c 2010/07/25 05:50:27 2.34 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: calexpr.c,v 2.22 2003/06/07 12:50:20 schorsch Exp $"; +static const char RCSid[] = "$Id: calexpr.c,v 2.34 2010/07/25 05:50:27 greg Exp $"; #endif /* * Compute data values using expression parser @@ -26,6 +26,9 @@ static const char RCSid[] = "$Id: calexpr.c,v 2.22 200 #include #include +#include "rtmisc.h" +#include "rtio.h" +#include "rterror.h" #include "calcomp.h" #define MAXLINE 256 /* maximum line length */ @@ -34,17 +37,20 @@ static const char RCSid[] = "$Id: calexpr.c,v 2.22 200 #define isdecimal(c) (isdigit(c) || (c) == '.') -static double euminus(), eargument(), enumber(); -static double echannel(); -static double eadd(), esubtr(), emult(), edivi(), epow(); -static double ebotch(); +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, evariable, enumber, @@ -79,8 +85,9 @@ static int linepos; /* position in buffer */ EPNODE * -eparse(expr) /* parse an expression string */ -char *expr; +eparse( /* parse an expression string */ + char *expr +) { EPNODE *ep; @@ -94,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; @@ -108,8 +116,10 @@ char *expr; int -epcmp(ep1, ep2) /* compare two expressions for equivalence */ -register EPNODE *ep1, *ep2; +epcmp( /* compare two expressions for equivalence */ + register EPNODE *ep1, + register EPNODE *ep2 +) { double d; @@ -125,7 +135,7 @@ register EPNODE *ep1, *ep2; if (ep2->v.num == 0) return(ep1->v.num != 0); d = ep1->v.num / ep2->v.num; - return(d > 1.000000000001 | d < 0.999999999999); + return((d > 1.000000000001) | (d < 0.999999999999)); case CHAN: case ARG: @@ -135,7 +145,7 @@ register EPNODE *ep1, *ep2; case ':': return(epcmp(ep1->v.kid->sibling, ep2->v.kid->sibling)); - case TICK: + case CLKT: case SYM: /* should never get this one */ return(0); @@ -156,8 +166,9 @@ register EPNODE *ep1, *ep2; void -epfree(epar) /* free a parse tree */ -register EPNODE *epar; +epfree( /* free a parse tree */ + register EPNODE *epar +) { register EPNODE *ep; @@ -174,7 +185,7 @@ register EPNODE *epar; case NUM: case CHAN: case ARG: - case TICK: + case CLKT: break; default: @@ -191,22 +202,25 @@ register EPNODE *epar; /* the following used to be a switch */ static double -eargument(ep) -EPNODE *ep; +eargument( + EPNODE *ep +) { return(argument(ep->v.chan)); } 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; @@ -214,15 +228,17 @@ EPNODE *ep; } static double -echannel(ep) -EPNODE *ep; +echannel( + EPNODE *ep +) { return(chanvalue(ep->v.chan)); } static double -eadd(ep) -EPNODE *ep; +eadd( + EPNODE *ep +) { register EPNODE *ep1 = ep->v.kid; @@ -230,8 +246,9 @@ EPNODE *ep; } static double -esubtr(ep) -EPNODE *ep; +esubtr( + EPNODE *ep +) { register EPNODE *ep1 = ep->v.kid; @@ -239,8 +256,9 @@ EPNODE *ep; } static double -emult(ep) -EPNODE *ep; +emult( + EPNODE *ep +) { register EPNODE *ep1 = ep->v.kid; @@ -248,8 +266,9 @@ EPNODE *ep; } static double -edivi(ep) -EPNODE *ep; +edivi( + EPNODE *ep +) { register EPNODE *ep1 = ep->v.kid; double d; @@ -264,8 +283,9 @@ EPNODE *ep; } static double -epow(ep) -EPNODE *ep; +epow( + EPNODE *ep +) { register EPNODE *ep1 = ep->v.kid; double d; @@ -274,9 +294,12 @@ EPNODE *ep; 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 == EDOM || errno == ERANGE) { wputs("Illegal power\n"); @@ -287,8 +310,9 @@ EPNODE *ep; } static double -ebotch(ep) -EPNODE *ep; +ebotch( + EPNODE *ep +) { eputs("Bad expression!\n"); quit(1); @@ -297,9 +321,10 @@ EPNODE *ep; 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) @@ -311,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; @@ -324,10 +350,11 @@ register EPNODE *ep; void -initfile(fp, fn, ln) /* prepare input file */ -FILE *fp; -char *fn; -int ln; +initfile( /* prepare input file */ + FILE *fp, + char *fn, + int ln +) { static char inpbuf[MAXLINE]; @@ -342,10 +369,11 @@ int ln; void -initstr(s, fn, ln) /* prepare input string */ -char *s; -char *fn; -int ln; +initstr( /* prepare input string */ + char *s, + char *fn, + int ln +) { infp = NULL; infile = fn; @@ -357,11 +385,12 @@ int ln; void -getscanpos(fnp, lnp, spp, fpp) /* return current scan position */ -char **fnp; -int *lnp; -char **spp; -FILE **fpp; +getscanpos( /* return current scan position */ + char **fnp, + int *lnp, + char **spp, + FILE **fpp +) { if (fnp != NULL) *fnp = infile; if (lnp != NULL) *lnp = lineno; @@ -371,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; @@ -388,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 != '}') @@ -403,8 +436,9 @@ scan() /* scan next character, return literal next char * -long2ascii(l) /* convert long to ascii */ -long l; +long2ascii( /* convert long to ascii */ + long l +) { static char buf[16]; register char *cp; @@ -429,8 +463,9 @@ long l; void -syntax(err) /* report syntax error and quit */ -char *err; +syntax( /* report syntax error and quit */ + char *err +) { register int i; @@ -455,9 +490,10 @@ char *err; void -addekid(ep, ekid) /* add a child to ep */ -register EPNODE *ep; -EPNODE *ekid; +addekid( /* add a child to ep */ + register EPNODE *ep, + EPNODE *ekid +) { if (ep->v.kid == NULL) ep->v.kid = ekid; @@ -471,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 */ @@ -488,7 +524,7 @@ getname() /* scan an identifier */ int -getinum() /* scan a positive integer */ +getinum(void) /* scan a positive integer */ { register int n, lnext; @@ -503,37 +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) { + if (lnext == '.' && i < RMAXWORD) { str[i++] = lnext; lnext = scan(); if (i == 1 && !isdigit(lnext)) syntax("badly formed number"); - while (isdigit(lnext) && i < MAXWORD) { + while (isdigit(lnext) && i < RMAXWORD) { str[i++] = lnext; lnext = scan(); } } - if ((lnext == 'e' | lnext == 'E') && i < MAXWORD) { + if ((lnext == 'e') | (lnext == 'E') && i < RMAXWORD) { str[i++] = lnext; lnext = scan(); - if ((lnext == '-' | lnext == '+') && i < MAXWORD) { + if ((lnext == '-') | (lnext == '+') && i < RMAXWORD) { str[i++] = lnext; lnext = scan(); } if (!isdigit(lnext)) syntax("missing exponent"); - while (isdigit(lnext) && i < MAXWORD) { + while (isdigit(lnext) && i < RMAXWORD) { str[i++] = lnext; lnext = scan(); } @@ -545,7 +581,7 @@ getnum() /* scan a positive float */ EPNODE * -getE1() /* E1 -> E1 ADDOP E2 */ +getE1(void) /* E1 -> E1 ADDOP E2 */ /* E2 */ { register EPNODE *ep1, *ep2; @@ -567,7 +603,7 @@ getE1() /* E1 -> E1 ADDOP E2 */ EPNODE * -getE2() /* E2 -> E2 MULOP E3 */ +getE2(void) /* E2 -> E2 MULOP E3 */ /* E3 */ { register EPNODE *ep1, *ep2; @@ -579,9 +615,23 @@ getE2() /* E2 -> E2 MULOP E3 */ scan(); addekid(ep2, ep1); addekid(ep2, getE3()); - if (esupport&E_RCONST && - ep1->type == NUM && ep1->sibling->type == NUM) - ep2 = rconst(ep2); + if (esupport&E_RCONST) { + EPNODE *ep3 = ep1->sibling; + if (ep1->type == NUM && ep3->type == NUM) { + ep2 = rconst(ep2); + } else if (ep3->type == NUM && ep3->v.num == 0) { + if (ep2->type == '/') + syntax("divide by zero constant"); + ep1->sibling = NULL; /* (E2 * 0) */ + epfree(ep2); + ep2 = ep3; + } else if (ep1->type == NUM && ep1->v.num == 0) { + epfree(ep3); /* (0 * E3) or (0 / E3) */ + ep1->sibling = NULL; + efree((char *)ep2); + ep2 = ep1; + } + } ep1 = ep2; } return(ep1); @@ -589,29 +639,42 @@ getE2() /* E2 -> E2 MULOP E3 */ EPNODE * -getE3() /* E3 -> E4 ^ E3 */ +getE3(void) /* E3 -> E4 ^ E3 */ /* E4 */ { - register EPNODE *ep1, *ep2; + register EPNODE *ep1, *ep2; - ep1 = getE4(); - if (nextc == '^') { + ep1 = getE4(); + if (nextc != '^') + return(ep1); ep2 = newnode(); ep2->type = nextc; scan(); addekid(ep2, ep1); addekid(ep2, getE3()); - if (esupport&E_RCONST && - ep1->type == NUM && ep1->sibling->type == NUM) - ep2 = rconst(ep2); + if (esupport&E_RCONST) { + EPNODE *ep3 = ep1->sibling; + if (ep1->type == NUM && ep3->type == NUM) { + ep2 = rconst(ep2); + } else if (ep1->type == NUM && ep1->v.num == 0) { + epfree(ep3); /* (0 ^ E3) */ + ep1->sibling = NULL; + efree((char *)ep2); + ep2 = ep1; + } else if ((ep3->type == NUM && ep3->v.num == 0) || + (ep1->type == NUM && ep1->v.num == 1)) { + epfree(ep2); /* (E4 ^ 0) or (1 ^ E3) */ + ep2 = newnode(); + ep2->type = NUM; + ep2->v.num = 1; + } + } return(ep2); - } - return(ep1); } EPNODE * -getE4() /* E4 -> ADDOP E5 */ +getE4(void) /* E4 -> ADDOP E5 */ /* E5 */ { register EPNODE *ep1, *ep2; @@ -624,8 +687,9 @@ getE4() /* E4 -> ADDOP E5 */ return(ep2); } if (ep2->type == UMINUS) { /* don't generate -(-E5) */ + ep1 = ep2->v.kid; efree((char *)ep2); - return(ep2->v.kid); + return(ep1); } ep1 = newnode(); ep1->type = UMINUS; @@ -639,7 +703,7 @@ getE4() /* E4 -> ADDOP E5 */ EPNODE * -getE5() /* E5 -> (E1) */ +getE5(void) /* E5 -> (E1) */ /* VAR */ /* NUM */ /* $N */ @@ -717,8 +781,9 @@ getE5() /* E5 -> (E1) */ EPNODE * -rconst(epar) /* reduce a constant expression */ -register EPNODE *epar; +rconst( /* reduce a constant expression */ + register EPNODE *epar +) { register EPNODE *ep; @@ -735,8 +800,9 @@ register EPNODE *epar; int -isconstvar(ep) /* is ep linked to a constant expression? */ -register EPNODE *ep; +isconstvar( /* is ep linked to a constant expression? */ + register EPNODE *ep +) { register EPNODE *ep1; @@ -760,19 +826,21 @@ register EPNODE *ep; int -isconstfun(ep) /* is ep linked to a constant function? */ -register EPNODE *ep; +isconstfun( /* is ep linked to a constant function? */ + register EPNODE *ep +) { register EPNODE *dp; register LIBR *lp; if (ep->type != VAR) return(0); - if ((dp = ep->v.ln->def) != NULL) + 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);