FAUST compiler  0.9.9.6b8
Functions | Variables
boxtype.cpp File Reference

A simple type system for block diagram expressions. The type of a block diagram is defined by a number of inputs and outputs. More...

#include <stdio.h>
#include <string.h>
#include "boxes.hh"
#include "ppbox.hh"
#include "prim2.hh"
#include "xtended.hh"
Include dependency graph for boxtype.cpp:

Go to the source code of this file.

Functions

static bool infereBoxType (Tree t, int *inum, int *onum)
 Infere the type (number of inputs and outputs) of a box. More...
 
bool getBoxType (Tree box, int *inum, int *onum)
 Return the type (number of inputs and outputs) of a box or false if undefined. More...
 

Variables

Tree BOXTYPEPROP = tree(symbol("boxTypeProp"))
 

Detailed Description

A simple type system for block diagram expressions. The type of a block diagram is defined by a number of inputs and outputs.

Author
Yann Orlarey
Version
1.0
Date
2003

Definition in file boxtype.cpp.

Function Documentation

bool getBoxType ( Tree  box,
int *  inum,
int *  onum 
)

Return the type (number of inputs and outputs) of a box or false if undefined.

Parameters
boxthe box we want to know the type
inumthe place to return the number of inputs
onumthe place to return the number of outputs
Returns
true if type is defined, false if undefined

Definition at line 63 of file boxtype.cpp.

References cons(), Node::getInt(), getProperty(), hd(), infereBoxType(), isNil(), nil, CTree::node(), setProperty(), tl(), and tree().

Referenced by applyList(), boxlistOutputs(), eval2double(), eval2int(), generateDiagramSchema(), getBoxInputsAndOutputs(), infereBoxType(), isBoxNumeric(), main(), mapGetEqName(), numericBoxSimplification(), realeval(), realPropagate(), and writeSchemaFile().

64 {
65  Tree t;
66  if (getProperty(box, BOXTYPEPROP, t)) {
67 
68  if (isNil(t)) {
69  return false;
70  } else {
71  *inum = hd(t)->node().getInt();
72  *onum = tl(t)->node().getInt();
73  return true;
74  }
75 
76  } else {
77 
78  if (infereBoxType(box, inum, onum)) {
79  setProperty(box, BOXTYPEPROP, cons(tree(*inum), tree(*onum)));
80  return true;
81  } else {
83  return false;
84  }
85  }
86 }
static bool infereBoxType(Tree box, int *inum, int *onum)
Infere the type (number of inputs and outputs) of a box.
Definition: boxtype.cpp:101
Tree cons(Tree a, Tree b)
Definition: list.hh:124
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
Tree BOXTYPEPROP
Definition: boxtype.cpp:50
Tree hd(Tree l)
Definition: list.hh:133
const Node & node() const
return the content of the tree
Definition: tree.hh:143
bool isNil(Tree l)
Definition: list.hh:137
Tree tree(const Node &n)
Definition: tree.hh:186
int getInt() const
Definition: node.hh:104
void setProperty(Tree t, Tree key, Tree val)
Definition: list.cpp:418
Tree nil
Definition: list.cpp:116
Tree tl(Tree l)
Definition: list.hh:134
bool getProperty(Tree t, Tree key, Tree &val)
Definition: list.cpp:423

Here is the call graph for this function:

Here is the caller graph for this function:

static bool infereBoxType ( Tree  t,
int *  inum,
int *  onum 
)
static

Infere the type (number of inputs and outputs) of a box.

The box expression is assumed to be in 'propagation normal form' that is to have been evaluated and residual abstractions to have been converted to symbolic boxes (using a2sb()).

Parameters
boxthe box we want to know the type
inumthe place to return the number of inputs
onumthe place to return the number of outputs
Returns
true if the box expression has a type

Definition at line 101 of file boxtype.cpp.

References xtended::arity(), ffarity(), getBoxType(), getUserData(), isBoxButton(), isBoxCheckbox(), isBoxCut(), isBoxEnvironment(), isBoxFConst(), isBoxFFun(), isBoxFVar(), isBoxHBargraph(), isBoxHGroup(), isBoxHSlider(), isBoxInt(), isBoxMerge(), isBoxNumEntry(), isBoxPar(), isBoxPatternVar(), isBoxPrim0(), isBoxPrim1(), isBoxPrim2(), isBoxPrim3(), isBoxPrim4(), isBoxPrim5(), isBoxReal(), isBoxRec(), isBoxSeq(), isBoxSlot(), isBoxSplit(), isBoxSymbolic(), isBoxTGroup(), isBoxVBargraph(), isBoxVGroup(), isBoxVSlider(), isBoxWaveform(), isBoxWire(), max(), and Automaton::s.

Referenced by getBoxType().

102 {
103  Tree a, b, ff, l, s;
104  //Tree abstr, genv, vis, lenv;
105 
106  xtended* p = (xtended*) getUserData(t);
107 
108  if (p) { *inum = p->arity(); *onum = 1; }
109  else if (isBoxInt(t)) { *inum = 0; *onum = 1; }
110  else if (isBoxReal(t)) { *inum = 0; *onum = 1; }
111 
112  else if (isBoxWaveform(t)) { *inum = 0; *onum = 2; }
113 
114  else if (isBoxWire(t)) { *inum = 1; *onum = 1; }
115  else if (isBoxCut(t)) { *inum = 1; *onum = 0; }
116 
117  else if (isBoxSlot(t)) { *inum = 0; *onum = 1; }
118  else if (isBoxSymbolic(t,s,b)) { if (!getBoxType(b, inum, onum)) return false; *inum += 1; }
119 
120  else if (isBoxPatternVar(t,a)) { return false; }
121 
122  else if (isBoxPrim0(t)) { *inum = 0; *onum = 1; }
123  else if (isBoxPrim1(t)) { *inum = 1; *onum = 1; }
124  else if (isBoxPrim2(t)) { *inum = 2; *onum = 1; }
125  else if (isBoxPrim3(t)) { *inum = 3; *onum = 1; }
126  else if (isBoxPrim4(t)) { *inum = 4; *onum = 1; }
127  else if (isBoxPrim5(t)) { *inum = 5; *onum = 1; }
128 
129  else if (isBoxFFun(t,ff)) { *inum = ffarity(ff); *onum = 1; }
130  else if (isBoxFConst(t)) { *inum = 0; *onum = 1; }
131  else if (isBoxFVar(t)) { *inum = 0; *onum = 1; }
132 
133  else if (isBoxButton(t)) { *inum = 0; *onum = 1; }
134  else if (isBoxCheckbox(t)) { *inum = 0; *onum = 1; }
135  else if (isBoxVSlider(t)) { *inum = 0; *onum = 1; }
136  else if (isBoxHSlider(t)) { *inum = 0; *onum = 1; }
137  else if (isBoxNumEntry(t)) { *inum = 0; *onum = 1; }
138  else if (isBoxVGroup(t,l,a)){ return getBoxType(a, inum, onum); }
139  else if (isBoxHGroup(t,l,a)){ return getBoxType(a, inum, onum); }
140  else if (isBoxTGroup(t,l,a)){ return getBoxType(a, inum, onum); }
141 
142  else if (isBoxVBargraph(t)) { *inum = 1; *onum = 1; }
143  else if (isBoxHBargraph(t)) { *inum = 1; *onum = 1; }
144 
145  else if (isBoxSeq(t, a, b)) {
146 
147  int u,v,x,y;
148  if (!getBoxType(a, &u, &v)) return false;
149  if (!getBoxType(b, &x, &y)) return false;
150 
151  if (v != x) {
152  cerr << "Error in sequential composition (A:B)" << endl
153  << "The number of outputs (" << v << ") of A = " << boxpp(a) << endl
154  << "must be equal to the number of inputs (" << x << ") of B : " << boxpp(b) << endl;
155  exit(1);
156  } else {
157  *inum = u; *onum = y;
158  }
159 
160  } else if (isBoxPar(t, a, b)) {
161 
162  int u,v,x,y;
163  if (!getBoxType(a, &u, &v)) return false;
164  if (!getBoxType(b, &x, &y)) return false;
165 
166  *inum = u+x; *onum = v+y;
167 
168  } else if (isBoxSplit(t, a, b)) {
169 
170  int u,v,x,y;
171  if (!getBoxType(a, &u, &v)) return false;
172  if (!getBoxType(b, &x, &y)) return false;
173 
174  if (v == 0) {
175  cerr << "Connection error in : " << boxpp(t) << endl
176  << "The first expression : " << boxpp(a) << " has no outputs" << endl;
177  exit(1);
178  }
179 
180  if (x == 0) {
181  cerr << "Connection error in : " << boxpp(t) << endl
182  << "The second expression : " << boxpp(b) << " has no inputs" << endl;
183  exit(1);
184  }
185 
186  if (x % v != 0) {
187  cerr << "Connection error in : " << boxpp(t) << endl
188  << "The number of outputs " << v
189  << " of the first expression should be a divisor of the number of inputs " << x
190  << " of the second expression" << endl;
191  exit(1);
192  }
193 
194  *inum = u; *onum = y;
195 
196  } else if (isBoxMerge(t, a, b)) {
197 
198  int u,v,x,y;
199  if (!getBoxType(a, &u, &v)) return false;
200  if (!getBoxType(b, &x, &y)) return false;
201 
202  if (v == 0) {
203  cerr << "Connection error in : " << boxpp(t) << endl
204  << "The first expression : " << boxpp(a) << " has no outputs" << endl;
205  exit(1);
206  }
207 
208  if (x == 0) {
209  cerr << "Connection error in : " << boxpp(t) << endl
210  << "The second expression : " << boxpp(b) << " has no inputs" << endl;
211  exit(1);
212  }
213 
214  if (v % x != 0) {
215  cerr << "Connection error in : " << boxpp(t) << endl
216  << "The number of outputs " << v
217  << " of the first expression should be a multiple of the number of inputs " << x
218  << " of the second expression" << endl;
219  exit(1);
220  }
221 
222  *inum = u; *onum = y;
223 
224  } else if (isBoxRec(t, a, b)) {
225 
226  int u,v,x,y;
227  if (!getBoxType(a, &u, &v)) return false;
228  if (!getBoxType(b, &x, &y)) return false;
229  if ( (x > v) | (y > u) ) {
230  cerr << "Connection error in : " << boxpp(t) << endl;
231  if (x > v) cerr << "The number of outputs " << v
232  << " of the first expression should be greater or equal \n to the number of inputs " << x
233  << " of the second expression" << endl;
234  if (y > u) cerr << "The number of inputs " << u
235  << " of the first expression should be greater or equal \n to the number of outputs " << y
236  << " of the second expression" << endl;
237  exit(1);
238  }
239  *inum = max(0,u-y); *onum = v;
240 
241  } else if (isBoxEnvironment(t)) {
242  cerr << "Connection error : an environment is not a block-diagram : " << boxpp(t) << endl;
243  exit(1);
244  } else {
245  cerr << "boxType() internal error : unrecognized box expression " << boxpp(t) << endl;
246  exit(1);
247  }
248  return true;
249 }
bool isBoxPrim1(Tree s)
Definition: boxes.cpp:315
bool isBoxPrim5(Tree s)
Definition: boxes.cpp:335
bool isBoxWire(Tree t)
Definition: boxes.cpp:109
bool isBoxSeq(Tree t, Tree &x, Tree &y)
Definition: boxes.cpp:136
bool isBoxInt(Tree t)
Definition: boxes.cpp:78
bool isBoxSplit(Tree t, Tree &x, Tree &y)
Definition: boxes.cpp:148
bool isBoxFFun(Tree s)
Definition: boxes.cpp:344
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
bool isBoxCut(Tree t)
Definition: boxes.cpp:105
int ffarity(Tree t)
Definition: prim2.cpp:67
bool isBoxPrim0(Tree s)
Definition: boxes.cpp:310
bool isBoxPrim4(Tree s)
Definition: boxes.cpp:330
bool isBoxHGroup(Tree s)
Definition: boxes.cpp:437
bool isBoxHBargraph(Tree s)
Definition: boxes.cpp:455
bool isBoxRec(Tree t, Tree &x, Tree &y)
Definition: boxes.cpp:144
double max(double x, double y)
Definition: interval.hh:60
bool isBoxSymbolic(Tree t)
Definition: boxes.cpp:126
bool isBoxTGroup(Tree s)
Definition: boxes.cpp:449
bool isBoxEnvironment(Tree s)
Definition: boxes.cpp:285
bool isBoxButton(Tree s)
Definition: boxes.cpp:366
bool isBoxReal(Tree t)
Definition: boxes.cpp:79
bool isBoxPar(Tree t, Tree &x, Tree &y)
Definition: boxes.cpp:140
bool isBoxHSlider(Tree s)
Definition: boxes.cpp:379
bool isBoxVBargraph(Tree s)
Definition: boxes.cpp:461
bool isBoxWaveform(Tree s)
Definition: boxes.cpp:97
bool isBoxCheckbox(Tree s)
Definition: boxes.cpp:372
bool isBoxVGroup(Tree s)
Definition: boxes.cpp:443
virtual unsigned int arity()=0
bool isBoxFVar(Tree s)
Definition: boxes.cpp:356
bool isBoxVSlider(Tree s)
Definition: boxes.cpp:399
Definition: ppbox.hh:58
bool isBoxNumEntry(Tree s)
Definition: boxes.cpp:418
bool isBoxSlot(Tree t)
Definition: boxes.cpp:119
void * getUserData(Symbol *sym)
Returns user data.
Definition: symbol.hh:100
bool isBoxPrim3(Tree s)
Definition: boxes.cpp:325
bool isBoxPatternVar(Tree s, Tree &id)
Definition: boxes.cpp:622
bool isBoxMerge(Tree t, Tree &x, Tree &y)
Definition: boxes.cpp:152
bool isBoxFConst(Tree s)
Definition: boxes.cpp:350
bool isBoxPrim2(Tree s)
Definition: boxes.cpp:320
bool getBoxType(Tree box, int *inum, int *onum)
Return the type (number of inputs and outputs) of a box or false if undefined.
Definition: boxtype.cpp:63

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

Tree BOXTYPEPROP = tree(symbol("boxTypeProp"))

Definition at line 50 of file boxtype.cpp.