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 1.3 by greg, Wed Jun 14 22:29:01 1989 UTC vs.
Revision 2.5 by greg, Tue May 19 20:21:58 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 27 | Line 27 | static char SCCSid[] = "$SunId$ LBL";
27   #include  "calcomp.h"
28  
29   #define  MAXLINE        256             /* maximum line length */
30 #define  MAXWORD        64              /* maximum word length */
30  
31   #define  newnode()      (EPNODE *)ecalloc(1, sizeof(EPNODE))
32  
34 #define  isid(c)        (isalnum(c) || (c) == '_' || (c) == '.')
35
33   #define  isdecimal(c)   (isdigit(c) || (c) == '.')
34  
35 < extern double  atof(), pow();
35 > #ifndef atof
36 > extern double  atof();
37 > #endif
38 > extern double  pow();
39   extern char  *fgets(), *savestr();
40   extern char  *emalloc(), *ecalloc();
41   extern EPNODE  *curfunc;
42 < extern double  efunc(), evariable(), enumber(), euminus(), echannel();
43 < extern double  eargument(), eadd(), esubtr(), emult(), edivi(), epow();
44 < extern double  ebotch();
42 > extern double  efunc(), evariable();
43 > static double  euminus(), echannel(), eargument(), enumber();
44 > static double  eadd(), esubtr(), emult(), edivi(), epow();
45 > static double  ebotch();
46   extern int  errno;
47  
48   int  nextc;                             /* lookahead character */
# Line 77 | Line 78 | double  (*eoper[])() = {               /* expression operations */
78          esubtr,
79          0,
80          edivi,
81 <        0,0,0,0,0,0,0,0,0,0,0,0,0,
81 >        0,0,0,0,0,0,0,0,0,0,
82          ebotch,
83 +        0,0,
84 +        ebotch,
85          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
86          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
87          epow,
88   };
89  
87 static char  *infile;                   /* input file name */
90   static FILE  *infp;                     /* input file pointer */
91   static char  *linbuf;                   /* line buffer */
92 + static char  *infile;                   /* input file name */
93 + static int  lineno;                     /* input line number */
94   static int  linepos;                    /* position in buffer */
95  
96  
# Line 96 | Line 100 | char  *expr;
100   {
101      EPNODE  *ep;
102  
103 <    initstr(NULL, expr);
103 >    initstr(expr, NULL, 0);
104   #if  defined(VARIABLE) && defined(FUNCTION)
105      curfunc = NULL;
106   #endif
# Line 132 | Line 136 | register EPNODE  *epar;
136          case VAR:
137              varfree(epar->v.ln);
138              break;
135 #endif
139              
140          case SYM:
141              freestr(epar->v.name);
142              break;
143 + #endif
144  
145          case NUM:
146          case CHAN:
# Line 291 | Line 295 | register EPNODE  *ep;
295   }
296  
297  
298 < initfile(file, fp)              /* prepare input file */
295 < char  *file;
298 > initfile(fp, fn, ln)            /* prepare input file */
299   FILE  *fp;
300 + char  *fn;
301 + int  ln;
302   {
303      static char  inpbuf[MAXLINE];
304  
300    infile = file;
305      infp = fp;
306      linbuf = inpbuf;
307 +    infile = fn;
308 +    lineno = ln;
309      linepos = 0;
310      inpbuf[0] = '\0';
311      scan();
312   }
313  
314  
315 < initstr(file, s)                /* prepare input string */
310 < char  *file;
315 > initstr(s, fn, ln)              /* prepare input string */
316   char  *s;
317 + char  *fn;
318 + int  ln;
319   {
313    infile = file;
320      infp = NULL;
321 +    infile = fn;
322 +    lineno = ln;
323      linbuf = s;
324      linepos = 0;
325      scan();
326   }
327  
328  
329 < scan()                          /* scan next character */
329 > getscanpos(fnp, lnp, spp, fpp)  /* return current scan position */
330 > char  **fnp;
331 > int  *lnp;
332 > char  **spp;
333 > FILE  **fpp;
334   {
335 +    if (fnp != NULL) *fnp = infile;
336 +    if (lnp != NULL) *lnp = lineno;
337 +    if (spp != NULL) *spp = linbuf+linepos;
338 +    if (fpp != NULL) *fpp = infp;
339 + }
340 +
341 +
342 + int
343 + scan()                          /* scan next character, return literal next */
344 + {
345 +    register int  lnext = 0;
346 +
347      do {
348          if (linbuf[linepos] == '\0')
349              if (infp == NULL || fgets(linbuf, MAXLINE, infp) == NULL)
350                  nextc = EOF;
351              else {
352                  nextc = linbuf[0];
353 +                lineno++;
354                  linepos = 1;
355              }
356          else
357              nextc = linbuf[linepos++];
358 +        if (!lnext)
359 +                lnext = nextc;
360          if (nextc == '{') {
361              scan();
362              while (nextc != '}')
# Line 340 | Line 367 | scan()                         /* scan next character */
367              scan();
368          }
369      } while (isspace(nextc));
370 +    return(lnext);
371   }
372  
373  
374 + char *
375 + ltoa(l)                         /* convert long to ascii */
376 + long  l;
377 + {
378 +    static char  buf[16];
379 +    register char  *cp;
380 +    int  neg = 0;
381 +
382 +    if (l == 0)
383 +        return("0");
384 +    if (l < 0) {
385 +        l = -l;
386 +        neg++;
387 +    }
388 +    cp = buf + sizeof(buf);
389 +    *--cp = '\0';
390 +    while (l) {
391 +        *--cp = l % 10 + '0';
392 +        l /= 10;
393 +    }
394 +    if (neg)
395 +        *--cp = '-';
396 +    return(cp);
397 + }
398 +
399 +
400   syntax(err)                     /* report syntax error and quit */
401   char  *err;
402   {
403      register int  i;
404  
405 +    if (infile != NULL || lineno != 0) {
406 +        if (infile != NULL) eputs(infile);
407 +        if (lineno != 0) {
408 +            eputs(infile != NULL ? ", line " : "line ");
409 +            eputs(ltoa((long)lineno));
410 +        }
411 +        eputs(": syntax error:\n");
412 +    }
413      eputs(linbuf);
414 <    if (linbuf[0] == '\0' || linbuf[strlen(linbuf)-1] != '\n')
414 >    if (linbuf[strlen(linbuf)-1] != '\n')
415          eputs("\n");
416      for (i = 0; i < linepos-1; i++)
417          eputs(linbuf[i] == '\t' ? "\t" : " ");
418      eputs("^ ");
357    if (infile != NULL) {
358        eputs(infile);
359        eputs(": ");
360    }
419      eputs(err);
420      eputs("\n");
421      quit(1);
# Line 379 | Line 437 | EPNODE  *ekid;
437   }
438  
439  
440 + #if  defined(VARIABLE) || defined(FUNCTION)
441   char *
442   getname()                       /* scan an identifier */
443   {
444      static char  str[MAXWORD+1];
445 <    register int  i;
445 >    register int  i, lnext;
446  
447 <    for (i = 0; i < MAXWORD && isid(nextc); i++, scan())
448 <        str[i] = nextc;
447 >    lnext = nextc;
448 >    for (i = 0; i < MAXWORD && isid(lnext); i++, lnext = scan())
449 >        str[i] = lnext;
450      str[i] = '\0';
451 +    while (isid(lnext))         /* skip rest of name */
452 +        lnext = scan();
453  
454      return(str);
455   }
456 + #endif
457  
458  
459   int
460   getinum()                       /* scan a positive integer */
461   {
462 <    register int  n;
462 >    register int  n, lnext;
463  
464      n = 0;
465 <    while (isdigit(nextc)) {
466 <        n = n * 10 + nextc - '0';
467 <        scan();
465 >    lnext = nextc;
466 >    while (isdigit(lnext)) {
467 >        n = n * 10 + lnext - '0';
468 >        lnext = scan();
469      }
470      return(n);
471   }
# Line 410 | Line 474 | getinum()                      /* scan a positive integer */
474   double
475   getnum()                        /* scan a positive float */
476   {
477 <    register int  i;
477 >    register int  i, lnext;
478      char  str[MAXWORD+1];
479  
480      i = 0;
481 <    while (isdigit(nextc) && i < MAXWORD) {
482 <        str[i++] = nextc;
483 <        scan();
481 >    lnext = nextc;
482 >    while (isdigit(lnext) && i < MAXWORD) {
483 >        str[i++] = lnext;
484 >        lnext = scan();
485      }
486 <    if (nextc == '.' && i < MAXWORD) {
487 <        str[i++] = nextc;
488 <        scan();
489 <        while (isdigit(nextc) && i < MAXWORD) {
490 <            str[i++] = nextc;
491 <            scan();
486 >    if (lnext == '.' && i < MAXWORD) {
487 >        str[i++] = lnext;
488 >        lnext = scan();
489 >        while (isdigit(lnext) && i < MAXWORD) {
490 >            str[i++] = lnext;
491 >            lnext = scan();
492          }
493      }
494 <    if ((nextc == 'e' || nextc == 'E') && i < MAXWORD) {
495 <        str[i++] = nextc;
496 <        scan();
497 <        if ((nextc == '-' || nextc == '+') && i < MAXWORD) {
498 <            str[i++] = nextc;
499 <            scan();
494 >    if ((lnext == 'e' || lnext == 'E') && i < MAXWORD) {
495 >        str[i++] = lnext;
496 >        lnext = scan();
497 >        if ((lnext == '-' || lnext == '+') && i < MAXWORD) {
498 >            str[i++] = lnext;
499 >            lnext = scan();
500          }
501 <        while (isdigit(nextc) && i < MAXWORD) {
502 <            str[i++] = nextc;
503 <            scan();
501 >        while (isdigit(lnext) && i < MAXWORD) {
502 >            str[i++] = lnext;
503 >            lnext = scan();
504          }
505      }
506      str[i] = '\0';
# Line 491 | Line 556 | getE2()                                /* E2 -> E2 MULOP E3 */
556  
557  
558   EPNODE *
559 < getE3()                         /* E3 -> E3 ^ E4 */
559 > getE3()                         /* E3 -> E4 ^ E3 */
560                                  /*       E4 */
561   {
562      register EPNODE  *ep1, *ep2;
563  
564      ep1 = getE4();
565 <    while (nextc == '^') {
565 >    if (nextc == '^') {
566          ep2 = newnode();
567          ep2->type = nextc;
568          scan();
569          addekid(ep2, ep1);
570 <        addekid(ep2, getE4());
570 >        addekid(ep2, getE3());
571   #ifdef  RCONST
572          if (ep1->type == NUM && ep1->sibling->type == NUM)
573                  ep2 = rconst(ep2);
574   #endif
575 <        ep1 = ep2;
575 >        return(ep2);
576      }
577      return(ep1);
578   }
# Line 526 | Line 591 | getE4()                                /* E4 -> ADDOP E5 */
591                  ep2->v.num = -ep2->v.num;
592                  return(ep2);
593          }
594 +        if (ep2->type == UMINUS) {      /* don't generate -(-E5) */
595 +            efree((char *)ep2);
596 +            return(ep2->v.kid);
597 +        }
598          ep1 = newnode();
599          ep1->type = UMINUS;
600          addekid(ep1, ep2);
# Line 546 | Line 615 | getE5()                                /* E5 -> (E1) */
615                                  /*       ARG */
616   {
617      int  i;
618 +    char  *nam;
619      register EPNODE  *ep1, *ep2;
620  
621      if (nextc == '(') {
# Line 568 | Line 638 | getE5()                                /* E5 -> (E1) */
638   #endif
639  
640   #if  defined(VARIABLE) || defined(FUNCTION)
641 <    if (isalpha(nextc)) {
642 <        ep1 = newnode();
573 <        ep1->type = VAR;
574 <        ep1->v.ln = varinsert(getname());
575 <
641 >    if (isalpha(nextc) || nextc == CNTXMARK) {
642 >        nam = getname();
643   #if  defined(VARIABLE) && defined(FUNCTION)
644 +        ep1 = NULL;
645          if (curfunc != NULL)
646              for (i = 1, ep2 = curfunc->v.kid->sibling;
647                                  ep2 != NULL; i++, ep2 = ep2->sibling)
648 <                if (!strcmp(ep2->v.name, ep1->v.ln->name)) {
581 <                    epfree(ep1);
648 >                if (!strcmp(ep2->v.name, nam)) {
649                      ep1 = newnode();
650                      ep1->type = ARG;
651                      ep1->v.chan = i;
652                      break;
653                  }
654 +        if (ep1 == NULL)
655   #endif
656 +        {
657 +            ep1 = newnode();
658 +            ep1->type = VAR;
659 +            ep1->v.ln = varinsert(nam);
660 +        }
661   #ifdef  FUNCTION
662          if (nextc == '(') {
663              ep2 = newnode();
# Line 604 | Line 677 | getE5()                                /* E5 -> (E1) */
677              syntax("'(' expected");
678   #endif
679   #endif
680 + #ifdef  RCONST
681 +        if (isconstvar(ep1))
682 +            ep1 = rconst(ep1);
683 + #endif
684          return(ep1);
685      }
686   #endif
# Line 635 | Line 712 | register EPNODE  *epar;
712  
713      return(ep);
714   }
715 +
716 +
717 + isconstvar(ep)                  /* is ep linked to a constant expression? */
718 + register EPNODE  *ep;
719 + {
720 + #ifdef  VARIABLE
721 +    register EPNODE  *ep1;
722 + #ifdef  FUNCTION
723 +
724 +    if (ep->type == FUNC) {
725 +        if (!isconstfun(ep->v.kid))
726 +                return(0);
727 +        for (ep1 = ep->v.kid->sibling; ep1 != NULL; ep1 = ep1->sibling)
728 +            if (ep1->type != NUM && !isconstfun(ep1))
729 +                return(0);
730 +        return(1);
731 +    }
732 + #endif
733 +    if (ep->type != VAR)
734 +        return(0);
735 +    ep1 = ep->v.ln->def;
736 +    if (ep1 == NULL || ep1->type != ':')
737 +        return(0);
738 + #ifdef  FUNCTION
739 +    if (ep1->v.kid->type != SYM)
740 +        return(0);
741 + #endif
742 +    return(1);
743 + #else
744 +    return(ep->type == FUNC);
745 + #endif
746 + }
747 +
748 +
749 + #if  defined(FUNCTION) && defined(VARIABLE)
750 + isconstfun(ep)                  /* is ep linked to a constant function? */
751 + register EPNODE  *ep;
752 + {
753 +    register EPNODE  *dp;
754 +    register LIBR  *lp;
755 +
756 +    if (ep->type != VAR)
757 +        return(0);
758 +    if ((dp = ep->v.ln->def) != NULL && dp->v.kid->type == FUNC)
759 +        return(dp->type == ':');
760 +    if ((lp = ep->v.ln->lib) != NULL)
761 +        return(lp->atyp == ':');
762 +    return(0);
763 + }
764 + #endif
765   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines