ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/calexpr.c
(Generate patch)

Comparing ray/src/common/calexpr.c (file contents):
Revision 2.26 by schorsch, Sun Jul 27 22:12:01 2003 UTC vs.
Revision 2.38 by greg, Sat Dec 7 16:38:08 2019 UTC

# Line 26 | Line 26 | static const char      RCSid[] = "$Id$";
26   #include  <math.h>
27   #include  <stdlib.h>
28  
29 + #include  "rtmisc.h"
30 + #include  "rtio.h"
31   #include  "rterror.h"
32   #include  "calcomp.h"
33  
# Line 45 | Line 47 | static double  ebotch(EPNODE *);
47   unsigned int  esupport =                /* what to support */
48                  E_VARIABLE | E_FUNCTION ;
49  
50 + int  eofc = 0;                          /* optional end-of-file character */
51   int  nextc;                             /* lookahead character */
52  
53 < double  (*eoper[])() = {                /* expression operations */
53 > double  (*eoper[])(EPNODE *) = {        /* expression operations */
54          ebotch,
55          evariable,
56          enumber,
# Line 82 | Line 85 | static int  linepos;                   /* position in buffer */
85  
86  
87   EPNODE *
88 < eparse(expr)                    /* parse an expression string */
89 < char  *expr;
88 > eparse(                 /* parse an expression string */
89 >    char  *expr
90 > )
91   {
92      EPNODE  *ep;
93  
# Line 97 | Line 101 | char  *expr;
101  
102  
103   double
104 < eval(expr)                      /* evaluate an expression string */
105 < char  *expr;
104 > eval(                   /* evaluate an expression string */
105 >    char  *expr
106 > )
107   {
108 <    register EPNODE  *ep;
108 >    int  prev_support = esupport;
109 >    EPNODE  *ep;
110      double  rval;
111  
112 +    esupport &= ~E_RCONST;      /* don't bother reducing constant expr */
113      ep = eparse(expr);
114 +    esupport = prev_support;    /* as you were */
115      rval = evalue(ep);
116      epfree(ep);
117      return(rval);
# Line 111 | Line 119 | char  *expr;
119  
120  
121   int
122 < epcmp(ep1, ep2)                 /* compare two expressions for equivalence */
123 < register EPNODE  *ep1, *ep2;
122 > epcmp(                  /* compare two expressions for equivalence */
123 >    EPNODE  *ep1,
124 >    EPNODE  *ep2
125 > )
126   {
127          double  d;
128  
# Line 138 | Line 148 | register EPNODE  *ep1, *ep2;
148          case ':':
149                  return(epcmp(ep1->v.kid->sibling, ep2->v.kid->sibling));
150  
151 <        case TICK:
151 >        case CLKT:
152          case SYM:                       /* should never get this one */
153                  return(0);
154  
# Line 159 | Line 169 | register EPNODE  *ep1, *ep2;
169  
170  
171   void
172 < epfree(epar)                    /* free a parse tree */
173 < register EPNODE  *epar;
172 > epfree(                 /* free a parse tree */
173 >    EPNODE       *epar
174 > )
175   {
176 <    register EPNODE  *ep;
176 >    EPNODE  *ep;
177  
178      switch (epar->type) {
179  
# Line 177 | Line 188 | register EPNODE         *epar;
188          case NUM:
189          case CHAN:
190          case ARG:
191 <        case TICK:
191 >        case CLKT:
192              break;
193  
194          default:
# Line 194 | Line 205 | register EPNODE         *epar;
205  
206                                  /* the following used to be a switch */
207   static double
208 < eargument(ep)
209 < EPNODE  *ep;
208 > eargument(
209 >    EPNODE      *ep
210 > )
211   {
212      return(argument(ep->v.chan));
213   }
214  
215   static double
216 < enumber(ep)
217 < EPNODE  *ep;
216 > enumber(
217 >    EPNODE      *ep
218 > )
219   {
220      return(ep->v.num);
221   }
222  
223   static double
224 < euminus(ep)
225 < EPNODE  *ep;
224 > euminus(
225 >    EPNODE      *ep
226 > )
227   {
228 <    register EPNODE  *ep1 = ep->v.kid;
228 >    EPNODE  *ep1 = ep->v.kid;
229  
230      return(-evalue(ep1));
231   }
232  
233   static double
234 < echannel(ep)
235 < EPNODE  *ep;
234 > echannel(
235 >    EPNODE      *ep
236 > )
237   {
238      return(chanvalue(ep->v.chan));
239   }
240  
241   static double
242 < eadd(ep)
243 < EPNODE  *ep;
242 > eadd(
243 >    EPNODE      *ep
244 > )
245   {
246 <    register EPNODE  *ep1 = ep->v.kid;
246 >    EPNODE  *ep1 = ep->v.kid;
247  
248      return(evalue(ep1) + evalue(ep1->sibling));
249   }
250  
251   static double
252 < esubtr(ep)
253 < EPNODE  *ep;
252 > esubtr(
253 >    EPNODE      *ep
254 > )
255   {
256 <    register EPNODE  *ep1 = ep->v.kid;
256 >    EPNODE  *ep1 = ep->v.kid;
257  
258      return(evalue(ep1) - evalue(ep1->sibling));
259   }
260  
261   static double
262 < emult(ep)
263 < EPNODE  *ep;
262 > emult(
263 >    EPNODE      *ep
264 > )
265   {
266 <    register EPNODE  *ep1 = ep->v.kid;
266 >    EPNODE  *ep1 = ep->v.kid;
267  
268      return(evalue(ep1) * evalue(ep1->sibling));
269   }
270  
271   static double
272 < edivi(ep)
273 < EPNODE  *ep;
272 > edivi(
273 >    EPNODE      *ep
274 > )
275   {
276 <    register EPNODE  *ep1 = ep->v.kid;
276 >    EPNODE  *ep1 = ep->v.kid;
277      double  d;
278  
279      d = evalue(ep1->sibling);
# Line 267 | Line 286 | EPNODE *ep;
286   }
287  
288   static double
289 < epow(ep)
290 < EPNODE  *ep;
289 > epow(
290 >    EPNODE      *ep
291 > )
292   {
293 <    register EPNODE  *ep1 = ep->v.kid;
293 >    EPNODE  *ep1 = ep->v.kid;
294      double  d;
295      int  lasterrno;
296  
297      lasterrno = errno;
298      errno = 0;
299      d = pow(evalue(ep1), evalue(ep1->sibling));
300 < #ifdef  IEEE
301 <    if (!finite(d))
302 <        errno = EDOM;
300 > #ifdef  isnan
301 >    if (errno == 0) {
302 >        if (isnan(d))
303 >            errno = EDOM;
304 >        else if (isinf(d))
305 >            errno = ERANGE;
306 >    }
307   #endif
308      if (errno == EDOM || errno == ERANGE) {
309          wputs("Illegal power\n");
# Line 290 | Line 314 | EPNODE *ep;
314   }
315  
316   static double
317 < ebotch(ep)
318 < EPNODE  *ep;
317 > ebotch(
318 >    EPNODE      *ep
319 > )
320   {
321      eputs("Bad expression!\n");
322      quit(1);
# Line 300 | Line 325 | EPNODE *ep;
325  
326  
327   EPNODE *
328 < ekid(ep, n)                     /* return pointer to a node's nth kid */
329 < register EPNODE  *ep;
330 < register int  n;
328 > ekid(                   /* return pointer to a node's nth kid */
329 >    EPNODE       *ep,
330 >    int  n
331 > )
332   {
333  
334      for (ep = ep->v.kid; ep != NULL; ep = ep->sibling)
# Line 314 | Line 340 | register int  n;
340  
341  
342   int
343 < nekids(ep)                      /* return # of kids for node ep */
344 < register EPNODE  *ep;
343 > nekids(                 /* return # of kids for node ep */
344 >    EPNODE       *ep
345 > )
346   {
347 <    register int  n = 0;
347 >    int  n = 0;
348  
349      for (ep = ep->v.kid; ep != NULL; ep = ep->sibling)
350          n++;
# Line 327 | Line 354 | register EPNODE         *ep;
354  
355  
356   void
357 < initfile(fp, fn, ln)            /* prepare input file */
358 < FILE  *fp;
359 < char  *fn;
360 < int  ln;
357 > initfile(               /* prepare input file */
358 >    FILE  *fp,
359 >    char  *fn,
360 >    int  ln
361 > )
362   {
363      static char  inpbuf[MAXLINE];
364  
# Line 345 | Line 373 | int  ln;
373  
374  
375   void
376 < initstr(s, fn, ln)              /* prepare input string */
377 < char  *s;
378 < char  *fn;
379 < int  ln;
376 > initstr(                /* prepare input string */
377 >    char  *s,
378 >    char  *fn,
379 >    int  ln
380 > )
381   {
382      infp = NULL;
383      infile = fn;
# Line 360 | Line 389 | int  ln;
389  
390  
391   void
392 < getscanpos(fnp, lnp, spp, fpp)  /* return current scan position */
393 < char  **fnp;
394 < int  *lnp;
395 < char  **spp;
396 < FILE  **fpp;
392 > getscanpos(     /* return current scan position */
393 >    char  **fnp,
394 >    int  *lnp,
395 >    char  **spp,
396 >    FILE  **fpp
397 > )
398   {
399      if (fnp != NULL) *fnp = infile;
400      if (lnp != NULL) *lnp = lineno;
# Line 374 | Line 404 | FILE  **fpp;
404  
405  
406   int
407 < scan()                          /* scan next character, return literal next */
407 > scan(void)              /* scan next character, return literal next */
408   {
409 <    register int  lnext = 0;
409 >    int  lnext = 0;
410  
411      do {
412          if (linbuf[linepos] == '\0')
# Line 391 | Line 421 | scan()                         /* scan next character, return literal next
421              nextc = linbuf[linepos++];
422          if (!lnext)
423                  lnext = nextc;
424 +        if (nextc == eofc) {
425 +                nextc = EOF;
426 +                break;
427 +        }
428          if (nextc == '{') {
429              scan();
430              while (nextc != '}')
# Line 406 | Line 440 | scan()                         /* scan next character, return literal next
440  
441  
442   char *
443 < long2ascii(l)                         /* convert long to ascii */
444 < long  l;
443 > long2ascii(                           /* convert long to ascii */
444 >    long  l
445 > )
446   {
447      static char  buf[16];
448 <    register char  *cp;
448 >    char  *cp;
449      int  neg = 0;
450  
451      if (l == 0)
# Line 432 | Line 467 | long  l;
467  
468  
469   void
470 < syntax(err)                     /* report syntax error and quit */
471 < char  *err;
470 > syntax(                 /* report syntax error and quit */
471 >    char  *err
472 > )
473   {
474 <    register int  i;
474 >    int  i;
475  
476      if (infile != NULL || lineno != 0) {
477          if (infile != NULL) eputs(infile);
# Line 458 | Line 494 | char  *err;
494  
495  
496   void
497 < addekid(ep, ekid)                       /* add a child to ep */
498 < register EPNODE  *ep;
499 < EPNODE  *ekid;
497 > addekid(                        /* add a child to ep */
498 >    EPNODE       *ep,
499 >    EPNODE      *ekid
500 > )
501   {
502      if (ep->v.kid == NULL)
503          ep->v.kid = ekid;
# Line 474 | Line 511 | EPNODE *ekid;
511  
512  
513   char *
514 < getname()                       /* scan an identifier */
514 > getname(void)                   /* scan an identifier */
515   {
516      static char  str[RMAXWORD+1];
517 <    register int  i, lnext;
517 >    int  i, lnext;
518  
519      lnext = nextc;
520      for (i = 0; i < RMAXWORD && isid(lnext); i++, lnext = scan())
# Line 491 | Line 528 | getname()                      /* scan an identifier */
528  
529  
530   int
531 < getinum()                       /* scan a positive integer */
531 > getinum(void)                   /* scan a positive integer */
532   {
533 <    register int  n, lnext;
533 >    int  n, lnext;
534  
535      n = 0;
536      lnext = nextc;
# Line 506 | Line 543 | getinum()                      /* scan a positive integer */
543  
544  
545   double
546 < getnum()                        /* scan a positive float */
546 > getnum(void)                    /* scan a positive float */
547   {
548 <    register int  i, lnext;
548 >    int  i, lnext;
549      char  str[RMAXWORD+1];
550  
551      i = 0;
# Line 548 | Line 585 | getnum()                       /* scan a positive float */
585  
586  
587   EPNODE *
588 < getE1()                         /* E1 -> E1 ADDOP E2 */
588 > getE1(void)                     /* E1 -> E1 ADDOP E2 */
589                                  /*       E2 */
590   {
591 <    register EPNODE  *ep1, *ep2;
591 >    EPNODE  *ep1, *ep2;
592  
593      ep1 = getE2();
594      while (nextc == '+' || nextc == '-') {
# Line 570 | Line 607 | getE1()                                /* E1 -> E1 ADDOP E2 */
607  
608  
609   EPNODE *
610 < getE2()                         /* E2 -> E2 MULOP E3 */
610 > getE2(void)                     /* E2 -> E2 MULOP E3 */
611                                  /*       E3 */
612   {
613 <    register EPNODE  *ep1, *ep2;
613 >    EPNODE  *ep1, *ep2;
614  
615      ep1 = getE3();
616      while (nextc == '*' || nextc == '/') {
# Line 582 | Line 619 | getE2()                                /* E2 -> E2 MULOP E3 */
619          scan();
620          addekid(ep2, ep1);
621          addekid(ep2, getE3());
622 <        if (esupport&E_RCONST &&
623 <                        ep1->type == NUM && ep1->sibling->type == NUM)
624 <                ep2 = rconst(ep2);
622 >        if (esupport&E_RCONST) {
623 >                EPNODE  *ep3 = ep1->sibling;
624 >                if (ep1->type == NUM && ep3->type == NUM) {
625 >                        ep2 = rconst(ep2);
626 >                } else if (ep3->type == NUM) {
627 >                        if (ep2->type == '/') {
628 >                                if (ep3->v.num == 0)
629 >                                        syntax("divide by zero constant");
630 >                                ep2->type = '*';        /* for speed */
631 >                                ep3->v.num = 1./ep3->v.num;
632 >                        } else if (ep3->v.num == 0) {
633 >                                ep1->sibling = NULL;    /* (E2 * 0) */
634 >                                epfree(ep2);
635 >                                ep2 = ep3;
636 >                        }
637 >                } else if (ep1->type == NUM && ep1->v.num == 0) {
638 >                        epfree(ep3);            /* (0 * E3) or (0 / E3) */
639 >                        ep1->sibling = NULL;
640 >                        efree((char *)ep2);
641 >                        ep2 = ep1;
642 >                }
643 >        }
644          ep1 = ep2;
645      }
646      return(ep1);
# Line 592 | Line 648 | getE2()                                /* E2 -> E2 MULOP E3 */
648  
649  
650   EPNODE *
651 < getE3()                         /* E3 -> E4 ^ E3 */
651 > getE3(void)                     /* E3 -> E4 ^ E3 */
652                                  /*       E4 */
653   {
654 <    register EPNODE  *ep1, *ep2;
654 >        EPNODE  *ep1, *ep2;
655  
656 <    ep1 = getE4();
657 <    if (nextc == '^') {
656 >        ep1 = getE4();
657 >        if (nextc != '^')
658 >                return(ep1);
659          ep2 = newnode();
660          ep2->type = nextc;
661          scan();
662          addekid(ep2, ep1);
663          addekid(ep2, getE3());
664 <        if (esupport&E_RCONST &&
665 <                        ep1->type == NUM && ep1->sibling->type == NUM)
666 <                ep2 = rconst(ep2);
664 >        if (esupport&E_RCONST) {
665 >                EPNODE  *ep3 = ep1->sibling;
666 >                if (ep1->type == NUM && ep3->type == NUM) {
667 >                        ep2 = rconst(ep2);
668 >                } else if (ep1->type == NUM && ep1->v.num == 0) {
669 >                        epfree(ep3);            /* (0 ^ E3) */
670 >                        ep1->sibling = NULL;
671 >                        efree((char *)ep2);
672 >                        ep2 = ep1;
673 >                } else if ((ep3->type == NUM && ep3->v.num == 0) ||
674 >                                (ep1->type == NUM && ep1->v.num == 1)) {
675 >                        epfree(ep2);            /* (E4 ^ 0) or (1 ^ E3) */
676 >                        ep2 = newnode();
677 >                        ep2->type = NUM;
678 >                        ep2->v.num = 1;
679 >                }
680 >        }
681          return(ep2);
611    }
612    return(ep1);
682   }
683  
684  
685   EPNODE *
686 < getE4()                         /* E4 -> ADDOP E5 */
686 > getE4(void)                     /* E4 -> ADDOP E5 */
687                                  /*       E5 */
688   {
689 <    register EPNODE  *ep1, *ep2;
689 >    EPNODE  *ep1, *ep2;
690  
691      if (nextc == '-') {
692          scan();
# Line 627 | Line 696 | getE4()                                /* E4 -> ADDOP E5 */
696                  return(ep2);
697          }
698          if (ep2->type == UMINUS) {      /* don't generate -(-E5) */
699 +            ep1 = ep2->v.kid;
700              efree((char *)ep2);
701 <            return(ep2->v.kid);
701 >            return(ep1);
702          }
703          ep1 = newnode();
704          ep1->type = UMINUS;
# Line 642 | Line 712 | getE4()                                /* E4 -> ADDOP E5 */
712  
713  
714   EPNODE *
715 < getE5()                         /* E5 -> (E1) */
715 > getE5(void)                     /* E5 -> (E1) */
716                                  /*       VAR */
717                                  /*       NUM */
718                                  /*       $N */
# Line 651 | Line 721 | getE5()                                /* E5 -> (E1) */
721   {
722          int      i;
723          char  *nam;
724 <        register EPNODE  *ep1, *ep2;
724 >        EPNODE  *ep1, *ep2;
725  
726          if (nextc == '(') {
727                  scan();
# Line 720 | Line 790 | getE5()                                /* E5 -> (E1) */
790  
791  
792   EPNODE *
793 < rconst(epar)                    /* reduce a constant expression */
794 < register EPNODE  *epar;
793 > rconst(                 /* reduce a constant expression */
794 >    EPNODE       *epar
795 > )
796   {
797 <    register EPNODE  *ep;
797 >    EPNODE  *ep;
798  
799      ep = newnode();
800      ep->type = NUM;
# Line 738 | Line 809 | register EPNODE         *epar;
809  
810  
811   int
812 < isconstvar(ep)                  /* is ep linked to a constant expression? */
813 < register EPNODE  *ep;
812 > isconstvar(                     /* is ep linked to a constant expression? */
813 >    EPNODE       *ep
814 > )
815   {
816 <    register EPNODE  *ep1;
816 >    EPNODE  *ep1;
817  
818      if (esupport&E_FUNCTION && ep->type == FUNC) {
819          if (!isconstfun(ep->v.kid))
# Line 763 | Line 835 | register EPNODE         *ep;
835  
836  
837   int
838 < isconstfun(ep)                  /* is ep linked to a constant function? */
839 < register EPNODE  *ep;
838 > isconstfun(                     /* is ep linked to a constant function? */
839 >    EPNODE       *ep
840 > )
841   {
842 <    register EPNODE  *dp;
843 <    register LIBR  *lp;
842 >    EPNODE  *dp;
843 >    LIBR  *lp;
844  
845      if (ep->type != VAR)
846          return(0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines