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

Comparing ray/src/common/calprnt.c (file contents):
Revision 2.6 by greg, Wed Oct 24 00:39:09 2012 UTC vs.
Revision 2.7 by greg, Fri Jul 30 00:22:32 2021 UTC

# Line 8 | Line 8 | static const char      RCSid[] = "$Id$";
8   #include "copyright.h"
9  
10   #include  <stdio.h>
11 + #include  <string.h>
12  
13   #include  "rterror.h"
14   #include  "calcomp.h"
15  
16  
17 + /* is child binary operation lower precedence than parent? */
18 + static int
19 + lower_precedent_binop(int typ, EPNODE *ekid)
20 + {
21 +    if (ekid == NULL)
22 +        return(0);
23 +    switch (typ) {
24 +        case '+':
25 +                return(0);
26 +        case '-':
27 +                return(ekid->type == '+');
28 +        case '*':
29 +                return(strchr("+-", ekid->type) != NULL);
30 +        case '/':
31 +                return(strchr("+-*", ekid->type) != NULL);
32 +        case '^':
33 +                return(strchr("+-*/^", ekid->type) != NULL);
34 +        }
35 +    return(0);                  /* child not binary op */
36 + }
37 +
38   void
39   eprint(                         /* print a parse tree */
40          EPNODE  *ep,
# Line 22 | Line 44 | eprint(                                /* print a parse tree */
44      static EPNODE  *curdef = NULL;
45      EPNODE  *ep1 = NULL;
46  
47 <    switch (ep->type) {
47 >    switch (ep==NULL ? -1 : ep->type) {
48  
49          case VAR:
50              fputs(ep->v.ln->name, fp);
# Line 57 | Line 79 | eprint(                                /* print a parse tree */
79              fprintf(fp, "%.9g", ep->v.num);
80              break;
81  
60        case UMINUS:
61            fputc('-', fp);
62            eprint(ep->v.kid, fp);
63            break;
64
82          case CHAN:
83              fprintf(fp, "$%d", ep->v.chan);
84              break;
# Line 77 | Line 94 | eprint(                                /* print a parse tree */
94              eprint(ep->v.kid->sibling, fp);
95              curdef = ep1;
96              break;
97 <            
97 >
98 >        case UMINUS:
99 >            fputc('-', fp);
100 >            if (ep->v.kid != NULL && strchr("+-*/^", ep->v.kid->type)) {
101 >                fputc('(', fp);
102 >                eprint(ep->v.kid, fp);
103 >                fputc(')', fp);
104 >            } else
105 >                eprint(ep->v.kid, fp);
106 >            break;
107 >
108          case '+':
109          case '-':
110          case '*':
111          case '/':
112          case '^':
113 <            fputc('(', fp);
114 <            eprint(ep->v.kid, fp);
115 <            fputc(' ', fp);
116 <            fputc(ep->type, fp);
117 <            fputc(' ', fp);
118 <            eprint(ep->v.kid->sibling, fp);
119 <            fputc(')', fp);
113 >            if (lower_precedent_binop(ep->type, ep->v.kid)) {
114 >                fputc('(', fp);
115 >                eprint(ep->v.kid, fp);
116 >                fputc(')', fp);
117 >            } else
118 >                eprint(ep->v.kid, fp);
119 >            if (ep->type != '^') {
120 >                fputc(' ', fp);
121 >                fputc(ep->type, fp);
122 >                fputc(' ', fp);
123 >            } else
124 >                fputc(ep->type, fp);
125 >            if (lower_precedent_binop(ep->type, ep->v.kid->sibling)) {
126 >                fputc('(', fp);
127 >                eprint(ep->v.kid->sibling, fp);
128 >                fputc(')', fp);
129 >            } else
130 >                eprint(ep->v.kid->sibling, fp);
131              break;
132  
133          default:
# Line 97 | Line 135 | eprint(                                /* print a parse tree */
135              quit(1);
136  
137      }
100
138   }
139  
140  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines