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

Comparing ray/src/common/calexpr.c (file contents):
Revision 2.43 by greg, Fri Feb 23 03:47:57 2024 UTC vs.
Revision 2.56 by greg, Sat Dec 6 16:39:20 2025 UTC

# Line 35 | Line 35 | static const char      RCSid[] = "$Id$";
35  
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 90 | Line 92 | eparse(                        /* parse an expression string */
92      EPNODE  *ep;
93  
94      initstr(expr, NULL, 0);
95 <    curfunc = NULL;
95 >    ecurfunc = NULL;
96      ep = getE1();
97      if (nextc != EOF)
98 <        syntax("unexpected character");
98 >        esyntax("unexpected character");
99      return(ep);
100   }
101  
# Line 206 | 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  
216 + static void
217 + epflatten(                      /* flatten hierarchies for '+', '*' */
218 +        EPNODE *epar
219 + )
220 + {
221 +    EPNODE      *ep, *ep1;
222 +    double      combined;
223 +
224 +    if (epar->nkids <= 0)       /* can't handle array allocations */
225 +        return;
226 +
227 +    for (ep = epar->v.kid; ep != NULL; ep = ep->sibling)
228 +        while ((ep->type == epar->type) & (ep->nkids > 0)) {
229 +            ep1 = ep->v.kid;
230 +            while (ep1->sibling != NULL)
231 +                ep1 = ep1->sibling;
232 +            ep1->sibling = ep->sibling;
233 +            epar->nkids += ep->nkids - 1;
234 +            ep1 = ep->v.kid;
235 +            *ep = *ep1;
236 +            efree(ep1);         /* not epfree()! */
237 +        }
238 +    if (!(esupport & E_RCONST))
239 +        return;
240 +    ep1 = NULL;                 /* combine constants in sum/product */
241 +    for (ep = epar->v.kid; ep != NULL; ep = ep->sibling)
242 +        if (ep->type == NUM) {
243 +            if (ep1 == NULL) combined = (ep1 = ep)->v.num;
244 +            else if (epar->type == '+') combined += ep->v.num;
245 +            else /* epar->type=='*' */ combined *= ep->v.num;
246 +        }
247 +    if (ep1 == NULL)
248 +        return;
249 +    ep1->v.num = combined;      /* assumes commutative property, also */
250 +    while (ep1->sibling != NULL)
251 +        if (ep1->sibling->type == NUM) {
252 +            ep = ep1->sibling;
253 +            ep1->sibling = ep->sibling;
254 +            epar->nkids--;
255 +            efree(ep);          /* drop subsumed constant */
256 +        } else
257 +            ep1 = ep1->sibling;
258 +
259 +    if (epar->nkids == 1) {     /* late constant expression? */
260 +        ep = epar->v.kid;
261 +        *epar = *ep;
262 +        efree(ep);
263 +    }
264 + }
265 +
266 +
267   void
268 < epoptimize(                     /* realloc lists as arrays if > length 3 */
268 > epoptimize(                     /* flatten operations, lists -> arrays */
269          EPNODE  *epar
270   )
271   {
272      EPNODE      *ep;
273  
274 <    if (epar->nkids > 3) {      /* do this node if > 3 kids */
274 >    if ((epar->type == '+') | (epar->type == '*'))
275 >        epflatten(epar);        /* flatten associative operations */
276 >
277 >    if (epar->nkids)            /* do children if any */
278 >        for (ep = epar->v.kid; ep != NULL; ep = ep->sibling)
279 >            epoptimize(ep);
280 >
281 >    if (epar->nkids > 4) {      /* make list into array if > 4 kids */
282          int     n = 1;
283          epar->v.kid = (EPNODE *)erealloc(epar->v.kid,
284                                          sizeof(EPNODE)*epar->nkids);
# Line 227 | Line 289 | epoptimize(                    /* realloc lists as arrays if > length 3
289              epar->v.kid[n-1].sibling = epar->v.kid + n;
290              n++;
291          }
292 <        epar->nkids = -epar->nkids;
292 >        epar->nkids = -n;
293      }
232    if (epar->nkids)            /* do children if any */
233        for (ep = epar->v.kid; ep != NULL; ep = ep->sibling)
234            epoptimize(ep);
294   }
295  
296                                  /* the following used to be a switch */
297   static double
239 eargument(
240    EPNODE      *ep
241 )
242 {
243    return(argument(ep->v.chan));
244 }
245
246 static double
298   enumber(
299      EPNODE      *ep
300   )
# Line 274 | Line 325 | eadd(
325      EPNODE      *ep
326   )
327   {
328 +    double  sum = 0;
329      EPNODE  *ep1 = ep->v.kid;
330  
331 <    return(evalue(ep1) + evalue(ep1->sibling));
331 >    do
332 >        sum += envalue(ep1);
333 >    while ((ep1 = ep1->sibling) != NULL);
334 >
335 >    return(sum);
336   }
337  
338   static double
# Line 285 | Line 341 | esubtr(
341   )
342   {
343      EPNODE  *ep1 = ep->v.kid;
344 +    EPNODE  *ep2 = ep1->sibling;
345  
346 <    return(evalue(ep1) - evalue(ep1->sibling));
346 >    return(envalue(ep1) - envalue(ep2));
347   }
348  
349   static double
# Line 294 | Line 351 | emult(
351      EPNODE      *ep
352   )
353   {
354 +    double  prod = 1;
355      EPNODE  *ep1 = ep->v.kid;
356  
357 <    return(evalue(ep1) * evalue(ep1->sibling));
357 >    do
358 >        prod *= envalue(ep1);
359 >    while ((ep1 = ep1->sibling) != NULL);
360 >
361 >    return(prod);
362   }
363  
364   static double
# Line 305 | Line 367 | edivi(
367   )
368   {
369      EPNODE  *ep1 = ep->v.kid;
370 <    double  d;
370 >    double  den = evalue(ep1->sibling);
371  
372 <    d = evalue(ep1->sibling);
311 <    if (d == 0.0) {
372 >    if (den == 0.0) {
373          wputs("Division by zero\n");
374          errno = ERANGE;
375          return(0.0);
376      }
377 <    return(evalue(ep1) / d);
377 >    return(envalue(ep1) / den);
378   }
379  
380   static double
# Line 389 | Line 450 | initfile(              /* prepare input file */
450      lineno = ln;
451      linepos = 0;
452      inpbuf[0] = '\0';
453 <    scan();
453 >    escan();
454   }
455  
456  
# Line 405 | Line 466 | initstr(               /* prepare input string */
466      lineno = ln;
467      linbuf = s;
468      linepos = 0;
469 <    scan();
469 >    escan();
470   }
471  
472  
# Line 425 | Line 486 | getscanpos(    /* return current scan position */
486  
487  
488   int
489 < scan(void)              /* scan next character, return literal next */
489 > escan(void)             /* scan next character, return literal next */
490   {
491      int  lnext = 0;
492  
# Line 447 | Line 508 | scan(void)             /* scan next character, return literal nex
508                  break;
509          }
510          if (nextc == '{') {
511 <            scan();
511 >            escan();
512              while (nextc != '}')
513                  if (nextc == EOF)
514 <                    syntax("'}' expected");
514 >                    esyntax("'}' expected");
515                  else
516 <                    scan();
517 <            scan();
516 >                    escan();
517 >            escan();
518          }
519      } while (isspace(nextc));
520      return(lnext);
# Line 488 | Line 549 | long2ascii(                          /* convert long to ascii */
549  
550  
551   void
552 < syntax(                 /* report syntax error and quit */
552 > esyntax(                        /* report syntax error and quit */
553      char  *err
554   )
555   {
# Line 521 | Line 582 | addekid(                       /* add a child to ep */
582   )
583   {
584      if (ep->nkids < 0) {
585 <        eputs("Cannot add child after optimization\n");
585 >        eputs("Cannot add kid to EPNODE array\n");
586          quit(1);
587      }
588      ep->nkids++;
# Line 532 | Line 593 | addekid(                       /* add a child to ep */
593              ;
594          ep->sibling = ek;
595      }
596 <    ek->sibling = NULL;
596 >    ek->sibling = NULL;         /* shouldn't be necessary */
597   }
598  
599  
# Line 543 | Line 604 | getname(void)                  /* scan an identifier */
604      int  i, lnext;
605  
606      lnext = nextc;
607 <    for (i = 0; i < RMAXWORD && isid(lnext); i++, lnext = scan())
607 >    for (i = 0; i < RMAXWORD && isid(lnext); i++, lnext = escan())
608          str[i] = lnext;
609      str[i] = '\0';
610      while (isid(lnext))         /* skip rest of name */
611 <        lnext = scan();
611 >        lnext = escan();
612  
613      return(str);
614   }
# Line 562 | Line 623 | getinum(void)                  /* scan a positive integer */
623      lnext = nextc;
624      while (isdigit(lnext)) {
625          n = n * 10 + lnext - '0';
626 <        lnext = scan();
626 >        lnext = escan();
627      }
628      return(n);
629   }
# Line 578 | Line 639 | getnum(void)                   /* scan a positive float */
639      lnext = nextc;
640      while (isdigit(lnext) && i < RMAXWORD) {
641          str[i++] = lnext;
642 <        lnext = scan();
642 >        lnext = escan();
643      }
644      if ((lnext == '.') & (i < RMAXWORD)) {
645          str[i++] = lnext;
646 <        lnext = scan();
646 >        lnext = escan();
647          if (i == 1 && !isdigit(lnext))
648 <            syntax("badly formed number");
648 >            esyntax("badly formed number");
649          while (isdigit(lnext) && i < RMAXWORD) {
650              str[i++] = lnext;
651 <            lnext = scan();
651 >            lnext = escan();
652          }
653      }
654      if ((lnext == 'e') | (lnext == 'E') && i < RMAXWORD) {
655          str[i++] = lnext;
656 <        lnext = scan();
656 >        lnext = escan();
657          if ((lnext == '-') | (lnext == '+') && i < RMAXWORD) {
658              str[i++] = lnext;
659 <            lnext = scan();
659 >            lnext = escan();
660          }
661          if (!isdigit(lnext))
662 <            syntax("missing exponent");
662 >            esyntax("missing exponent");
663          while (isdigit(lnext) && i < RMAXWORD) {
664              str[i++] = lnext;
665 <            lnext = scan();
665 >            lnext = escan();
666          }
667      }
668      str[i] = '\0';
# Line 620 | Line 681 | getE1(void)                    /* E1 -> E1 ADDOP E2 */
681      while ((nextc == '+') | (nextc == '-')) {
682          ep2 = newnode();
683          ep2->type = nextc;
684 <        scan();
684 >        escan();
685          addekid(ep2, ep1);
686          addekid(ep2, getE2());
687 <        if (esupport&E_RCONST &&
688 <                        (ep1->type == NUM) & (ep1->sibling->type == NUM))
687 >        if (esupport&E_RCONST && ep1->sibling->type == NUM) {
688 >            if (ep1->type == NUM) {
689                  ep2 = rconst(ep2);
690 +            } else if (ep2->type == '-') {
691 +                ep1->sibling->v.num *= -1;
692 +                ep2->type = '+';        /* associative&commutative */
693 +            }
694 +        }
695          ep1 = ep2;
696      }
697      return(ep1);
# Line 642 | Line 708 | getE2(void)                    /* E2 -> E2 MULOP E3 */
708      while ((nextc == '*') | (nextc == '/')) {
709          ep2 = newnode();
710          ep2->type = nextc;
711 <        scan();
711 >        escan();
712          addekid(ep2, ep1);
713          addekid(ep2, getE3());
714          if (esupport&E_RCONST) {
715 <                EPNODE  *ep3 = ep1->sibling;
716 <                if ((ep1->type == NUM) & (ep3->type == NUM)) {
717 <                        ep2 = rconst(ep2);
718 <                } else if (ep3->type == NUM) {
719 <                        if (ep2->type == '/') {
720 <                                if (ep3->v.num == 0)
721 <                                        syntax("divide by zero constant");
722 <                                ep2->type = '*';        /* for speed */
723 <                                ep3->v.num = 1./ep3->v.num;
724 <                        } else if (ep3->v.num == 0) {
725 <                                ep1->sibling = NULL;    /* (E2 * 0) */
726 <                                epfree(ep2,1);
727 <                                ep2 = ep3;
662 <                        }
663 <                } else if (ep1->type == NUM && ep1->v.num == 0) {
664 <                        epfree(ep3,1);          /* (0 * E3) or (0 / E3) */
665 <                        ep1->sibling = NULL;
666 <                        efree(ep2);
667 <                        ep2 = ep1;
715 >            EPNODE      *ep3 = ep1->sibling;
716 >            if ((ep1->type == NUM) & (ep3->type == NUM)) {
717 >                ep2 = rconst(ep2);
718 >            } else if (ep3->type == NUM) {
719 >                if (ep2->type == '/') {
720 >                    if (ep3->v.num == 0)
721 >                        esyntax("divide by zero constant");
722 >                    ep2->type = '*';            /* for speed */
723 >                    ep3->v.num = 1./ep3->v.num;
724 >                } else if (ep3->v.num == 0) {
725 >                    ep1->sibling = NULL;        /* (E2 * 0) */
726 >                    epfree(ep2,1);
727 >                    ep2 = ep3;
728                  }
729 +            } else if (ep1->type == NUM && ep1->v.num == 0) {
730 +                epfree(ep3,1);                  /* (0 * E3) or (0 / E3) */
731 +                ep1->sibling = NULL;
732 +                efree(ep2);
733 +                ep2 = ep1;
734 +            }
735          }
736          ep1 = ep2;
737      }
# Line 684 | Line 750 | getE3(void)                    /* E3 -> E4 ^ E3 */
750                  return(ep1);
751          ep2 = newnode();
752          ep2->type = nextc;
753 <        scan();
753 >        escan();
754          addekid(ep2, ep1);
755          addekid(ep2, getE3());
756          if (esupport&E_RCONST) {
757 <                EPNODE  *ep3 = ep1->sibling;
758 <                if ((ep1->type == NUM) & (ep3->type == NUM)) {
759 <                        ep2 = rconst(ep2);
760 <                } else if (ep1->type == NUM && ep1->v.num == 0) {
761 <                        epfree(ep3,1);          /* (0 ^ E3) */
762 <                        ep1->sibling = NULL;
763 <                        efree(ep2);
764 <                        ep2 = ep1;
765 <                } else if ((ep3->type == NUM && ep3->v.num == 0) |
757 >            EPNODE      *ep3 = ep1->sibling;
758 >            if ((ep1->type == NUM) & (ep3->type == NUM)) {
759 >                ep2 = rconst(ep2);
760 >            } else if (ep1->type == NUM && ep1->v.num == 0) {
761 >                epfree(ep3,1);          /* (0 ^ E3) */
762 >                ep1->sibling = NULL;
763 >                efree(ep2);
764 >                ep2 = ep1;
765 >            } else if ((ep3->type == NUM && ep3->v.num == 0) |
766                                  (ep1->type == NUM && ep1->v.num == 1)) {
767 <                        epfree(ep2,1);          /* (E4 ^ 0) or (1 ^ E3) */
768 <                        ep2 = newnode();
769 <                        ep2->type = NUM;
770 <                        ep2->v.num = 1;
771 <                } else if (ep3->type == NUM && ep3->v.num == 1) {
772 <                        efree(ep3);     /* (E4 ^ 1) */
773 <                        ep1->sibling = NULL;
774 <                        efree(ep2);
775 <                        ep2 = ep1;
710 <                }
767 >                epfree(ep2,0);          /* (E4 ^ 0) or (1 ^ E3) */
768 >                ep2->type = NUM;
769 >                ep2->v.num = 1;
770 >            } else if (ep3->type == NUM && ep3->v.num == 1) {
771 >                efree(ep3);             /* (E4 ^ 1) */
772 >                ep1->sibling = NULL;
773 >                efree(ep2);
774 >                ep2 = ep1;
775 >            }
776          }
777          return(ep2);
778   }
# Line 720 | Line 785 | getE4(void)                    /* E4 -> ADDOP E5 */
785      EPNODE  *ep1, *ep2;
786  
787      if (nextc == '-') {
788 <        scan();
788 >        escan();
789          ep2 = getE5();
790          if (ep2->type == NUM) {
791 <                ep2->v.num = -ep2->v.num;
792 <                return(ep2);
791 >            ep2->v.num = -ep2->v.num;
792 >            return(ep2);
793          }
794          if (ep2->type == UMINUS) {      /* don't generate -(-E5) */
795              ep1 = ep2->v.kid;
# Line 737 | Line 802 | getE4(void)                    /* E4 -> ADDOP E5 */
802          return(ep1);
803      }
804      if (nextc == '+')
805 <        scan();
805 >        escan();
806      return(getE5());
807   }
808  
# Line 755 | Line 820 | getE5(void)                    /* E5 -> (E1) */
820          EPNODE  *ep1, *ep2;
821  
822          if (nextc == '(') {
823 <                scan();
824 <                ep1 = getE1();
825 <                if (nextc != ')')
826 <                        syntax("')' expected");
827 <                scan();
828 <                return(ep1);
823 >            escan();
824 >            ep1 = getE1();
825 >            if (nextc != ')')
826 >                esyntax("')' expected");
827 >            escan();
828 >            return(ep1);
829          }
765
830          if (esupport&E_INCHAN && nextc == '$') {
831 <                scan();
832 <                ep1 = newnode();
833 <                ep1->type = CHAN;
834 <                ep1->v.chan = getinum();
835 <                return(ep1);
831 >            escan();
832 >            ep1 = newnode();
833 >            ep1->type = CHAN;
834 >            ep1->v.chan = getinum();
835 >            return(ep1);
836          }
773
837          if (esupport&(E_VARIABLE|E_FUNCTION) &&
838                          (isalpha(nextc) | (nextc == CNTXMARK))) {
839 <                nam = getname();
840 <                ep1 = NULL;
841 <                if ((esupport&(E_VARIABLE|E_FUNCTION)) == (E_VARIABLE|E_FUNCTION)
842 <                                && curfunc != NULL)
843 <                        for (i = 1, ep2 = curfunc->v.kid->sibling;
844 <                                        ep2 != NULL; i++, ep2 = ep2->sibling)
845 <                                if (!strcmp(ep2->v.name, nam)) {
783 <                                        ep1 = newnode();
784 <                                        ep1->type = ARG;
785 <                                        ep1->v.chan = i;
786 <                                        break;
787 <                                }
788 <                if (ep1 == NULL) {
839 >            nam = getname();
840 >            ep1 = NULL;
841 >            if ((esupport&(E_VARIABLE|E_FUNCTION)) == (E_VARIABLE|E_FUNCTION)
842 >                                && ecurfunc != NULL)
843 >                for (i = 1, ep2 = ecurfunc->v.kid->sibling;
844 >                                ep2 != NULL; i++, ep2 = ep2->sibling)
845 >                    if (!strcmp(ep2->v.name, nam)) {
846                          ep1 = newnode();
847 <                        ep1->type = VAR;
848 <                        ep1->v.ln = varinsert(nam);
849 <                }
850 <                if (esupport&E_FUNCTION && nextc == '(') {
851 <                        ep2 = newnode();
852 <                        ep2->type = FUNC;
853 <                        addekid(ep2, ep1);
854 <                        ep1 = ep2;
855 <                        do {
856 <                                scan();
857 <                                addekid(ep1, getE1());
858 <                        } while (nextc == ',');
859 <                        if (nextc != ')')
860 <                                syntax("')' expected");
861 <                        scan();
862 <                } else if (!(esupport&E_VARIABLE))
863 <                        syntax("'(' expected");
864 <                if (esupport&E_RCONST && isconstvar(ep1))
865 <                        ep1 = rconst(ep1);
866 <                return(ep1);
847 >                        ep1->type = ARG;
848 >                        ep1->v.chan = i;
849 >                        break;
850 >                    }
851 >            if (ep1 == NULL) {
852 >                ep1 = newnode();
853 >                ep1->type = VAR;
854 >                ep1->v.ln = varinsert(nam);
855 >            }
856 >            if (esupport&E_FUNCTION && nextc == '(') {
857 >                ep2 = newnode();
858 >                ep2->type = FUNC;
859 >                addekid(ep2, ep1);
860 >                ep1 = ep2;
861 >                do {
862 >                    escan();
863 >                    addekid(ep1, getE1());
864 >                } while (nextc == ',');
865 >                if (nextc != ')')
866 >                    esyntax("')' expected");
867 >                escan();
868 >            } else if (!(esupport&E_VARIABLE))
869 >                esyntax("'(' expected");
870 >            if (esupport&E_RCONST && isconstvar(ep1))
871 >                ep1 = rconst(ep1);
872 >            return(ep1);
873          }
811
874          if (isdecimal(nextc)) {
875 <                ep1 = newnode();
876 <                ep1->type = NUM;
877 <                ep1->v.num = getnum();
878 <                return(ep1);
875 >            ep1 = newnode();
876 >            ep1->type = NUM;
877 >            ep1->v.num = getnum();
878 >            return(ep1);
879          }
880 <        syntax("unexpected character");
880 >        esyntax("unexpected character");
881          return NULL; /* pro forma return */
882   }
883  
# Line 832 | Line 894 | rconst(                        /* reduce a constant expression */
894      errno = 0;
895      ep->v.num = evalue(epar);
896      if ((errno == EDOM) | (errno == ERANGE))
897 <        syntax("bad constant expression");
897 >        esyntax("bad constant expression");
898      epfree(epar,1);
899  
900      return(ep);
# Line 871 | Line 933 | isconstfun(                    /* is ep linked to a constant function?
933   )
934   {
935      EPNODE  *dp;
936 <    LIBR  *lp;
936 >    ELIBR  *lp;
937  
938      if (ep->type != VAR)
939          return(0);

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)