Initial import.
[Faustine.git] / interpretor / faust-0.9.47mr3 / compiler / signals / ppsig.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 "ppsig.hh"
25 #include "binop.hh"
26 #include "prim2.hh"
27 #include "xtended.hh"
28 #include "recursivness.hh"
29
30 ostream& ppsig::printinfix (ostream& fout, const string& opname, int priority, Tree x, Tree y) const
31 {
32 if (fPriority > priority) fout << "(";
33 fout << ppsig(x,fEnv,priority) << opname << ppsig(y,fEnv,priority);
34 if (fPriority > priority) fout << ")";
35 return fout;
36 }
37
38 ostream& ppsig::printfun (ostream& fout, const string& funame, Tree x) const
39 {
40 return fout << funame << '(' << ppsig(x,fEnv) << ')';
41 }
42
43 ostream& ppsig::printfun (ostream& fout, const string& funame, Tree x, Tree y) const
44 {
45 return fout << funame << '(' << ppsig(x,fEnv) << ',' << ppsig(y,fEnv) << ')';
46 }
47
48 ostream& ppsig::printfun (ostream& fout, const string& funame, Tree x, Tree y, Tree z) const
49 {
50 return fout << funame << '(' << ppsig(x,fEnv) << ',' << ppsig(y,fEnv) << ',' << ppsig(z,fEnv) << ')';
51 }
52
53 ostream& ppsig::printfun (ostream& fout, const string& funame, Tree x, Tree y, Tree z, Tree zz) const
54 {
55 return fout << funame << '(' << ppsig(x,fEnv) << ',' << ppsig(y,fEnv) << ',' << ppsig(z,fEnv) << ',' << ppsig(zz,fEnv) << ')';
56 }
57
58 ostream& ppsig::printfun (ostream& fout, const string& funame, Tree x, Tree y, Tree z, Tree z2, Tree z3) const
59 {
60 return fout << funame << '(' << ppsig(x,fEnv) << ',' << ppsig(y,fEnv) << ',' << ppsig(z,fEnv) << ',' << ppsig(z2,fEnv) << ',' << ppsig(z3,fEnv) << ')';
61 }
62
63 ostream& ppsig::printui (ostream& fout, const string& funame, Tree label) const
64 {
65 fout << funame << '(';
66 printlabel(fout, label);
67 return fout << ')';
68 }
69
70 ostream& ppsig::printui (ostream& fout, const string& funame, Tree label, Tree lo, Tree hi, Tree step) const
71 {
72 fout << funame << '(';
73 printlabel(fout, label);
74 return fout
75 << ',' << ppsig(lo,fEnv)
76 << ',' << ppsig(hi,fEnv)
77 << ',' << ppsig(step,fEnv)
78 << ')';
79 }
80
81 ostream& ppsig::printui (ostream& fout, const string& funame, Tree label, Tree cur, Tree lo, Tree hi, Tree step) const
82 {
83 fout << funame << '(';
84 printlabel(fout, label);
85 return fout
86 << ',' << ppsig(cur,fEnv)
87 << ',' << ppsig(lo,fEnv)
88 << ',' << ppsig(hi,fEnv)
89 << ',' << ppsig(step,fEnv)
90 << ')';
91 }
92
93 ostream& ppsig::printout (ostream& fout, int i, Tree x) const
94 {
95 if (fPriority > 0) fout << "(";
96 fout << "OUT" << i << " = " << ppsig(x, fEnv, 0);
97 if (fPriority > 0) fout << ")";
98 return fout;
99 }
100
101 ostream& ppsig::printlabel (ostream& fout, Tree pathname) const
102 {
103 fout << *hd(pathname);
104 pathname = tl(pathname);
105 while (!isNil(pathname)) {
106 fout << '/' << *tl(hd(pathname));
107 pathname = tl(pathname);
108 }
109 return fout;
110 }
111
112 ostream& ppsig::printlist (ostream& fout, Tree largs) const
113 {
114 string sep = "";
115 fout << '(';
116 while (!isNil(largs)) {
117 fout << sep << ppsig(hd(largs), fEnv);
118 sep = ", ";
119 largs = tl(largs);
120 }
121 fout << ')';
122 return fout;
123 }
124
125 ostream& ppsig::printff (ostream& fout, Tree ff, Tree largs) const
126 {
127 fout << ffname(ff); printlist(fout, largs);
128 return fout;
129 }
130
131 ostream& ppsig::printFixDelay (ostream& fout, Tree exp, Tree delay) const
132 {
133 int d;
134
135 if (isSigInt(delay, &d) && (d==1)) {
136 fout << ppsig(exp,fEnv,8) << "'";
137 } else {
138 printinfix(fout, "@", 8, exp, delay);
139 }
140 return fout;
141 }
142
143 // else if ( isSigFixDelay(sig, x, y) ) { printinfix(fout, "@", 8, x, y); }
144
145 ostream& ppsig::printrec (ostream& fout, Tree var, Tree lexp, bool hide) const
146 {
147 if (isElement(var, fEnv) ) {
148 fout << *var;
149 } else if (hide) {
150 fout << *var;
151 } else {
152 fout << "letrec(" << *var << " = " << ppsig(lexp, addElement(var, fEnv)) << ")";
153 }
154 return fout;
155 }
156
157 ostream& ppsig::printrec (ostream& fout, Tree lexp, bool hide) const
158 {
159 fout << "debruijn(" << ppsig(lexp,fEnv) << ")";
160 return fout;
161 }
162
163 ostream& ppsig::printextended (ostream& fout, Tree sig) const
164 {
165 string sep = "";
166 xtended* p = (xtended*) getUserData(sig);
167
168 fout << p->name() << '(';
169 for (int i = 0; i < sig->arity(); i++) {
170 fout << sep << ppsig(sig->branch(i), fEnv);
171 sep = ", ";
172 }
173 fout << ')';
174 return fout;
175 }
176
177
178 ostream& ppsig::print (ostream& fout) const
179 {
180 int i;
181 double r;
182 Tree c, sel, x, y, z, u, var, le, label, id, ff, largs, type, name, file;
183
184 if ( isList(sig) ) { printlist(fout, sig); }
185 else if ( isProj(sig, &i, x) ) { fout << "proj" << i << '(' << ppsig(x, fEnv) << ')'; }
186 else if ( isRec(sig, var, le) ) { printrec(fout, var, le, fHideRecursion /*&& (getRecursivness(sig)==0)*/ ); }
187
188 // debruinj notation
189 else if ( isRec(sig, le) ) { printrec(fout, le, fHideRecursion ); }
190 else if ( isRef(sig, i) ) { fout << "REF[" << i << "]"; }
191
192 else if ( getUserData(sig) ) { printextended(fout, sig); }
193 else if ( isSigInt(sig, &i) ) { fout << i; }
194 else if ( isSigReal(sig, &r) ) { fout << r; }
195 else if ( isSigInput(sig, &i) ) { fout << "IN[" << i << "]"; }
196 else if ( isSigOutput(sig, &i, x) ) { printout(fout, i, x) ; }
197
198 else if ( isSigDelay1(sig, x) ) { fout << ppsig(x, fEnv, 9) << "'"; }
199 //else if ( isSigFixDelay(sig, x, y) ) { printinfix(fout, "@", 8, x, y); }
200 else if ( isSigFixDelay(sig, x, y) ) { printFixDelay(fout, x, y); }
201 else if ( isSigPrefix(sig, x, y) ) { printfun(fout, "prefix", x, y); }
202 else if ( isSigIota(sig, x) ) { printfun(fout, "iota", x); }
203 else if ( isSigBinOp(sig, &i, x, y) ) { printinfix(fout, gBinOpTable[i]->fName, gBinOpTable[i]->fPriority, x, y); }
204 else if ( isSigFFun(sig, ff, largs) ) { printff(fout, ff, largs); }
205 else if ( isSigFConst(sig, type, name, file) ) { fout << tree2str(name); }
206 else if ( isSigFVar(sig, type, name, file) ) { fout << tree2str(name); }
207
208 else if ( isSigTable(sig, id, x, y) ) { printfun(fout, "TABLE", x, y); }
209 else if ( isSigWRTbl(sig, id, x, y, z) ) { printfun(fout, "write", x, y, z); }
210 else if ( isSigRDTbl(sig, x, y) ) { printfun(fout, "read", x, y); }
211 else if ( isSigGen(sig, x) ) { fout << ppsig(x, fEnv, fPriority); }
212
213 else if ( isSigDocConstantTbl(sig, x, y) ) { printfun(fout, "docConstantTbl", x, y); }
214 else if ( isSigDocWriteTbl(sig, x, y, z, u) ) { printfun(fout, "docWriteTbl", x, y, z, u); }
215 else if ( isSigDocAccessTbl(sig, x, y) ) { printfun(fout, "docAccessTbl", x, y); }
216
217 else if ( isSigSelect2(sig, sel, x, y) ) { printfun(fout, "select2", sel, x, y); }
218 else if ( isSigSelect3(sig, sel, x, y, z) ) { printfun(fout, "select3", sel, x, y, z); }
219
220 else if ( isSigIntCast(sig, x) ) { printfun(fout, "int", x); }
221 else if ( isSigFloatCast(sig, x) ) { printfun(fout, "float", x); }
222
223 else if ( isSigButton(sig, label) ) { printui(fout, "button", label); }
224 else if ( isSigCheckbox(sig, label) ) { printui(fout, "checkbox", label); }
225 else if ( isSigVSlider(sig, label,c,x,y,z) ) { printui(fout, "vslider", label, c, x, y, z); }
226 else if ( isSigHSlider(sig, label,c,x,y,z) ) { printui(fout, "hslider", label, c, x, y, z); }
227 else if ( isSigNumEntry(sig, label,c,x,y,z) ) { printui(fout, "nentry", label, c, x, y, z); }
228 else if ( isSigVBargraph(sig, label,x,y,z) ) { printui(fout, "vbargraph", label, x, y, z); }
229 else if ( isSigHBargraph(sig, label,x,y,z) ) { printui(fout, "hbargraph", label, x, y, z); }
230 else if ( isSigAttach(sig, x, y) ) { printfun(fout, "attach", x, y); }
231
232 else if ( isSigVectorize(sig,x,y)) { printfun(fout, "vectorize", x, y); }
233 else if ( isSigSerialize(sig,x)) { printfun(fout, "serialize", x); }
234 else if ( isSigVectorAt(sig,x,y)) { printfun(fout, "vectorAt", x, y); }
235 else if ( isSigConcat(sig,x,y)) { printfun(fout, "concat", x, y); }
236
237 else if ( isSigUpSample(sig,x,y)) { printfun(fout, "up", x, y); }
238 else if ( isSigDownSample(sig,x,y)) { printfun(fout, "down", x, y); }
239
240 else {
241 cerr << "NOT A SIGNAL : " << *sig << endl;
242 //exit(1);
243 }
244 return fout;
245 }
246