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.20 by greg, Wed Mar 5 16:16:52 2003 UTC vs.
Revision 2.29 by schorsch, Sun Mar 28 20:33:12 2004 UTC

# Line 20 | Line 20 | static const char      RCSid[] = "$Id$";
20   #include "copyright.h"
21  
22   #include  <stdio.h>
23 <
23 > #include  <string.h>
24   #include  <ctype.h>
25
25   #include  <errno.h>
27
26   #include  <math.h>
29
27   #include  <stdlib.h>
28  
29 + #include  "rtmisc.h"
30 + #include  "rtio.h"
31 + #include  "rterror.h"
32   #include  "calcomp.h"
33  
34   #define  MAXLINE        256             /* maximum line length */
# Line 37 | Line 37 | static const char      RCSid[] = "$Id$";
37  
38   #define  isdecimal(c)   (isdigit(c) || (c) == '.')
39  
40 < static double  euminus(), eargument(), enumber();
41 < static double  echannel();
42 < static double  eadd(), esubtr(), emult(), edivi(), epow();
43 < static double  ebotch();
40 > static double  euminus(EPNODE *), eargument(EPNODE *), enumber(EPNODE *);
41 > static double  echannel(EPNODE *);
42 > static double  eadd(EPNODE *), esubtr(EPNODE *),
43 >               emult(EPNODE *), edivi(EPNODE *),
44 >               epow(EPNODE *);
45 > static double  ebotch(EPNODE *);
46  
47   unsigned int  esupport =                /* what to support */
48 <                E_VARIABLE | E_FUNCTION | E_REDEFW;
48 >                E_VARIABLE | E_FUNCTION ;
49  
50   int  nextc;                             /* lookahead character */
51  
52 < double  (*eoper[])() = {                /* expression operations */
52 > double  (*eoper[])(EPNODE *) = {        /* expression operations */
53          ebotch,
54          evariable,
55          enumber,
# Line 82 | Line 84 | static int  linepos;                   /* position in buffer */
84  
85  
86   EPNODE *
87 < eparse(expr)                    /* parse an expression string */
88 < char  *expr;
87 > eparse(                 /* parse an expression string */
88 >    char  *expr
89 > )
90   {
91      EPNODE  *ep;
92  
# Line 97 | Line 100 | char  *expr;
100  
101  
102   double
103 < eval(expr)                      /* evaluate an expression string */
104 < char  *expr;
103 > eval(                   /* evaluate an expression string */
104 >    char  *expr
105 > )
106   {
107      register EPNODE  *ep;
108      double  rval;
# Line 111 | Line 115 | char  *expr;
115  
116  
117   int
118 < epcmp(ep1, ep2)                 /* compare two expressions for equivalence */
119 < register EPNODE  *ep1, *ep2;
118 > epcmp(                  /* compare two expressions for equivalence */
119 >    register EPNODE  *ep1,
120 >    register EPNODE  *ep2
121 > )
122   {
123          double  d;
124  
# Line 128 | Line 134 | register EPNODE  *ep1, *ep2;
134                  if (ep2->v.num == 0)
135                          return(ep1->v.num != 0);
136                  d = ep1->v.num / ep2->v.num;
137 <                return(d > 1.000000000001 | d < 0.999999999999);
137 >                return((d > 1.000000000001) | (d < 0.999999999999));
138  
139          case CHAN:
140          case ARG:
# Line 159 | Line 165 | register EPNODE  *ep1, *ep2;
165  
166  
167   void
168 < epfree(epar)                    /* free a parse tree */
169 < register EPNODE  *epar;
168 > epfree(                 /* free a parse tree */
169 >    register EPNODE      *epar
170 > )
171   {
172      register EPNODE  *ep;
173  
# Line 194 | Line 201 | register EPNODE         *epar;
201  
202                                  /* the following used to be a switch */
203   static double
204 < eargument(ep)
205 < EPNODE  *ep;
204 > eargument(
205 >    EPNODE      *ep
206 > )
207   {
208      return(argument(ep->v.chan));
209   }
210  
211   static double
212 < enumber(ep)
213 < EPNODE  *ep;
212 > enumber(
213 >    EPNODE      *ep
214 > )
215   {
216      return(ep->v.num);
217   }
218  
219   static double
220 < euminus(ep)
221 < EPNODE  *ep;
220 > euminus(
221 >    EPNODE      *ep
222 > )
223   {
224      register EPNODE  *ep1 = ep->v.kid;
225  
# Line 217 | Line 227 | EPNODE *ep;
227   }
228  
229   static double
230 < echannel(ep)
231 < EPNODE  *ep;
230 > echannel(
231 >    EPNODE      *ep
232 > )
233   {
234      return(chanvalue(ep->v.chan));
235   }
236  
237   static double
238 < eadd(ep)
239 < EPNODE  *ep;
238 > eadd(
239 >    EPNODE      *ep
240 > )
241   {
242      register EPNODE  *ep1 = ep->v.kid;
243  
# Line 233 | Line 245 | EPNODE *ep;
245   }
246  
247   static double
248 < esubtr(ep)
249 < EPNODE  *ep;
248 > esubtr(
249 >    EPNODE      *ep
250 > )
251   {
252      register EPNODE  *ep1 = ep->v.kid;
253  
# Line 242 | Line 255 | EPNODE *ep;
255   }
256  
257   static double
258 < emult(ep)
259 < EPNODE  *ep;
258 > emult(
259 >    EPNODE      *ep
260 > )
261   {
262      register EPNODE  *ep1 = ep->v.kid;
263  
# Line 251 | Line 265 | EPNODE *ep;
265   }
266  
267   static double
268 < edivi(ep)
269 < EPNODE  *ep;
268 > edivi(
269 >    EPNODE      *ep
270 > )
271   {
272      register EPNODE  *ep1 = ep->v.kid;
273      double  d;
# Line 267 | Line 282 | EPNODE *ep;
282   }
283  
284   static double
285 < epow(ep)
286 < EPNODE  *ep;
285 > epow(
286 >    EPNODE      *ep
287 > )
288   {
289      register EPNODE  *ep1 = ep->v.kid;
290      double  d;
# Line 290 | Line 306 | EPNODE *ep;
306   }
307  
308   static double
309 < ebotch(ep)
310 < EPNODE  *ep;
309 > ebotch(
310 >    EPNODE      *ep
311 > )
312   {
313      eputs("Bad expression!\n");
314      quit(1);
315 +        return 0.0; /* pro forma return */
316   }
317  
318  
319   EPNODE *
320 < ekid(ep, n)                     /* return pointer to a node's nth kid */
321 < register EPNODE  *ep;
322 < register int  n;
320 > ekid(                   /* return pointer to a node's nth kid */
321 >    register EPNODE      *ep,
322 >    register int  n
323 > )
324   {
325  
326      for (ep = ep->v.kid; ep != NULL; ep = ep->sibling)
# Line 313 | Line 332 | register int  n;
332  
333  
334   int
335 < nekids(ep)                      /* return # of kids for node ep */
336 < register EPNODE  *ep;
335 > nekids(                 /* return # of kids for node ep */
336 >    register EPNODE      *ep
337 > )
338   {
339      register int  n = 0;
340  
# Line 326 | Line 346 | register EPNODE         *ep;
346  
347  
348   void
349 < initfile(fp, fn, ln)            /* prepare input file */
350 < FILE  *fp;
351 < char  *fn;
352 < int  ln;
349 > initfile(               /* prepare input file */
350 >    FILE  *fp,
351 >    char  *fn,
352 >    int  ln
353 > )
354   {
355      static char  inpbuf[MAXLINE];
356  
# Line 344 | Line 365 | int  ln;
365  
366  
367   void
368 < initstr(s, fn, ln)              /* prepare input string */
369 < char  *s;
370 < char  *fn;
371 < int  ln;
368 > initstr(                /* prepare input string */
369 >    char  *s,
370 >    char  *fn,
371 >    int  ln
372 > )
373   {
374      infp = NULL;
375      infile = fn;
# Line 359 | Line 381 | int  ln;
381  
382  
383   void
384 < getscanpos(fnp, lnp, spp, fpp)  /* return current scan position */
385 < char  **fnp;
386 < int  *lnp;
387 < char  **spp;
388 < FILE  **fpp;
384 > getscanpos(     /* return current scan position */
385 >    char  **fnp,
386 >    int  *lnp,
387 >    char  **spp,
388 >    FILE  **fpp
389 > )
390   {
391      if (fnp != NULL) *fnp = infile;
392      if (lnp != NULL) *lnp = lineno;
# Line 373 | Line 396 | FILE  **fpp;
396  
397  
398   int
399 < scan()                          /* scan next character, return literal next */
399 > scan(void)              /* scan next character, return literal next */
400   {
401      register int  lnext = 0;
402  
# Line 405 | Line 428 | scan()                         /* scan next character, return literal next
428  
429  
430   char *
431 < long2ascii(l)                         /* convert long to ascii */
432 < long  l;
431 > long2ascii(                           /* convert long to ascii */
432 >    long  l
433 > )
434   {
435      static char  buf[16];
436      register char  *cp;
# Line 431 | Line 455 | long  l;
455  
456  
457   void
458 < syntax(err)                     /* report syntax error and quit */
459 < char  *err;
458 > syntax(                 /* report syntax error and quit */
459 >    char  *err
460 > )
461   {
462      register int  i;
463  
# Line 457 | Line 482 | char  *err;
482  
483  
484   void
485 < addekid(ep, ekid)                       /* add a child to ep */
486 < register EPNODE  *ep;
487 < EPNODE  *ekid;
485 > addekid(                        /* add a child to ep */
486 >    register EPNODE      *ep,
487 >    EPNODE      *ekid
488 > )
489   {
490      if (ep->v.kid == NULL)
491          ep->v.kid = ekid;
# Line 473 | Line 499 | EPNODE *ekid;
499  
500  
501   char *
502 < getname()                       /* scan an identifier */
502 > getname(void)                   /* scan an identifier */
503   {
504 <    static char  str[MAXWORD+1];
504 >    static char  str[RMAXWORD+1];
505      register int  i, lnext;
506  
507      lnext = nextc;
508 <    for (i = 0; i < MAXWORD && isid(lnext); i++, lnext = scan())
508 >    for (i = 0; i < RMAXWORD && isid(lnext); i++, lnext = scan())
509          str[i] = lnext;
510      str[i] = '\0';
511      while (isid(lnext))         /* skip rest of name */
# Line 490 | Line 516 | getname()                      /* scan an identifier */
516  
517  
518   int
519 < getinum()                       /* scan a positive integer */
519 > getinum(void)                   /* scan a positive integer */
520   {
521      register int  n, lnext;
522  
# Line 505 | Line 531 | getinum()                      /* scan a positive integer */
531  
532  
533   double
534 < getnum()                        /* scan a positive float */
534 > getnum(void)                    /* scan a positive float */
535   {
536      register int  i, lnext;
537 <    char  str[MAXWORD+1];
537 >    char  str[RMAXWORD+1];
538  
539      i = 0;
540      lnext = nextc;
541 <    while (isdigit(lnext) && i < MAXWORD) {
541 >    while (isdigit(lnext) && i < RMAXWORD) {
542          str[i++] = lnext;
543          lnext = scan();
544      }
545 <    if (lnext == '.' && i < MAXWORD) {
545 >    if (lnext == '.' && i < RMAXWORD) {
546          str[i++] = lnext;
547          lnext = scan();
548          if (i == 1 && !isdigit(lnext))
549              syntax("badly formed number");
550 <        while (isdigit(lnext) && i < MAXWORD) {
550 >        while (isdigit(lnext) && i < RMAXWORD) {
551              str[i++] = lnext;
552              lnext = scan();
553          }
554      }
555 <    if ((lnext == 'e' | lnext == 'E') && i < MAXWORD) {
555 >    if ((lnext == 'e') | (lnext == 'E') && i < RMAXWORD) {
556          str[i++] = lnext;
557          lnext = scan();
558 <        if ((lnext == '-' | lnext == '+') && i < MAXWORD) {
558 >        if ((lnext == '-') | (lnext == '+') && i < RMAXWORD) {
559              str[i++] = lnext;
560              lnext = scan();
561          }
562          if (!isdigit(lnext))
563              syntax("missing exponent");
564 <        while (isdigit(lnext) && i < MAXWORD) {
564 >        while (isdigit(lnext) && i < RMAXWORD) {
565              str[i++] = lnext;
566              lnext = scan();
567          }
# Line 547 | Line 573 | getnum()                       /* scan a positive float */
573  
574  
575   EPNODE *
576 < getE1()                         /* E1 -> E1 ADDOP E2 */
576 > getE1(void)                             /* E1 -> E1 ADDOP E2 */
577                                  /*       E2 */
578   {
579      register EPNODE  *ep1, *ep2;
# Line 569 | Line 595 | getE1()                                /* E1 -> E1 ADDOP E2 */
595  
596  
597   EPNODE *
598 < getE2()                         /* E2 -> E2 MULOP E3 */
598 > getE2(void)                             /* E2 -> E2 MULOP E3 */
599                                  /*       E3 */
600   {
601      register EPNODE  *ep1, *ep2;
# Line 591 | Line 617 | getE2()                                /* E2 -> E2 MULOP E3 */
617  
618  
619   EPNODE *
620 < getE3()                         /* E3 -> E4 ^ E3 */
620 > getE3(void)                             /* E3 -> E4 ^ E3 */
621                                  /*       E4 */
622   {
623      register EPNODE  *ep1, *ep2;
# Line 613 | Line 639 | getE3()                                /* E3 -> E4 ^ E3 */
639  
640  
641   EPNODE *
642 < getE4()                         /* E4 -> ADDOP E5 */
642 > getE4(void)                             /* E4 -> ADDOP E5 */
643                                  /*       E5 */
644   {
645      register EPNODE  *ep1, *ep2;
# Line 641 | Line 667 | getE4()                                /* E4 -> ADDOP E5 */
667  
668  
669   EPNODE *
670 < getE5()                         /* E5 -> (E1) */
670 > getE5(void)                     /* E5 -> (E1) */
671                                  /*       VAR */
672                                  /*       NUM */
673                                  /*       $N */
674                                  /*       FUNC(E1,..) */
675                                  /*       ARG */
676   {
677 <    int  i;
678 <    char  *nam;
679 <    register EPNODE  *ep1, *ep2;
677 >        int      i;
678 >        char  *nam;
679 >        register EPNODE  *ep1, *ep2;
680  
681 <    if (nextc == '(') {
682 <        scan();
683 <        ep1 = getE1();
684 <        if (nextc != ')')
685 <            syntax("')' expected");
686 <        scan();
687 <        return(ep1);
688 <    }
681 >        if (nextc == '(') {
682 >                scan();
683 >                ep1 = getE1();
684 >                if (nextc != ')')
685 >                        syntax("')' expected");
686 >                scan();
687 >                return(ep1);
688 >        }
689  
690 <    if (esupport&E_INCHAN && nextc == '$') {
691 <        scan();
692 <        ep1 = newnode();
693 <        ep1->type = CHAN;
694 <        ep1->v.chan = getinum();
695 <        return(ep1);
696 <    }
690 >        if (esupport&E_INCHAN && nextc == '$') {
691 >                scan();
692 >                ep1 = newnode();
693 >                ep1->type = CHAN;
694 >                ep1->v.chan = getinum();
695 >                return(ep1);
696 >        }
697  
698 <  if (esupport&(E_VARIABLE|E_FUNCTION) &&
699 <                (isalpha(nextc) || nextc == CNTXMARK)) {
700 <      nam = getname();
701 <      ep1 = NULL;
702 <      if ((esupport&(E_VARIABLE|E_FUNCTION)) == (E_VARIABLE|E_FUNCTION)
703 <                        && curfunc != NULL)
704 <            for (i = 1, ep2 = curfunc->v.kid->sibling;
705 <                                ep2 != NULL; i++, ep2 = ep2->sibling)
706 <                if (!strcmp(ep2->v.name, nam)) {
707 <                    ep1 = newnode();
708 <                    ep1->type = ARG;
709 <                    ep1->v.chan = i;
710 <                    break;
698 >        if (esupport&(E_VARIABLE|E_FUNCTION) &&
699 >                        (isalpha(nextc) || nextc == CNTXMARK)) {
700 >                nam = getname();
701 >                ep1 = NULL;
702 >                if ((esupport&(E_VARIABLE|E_FUNCTION)) == (E_VARIABLE|E_FUNCTION)
703 >                                && curfunc != NULL)
704 >                        for (i = 1, ep2 = curfunc->v.kid->sibling;
705 >                                        ep2 != NULL; i++, ep2 = ep2->sibling)
706 >                                if (!strcmp(ep2->v.name, nam)) {
707 >                                        ep1 = newnode();
708 >                                        ep1->type = ARG;
709 >                                        ep1->v.chan = i;
710 >                                        break;
711 >                                }
712 >                if (ep1 == NULL) {
713 >                        ep1 = newnode();
714 >                        ep1->type = VAR;
715 >                        ep1->v.ln = varinsert(nam);
716                  }
717 <        if (ep1 == NULL) {
718 <            ep1 = newnode();
719 <            ep1->type = VAR;
720 <            ep1->v.ln = varinsert(nam);
717 >                if (esupport&E_FUNCTION && nextc == '(') {
718 >                        ep2 = newnode();
719 >                        ep2->type = FUNC;
720 >                        addekid(ep2, ep1);
721 >                        ep1 = ep2;
722 >                        do {
723 >                                scan();
724 >                                addekid(ep1, getE1());
725 >                        } while (nextc == ',');
726 >                        if (nextc != ')')
727 >                                syntax("')' expected");
728 >                        scan();
729 >                } else if (!(esupport&E_VARIABLE))
730 >                        syntax("'(' expected");
731 >                if (esupport&E_RCONST && isconstvar(ep1))
732 >                        ep1 = rconst(ep1);
733 >                return(ep1);
734          }
691        if (esupport&E_FUNCTION && nextc == '(') {
692            ep2 = newnode();
693            ep2->type = FUNC;
694            addekid(ep2, ep1);
695            ep1 = ep2;
696            do {
697                scan();
698                addekid(ep1, getE1());
699            } while (nextc == ',');
700            if (nextc != ')')
701                syntax("')' expected");
702            scan();
703        } else if (!(esupport&E_VARIABLE))
704            syntax("'(' expected");
705        if (esupport&E_RCONST && isconstvar(ep1))
706            ep1 = rconst(ep1);
707        return(ep1);
708    }
735  
736 <    if (isdecimal(nextc)) {
737 <        ep1 = newnode();
738 <        ep1->type = NUM;
739 <        ep1->v.num = getnum();
740 <        return(ep1);
741 <    }
742 <    syntax("unexpected character");
736 >        if (isdecimal(nextc)) {
737 >                ep1 = newnode();
738 >                ep1->type = NUM;
739 >                ep1->v.num = getnum();
740 >                return(ep1);
741 >        }
742 >        syntax("unexpected character");
743 >        return NULL; /* pro forma return */
744   }
745  
746  
747   EPNODE *
748 < rconst(epar)                    /* reduce a constant expression */
749 < register EPNODE  *epar;
748 > rconst(                 /* reduce a constant expression */
749 >    register EPNODE      *epar
750 > )
751   {
752      register EPNODE  *ep;
753  
# Line 736 | Line 764 | register EPNODE         *epar;
764  
765  
766   int
767 < isconstvar(ep)                  /* is ep linked to a constant expression? */
768 < register EPNODE  *ep;
767 > isconstvar(                     /* is ep linked to a constant expression? */
768 >    register EPNODE      *ep
769 > )
770   {
771      register EPNODE  *ep1;
772  
# Line 761 | Line 790 | register EPNODE         *ep;
790  
791  
792   int
793 < isconstfun(ep)                  /* is ep linked to a constant function? */
794 < register EPNODE  *ep;
793 > isconstfun(                     /* is ep linked to a constant function? */
794 >    register EPNODE      *ep
795 > )
796   {
797      register EPNODE  *dp;
798      register LIBR  *lp;
799  
800      if (ep->type != VAR)
801          return(0);
802 <    if ((dp = ep->v.ln->def) != NULL)
802 >    if ((dp = ep->v.ln->def) != NULL) {
803          if (dp->v.kid->type == FUNC)
804              return(dp->type == ':');
805          else
806              return(0);          /* don't identify masked library functions */
807 +    }
808      if ((lp = ep->v.ln->lib) != NULL)
809          return(lp->atyp == ':');
810      return(0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines