Refactoring of Signal class by adding a rate class.
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / compiler / boxes / ppbox.cpp
1 /************************************************************************
2 ************************************************************************
3 FAUST compiler
4 Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
5 ---------------------------------------------------------------------
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 ************************************************************************
20 ************************************************************************/
21
22
23
24 #include "list.hh"
25 #include "boxes.hh"
26 #include "ppbox.hh"
27 #include "signals.hh"
28 #include "prim2.hh"
29 #include "xtended.hh"
30
31
32 const char * prim0name(CTree *(*ptr) ())
33 {
34 return "prim0???";
35 }
36
37 const char * prim1name(CTree *(*ptr) (CTree *))
38 {
39 if (ptr == sigDelay1) return "mem";
40 if (ptr == sigIntCast) return "int";
41 if (ptr == sigFloatCast) return "float";
42 if (ptr == sigSerialize) return "serialize";
43
44 return "prim1???";
45 }
46
47 const char * prim2name(CTree *(*ptr) (CTree *, CTree *))
48 {
49 if (ptr == sigAdd) return "+";
50 if (ptr == sigSub) return "-";
51 if (ptr == sigMul) return "*";
52 if (ptr == sigDiv) return "/";
53 if (ptr == sigRem) return "%";
54
55 if (ptr == sigAND) return "&";
56 if (ptr == sigOR ) return "|";
57 if (ptr == sigXOR) return "^";
58
59 if (ptr == sigLeftShift ) return "<<";
60 if (ptr == sigRightShift) return ">>";
61
62 if (ptr == sigLT) return "< ";
63 if (ptr == sigLE) return "<=";
64 if (ptr == sigGT) return " >";
65 if (ptr == sigGE) return ">=";
66 if (ptr == sigEQ) return "==";
67 if (ptr == sigNE) return "!=";
68
69 if (ptr == sigFixDelay) return "@";
70 if (ptr == sigPrefix) return "prefix";
71 if (ptr == sigAttach) return "attach";
72
73 if (ptr == sigVectorize) return "vectorize";
74 if (ptr == sigVectorAt) return "[]";
75 if (ptr == sigConcat) return "#";
76
77
78
79
80 return "prim2???";
81 }
82
83 const char * prim3name(CTree *(*ptr) (CTree *, CTree *, CTree *))
84 {
85 if (ptr == sigReadOnlyTable) return "rdtable";
86 if (ptr == sigSelect2) return "selecttwo";
87 return "prim3???";
88 }
89
90 const char * prim4name(CTree *(*ptr) (CTree *, CTree *, CTree *, CTree *))
91 {
92 if (ptr == sigSelect3) return "selectthree";
93 return "prim4???";
94 }
95
96 const char * prim5name(CTree *(*ptr) (CTree *, CTree *, CTree *, CTree *, CTree *))
97 {
98 if (ptr == sigWriteReadTable) return "wrtable";
99 return "prim5???";
100 }
101
102
103 static void streambinop(ostream& fout, Tree t1, const char* op, Tree t2, int curPriority, int upPriority)
104 {
105 if (upPriority > curPriority) fout << '(';
106 fout << boxpp(t1,curPriority) << op << boxpp(t2,curPriority);
107 if (upPriority > curPriority) fout << ')';
108 }
109
110 static void printRule(ostream& fout, Tree rule)
111 {
112 Tree lhs = left(rule);
113 Tree rhs = right(rule);
114 char sep = '('; while (!isNil(lhs)) { fout << sep << boxpp(hd(lhs)); sep=','; lhs=tl(lhs); }
115 fout << ") => " << boxpp(rhs) << "; ";
116 }
117
118 /*****************************************************************************
119 affichage d'une expression box comme en entree
120 *****************************************************************************/
121
122 ostream& boxpp::print (ostream& fout) const
123 {
124 int i, id;
125 double r;
126 prim0 p0;
127 prim1 p1;
128 prim2 p2;
129 prim3 p3;
130 prim4 p4;
131 prim5 p5;
132
133 Tree t1, t2, t3, ff, label, cur, min, max, step, type, name, file, arg,
134 body, fun, args, abstr, genv, vis, lenv, ldef, slot,
135 ident, rules;
136
137 const char* str;
138
139 xtended* xt = (xtended*) getUserData(box);
140
141
142 // primitive elements
143 if (xt) fout << xt->name();
144 else if (isBoxInt(box, &i)) fout << i;
145 else if (isBoxReal(box, &r))
146 {
147 fout.setf( ios_base::fixed );
148 fout.precision(16);
149 fout << r;
150 //fout.setf( ios_base::fixed);
151 }
152
153 else if (isBoxCut(box)) fout << '!';
154 else if (isBoxWire(box)) fout << '_';
155 else if (isBoxIdent(box, &str)) fout << str;
156 else if (isBoxPrim0(box, &p0)) fout << prim0name(p0);
157 else if (isBoxPrim1(box, &p1)) fout << prim1name(p1);
158 else if (isBoxPrim2(box, &p2)) fout << prim2name(p2);
159 else if (isBoxPrim3(box, &p3)) fout << prim3name(p3);
160 else if (isBoxPrim4(box, &p4)) fout << prim4name(p4);
161 else if (isBoxPrim5(box, &p5)) fout << prim5name(p5);
162
163 else if (isBoxAbstr(box,arg,body)) fout << "\\" << boxpp(arg) << ".(" << boxpp(body) << ")";
164 else if (isBoxAppl(box, fun, args)) fout << boxpp(fun) << boxpp(args) ;
165
166 else if (isBoxWithLocalDef(box, body, ldef)) fout << boxpp(body) << " with { " << envpp(ldef) << " }";
167
168 // foreign elements
169 else if (isBoxFFun(box, ff)) fout << "ffunction(" << ffname(ff) << ')';
170 else if (isBoxFConst(box, type, name, file))
171 fout << "fconstant(" /*<< tree2str(type) */<< tree2str(name) << ')';
172 else if (isBoxFVar(box, type, name, file))
173 fout << "fvariable(" << tree2str(name) << ')';
174
175 // block diagram binary operator
176 else if (isBoxSeq(box, t1, t2)) streambinop(fout, t1, ":", t2, 1, priority);
177 else if (isBoxSplit(box, t1, t2)) streambinop(fout, t1, "<:", t2, 1, priority);
178 else if (isBoxMerge(box, t1, t2)) streambinop(fout, t1, ":>", t2, 1, priority);
179 else if (isBoxPar(box, t1, t2)) streambinop(fout, t1,",",t2, 2, priority);
180 else if (isBoxRec(box, t1, t2)) streambinop(fout, t1,"~",t2, 4, priority);
181
182 // iterative block diagram construction
183 else if (isBoxIPar(box, t1, t2, t3)) fout << "par(" << boxpp(t1) << ", " << boxpp(t2) << ") {" << boxpp(t3) << "}";
184 else if (isBoxISeq(box, t1, t2, t3)) fout << "seq(" << boxpp(t1) << ", " << boxpp(t2) << ") {" << boxpp(t3) << "}";
185 else if (isBoxISum(box, t1, t2, t3)) fout << "sum(" << boxpp(t1) << ", " << boxpp(t2) << ") {" << boxpp(t3) << "}";
186 else if (isBoxIProd(box, t1, t2, t3)) fout << "prod(" << boxpp(t1) << ", " << boxpp(t2) << ") {" << boxpp(t3) << "}";
187
188 // user interface
189 else if (isBoxButton(box, label)) fout << "button(" << tree2str(label) << ')';
190 else if (isBoxCheckbox(box, label)) fout << "checkbox(" << tree2str(label) << ')';
191 else if (isBoxVSlider(box, label, cur, min, max, step)) {
192 fout << "vslider("
193 << tree2str(label) << ", "
194 << boxpp(cur) << ", "
195 << boxpp(min) << ", "
196 << boxpp(max) << ", "
197 << boxpp(step)<< ')';
198 }
199 else if (isBoxHSlider(box, label, cur, min, max, step)) {
200 fout << "hslider("
201 << tree2str(label) << ", "
202 << boxpp(cur) << ", "
203 << boxpp(min) << ", "
204 << boxpp(max) << ", "
205 << boxpp(step)<< ')';
206 }
207 else if (isBoxVGroup(box, label, t1)) {
208 fout << "vgroup(" << tree2str(label) << ", " << boxpp(t1, 0) << ')';
209 }
210 else if (isBoxHGroup(box, label, t1)) {
211 fout << "hgroup(" << tree2str(label) << ", " << boxpp(t1, 0) << ')';
212 }
213 else if (isBoxTGroup(box, label, t1)) {
214 fout << "tgroup(" << tree2str(label) << ", " << boxpp(t1, 0) << ')';
215 }
216 else if (isBoxHBargraph(box, label, min, max)) {
217 fout << "hbargraph("
218 << tree2str(label) << ", "
219 << boxpp(min) << ", "
220 << boxpp(max) << ')';
221 }
222 else if (isBoxVBargraph(box, label, min, max)) {
223 fout << "vbargraph("
224 << tree2str(label) << ", "
225 << boxpp(min) << ", "
226 << boxpp(max) << ')';
227 }
228 else if (isBoxNumEntry(box, label, cur, min, max, step)) {
229 fout << "nentry("
230 << tree2str(label) << ", "
231 << boxpp(cur) << ", "
232 << boxpp(min) << ", "
233 << boxpp(max) << ", "
234 << boxpp(step)<< ')';
235 }
236 else if (isNil(box)) {
237 fout << "()" ;
238 }
239 else if (isList(box)) {
240
241 Tree l = box;
242 char sep = '(';
243
244 do {
245 fout << sep << boxpp(hd(l));
246 sep = ',';
247 l = tl(l);
248 } while (isList(l));
249
250 fout << ')';
251
252 }
253 else if (isBoxEnvironment(box)) {
254 fout << "environment";
255 }
256 else if (isClosure(box, abstr, genv, vis, lenv)) {
257 fout << "closure[" << boxpp(abstr)
258 << ", genv = " << envpp(genv)
259 << ", lenv = " << envpp(lenv)
260 << "]";
261 }
262 else if (isBoxComponent(box, label)) {
263 fout << "component("
264 << tree2str(label) << ')';
265 }
266 else if (isBoxAccess(box, t1, t2)) {
267 fout << boxpp(t1) << '.' << boxpp(t2);
268 }
269 else if (isImportFile(box, label)) {
270 fout << "import("
271 << tree2str(label) << ')';
272 }
273 else if (isBoxSlot(box, &id)) {
274 fout << "#" << id;
275 }
276 else if (isBoxSymbolic(box, slot, body)) {
277 fout << "[" << boxpp(slot) << ">" << boxpp(body) << "]";
278 }
279
280 // Pattern Matching Extensions
281 else if (isBoxCase(box, rules)) {
282 fout << "case {";
283 while (!isNil(rules)) { printRule(fout, hd(rules)); rules = tl(rules); }
284 fout << "}";
285 }
286 #if 1
287 // more useful for debugging output
288 else if (isBoxPatternVar(box, ident)) {
289 fout << "<" << boxpp(ident) << ">";
290 }
291 #else
292 // beautify messages involving lhs patterns
293 else if (isBoxPatternVar(box, ident)) {
294 fout << boxpp(ident);
295 }
296 #endif
297
298 else if (isBoxPatternMatcher(box)) {
299 fout << "PM[" << box << "]";
300 }
301
302 else if (isBoxError(box)) {
303 fout << "ERROR";
304 }
305
306
307 // None of the previous tests succeded, then it is not a valid box
308 else {
309 cerr << "Error in box::print() : " << *box << " is not a valid box" << endl;
310 exit(1);
311 }
312
313 return fout;
314 }
315
316
317 /*****************************************************************************
318 affichage d'un environnement
319 *****************************************************************************/
320
321 ostream& envpp::print (ostream& fout) const
322 {
323 const char* sep = "";
324 Tree l = fEnv;
325
326 fout << '{';
327 while (isList(l)) {
328 fout << sep << boxpp(hd(hd(l))) << "=" << boxpp(tl(hd(l)));
329 sep = ", ";
330 l = tl(l);
331 }
332 fout << '}';
333 return fout;
334 }
335