ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/calprnt.c
Revision: 2.9
Committed: Sat Feb 24 19:00:23 2024 UTC (2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.8: +19 -17 lines
Log Message:
perf: Flatten hierarchies for commutative & associative operators, '+' and '*'

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: calprnt.c,v 2.8 2024/02/23 03:47:57 greg Exp $";
3 #endif
4 /*
5 * calprint.c - routines for printing calcomp expressions.
6 */
7
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 operation lower precedence than parent? */
18 static int
19 lower_precedent_op(int typ, EPNODE *ek)
20 {
21 if (ek == NULL)
22 return(0);
23 switch (typ) {
24 case '+':
25 return(0);
26 case '-':
27 return(ek->type == '+');
28 case '*':
29 return(strchr("+-", ek->type) != NULL);
30 case '/':
31 return(strchr("+-*", ek->type) != NULL);
32 case '^':
33 return(strchr("+-*/^", ek->type) != NULL);
34 }
35 return(0); /* child not binary op */
36 }
37
38 void
39 eprint( /* print a parse tree */
40 EPNODE *ep,
41 FILE *fp
42 )
43 {
44 static EPNODE *curdef = NULL;
45 EPNODE *ep1 = NULL;
46
47 switch (ep==NULL ? -1 : ep->type) {
48
49 case VAR:
50 fputs(ep->v.ln->name, fp);
51 break;
52
53 case SYM:
54 fputs(ep->v.name, fp);
55 break;
56
57 case FUNC:
58 eprint(ep->v.kid, fp);
59 fputc('(', fp);
60 ep1 = ep->v.kid->sibling;
61 while (ep1 != NULL) {
62 eprint(ep1, fp);
63 if ((ep1 = ep1->sibling) != NULL)
64 fputs(", ", fp);
65 }
66 fputc(')', fp);
67 break;
68
69 case ARG:
70 if (curdef == NULL || curdef->v.kid->type != FUNC ||
71 (ep1 = ekid(curdef->v.kid, ep->v.chan)) == NULL) {
72 eputs("Bad argument!\n");
73 quit(1);
74 }
75 eprint(ep1, fp);
76 break;
77
78 case NUM:
79 fprintf(fp, "%.9g", ep->v.num);
80 break;
81
82 case CHAN:
83 fprintf(fp, "$%d", ep->v.chan);
84 break;
85
86 case '=':
87 case ':':
88 ep1 = curdef;
89 curdef = ep;
90 eprint(ep->v.kid, fp);
91 fputc(' ', fp);
92 fputc(ep->type, fp);
93 fputc(' ', fp);
94 eprint(ep->v.kid->sibling, fp);
95 curdef = ep1;
96 break;
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 if (lower_precedent_op(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 for (ep1 = ep->v.kid->sibling; ep1 != NULL; ep1 = ep1->sibling) {
120 if (ep->type != '^') {
121 fputc(' ', fp);
122 fputc(ep->type, fp);
123 fputc(' ', fp);
124 } else
125 fputc(ep->type, fp);
126 if (lower_precedent_op(ep->type, ep1)) {
127 fputc('(', fp);
128 eprint(ep1, fp);
129 fputc(')', fp);
130 } else
131 eprint(ep1, fp);
132 }
133 break;
134
135 default:
136 eputs("Bad expression!\n");
137 quit(1);
138
139 }
140 }
141
142
143 void
144 dprint( /* print a definition (all if no name) */
145 char *name,
146 FILE *fp
147 )
148 {
149 EPNODE *ep;
150
151 if (name == NULL)
152 for (ep = dfirst(); ep != NULL; ep = dnext()) {
153 eprint(ep, fp);
154 fputs(";\n", fp);
155 }
156 else if ((ep = dlookup(name)) != NULL) {
157 eprint(ep, fp);
158 fputs(";\n", fp);
159 } else {
160 wputs(name);
161 wputs(": undefined\n");
162 }
163 }