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.46 by greg, Sat Feb 24 19:26:44 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)        /* we don't really handle this properly */
222 >        epar->nkids *= -1;
223 >
224 >    for (ep = epar->v.kid; ep != NULL; ep = ep->sibling)
225 >        while (ep->type == epar->type) {
226 >            EPNODE      *ep1 = ep->v.kid;
227 >            while (ep1->sibling != NULL)
228 >                ep1 = ep1->sibling;
229 >            ep1->sibling = ep->sibling;
230 >            epar->nkids += nekids(ep) - 1;
231 >            ep1 = ep->v.kid;
232 >            *ep = *ep1;
233 >            efree(ep1);         /* not epfree()! */
234 >        }
235   }
236  
237 +
238 + void
239 + epoptimize(                     /* flatten operations and lists -> arrays */
240 +        EPNODE  *epar
241 + )
242 + {
243 +    EPNODE      *ep;
244 +
245 +    if ((epar->type == '+') | (epar->type == '*'))
246 +        epflatten(epar);        /* commutative & associative */
247 +
248 +    if (epar->nkids)            /* do children if any */
249 +        for (ep = epar->v.kid; ep != NULL; ep = ep->sibling)
250 +            epoptimize(ep);
251 +
252 +    if (epar->nkids > 4) {      /* make list into array if > 4 kids */
253 +        int     n = 1;
254 +        epar->v.kid = (EPNODE *)erealloc(epar->v.kid,
255 +                                        sizeof(EPNODE)*epar->nkids);
256 +        while (n < epar->nkids) {
257 +            ep = epar->v.kid[n-1].sibling;
258 +            epar->v.kid[n] = *ep;
259 +            efree(ep);          /* not epfree()! */
260 +            epar->v.kid[n-1].sibling = epar->v.kid + n;
261 +            n++;
262 +        }
263 +        epar->nkids = -n;
264 +    }
265 + }
266 +
267                                  /* the following used to be a switch */
268   static double
269   eargument(
# Line 241 | Line 304 | eadd(
304      EPNODE      *ep
305   )
306   {
307 +    double  sum = 0;
308      EPNODE  *ep1 = ep->v.kid;
309  
310 <    return(evalue(ep1) + evalue(ep1->sibling));
310 >    do
311 >        sum += envalue(ep1);
312 >    while ((ep1 = ep1->sibling) != NULL);
313 >
314 >    return(sum);
315   }
316  
317   static double
# Line 252 | Line 320 | esubtr(
320   )
321   {
322      EPNODE  *ep1 = ep->v.kid;
323 +    EPNODE  *ep2 = ep1->sibling;
324  
325 <    return(evalue(ep1) - evalue(ep1->sibling));
325 >    return(envalue(ep1) - envalue(ep2));
326   }
327  
328   static double
# Line 261 | Line 330 | emult(
330      EPNODE      *ep
331   )
332   {
333 +    double  prod = 1;
334      EPNODE  *ep1 = ep->v.kid;
335  
336 <    return(evalue(ep1) * evalue(ep1->sibling));
336 >    do
337 >        prod *= envalue(ep1);
338 >    while ((ep1 = ep1->sibling) != NULL);
339 >
340 >    return(prod);
341   }
342  
343   static double
# Line 272 | Line 346 | edivi(
346   )
347   {
348      EPNODE  *ep1 = ep->v.kid;
349 +    EPNODE  *ep2 = ep1->sibling;
350      double  d;
351  
352 <    d = evalue(ep1->sibling);
352 >    d = evalue(ep2);
353      if (d == 0.0) {
354          wputs("Division by zero\n");
355          errno = ERANGE;
356          return(0.0);
357      }
358 <    return(evalue(ep1) / d);
358 >    return(envalue(ep1) / d);
359   }
360  
361   static double
# Line 303 | Line 378 | epow(
378              errno = ERANGE;
379      }
380   #endif
381 <    if (errno == EDOM || errno == ERANGE) {
381 >    if ((errno == EDOM) | (errno == ERANGE)) {
382          wputs("Illegal power\n");
383          return(0.0);
384      }
# Line 328 | Line 403 | ekid(                  /* return pointer to a node's nth kid */
403      int  n
404   )
405   {
406 <
407 <    for (ep = ep->v.kid; ep != NULL; ep = ep->sibling)
408 <        if (--n < 0)
409 <            break;
410 <
406 >    if (ep->nkids < 0) {        /* allocated array? */
407 >        if (n >= -ep->nkids)
408 >            return(NULL);
409 >        return(ep->v.kid + n);
410 >    }
411 >    ep = ep->v.kid;             /* else get from list */
412 >    while (n-- > 0)
413 >        if ((ep = ep->sibling) == NULL)
414 >                break;
415      return(ep);
416   }
417  
418  
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
419   void
420   initfile(               /* prepare input file */
421      FILE  *fp,
# Line 471 | Line 536 | syntax(                        /* report syntax error and quit */
536   {
537      int  i;
538  
539 <    if (infile != NULL || lineno != 0) {
539 >    if ((infile != NULL) | (lineno != 0)) {
540          if (infile != NULL) eputs(infile);
541          if (lineno != 0) {
542              eputs(infile != NULL ? ", line " : "line ");
# Line 494 | Line 559 | syntax(                        /* report syntax error and quit */
559   void
560   addekid(                        /* add a child to ep */
561      EPNODE       *ep,
562 <    EPNODE      *ekid
562 >    EPNODE      *ek
563   )
564   {
565 +    if (ep->nkids < 0)          /* we don't really handle this properly */
566 +        ep->nkids *= -1;
567 +    ep->nkids++;
568      if (ep->v.kid == NULL)
569 <        ep->v.kid = ekid;
569 >        ep->v.kid = ek;
570      else {
571          for (ep = ep->v.kid; ep->sibling != NULL; ep = ep->sibling)
572              ;
573 <        ep->sibling = ekid;
573 >        ep->sibling = ek;
574      }
575 <    ekid->sibling = NULL;
575 >    ek->sibling = NULL;         /* shouldn't be necessary */
576   }
577  
578  
# Line 552 | Line 620 | getnum(void)                   /* scan a positive float */
620          str[i++] = lnext;
621          lnext = scan();
622      }
623 <    if (lnext == '.' && i < RMAXWORD) {
623 >    if ((lnext == '.') & (i < RMAXWORD)) {
624          str[i++] = lnext;
625          lnext = scan();
626          if (i == 1 && !isdigit(lnext))
# Line 589 | Line 657 | getE1(void)                    /* E1 -> E1 ADDOP E2 */
657      EPNODE  *ep1, *ep2;
658  
659      ep1 = getE2();
660 <    while (nextc == '+' || nextc == '-') {
660 >    while ((nextc == '+') | (nextc == '-')) {
661          ep2 = newnode();
662          ep2->type = nextc;
663          scan();
664          addekid(ep2, ep1);
665          addekid(ep2, getE2());
666          if (esupport&E_RCONST &&
667 <                        ep1->type == NUM && ep1->sibling->type == NUM)
667 >                        (ep1->type == NUM) & (ep1->sibling->type == NUM))
668                  ep2 = rconst(ep2);
669          ep1 = ep2;
670      }
# Line 611 | Line 679 | getE2(void)                    /* E2 -> E2 MULOP E3 */
679      EPNODE  *ep1, *ep2;
680  
681      ep1 = getE3();
682 <    while (nextc == '*' || nextc == '/') {
682 >    while ((nextc == '*') | (nextc == '/')) {
683          ep2 = newnode();
684          ep2->type = nextc;
685          scan();
# Line 619 | Line 687 | getE2(void)                    /* E2 -> E2 MULOP E3 */
687          addekid(ep2, getE3());
688          if (esupport&E_RCONST) {
689                  EPNODE  *ep3 = ep1->sibling;
690 <                if (ep1->type == NUM && ep3->type == NUM) {
690 >                if ((ep1->type == NUM) & (ep3->type == NUM)) {
691                          ep2 = rconst(ep2);
692                  } else if (ep3->type == NUM) {
693                          if (ep2->type == '/') {
# Line 629 | Line 697 | getE2(void)                    /* E2 -> E2 MULOP E3 */
697                                  ep3->v.num = 1./ep3->v.num;
698                          } else if (ep3->v.num == 0) {
699                                  ep1->sibling = NULL;    /* (E2 * 0) */
700 <                                epfree(ep2);
700 >                                epfree(ep2,1);
701                                  ep2 = ep3;
702                          }
703                  } else if (ep1->type == NUM && ep1->v.num == 0) {
704 <                        epfree(ep3);            /* (0 * E3) or (0 / E3) */
704 >                        epfree(ep3,1);          /* (0 * E3) or (0 / E3) */
705                          ep1->sibling = NULL;
706 <                        efree((char *)ep2);
706 >                        efree(ep2);
707                          ep2 = ep1;
708                  }
709          }
# Line 661 | Line 729 | getE3(void)                    /* E3 -> E4 ^ E3 */
729          addekid(ep2, getE3());
730          if (esupport&E_RCONST) {
731                  EPNODE  *ep3 = ep1->sibling;
732 <                if (ep1->type == NUM && ep3->type == NUM) {
732 >                if ((ep1->type == NUM) & (ep3->type == NUM)) {
733                          ep2 = rconst(ep2);
734                  } else if (ep1->type == NUM && ep1->v.num == 0) {
735 <                        epfree(ep3);            /* (0 ^ E3) */
735 >                        epfree(ep3,1);          /* (0 ^ E3) */
736                          ep1->sibling = NULL;
737 <                        efree((char *)ep2);
737 >                        efree(ep2);
738                          ep2 = ep1;
739 <                } else if ((ep3->type == NUM && ep3->v.num == 0) ||
739 >                } else if ((ep3->type == NUM && ep3->v.num == 0) |
740                                  (ep1->type == NUM && ep1->v.num == 1)) {
741 <                        epfree(ep2);            /* (E4 ^ 0) or (1 ^ E3) */
741 >                        epfree(ep2,1);          /* (E4 ^ 0) or (1 ^ E3) */
742                          ep2 = newnode();
743                          ep2->type = NUM;
744                          ep2->v.num = 1;
745 +                } else if (ep3->type == NUM && ep3->v.num == 1) {
746 +                        efree(ep3);     /* (E4 ^ 1) */
747 +                        ep1->sibling = NULL;
748 +                        efree(ep2);
749 +                        ep2 = ep1;
750                  }
751          }
752          return(ep2);
# Line 695 | Line 768 | getE4(void)                    /* E4 -> ADDOP E5 */
768          }
769          if (ep2->type == UMINUS) {      /* don't generate -(-E5) */
770              ep1 = ep2->v.kid;
771 <            efree((char *)ep2);
771 >            efree(ep2);
772              return(ep1);
773          }
774          ep1 = newnode();
# Line 729 | Line 802 | getE5(void)                    /* E5 -> (E1) */
802                  scan();
803                  return(ep1);
804          }
732
805          if (esupport&E_INCHAN && nextc == '$') {
806                  scan();
807                  ep1 = newnode();
# Line 737 | Line 809 | getE5(void)                    /* E5 -> (E1) */
809                  ep1->v.chan = getinum();
810                  return(ep1);
811          }
740
812          if (esupport&(E_VARIABLE|E_FUNCTION) &&
813 <                        (isalpha(nextc) || nextc == CNTXMARK)) {
813 >                        (isalpha(nextc) | (nextc == CNTXMARK))) {
814                  nam = getname();
815                  ep1 = NULL;
816                  if ((esupport&(E_VARIABLE|E_FUNCTION)) == (E_VARIABLE|E_FUNCTION)
# Line 775 | Line 846 | getE5(void)                    /* E5 -> (E1) */
846                          ep1 = rconst(ep1);
847                  return(ep1);
848          }
778
849          if (isdecimal(nextc)) {
850                  ep1 = newnode();
851                  ep1->type = NUM;
# Line 798 | Line 868 | rconst(                        /* reduce a constant expression */
868      ep->type = NUM;
869      errno = 0;
870      ep->v.num = evalue(epar);
871 <    if (errno == EDOM || errno == ERANGE)
871 >    if ((errno == EDOM) | (errno == ERANGE))
872          syntax("bad constant expression");
873 <    epfree(epar);
873 >    epfree(epar,1);
874  
875      return(ep);
876   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines