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.46 by greg, Sat Feb 24 19:26:44 2024 UTC vs.
Revision 2.50 by greg, Tue Feb 27 01:24:10 2024 UTC

# Line 37 | Line 37 | static const char      RCSid[] = "$Id$";
37  
38   #define  envalue(ep)    ((ep)->type==NUM ? (ep)->v.num : evalue(ep))
39  
40 < static double  euminus(EPNODE *), eargument(EPNODE *), enumber(EPNODE *);
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 208 | Line 208 | epfree(                        /* free a parse tree */
208      }
209      if (frep)
210          efree(epar);
211 +    else
212 +        memset(epar, 0, sizeof(EPNODE));
213   }
214  
215  
# Line 218 | Line 220 | epflatten(                     /* flatten hierarchies for '+', '*' */
220   {
221      EPNODE      *ep;
222  
223 <    if (epar->nkids < 0)        /* we don't really handle this properly */
224 <        epar->nkids *= -1;
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) {
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 += nekids(ep) - 1;
232 >            epar->nkids += ep->nkids - 1;
233              ep1 = ep->v.kid;
234              *ep = *ep1;
235              efree(ep1);         /* not epfree()! */
# Line 236 | Line 238 | epflatten(                     /* flatten hierarchies for '+', '*' */
238  
239  
240   void
241 < epoptimize(                     /* flatten operations and lists -> arrays */
241 > epoptimize(                     /* flatten operations, lists -> arrays */
242          EPNODE  *epar
243   )
244   {
245      EPNODE      *ep;
246  
247      if ((epar->type == '+') | (epar->type == '*'))
248 <        epflatten(epar);        /* commutative & associative */
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)
# Line 266 | Line 268 | epoptimize(                    /* flatten operations and lists -> array
268  
269                                  /* the following used to be a switch */
270   static double
269 eargument(
270    EPNODE      *ep
271 )
272 {
273    return(argument(ep->v.chan));
274 }
275
276 static double
271   enumber(
272      EPNODE      *ep
273   )
# Line 346 | Line 340 | edivi(
340   )
341   {
342      EPNODE  *ep1 = ep->v.kid;
343 <    EPNODE  *ep2 = ep1->sibling;
350 <    double  d;
343 >    double  den = evalue(ep1->sibling);
344  
345 <    d = evalue(ep2);
353 <    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(envalue(ep1) / d);
350 >    return(envalue(ep1) / den);
351   }
352  
353   static double
# Line 562 | Line 554 | addekid(                       /* add a child to ep */
554      EPNODE      *ek
555   )
556   {
557 <    if (ep->nkids < 0)          /* we don't really handle this properly */
558 <        ep->nkids *= -1;
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 = ek;
# Line 738 | Line 732 | getE3(void)                    /* E3 -> E4 ^ E3 */
732                          ep2 = ep1;
733                  } else if ((ep3->type == NUM && ep3->v.num == 0) |
734                                  (ep1->type == NUM && ep1->v.num == 1)) {
735 <                        epfree(ep2,1);          /* (E4 ^ 0) or (1 ^ E3) */
742 <                        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) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines