Merge branch 'newtree'
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / compiler / boxes / boxcomplexity.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 * @file boxcomplexity.cpp
23 * Implement complexity computation for box diagrams.
24 */
25
26
27
28
29 // construction des representations graphiques
30
31
32 #include <ostream>
33 #include "xtended.hh"
34 #include "boxcomplexity.h"
35
36 using namespace std;
37
38 /**
39 * property Key used to store box complexity
40 */
41 Tree BCOMPLEXITY = tree ("BCOMPLEXITY");
42
43 static int computeBoxComplexity (Tree box);
44
45 /**
46 * Return the complexity propety of a box expression tree.
47 * Return the complexity propety of a box expression tree. If no
48 * complexity property exist, it is created an computeBoxComplexity
49 * is called do to the job.
50 *
51 * @param box an evaluated box expression tree
52 * @return the complexity of box
53 *
54 * @see computeBoxComplexity
55 */
56 int boxComplexity (Tree box)
57 {
58 Tree prop = box->getProperty(BCOMPLEXITY);
59
60 if (prop) {
61 return tree2int(prop);
62
63 } else {
64 int v = computeBoxComplexity(box);
65 box->setProperty(BCOMPLEXITY,tree(v));
66 return v;
67 }
68 }
69
70 /**
71 * internal shortcut to simplify computeBoxComplexity code
72 */
73 #define BC boxComplexity
74
75
76 /**
77 * Compute the complexity of a box expression.
78 *
79 * Compute the complexity of a box expression tree according to the
80 * complexity of its subexpressions. Basically it counts the number
81 * of boxes to be drawn. The box-diagram expression is supposed
82 * to be evaluated. It will exit with an error if it is not the case.
83 *
84 * @param box an evaluated box expression tree
85 * @return the complexity of box
86 */
87 int computeBoxComplexity (Tree box)
88 {
89 int i;
90 double r;
91 prim0 p0;
92 prim1 p1;
93 prim2 p2;
94 prim3 p3;
95 prim4 p4;
96 prim5 p5;
97
98 Tree t1, t2, ff, label, cur, min, max, step, type, name, file;
99
100 xtended* xt = (xtended*) getUserData(box);
101
102
103 // simple elements
104 if (xt) return 1;
105 else if (isBoxInt(box, &i)) return 1;
106 else if (isBoxReal(box, &r)) return 1;
107
108 else if (isBoxCut(box)) return 0;
109 else if (isBoxWire(box)) return 0;
110
111 else if (isBoxPrim0(box, &p0)) return 1;
112 else if (isBoxPrim1(box, &p1)) return 1;
113 else if (isBoxPrim2(box, &p2)) return 1;
114 else if (isBoxPrim3(box, &p3)) return 1;
115 else if (isBoxPrim4(box, &p4)) return 1;
116 else if (isBoxPrim5(box, &p5)) return 1;
117
118 // foreign elements
119 else if (isBoxFFun(box, ff)) return 1;
120 else if (isBoxFConst(box, type, name, file))
121 return 1;
122 else if (isBoxFVar(box, type, name, file))
123 return 1;
124 // slots and symbolic boxes
125 else if (isBoxSlot(box, &i)) return 1;
126 else if (isBoxSymbolic(box,t1,t2)) return 1 + BC(t2);
127
128 // block diagram binary operator
129 else if (isBoxSeq(box, t1, t2)) return BC(t1) + BC(t2);
130 else if (isBoxSplit(box, t1, t2)) return BC(t1) + BC(t2);
131 else if (isBoxMerge(box, t1, t2)) return BC(t1) + BC(t2);
132 else if (isBoxPar(box, t1, t2)) return BC(t1) + BC(t2);
133 else if (isBoxRec(box, t1, t2)) return BC(t1) + BC(t2);
134
135 // user interface widgets
136 else if (isBoxButton(box, label)) return 1;
137 else if (isBoxCheckbox(box, label)) return 1;
138 else if (isBoxVSlider(box, label, cur, min, max, step)) return 1;
139 else if (isBoxHSlider(box, label, cur, min, max, step)) return 1;
140 else if (isBoxHBargraph(box, label, min, max)) return 1;
141 else if (isBoxVBargraph(box, label, min, max)) return 1;
142 else if (isBoxNumEntry(box, label, cur, min, max, step))return 1;
143
144 // user interface groups
145 else if (isBoxVGroup(box, label, t1)) return BC(t1);
146 else if (isBoxHGroup(box, label, t1)) return BC(t1);
147 else if (isBoxTGroup(box, label, t1)) return BC(t1);
148
149 //a completer
150 else {
151 //fout << tree2str(box);
152 cerr << "ERROR in boxComplexity : not an evaluated box [[ " << *box << " ]]";
153 exit(-1);
154 }
155
156 return -1;
157 }