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.39 by greg, Sat Dec 28 18:05:13 2019 UTC vs.
Revision 2.47 by greg, Sun Feb 25 04:11:10 2024 UTC

# Line 33 | Line 33 | static const char      RCSid[] = "$Id$";
33  
34   #define  newnode()      (EPNODE *)ecalloc(1, sizeof(EPNODE))
35  
36 < #define  isdecimal(c)   (isdigit(c) || (c) == '.')
36 > #define  isdecimal(c)   (isdigit(c) | ((c) == '.'))
37  
38 + #define  envalue(ep)    ((ep)->type==NUM ? (ep)->v.num : evalue(ep))
39 +
40   static double  euminus(EPNODE *), eargument(EPNODE *), enumber(EPNODE *);
41   static double  echannel(EPNODE *);
42   static double  eadd(EPNODE *), esubtr(EPNODE *),
# Line 111 | Line 113 | eval(                  /* evaluate an expression string */
113      ep = eparse(expr);
114      esupport = prev_support;    /* as you were */
115      rval = evalue(ep);
116 <    epfree(ep);
116 >    epfree(ep,1);
117      return(rval);
118   }
119  
# Line 168 | Line 170 | epcmp(                 /* compare two expressions for equivalence */
170  
171   void
172   epfree(                 /* free a parse tree */
173 <    EPNODE       *epar
173 >    EPNODE       *epar,
174 >    int         frep
175   )
176   {
177 <    EPNODE  *ep;
177 >    EPNODE      *ep;
178  
179      switch (epar->type) {
180  
# Line 190 | Line 193 | epfree(                        /* free a parse tree */
193              break;
194  
195          default:
196 <            while ((ep = epar->v.kid) != NULL) {
197 <                epar->v.kid = ep->sibling;
198 <                epfree(ep);
199 <            }
196 >            if (epar->nkids < 0) {
197 >                ep = epar->v.kid - epar->nkids;
198 >                while (ep > epar->v.kid)
199 >                        epfree(--ep, 0);
200 >                efree(ep);      /* free array space */
201 >            } else
202 >                while ((ep = epar->v.kid) != NULL) {
203 >                    epar->v.kid = ep->sibling;
204 >                    epfree(ep, 1);
205 >                }
206              break;
207  
208      }
209 +    if (frep)
210 +        efree(epar);
211 + }
212  
213 <    efree((char *)epar);
213 >
214 > static void
215 > epflatten(                      /* flatten hierarchies for '+', '*' */
216 >        EPNODE *epar
217 > )
218 > {
219 >    EPNODE      *ep;
220 >
221 >    if (epar->nkids < 0) {
222 >        eputs("Cannot flatten EPNODE array\n");
223 >        quit(1);
224 >    }
225 >    for (ep = epar->v.kid; ep != NULL; ep = ep->sibling)
226 >        while (ep->type == epar->type) {
227 >            EPNODE      *ep1 = ep->v.kid;
228 >            while (ep1->sibling != NULL)
229 >                ep1 = ep1->sibling;
230 >            ep1->sibling = ep->sibling;
231 >            epar->nkids += nekids(ep) - 1;
232 >            ep1 = ep->v.kid;
233 >            *ep = *ep1;
234 >            efree(ep1);         /* not epfree()! */
235 >        }
236   }
237  
238 +
239 + void
240 + epoptimize(                     /* flatten operations, lists -> arrays */
241 +        EPNODE  *epar
242 + )
243 + {
244 +    EPNODE      *ep;
245 +
246 +    if ((epar->type == '+') | (epar->type == '*'))
247 +        epflatten(epar);        /* flatten associative operations */
248 +
249 +    if (epar->nkids)            /* do children if any */
250 +        for (ep = epar->v.kid; ep != NULL; ep = ep->sibling)
251 +            epoptimize(ep);
252 +
253 +    if (epar->nkids > 4) {      /* make list into array if > 4 kids */
254 +        int     n = 1;
255 +        epar->v.kid = (EPNODE *)erealloc(epar->v.kid,
256 +                                        sizeof(EPNODE)*epar->nkids);
257 +        while (n < epar->nkids) {
258 +            ep = epar->v.kid[n-1].sibling;
259 +            epar->v.kid[n] = *ep;
260 +            efree(ep);          /* not epfree()! */
261 +            epar->v.kid[n-1].sibling = epar->v.kid + n;
262 +            n++;
263 +        }
264 +        epar->nkids = -n;
265 +    }
266 + }
267 +
268                                  /* the following used to be a switch */
269   static double
270   eargument(
# Line 241 | Line 305 | eadd(
305      EPNODE      *ep
306   )
307   {
308 +    double  sum = 0;
309      EPNODE  *ep1 = ep->v.kid;
310  
311 <    return(evalue(ep1) + evalue(ep1->sibling));
311 >    do
312 >        sum += envalue(ep1);
313 >    while ((ep1 = ep1->sibling) != NULL);
314 >
315 >    return(sum);
316   }
317  
318   static double
# Line 252 | Line 321 | esubtr(
321   )
322   {
323      EPNODE  *ep1 = ep->v.kid;
324 +    EPNODE  *ep2 = ep1->sibling;
325  
326 <    return(evalue(ep1) - evalue(ep1->sibling));
326 >    return(envalue(ep1) - envalue(ep2));
327   }
328  
329   static double
# Line 261 | Line 331 | emult(
331      EPNODE      *ep
332   )
333   {
334 +    double  prod = 1;
335      EPNODE  *ep1 = ep->v.kid;
336  
337 <    return(evalue(ep1) * evalue(ep1->sibling));
337 >    do
338 >        prod *= envalue(ep1);
339 >    while ((ep1 = ep1->sibling) != NULL);
340 >
341 >    return(prod);
342   }
343  
344   static double
# Line 272 | Line 347 | edivi(
347   )
348   {
349      EPNODE  *ep1 = ep->v.kid;
350 +    EPNODE  *ep2 = ep1->sibling;
351      double  d;
352  
353 <    d = evalue(ep1->sibling);
353 >    d = evalue(ep2);
354      if (d == 0.0) {
355          wputs("Division by zero\n");
356          errno = ERANGE;
357          return(0.0);
358      }
359 <    return(evalue(ep1) / d);
359 >    return(envalue(ep1) / d);
360   }
361  
362   static double
# Line 303 | Line 379 | epow(
379              errno = ERANGE;
380      }
381   #endif
382 <    if (errno == EDOM || errno == ERANGE) {
382 >    if ((errno == EDOM) | (errno == ERANGE)) {
383          wputs("Illegal power\n");
384          return(0.0);
385      }
# Line 328 | Line 404 | ekid(                  /* return pointer to a node's nth kid */
404      int  n
405   )
406   {
407 <
408 <    for (ep = ep->v.kid; ep != NULL; ep = ep->sibling)
409 <        if (--n < 0)
410 <            break;
411 <
407 >    if (ep->nkids < 0) {        /* allocated array? */
408 >        if (n >= -ep->nkids)
409 >            return(NULL);
410 >        return(ep->v.kid + n);
411 >    }
412 >    ep = ep->v.kid;             /* else get from list */
413 >    while (n-- > 0)
414 >        if ((ep = ep->sibling) == NULL)
415 >                break;
416      return(ep);
417   }
418  
419  
340 int
341 nekids(                 /* return # of kids for node ep */
342    EPNODE       *ep
343 )
344 {
345    int  n = 0;
346
347    for (ep = ep->v.kid; ep != NULL; ep = ep->sibling)
348        n++;
349
350    return(n);
351 }
352
353
420   void
421   initfile(               /* prepare input file */
422      FILE  *fp,
# Line 471 | Line 537 | syntax(                        /* report syntax error and quit */
537   {
538      int  i;
539  
540 <    if (infile != NULL || lineno != 0) {
540 >    if ((infile != NULL) | (lineno != 0)) {
541          if (infile != NULL) eputs(infile);
542          if (lineno != 0) {
543              eputs(infile != NULL ? ", line " : "line ");
# Line 494 | Line 560 | syntax(                        /* report syntax error and quit */
560   void
561   addekid(                        /* add a child to ep */
562      EPNODE       *ep,
563 <    EPNODE      *ekid
563 >    EPNODE      *ek
564   )
565   {
566 +    if (ep->nkids < 0) {
567 +        eputs("Cannot add kid to EPNODE array\n");
568 +        quit(1);
569 +    }
570 +    ep->nkids++;
571      if (ep->v.kid == NULL)
572 <        ep->v.kid = ekid;
572 >        ep->v.kid = ek;
573      else {
574          for (ep = ep->v.kid; ep->sibling != NULL; ep = ep->sibling)
575              ;
576 <        ep->sibling = ekid;
576 >        ep->sibling = ek;
577      }
578 <    ekid->sibling = NULL;
578 >    ek->sibling = NULL;         /* shouldn't be necessary */
579   }
580  
581  
# Line 552 | Line 623 | getnum(void)                   /* scan a positive float */
623          str[i++] = lnext;
624          lnext = scan();
625      }
626 <    if (lnext == '.' && i < RMAXWORD) {
626 >    if ((lnext == '.') & (i < RMAXWORD)) {
627          str[i++] = lnext;
628          lnext = scan();
629          if (i == 1 && !isdigit(lnext))
# Line 589 | Line 660 | getE1(void)                    /* E1 -> E1 ADDOP E2 */
660      EPNODE  *ep1, *ep2;
661  
662      ep1 = getE2();
663 <    while (nextc == '+' || nextc == '-') {
663 >    while ((nextc == '+') | (nextc == '-')) {
664          ep2 = newnode();
665          ep2->type = nextc;
666          scan();
667          addekid(ep2, ep1);
668          addekid(ep2, getE2());
669          if (esupport&E_RCONST &&
670 <                        ep1->type == NUM && ep1->sibling->type == NUM)
670 >                        (ep1->type == NUM) & (ep1->sibling->type == NUM))
671                  ep2 = rconst(ep2);
672          ep1 = ep2;
673      }
# Line 611 | Line 682 | getE2(void)                    /* E2 -> E2 MULOP E3 */
682      EPNODE  *ep1, *ep2;
683  
684      ep1 = getE3();
685 <    while (nextc == '*' || nextc == '/') {
685 >    while ((nextc == '*') | (nextc == '/')) {
686          ep2 = newnode();
687          ep2->type = nextc;
688          scan();
# Line 619 | Line 690 | getE2(void)                    /* E2 -> E2 MULOP E3 */
690          addekid(ep2, getE3());
691          if (esupport&E_RCONST) {
692                  EPNODE  *ep3 = ep1->sibling;
693 <                if (ep1->type == NUM && ep3->type == NUM) {
693 >                if ((ep1->type == NUM) & (ep3->type == NUM)) {
694                          ep2 = rconst(ep2);
695                  } else if (ep3->type == NUM) {
696                          if (ep2->type == '/') {
# Line 629 | Line 700 | getE2(void)                    /* E2 -> E2 MULOP E3 */
700                                  ep3->v.num = 1./ep3->v.num;
701                          } else if (ep3->v.num == 0) {
702                                  ep1->sibling = NULL;    /* (E2 * 0) */
703 <                                epfree(ep2);
703 >                                epfree(ep2,1);
704                                  ep2 = ep3;
705                          }
706                  } else if (ep1->type == NUM && ep1->v.num == 0) {
707 <                        epfree(ep3);            /* (0 * E3) or (0 / E3) */
707 >                        epfree(ep3,1);          /* (0 * E3) or (0 / E3) */
708                          ep1->sibling = NULL;
709 <                        efree((char *)ep2);
709 >                        efree(ep2);
710                          ep2 = ep1;
711                  }
712          }
# Line 661 | Line 732 | getE3(void)                    /* E3 -> E4 ^ E3 */
732          addekid(ep2, getE3());
733          if (esupport&E_RCONST) {
734                  EPNODE  *ep3 = ep1->sibling;
735 <                if (ep1->type == NUM && ep3->type == NUM) {
735 >                if ((ep1->type == NUM) & (ep3->type == NUM)) {
736                          ep2 = rconst(ep2);
737                  } else if (ep1->type == NUM && ep1->v.num == 0) {
738 <                        epfree(ep3);            /* (0 ^ E3) */
738 >                        epfree(ep3,1);          /* (0 ^ E3) */
739                          ep1->sibling = NULL;
740 <                        efree((char *)ep2);
740 >                        efree(ep2);
741                          ep2 = ep1;
742 <                } else if ((ep3->type == NUM && ep3->v.num == 0) ||
742 >                } else if ((ep3->type == NUM && ep3->v.num == 0) |
743                                  (ep1->type == NUM && ep1->v.num == 1)) {
744 <                        epfree(ep2);            /* (E4 ^ 0) or (1 ^ E3) */
744 >                        epfree(ep2,1);          /* (E4 ^ 0) or (1 ^ E3) */
745                          ep2 = newnode();
746                          ep2->type = NUM;
747                          ep2->v.num = 1;
748 +                } else if (ep3->type == NUM && ep3->v.num == 1) {
749 +                        efree(ep3);     /* (E4 ^ 1) */
750 +                        ep1->sibling = NULL;
751 +                        efree(ep2);
752 +                        ep2 = ep1;
753                  }
754          }
755          return(ep2);
# Line 695 | Line 771 | getE4(void)                    /* E4 -> ADDOP E5 */
771          }
772          if (ep2->type == UMINUS) {      /* don't generate -(-E5) */
773              ep1 = ep2->v.kid;
774 <            efree((char *)ep2);
774 >            efree(ep2);
775              return(ep1);
776          }
777          ep1 = newnode();
# Line 729 | Line 805 | getE5(void)                    /* E5 -> (E1) */
805                  scan();
806                  return(ep1);
807          }
732
808          if (esupport&E_INCHAN && nextc == '$') {
809                  scan();
810                  ep1 = newnode();
# Line 737 | Line 812 | getE5(void)                    /* E5 -> (E1) */
812                  ep1->v.chan = getinum();
813                  return(ep1);
814          }
740
815          if (esupport&(E_VARIABLE|E_FUNCTION) &&
816 <                        (isalpha(nextc) || nextc == CNTXMARK)) {
816 >                        (isalpha(nextc) | (nextc == CNTXMARK))) {
817                  nam = getname();
818                  ep1 = NULL;
819                  if ((esupport&(E_VARIABLE|E_FUNCTION)) == (E_VARIABLE|E_FUNCTION)
# Line 775 | Line 849 | getE5(void)                    /* E5 -> (E1) */
849                          ep1 = rconst(ep1);
850                  return(ep1);
851          }
778
852          if (isdecimal(nextc)) {
853                  ep1 = newnode();
854                  ep1->type = NUM;
# Line 798 | Line 871 | rconst(                        /* reduce a constant expression */
871      ep->type = NUM;
872      errno = 0;
873      ep->v.num = evalue(epar);
874 <    if (errno == EDOM || errno == ERANGE)
874 >    if ((errno == EDOM) | (errno == ERANGE))
875          syntax("bad constant expression");
876 <    epfree(epar);
876 >    epfree(epar,1);
877  
878      return(ep);
879   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines