File: boxtype.cpp

package info (click to toggle)
faust 0.9.9.4b-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,164 kB
  • ctags: 6,050
  • sloc: cpp: 25,384; xml: 4,102; makefile: 648; yacc: 372; ruby: 247; lex: 161; sh: 50
file content (236 lines) | stat: -rw-r--r-- 7,537 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/************************************************************************
 ************************************************************************
    FAUST compiler
	Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
    ---------------------------------------------------------------------
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 ************************************************************************
 ************************************************************************/
 
 
 
/*****************************************************************************
******************************************************************************
	
							  Box Type System
	
******************************************************************************
*****************************************************************************/


/**\file boxtype.cpp
 * \author Yann Orlarey
 * \version 1.0
 * \date 2003
 * \brief A simple type system for block diagram expressions.
 *  The type of a block diagram is defined by a number of inputs and outputs.
 */


#include <stdio.h>
#include <string.h>
#include "boxes.hh"
#include "ppbox.hh"
#include "prim2.hh"
#include "xtended.hh"


Tree BOXTYPEPROP = tree(symbol("boxTypeProp"));
static bool infereBoxType (Tree box, int* inum, int* onum);



/**
 * Return the type (number of inputs and outputs) of a box or false if undefined
 * \param box the box we want to know the type
 * \param inum the place to return the number of inputs
 * \param onum the place to return the number of outputs
 * \return true if type is defined, false if undefined
 */

bool getBoxType (Tree box, int* inum, int* onum)
{
	Tree t;
	if (getProperty(box, BOXTYPEPROP, t)) {
		
		if (isNil(t)) {
			return false;
		} else {
			*inum = hd(t)->node().getInt();
			*onum = tl(t)->node().getInt();
			return true;
		}
		
	} else {
	
		if (infereBoxType(box, inum, onum)) {
			setProperty(box, BOXTYPEPROP, cons(tree(*inum), tree(*onum)));
			return true;
		} else {
			setProperty(box, BOXTYPEPROP, nil);
			return false;
		}
	}
}



/**
 * Infere the type (number of inputs and outputs) of a box.
 * \param box the box we want to know the type
 * \param inum the place to return the number of inputs
 * \param onum the place to return the number of outputs
 */

static bool infereBoxType (Tree t, int* inum, int* onum)
{
	Tree a, b, ff, l, s;
	//Tree abstr, genv, vis, lenv;
	
	xtended* p = (xtended*) getUserData(t);

	if (p) 						{ *inum = p->arity(); *onum = 1; }
	else if (isBoxInt(t)) 		{ *inum = 0; *onum = 1; } 
	else if (isBoxReal(t)) 		{ *inum = 0; *onum = 1; } 
	else if (isBoxWire(t)) 		{ *inum = 1; *onum = 1; }
	else if (isBoxCut(t)) 		{ *inum = 1; *onum = 0; } 

	else if (isBoxSlot(t)) 		{ *inum = 0; *onum = 1; } 
	else if (isBoxSymbolic(t,s,b)) 	{ if (!getBoxType(b, inum, onum)) return false; *inum += 1; } 
	
	else if (isBoxPrim0(t)) 	{ *inum = 0; *onum = 1; } 
	else if (isBoxPrim1(t)) 	{ *inum = 1; *onum = 1; } 
	else if (isBoxPrim2(t)) 	{ *inum = 2; *onum = 1; } 
	else if (isBoxPrim3(t)) 	{ *inum = 3; *onum = 1; } 
	else if (isBoxPrim4(t)) 	{ *inum = 4; *onum = 1; } 
	else if (isBoxPrim5(t)) 	{ *inum = 5; *onum = 1; } 
		
	else if (isBoxFFun(t,ff)) 	{ *inum = ffarity(ff); *onum = 1; } 
    else if (isBoxFConst(t))    { *inum = 0; *onum = 1; } 
    else if (isBoxFVar(t))      { *inum = 0; *onum = 1; } 
	
	else if (isBoxButton(t)) 	{ *inum = 0; *onum = 1; } 
	else if (isBoxCheckbox(t)) 	{ *inum = 0; *onum = 1; } 
	else if (isBoxVSlider(t)) 	{ *inum = 0; *onum = 1; } 
	else if (isBoxHSlider(t)) 	{ *inum = 0; *onum = 1; } 
	else if (isBoxNumEntry(t)) 	{ *inum = 0; *onum = 1; } 
	else if (isBoxVGroup(t,l,a)){ if (!getBoxType(a, inum, onum)) return false; } 
	else if (isBoxHGroup(t,l,a)){ if (!getBoxType(a, inum, onum)) return false; } 
	else if (isBoxTGroup(t,l,a)){ if (!getBoxType(a, inum, onum)) return false; } 
	
	else if (isBoxVBargraph(t)) 	{ *inum = 1; *onum = 1; } 
	else if (isBoxHBargraph(t)) 	{ *inum = 1; *onum = 1; } 

	else if (isBoxSeq(t, a, b)) {
		
		int u,v,x,y;
		if (!getBoxType(a, &u, &v)) return false;
		if (!getBoxType(b, &x, &y)) return false;

		if (v >= x) {
			*inum = u; *onum = y+v-x;
		} else {
			*inum = u+x-v; *onum = y;
		}

	} else if (isBoxPar(t, a, b)) {
		
		int u,v,x,y;
		if (!getBoxType(a, &u, &v)) return false;
		if (!getBoxType(b, &x, &y)) return false;

		*inum = u+x; *onum = v+y;

	} else if (isBoxSplit(t, a, b)) {
		
		int u,v,x,y;
		if (!getBoxType(a, &u, &v)) return false;
		if (!getBoxType(b, &x, &y)) return false;

        if (v == 0) {
            cerr    << "Connection error in : " << boxpp(t) << endl
                    << "The first expression : " << boxpp(a) << " has no outputs" << endl;
            exit(1);
        }
        
        if (x == 0) {
            cerr    << "Connection error in : " << boxpp(t) << endl
                    << "The second expression : " << boxpp(b) << " has no inputs" << endl;
            exit(1);
        }
		 
		if (x % v != 0) {
			cerr 	<< "Connection error in : " << boxpp(t) << endl
					<< "The number of outputs " << v
					<< " of the first expression should be a divisor of the number of inputs " << x
					<< " of the second expression" << endl;
			exit(1);
		}
		
		*inum = u; *onum = y;

	} else if (isBoxMerge(t, a, b)) {
		
		int u,v,x,y;
		if (!getBoxType(a, &u, &v)) return false;
		if (!getBoxType(b, &x, &y)) return false;

        if (v == 0) {
            cerr    << "Connection error in : " << boxpp(t) << endl
                    << "The first expression : " << boxpp(a) << " has no outputs" << endl;
            exit(1);
        }
        
        if (x == 0) {
            cerr    << "Connection error in : " << boxpp(t) << endl
                    << "The second expression : " << boxpp(b) << " has no inputs" << endl;
            exit(1);
        }
        
		if (v % x != 0) { 
			cerr 	<< "Connection error in : " << boxpp(t) << endl
					<< "The number of outputs " << v
					<< " of the first expression should be a multiple of the number of inputs " << x
					<< " of the second expression" << endl;
			exit(1);
		}

		*inum = u; *onum = y;

	} else if (isBoxRec(t, a, b)) {
		
		int u,v,x,y;
		if (!getBoxType(a, &u, &v)) return false;
		if (!getBoxType(b, &x, &y)) return false;
		if ( (x > v) | (y > u) ) { 
			cerr 	<< "Connection error in : " << boxpp(t) << endl;
			if (x > v) cerr << "The number of outputs " << v 
							<< " of the first expression should be greater or equal \n  to the number of inputs " << x 
							<< " of the second exepression" << endl;
			if (y > u) cerr	<< "The number of inputs " << u
							<< " of the first expression should be greater or equal \n  to the number of outputs " << y
							<< " of the second exepression" << endl;
			exit(1);
		}
		*inum = max(0,u-y); *onum = v;
		
	} else {
	  return false;
	}
	return true;
}