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.38 by greg, Sat Dec 7 16:38:08 2019 UTC vs.
Revision 2.50 by greg, Tue Feb 27 01:24:10 2024 UTC

# Line 19 | Line 19 | static const char      RCSid[] = "$Id$";
19  
20   #include "copyright.h"
21  
22 #include  <stdio.h>
23 #include  <string.h>
22   #include  <ctype.h>
23   #include  <errno.h>
24   #include  <math.h>
# Line 35 | 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 < static double  euminus(EPNODE *), eargument(EPNODE *), enumber(EPNODE *);
38 > #define  envalue(ep)    ((ep)->type==NUM ? (ep)->v.num : evalue(ep))
39 >
40 > static double  euminus(EPNODE *), enumber(EPNODE *);
41   static double  echannel(EPNODE *);
42   static double  eadd(EPNODE *), esubtr(EPNODE *),
43                 emult(EPNODE *), edivi(EPNODE *),
# Line 113 | 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 170 | 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 192 | 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 +    else
212 +        memset(epar, 0, sizeof(EPNODE));
213 + }
214  
215 <    efree((char *)epar);
215 >
216 > static void
217 > epflatten(                      /* flatten hierarchies for '+', '*' */
218 >        EPNODE *epar
219 > )
220 > {
221 >    EPNODE      *ep;
222 >
223 >    if (epar->nkids < 0)        /* can't handle array allocations */
224 >        return;
225 >
226 >    for (ep = epar->v.kid; ep != NULL; ep = ep->sibling)
227 >        while (ep->type == epar->type && ep->nkids > 0) {
228 >            EPNODE      *ep1 = ep->v.kid;
229 >            while (ep1->sibling != NULL)
230 >                ep1 = ep1->sibling;
231 >            ep1->sibling = ep->sibling;
232 >            epar->nkids += ep->nkids - 1;
233 >            ep1 = ep->v.kid;
234 >            *ep = *ep1;
235 >            efree(ep1);         /* not epfree()! */
236 >        }
237   }
238  
239 <                                /* the following used to be a switch */
240 < static double
241 < eargument(
242 <    EPNODE      *ep
239 >
240 > void
241 > epoptimize(                     /* flatten operations, lists -> arrays */
242 >        EPNODE  *epar
243   )
244   {
245 <    return(argument(ep->v.chan));
245 >    EPNODE      *ep;
246 >
247 >    if ((epar->type == '+') | (epar->type == '*'))
248 >        epflatten(epar);        /* flatten associative operations */
249 >
250 >    if (epar->nkids)            /* do children if any */
251 >        for (ep = epar->v.kid; ep != NULL; ep = ep->sibling)
252 >            epoptimize(ep);
253 >
254 >    if (epar->nkids > 4) {      /* make list into array if > 4 kids */
255 >        int     n = 1;
256 >        epar->v.kid = (EPNODE *)erealloc(epar->v.kid,
257 >                                        sizeof(EPNODE)*epar->nkids);
258 >        while (n < epar->nkids) {
259 >            ep = epar->v.kid[n-1].sibling;
260 >            epar->v.kid[n] = *ep;
261 >            efree(ep);          /* not epfree()! */
262 >            epar->v.kid[n-1].sibling = epar->v.kid + n;
263 >            n++;
264 >        }
265 >        epar->nkids = -n;
266 >    }
267   }
268  
269 +                                /* the following used to be a switch */
270   static double
271   enumber(
272      EPNODE      *ep
# Line 243 | Line 298 | eadd(
298      EPNODE      *ep
299   )
300   {
301 +    double  sum = 0;
302      EPNODE  *ep1 = ep->v.kid;
303  
304 <    return(evalue(ep1) + evalue(ep1->sibling));
304 >    do
305 >        sum += envalue(ep1);
306 >    while ((ep1 = ep1->sibling) != NULL);
307 >
308 >    return(sum);
309   }
310  
311   static double
# Line 254 | Line 314 | esubtr(
314   )
315   {
316      EPNODE  *ep1 = ep->v.kid;
317 +    EPNODE  *ep2 = ep1->sibling;
318  
319 <    return(evalue(ep1) - evalue(ep1->sibling));
319 >    return(envalue(ep1) - envalue(ep2));
320   }
321  
322   static double
# Line 263 | Line 324 | emult(
324      EPNODE      *ep
325   )
326   {
327 +    double  prod = 1;
328      EPNODE  *ep1 = ep->v.kid;
329  
330 <    return(evalue(ep1) * evalue(ep1->sibling));
330 >    do
331 >        prod *= envalue(ep1);
332 >    while ((ep1 = ep1->sibling) != NULL);
333 >
334 >    return(prod);
335   }
336  
337   static double
# Line 274 | Line 340 | edivi(
340   )
341   {
342      EPNODE  *ep1 = ep->v.kid;
343 <    double  d;
343 >    double  den = evalue(ep1->sibling);
344  
345 <    d = evalue(ep1->sibling);
280 <    if (d == 0.0) {
345 >    if (den == 0.0) {
346          wputs("Division by zero\n");
347          errno = ERANGE;
348          return(0.0);
349      }
350 <    return(evalue(ep1) / d);
350 >    return(envalue(ep1) / den);
351   }
352  
353   static double
# Line 305 | Line 370 | epow(
370              errno = ERANGE;
371      }
372   #endif
373 <    if (errno == EDOM || errno == ERANGE) {
373 >    if ((errno == EDOM) | (errno == ERANGE)) {
374          wputs("Illegal power\n");
375          return(0.0);
376      }
# Line 330 | Line 395 | ekid(                  /* return pointer to a node's nth kid */
395      int  n
396   )
397   {
398 <
399 <    for (ep = ep->v.kid; ep != NULL; ep = ep->sibling)
400 <        if (--n < 0)
401 <            break;
402 <
398 >    if (ep->nkids < 0) {        /* allocated array? */
399 >        if (n >= -ep->nkids)
400 >            return(NULL);
401 >        return(ep->v.kid + n);
402 >    }
403 >    ep = ep->v.kid;             /* else get from list */
404 >    while (n-- > 0)
405 >        if ((ep = ep->sibling) == NULL)
406 >                break;
407      return(ep);
408   }
409  
410  
342 int
343 nekids(                 /* return # of kids for node ep */
344    EPNODE       *ep
345 )
346 {
347    int  n = 0;
348
349    for (ep = ep->v.kid; ep != NULL; ep = ep->sibling)
350        n++;
351
352    return(n);
353 }
354
355
411   void
412   initfile(               /* prepare input file */
413      FILE  *fp,
# Line 473 | Line 528 | syntax(                        /* report syntax error and quit */
528   {
529      int  i;
530  
531 <    if (infile != NULL || lineno != 0) {
531 >    if ((infile != NULL) | (lineno != 0)) {
532          if (infile != NULL) eputs(infile);
533          if (lineno != 0) {
534              eputs(infile != NULL ? ", line " : "line ");
# Line 496 | Line 551 | syntax(                        /* report syntax error and quit */
551   void
552   addekid(                        /* add a child to ep */
553      EPNODE       *ep,
554 <    EPNODE      *ekid
554 >    EPNODE      *ek
555   )
556   {
557 +    if (ep->nkids < 0) {
558 +        eputs("Cannot add kid to EPNODE array\n");
559 +        quit(1);
560 +    }
561 +    ep->nkids++;
562      if (ep->v.kid == NULL)
563 <        ep->v.kid = ekid;
563 >        ep->v.kid = ek;
564      else {
565          for (ep = ep->v.kid; ep->sibling != NULL; ep = ep->sibling)
566              ;
567 <        ep->sibling = ekid;
567 >        ep->sibling = ek;
568      }
569 <    ekid->sibling = NULL;
569 >    ek->sibling = NULL;         /* shouldn't be necessary */
570   }
571  
572  
# Line 554 | Line 614 | getnum(void)                   /* scan a positive float */
614          str[i++] = lnext;
615          lnext = scan();
616      }
617 <    if (lnext == '.' && i < RMAXWORD) {
617 >    if ((lnext == '.') & (i < RMAXWORD)) {
618          str[i++] = lnext;
619          lnext = scan();
620          if (i == 1 && !isdigit(lnext))
# Line 591 | Line 651 | getE1(void)                    /* E1 -> E1 ADDOP E2 */
651      EPNODE  *ep1, *ep2;
652  
653      ep1 = getE2();
654 <    while (nextc == '+' || nextc == '-') {
654 >    while ((nextc == '+') | (nextc == '-')) {
655          ep2 = newnode();
656          ep2->type = nextc;
657          scan();
658          addekid(ep2, ep1);
659          addekid(ep2, getE2());
660          if (esupport&E_RCONST &&
661 <                        ep1->type == NUM && ep1->sibling->type == NUM)
661 >                        (ep1->type == NUM) & (ep1->sibling->type == NUM))
662                  ep2 = rconst(ep2);
663          ep1 = ep2;
664      }
# Line 613 | Line 673 | getE2(void)                    /* E2 -> E2 MULOP E3 */
673      EPNODE  *ep1, *ep2;
674  
675      ep1 = getE3();
676 <    while (nextc == '*' || nextc == '/') {
676 >    while ((nextc == '*') | (nextc == '/')) {
677          ep2 = newnode();
678          ep2->type = nextc;
679          scan();
# Line 621 | Line 681 | getE2(void)                    /* E2 -> E2 MULOP E3 */
681          addekid(ep2, getE3());
682          if (esupport&E_RCONST) {
683                  EPNODE  *ep3 = ep1->sibling;
684 <                if (ep1->type == NUM && ep3->type == NUM) {
684 >                if ((ep1->type == NUM) & (ep3->type == NUM)) {
685                          ep2 = rconst(ep2);
686                  } else if (ep3->type == NUM) {
687                          if (ep2->type == '/') {
# Line 631 | Line 691 | getE2(void)                    /* E2 -> E2 MULOP E3 */
691                                  ep3->v.num = 1./ep3->v.num;
692                          } else if (ep3->v.num == 0) {
693                                  ep1->sibling = NULL;    /* (E2 * 0) */
694 <                                epfree(ep2);
694 >                                epfree(ep2,1);
695                                  ep2 = ep3;
696                          }
697                  } else if (ep1->type == NUM && ep1->v.num == 0) {
698 <                        epfree(ep3);            /* (0 * E3) or (0 / E3) */
698 >                        epfree(ep3,1);          /* (0 * E3) or (0 / E3) */
699                          ep1->sibling = NULL;
700 <                        efree((char *)ep2);
700 >                        efree(ep2);
701                          ep2 = ep1;
702                  }
703          }
# Line 663 | Line 723 | getE3(void)                    /* E3 -> E4 ^ E3 */
723          addekid(ep2, getE3());
724          if (esupport&E_RCONST) {
725                  EPNODE  *ep3 = ep1->sibling;
726 <                if (ep1->type == NUM && ep3->type == NUM) {
726 >                if ((ep1->type == NUM) & (ep3->type == NUM)) {
727                          ep2 = rconst(ep2);
728                  } else if (ep1->type == NUM && ep1->v.num == 0) {
729 <                        epfree(ep3);            /* (0 ^ E3) */
729 >                        epfree(ep3,1);          /* (0 ^ E3) */
730                          ep1->sibling = NULL;
731 <                        efree((char *)ep2);
731 >                        efree(ep2);
732                          ep2 = ep1;
733 <                } else if ((ep3->type == NUM && ep3->v.num == 0) ||
733 >                } else if ((ep3->type == NUM && ep3->v.num == 0) |
734                                  (ep1->type == NUM && ep1->v.num == 1)) {
735 <                        epfree(ep2);            /* (E4 ^ 0) or (1 ^ E3) */
676 <                        ep2 = newnode();
735 >                        epfree(ep2,0);          /* (E4 ^ 0) or (1 ^ E3) */
736                          ep2->type = NUM;
737                          ep2->v.num = 1;
738 +                } else if (ep3->type == NUM && ep3->v.num == 1) {
739 +                        efree(ep3);     /* (E4 ^ 1) */
740 +                        ep1->sibling = NULL;
741 +                        efree(ep2);
742 +                        ep2 = ep1;
743                  }
744          }
745          return(ep2);
# Line 697 | Line 761 | getE4(void)                    /* E4 -> ADDOP E5 */
761          }
762          if (ep2->type == UMINUS) {      /* don't generate -(-E5) */
763              ep1 = ep2->v.kid;
764 <            efree((char *)ep2);
764 >            efree(ep2);
765              return(ep1);
766          }
767          ep1 = newnode();
# Line 731 | Line 795 | getE5(void)                    /* E5 -> (E1) */
795                  scan();
796                  return(ep1);
797          }
734
798          if (esupport&E_INCHAN && nextc == '$') {
799                  scan();
800                  ep1 = newnode();
# Line 739 | Line 802 | getE5(void)                    /* E5 -> (E1) */
802                  ep1->v.chan = getinum();
803                  return(ep1);
804          }
742
805          if (esupport&(E_VARIABLE|E_FUNCTION) &&
806 <                        (isalpha(nextc) || nextc == CNTXMARK)) {
806 >                        (isalpha(nextc) | (nextc == CNTXMARK))) {
807                  nam = getname();
808                  ep1 = NULL;
809                  if ((esupport&(E_VARIABLE|E_FUNCTION)) == (E_VARIABLE|E_FUNCTION)
# Line 777 | Line 839 | getE5(void)                    /* E5 -> (E1) */
839                          ep1 = rconst(ep1);
840                  return(ep1);
841          }
780
842          if (isdecimal(nextc)) {
843                  ep1 = newnode();
844                  ep1->type = NUM;
# Line 800 | Line 861 | rconst(                        /* reduce a constant expression */
861      ep->type = NUM;
862      errno = 0;
863      ep->v.num = evalue(epar);
864 <    if (errno == EDOM || errno == ERANGE)
864 >    if ((errno == EDOM) | (errno == ERANGE))
865          syntax("bad constant expression");
866 <    epfree(epar);
866 >    epfree(epar,1);
867  
868      return(ep);
869   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines