FAUST compiler  0.9.9.6b8
boxes.cpp
Go to the documentation of this file.
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 /*****************************************************************************
25 ******************************************************************************
26 
27 
28  The Box Language
29 
30 
31 ******************************************************************************
32 *****************************************************************************/
33 
34 
44 #include <stdio.h>
45 #include <string.h>
46 #include "boxes.hh"
47 #include "ppbox.hh"
48 #include "prim2.hh"
49 #include "xtended.hh"
50 
51 
52 /*****************************************************************************
53  Identifiers
54 *****************************************************************************/
55 Sym BOXIDENT = symbol ("BoxIdent");
56 
57 Tree boxIdent(const char* name) { return tree(BOXIDENT, tree(symbol(name)) ); }
58 bool isBoxIdent(Tree t) { return t->node() == Node(BOXIDENT); }
59 bool isBoxIdent(Tree t0, const char** str)
60 {
61  Tree t1; Sym s;
62  if ( isTree(t0, BOXIDENT, t1) && isSym(t1->node(), &s) ) {
63  *str = name(s);
64  return true;
65  } else {
66  return false;
67  }
68 }
69 
70 
71 /*****************************************************************************
72  Numbers
73 *****************************************************************************/
74 
75 Tree boxInt(int n) { return tree(n); }
76 Tree boxReal(double n) { return tree(n); }
77 
78 bool isBoxInt(Tree t) { return isInt(t->node()); }
79 bool isBoxReal(Tree t) { return isDouble(t->node()); }
80 
81 bool isBoxInt(Tree t, int* i) { return isInt(t->node(), i); }
82 bool isBoxReal(Tree t, double* r) { return isDouble(t->node(), r); }
83 
84 
85 
86 /*****************************************************************************
87  Waveform
88 *****************************************************************************/
89 
90 Sym BOXWAVEFORM = symbol ("BoxWaveform");
91 
92 Tree boxWaveform (const tvec& br)
93 {
94  return tree(BOXWAVEFORM, br);
95 }
96 
97 bool isBoxWaveform (Tree s) { return isTree(s, BOXWAVEFORM); }
98 
99 /*****************************************************************************
100  Wire and Cut
101 *****************************************************************************/
102 
103 Sym BOXCUT = symbol ("BoxCut");
104 Tree boxCut() { return tree(BOXCUT); }
105 bool isBoxCut(Tree t) { return isTree(t, BOXCUT); }
106 
107 Sym BOXWIRE = symbol ("BoxWire");
108 Tree boxWire() { return tree(BOXWIRE); }
109 bool isBoxWire(Tree t) { return isTree(t, BOXWIRE); }
110 
111 
112 /*****************************************************************************
113  Symbolic Boxes with symbolic slots
114 *****************************************************************************/
115 
116 Sym BOXSLOT = symbol ("BoxSlot");
117 
118 Tree boxSlot(int id) { return tree(BOXSLOT,tree(id)); }
119 bool isBoxSlot(Tree t) { Tree w; return isTree(t, BOXSLOT,w); }
120 bool isBoxSlot(Tree t, int* id) { Tree w; return isTree(t, BOXSLOT,w) && isInt(w->node(),id); }
121 
122 
123 Sym BOXSYMBOLIC = symbol ("BoxSymbolic");
124 
125 Tree boxSymbolic(Tree slot, Tree body) { return tree(BOXSYMBOLIC,slot, body); }
126 bool isBoxSymbolic(Tree t) { Tree slot, body; return isTree(t, BOXSYMBOLIC, slot, body); }
127 bool isBoxSymbolic(Tree t, Tree& slot, Tree& body) { return isTree(t, BOXSYMBOLIC, slot, body); }
128 
129 
130 /*****************************************************************************
131  Composition of Boxes
132 *****************************************************************************/
133 
134 Sym BOXSEQ = symbol ("BoxSeq");
135 Tree boxSeq(Tree x, Tree y) { return tree(BOXSEQ, x, y); }
136 bool isBoxSeq(Tree t, Tree& x, Tree& y) { return isTree(t, BOXSEQ, x, y); }
137 
138 Sym BOXPAR = symbol ("BoxPar");
139 Tree boxPar(Tree x, Tree y) { return tree(BOXPAR, x, y); }
140 bool isBoxPar(Tree t, Tree& x, Tree& y) { return isTree(t, BOXPAR, x, y); }
141 
142 Sym BOXREC = symbol ("BoxRec");
143 Tree boxRec(Tree x, Tree y) { return tree(BOXREC, x, y); }
144 bool isBoxRec(Tree t, Tree& x, Tree& y) { return isTree(t, BOXREC, x, y); }
145 
146 Sym BOXSPLIT = symbol ("BoxSplit");
147 Tree boxSplit(Tree x, Tree y) { return tree(BOXSPLIT, x, y); }
148 bool isBoxSplit(Tree t, Tree& x, Tree& y) { return isTree(t, BOXSPLIT, x, y); }
149 
150 Sym BOXMERGE = symbol ("BoxMerge");
151 Tree boxMerge(Tree x, Tree y) { return tree(BOXMERGE, x, y); }
152 bool isBoxMerge(Tree t, Tree& x, Tree& y) { return isTree(t, BOXMERGE, x, y); }
153 
154 
155 /*****************************************************************************
156  Algorithmic Composition of Boxes
157 *****************************************************************************/
158 
159 Sym BOXIPAR = symbol ("BoxIPar");
160 Sym BOXISEQ = symbol ("BoxISeq");
161 Sym BOXISUM = symbol ("BoxISum");
162 Sym BOXIPROD = symbol ("BoxIProd");
163 
164 Tree boxIPar(Tree x, Tree y, Tree z) { return tree(BOXIPAR, x, y, z); }
165 Tree boxISeq(Tree x, Tree y, Tree z) { return tree(BOXISEQ, x, y, z); }
166 Tree boxISum(Tree x, Tree y, Tree z) { return tree(BOXISUM, x, y, z); }
167 Tree boxIProd(Tree x, Tree y, Tree z) { return tree(BOXIPROD, x, y, z); }
168 
169 bool isBoxIPar(Tree t, Tree& x, Tree& y, Tree& z) { return isTree(t, BOXIPAR, x, y, z); }
170 bool isBoxISeq(Tree t, Tree& x, Tree& y, Tree& z) { return isTree(t, BOXISEQ, x, y, z); }
171 bool isBoxISum(Tree t, Tree& x, Tree& y, Tree& z) { return isTree(t, BOXISUM, x, y, z); }
172 bool isBoxIProd(Tree t, Tree& x, Tree& y, Tree& z) { return isTree(t, BOXIPROD, x, y, z); }
173 
174 
175 /*****************************************************************************
176  Static information on Boxes
177 *****************************************************************************/
178 
179 Sym BOXINPUTS = symbol ("BoxInputs");
180 Sym BOXOUTPUTS = symbol ("BoxOutputs");
181 
182 Tree boxInputs(Tree x) { return tree(BOXINPUTS, x); }
183 Tree boxOutputs(Tree x) { return tree(BOXOUTPUTS, x); }
184 
185 bool isBoxInputs(Tree t, Tree& x) { return isTree(t, BOXINPUTS, x); }
186 bool isBoxOutputs(Tree t, Tree& x) { return isTree(t, BOXOUTPUTS, x); }
187 
188 
189 
190 /*****************************************************************************
191  Lambda-Calculus of Boxes
192 *****************************************************************************/
193 Sym BOXABSTR = symbol ("BoxAbstr");
194 Sym BOXAPPL = symbol ("BoxAppl");
195 Sym CLOSURE = symbol ("Closure");
196 Sym BOXERROR = symbol ("BoxError");
197 Sym BOXACCESS = symbol ("BoxAccess");
198 
199 Tree boxAbstr (Tree x, Tree y) { return tree(BOXABSTR, x, y); }
200 Tree boxAppl (Tree x, Tree y) { return tree(BOXAPPL, x, y); }
201 
202 bool isBoxAbstr (Tree t) { return t->node() == Node(BOXABSTR); }
203 bool isBoxAppl (Tree t) { return t->node() == Node(BOXAPPL); }
204 
205 bool isBoxAbstr (Tree t, Tree& x, Tree& y) { return isTree(t, BOXABSTR, x, y); }
206 bool isBoxAppl (Tree t, Tree& x, Tree& y) { return isTree(t, BOXAPPL, x, y); }
207 
209 {
210  if (isNil(largs)) {
211  return body;
212  } else {
213  return buildBoxAbstr(tl(largs), boxAbstr(hd(largs), body));
214  }
215 }
216 
217 #if 0
218 Tree buildBoxAppl (Tree fun, Tree revarglist)
219 {
220  if (isNil(revarglist)) {
221  return fun;
222  } else {
223  return boxAppl(buildBoxAppl(fun, tl(revarglist)), hd(revarglist));
224  }
225 }
226 #else
227 Tree buildBoxAppl (Tree fun, Tree revarglist)
228 {
229  if (isNil (revarglist)) exit(1); // a revoir !!!!!!
230  return boxAppl(fun, revarglist);
231 }
232 #endif
233 
234 Tree closure (Tree abstr, Tree genv, Tree vis, Tree lenv)
235 {
236  return tree(CLOSURE, abstr, genv, vis, lenv);
237 }
238 
239 bool isClosure (Tree t, Tree& abstr, Tree& genv, Tree& vis, Tree& lenv)
240 {
241  return isTree(t, CLOSURE, abstr, genv, vis, lenv);
242 }
243 
245 {
246  return tree(BOXERROR);
247 }
248 
250 {
251  return isTree(t, BOXERROR);
252 }
253 
254 
255 Tree boxAccess (Tree exp, Tree id) { return tree(BOXACCESS, exp, id); }
256 bool isBoxAccess(Tree t, Tree& exp, Tree& id) { return isTree(t, BOXACCESS, exp, id); }
257 
258 
259 /*****************************************************************************
260  Boxes with local definitions
261 *****************************************************************************/
262 Sym BOXWITHLOCALDEF = symbol ("BoxWithLocalDef");
263 
264 Tree boxWithLocalDef (Tree body, Tree ldef) { return tree(BOXWITHLOCALDEF, body, ldef); }
265 bool isBoxWithLocalDef (Tree t, Tree& body, Tree& ldef) { return isTree(t, BOXWITHLOCALDEF, body, ldef); }
266 
267 
268 /*****************************************************************************
269  Boxes modif local definitions
270 *****************************************************************************/
271 Sym BOXMODIFLOCALDEF = symbol ("BoxModifLocalDef");
272 
273 
274 Tree boxModifLocalDef (Tree body, Tree ldef) { return tree(BOXMODIFLOCALDEF, body, ldef); }
275 bool isBoxModifLocalDef (Tree t, Tree& body, Tree& ldef) { return isTree(t, BOXMODIFLOCALDEF, body, ldef); }
276 
277 
278 /*****************************************************************************
279  Modules
280 *****************************************************************************/
281 
282 Sym BOXENVIRONMENT = symbol ("BoxEnvironment");
283 
284 Tree boxEnvironment () { return tree(BOXENVIRONMENT); }
285 bool isBoxEnvironment (Tree s) { return isTree(s, BOXENVIRONMENT); }
286 
287 Sym BOXCOMPONENT = symbol ("BoxComponent");
288 
289 Tree boxComponent (Tree filename) { return tree(BOXCOMPONENT, filename); }
290 bool isBoxComponent (Tree s, Tree& filename) { return isTree(s, BOXCOMPONENT, filename); }
291 
292 Sym BOXLIBRARY = symbol ("BoxLibrary");
293 
294 Tree boxLibrary (Tree filename) { return tree(BOXLIBRARY, filename); }
295 bool isBoxLibrary (Tree s, Tree& filename) { return isTree(s, BOXLIBRARY, filename); }
296 
297 
298 Sym IMPORTFILE = symbol ("ImportFile");
299 
300 Tree importFile(Tree filename) { return tree(IMPORTFILE, filename); }
301 bool isImportFile(Tree s, Tree& filename) { return isTree(s, IMPORTFILE, filename); }
302 
303 
304 /*****************************************************************************
305  External Primitive Boxes (n -> 1)
306 *****************************************************************************/
307 
308 Sym BOXPRIM0 = symbol ("BoxPrim0");
309 Tree boxPrim0(prim0 foo) { return tree(BOXPRIM0, tree((void*)foo)); }
310 bool isBoxPrim0 (Tree s) { Tree t; return isTree(s, BOXPRIM0, t); }
311 bool isBoxPrim0 (Tree s, prim0* p) { Tree t; return isTree(s, BOXPRIM0, t) && isPointer(t->node(),(void**)p); }
312 
313 Sym BOXPRIM1 = symbol ("BoxPrim1");
314 Tree boxPrim1(prim1 foo) { return tree(BOXPRIM1, tree((void*)foo)); }
315 bool isBoxPrim1 (Tree s) { Tree t; return isTree(s, BOXPRIM1, t); }
316 bool isBoxPrim1 (Tree s, prim1* p) { Tree t; return isTree(s, BOXPRIM1, t) && isPointer(t->node(),(void**)p); }
317 
318 Sym BOXPRIM2 = symbol ("BoxPrim2");
319 Tree boxPrim2(prim2 foo) { return tree(BOXPRIM2, tree((void*)foo)); }
320 bool isBoxPrim2 (Tree s) { Tree t; return isTree(s, BOXPRIM2, t); }
321 bool isBoxPrim2 (Tree s, prim2* p) { Tree t; return isTree(s, BOXPRIM2, t) && isPointer(t->node(),(void**)p); }
322 
323 Sym BOXPRIM3 = symbol ("BoxPrim3");
324 Tree boxPrim3(prim3 foo) { return tree(BOXPRIM3, tree((void*)foo)); }
325 bool isBoxPrim3 (Tree s) { Tree t; return isTree(s, BOXPRIM3, t); }
326 bool isBoxPrim3 (Tree s, prim3* p) { Tree t; return isTree(s, BOXPRIM3, t) && isPointer(t->node(),(void**)p); }
327 
328 Sym BOXPRIM4 = symbol ("BoxPrim4");
329 Tree boxPrim4(prim4 foo) { return tree(BOXPRIM4, tree((void*)foo)); }
330 bool isBoxPrim4 (Tree s) { Tree t; return isTree(s, BOXPRIM4, t); }
331 bool isBoxPrim4 (Tree s, prim4* p) { Tree t; return isTree(s, BOXPRIM4, t) && isPointer(t->node(),(void**)p); }
332 
333 Sym BOXPRIM5 = symbol ("BoxPrim5");
334 Tree boxPrim5(prim5 foo) { return tree(BOXPRIM5, tree((void*)foo)); }
335 bool isBoxPrim5 (Tree s) { Tree t; return isTree(s, BOXPRIM5, t); }
336 bool isBoxPrim5 (Tree s, prim5* p) { Tree t; return isTree(s, BOXPRIM5, t) && isPointer(t->node(),(void**)p); }
337 
338 /*****************************************************************************
339  Foreign Functions
340 *****************************************************************************/
341 
342 Sym BOXFFUN = symbol ("BoxFFun");
343 Tree boxFFun (Tree ff) { return tree(BOXFFUN, ff); }
344 bool isBoxFFun (Tree s) { Tree ff; return isTree(s, BOXFFUN, ff); }
345 bool isBoxFFun (Tree s, Tree& ff) { return isTree(s, BOXFFUN, ff); }
346 
347 
348 Sym BOXFCONST = symbol ("BoxFConst");
349 Tree boxFConst (Tree type, Tree name, Tree file) { return tree(BOXFCONST, type, name, file); }
350 bool isBoxFConst (Tree s) { Tree t,n,f; return isTree(s, BOXFCONST, t, n, f); }
351 bool isBoxFConst (Tree s, Tree& type, Tree& name, Tree& file) { return isTree(s, BOXFCONST,type, name, file); }
352 
353 
354 Sym BOXFVAR = symbol ("BoxFVar");
355 Tree boxFVar (Tree type, Tree name, Tree file) { return tree(BOXFVAR, type, name, file); }
356 bool isBoxFVar (Tree s) { Tree t,n,f; return isTree(s, BOXFVAR, t, n, f); }
357 bool isBoxFVar (Tree s, Tree& type, Tree& name, Tree& file) { return isTree(s, BOXFVAR,type, name, file); }
358 
359 
360 /*****************************************************************************
361  User Interface Elements
362 *****************************************************************************/
363 
364 Sym BOXBUTTON = symbol ("BoxButton");
365 Tree boxButton (Tree lbl) { return tree(BOXBUTTON, lbl); }
366 bool isBoxButton (Tree s) { Tree lbl; return isTree(s, BOXBUTTON, lbl); }
367 bool isBoxButton (Tree s, Tree& lbl) { return isTree(s, BOXBUTTON, lbl); }
368 
369 
370 Sym BOXCHECKBOX = symbol ("BoxCheckbox");
371 Tree boxCheckbox (Tree lbl) { return tree(BOXCHECKBOX, lbl); }
372 bool isBoxCheckbox (Tree s) { Tree lbl; return isTree(s, BOXCHECKBOX, lbl); }
373 bool isBoxCheckbox (Tree s, Tree& lbl) { return isTree(s, BOXCHECKBOX, lbl); }
374 
375 
376 Sym BOXHSLIDER = symbol ("BoxHSlider");
378  { return tree(BOXHSLIDER, lbl, list4(cur,min,max,step)); }
379 bool isBoxHSlider (Tree s) { Tree lbl, params; return isTree(s, BOXHSLIDER, lbl, params); }
380 
381 bool isBoxHSlider (Tree s, Tree& lbl, Tree& cur, Tree& min, Tree& max, Tree& step)
382 {
383  Tree params;
384  if (isTree(s, BOXHSLIDER, lbl, params)) {
385  cur = nth(params, 0);
386  min = nth(params, 1);
387  max = nth(params, 2);
388  step= nth(params, 3);
389  return true;
390  } else {
391  return false;
392  }
393 }
394 
395 
396 Sym BOXVSLIDER = symbol ("BoxVSlider");
398  { return tree(BOXVSLIDER, lbl, list4(cur,min,max,step)); }
399 bool isBoxVSlider (Tree s) { Tree lbl, params; return isTree(s, BOXVSLIDER, lbl, params); }
400 
401 bool isBoxVSlider (Tree s, Tree& lbl, Tree& cur, Tree& min, Tree& max, Tree& step)
402 {
403  Tree params;
404  if (isTree(s, BOXVSLIDER, lbl, params)) {
405  cur = nth(params, 0);
406  min = nth(params, 1);
407  max = nth(params, 2);
408  step= nth(params, 3);
409  return true;
410  } else {
411  return false;
412  }
413 }
414 
415 Sym BOXNUMENTRY = symbol ("BoxNumEntry");
417  { return tree(BOXNUMENTRY, lbl, list4(cur,min,max,step)); }
418 bool isBoxNumEntry (Tree s) { Tree lbl, params; return isTree(s, BOXNUMENTRY, lbl, params); }
419 
420 bool isBoxNumEntry (Tree s, Tree& lbl, Tree& cur, Tree& min, Tree& max, Tree& step)
421 {
422  Tree params;
423  if (isTree(s, BOXNUMENTRY, lbl, params)) {
424  cur = nth(params, 0);
425  min = nth(params, 1);
426  max = nth(params, 2);
427  step= nth(params, 3);
428  return true;
429  } else {
430  return false;
431  }
432 }
433 
434 
435 Sym BOXHGROUP = symbol ("BoxHGroup");
436 Tree boxHGroup (Tree lbl, Tree x) { return tree(BOXHGROUP, lbl, x); }
437 bool isBoxHGroup (Tree s) { Tree lbl, x; return isTree(s, BOXHGROUP, lbl, x); }
438 bool isBoxHGroup (Tree s, Tree& lbl, Tree& x) { return isTree(s, BOXHGROUP, lbl, x); }
439 
440 
441 Sym BOXVGROUP = symbol ("BoxVGroup");
442 Tree boxVGroup (Tree lbl, Tree x) { return tree(BOXVGROUP, lbl, x); }
443 bool isBoxVGroup (Tree s) { Tree lbl, x; return isTree(s, BOXVGROUP, lbl, x); }
444 bool isBoxVGroup (Tree s, Tree& lbl, Tree& x) { return isTree(s, BOXVGROUP, lbl, x); }
445 
446 
447 Sym BOXTGROUP = symbol ("BoxTGroup");
448 Tree boxTGroup (Tree lbl, Tree x) { return tree(BOXTGROUP, lbl, x); }
449 bool isBoxTGroup (Tree s) { Tree lbl, x; return isTree(s, BOXTGROUP, lbl, x); }
450 bool isBoxTGroup (Tree s, Tree& lbl, Tree& x) { return isTree(s, BOXTGROUP, lbl, x); }
451 
452 
453 Sym BOXHBARGRAPH = symbol ("BoxHBargraph");
454 Tree boxHBargraph(Tree lbl, Tree min, Tree max) { return tree(BOXHBARGRAPH, lbl, min, max); }
455 bool isBoxHBargraph (Tree s) { Tree lbl, min, max; return isTree(s, BOXHBARGRAPH, lbl, min, max); }
456 bool isBoxHBargraph (Tree s, Tree& lbl, Tree& min, Tree& max) { return isTree(s, BOXHBARGRAPH, lbl, min, max); }
457 
458 
459 Sym BOXVBARGRAPH = symbol ("BoxVBargraph");
460 Tree boxVBargraph(Tree lbl, Tree min, Tree max) { return tree(BOXVBARGRAPH, lbl, min, max); }
461 bool isBoxVBargraph (Tree s) { Tree lbl, min, max; return isTree(s, BOXVBARGRAPH, lbl, min, max); }
462 bool isBoxVBargraph (Tree s, Tree& lbl, Tree& min, Tree& max) { return isTree(s, BOXVBARGRAPH, lbl, min, max); }
463 
464 
465 /*****************************************************************************
466  pattern lmatching case
467 *****************************************************************************/
468 
469 Sym BOXCASE = symbol ("BoxCase");
470 Sym BOXPATMATCHER = symbol ("BoxPatMatcher");
471 Sym BOXPATVAR = symbol ("BoxPatVar");
472 
484 {
485 // cerr << "preparePattern(" << boxpp(box) << ")" << endl;
486 
487  int id;
488  double r;
489  prim0 p0;
490  prim1 p1;
491  prim2 p2;
492  prim3 p3;
493  prim4 p4;
494  prim5 p5;
495 
496  Tree t1, t2, t3, ff, label, cur, min, max, step, type, name, file, arg,
497  body, fun, args, ldef, slot,
498  ident, rules;
499 
500  xtended* xt = (xtended*) getUserData(box);
501 
502 
503  // primitive elements
504  if (xt) return box;
505  else if (isBoxIdent(box)) return boxPatternVar(box);
506  else if (isBoxAppl(box, fun, args)) {
507  if (isBoxIdent(fun)) return boxAppl( fun, lmap(preparePattern,args));
508  else return boxAppl( preparePattern(fun), lmap(preparePattern,args));
509  }
510  else if (isBoxAbstr(box,arg,body)) return box;
511  else if (isBoxInt(box)) return box;
512  else if (isBoxReal(box, &r)) return box;
513  else if (isBoxWaveform(box)) return box;
514  else if (isBoxCut(box)) return box;
515  else if (isBoxWire(box)) return box;
516  else if (isBoxPrim0(box, &p0)) return box;
517  else if (isBoxPrim1(box, &p1)) return box;
518  else if (isBoxPrim2(box, &p2)) return box;
519  else if (isBoxPrim3(box, &p3)) return box;
520  else if (isBoxPrim4(box, &p4)) return box;
521  else if (isBoxPrim5(box, &p5)) return box;
522 
523  else if (isBoxWithLocalDef(box, body, ldef)) return boxWithLocalDef(preparePattern(body), ldef);
524 
525 
526  // foreign elements
527  else if (isBoxFFun(box, ff)) return box;
528  else if (isBoxFConst(box, type, name, file))
529  return box;
530  else if (isBoxFVar(box, type, name, file))
531  return box;
532 
533  // block diagram binary operator
534  else if (isBoxSeq(box, t1, t2)) return boxSeq( preparePattern(t1), preparePattern(t2) );
535  else if (isBoxSplit(box, t1, t2)) return boxSplit( preparePattern(t1), preparePattern(t2) );
536  else if (isBoxMerge(box, t1, t2)) return boxMerge( preparePattern(t1), preparePattern(t2) );
537  else if (isBoxPar(box, t1, t2)) return boxPar( preparePattern(t1), preparePattern(t2) );
538  else if (isBoxRec(box, t1, t2)) return boxRec( preparePattern(t1), preparePattern(t2) );
539 
540  // iterative block diagram construction
541  else if (isBoxIPar(box, t1, t2, t3)) return boxIPar ( t1, t2, preparePattern(t3) );
542  else if (isBoxISeq(box, t1, t2, t3)) return boxISeq ( t1, t2, preparePattern(t3) );
543  else if (isBoxISum(box, t1, t2, t3)) return boxISum ( t1, t2, preparePattern(t3) );
544  else if (isBoxIProd(box, t1, t2, t3)) return boxIProd( t1, t2, preparePattern(t3) );
545 
546  // static information
547  else if (isBoxInputs(box, t1)) return boxInputs ( preparePattern(t1) );
548  else if (isBoxOutputs(box, t1)) return boxOutputs( preparePattern(t1) );
549 
550  // user interface
551  else if (isBoxButton(box, label)) return box;
552  else if (isBoxCheckbox(box, label)) return box;
553 
554  else if (isBoxVSlider(box, label, cur, min, max, step)) return box;
555  else if (isBoxHSlider(box, label, cur, min, max, step)) return box;
556 
557  else if (isBoxVGroup(box, label, t1)) return boxVGroup(label, preparePattern(t1));
558  else if (isBoxHGroup(box, label, t1)) return boxHGroup(label, preparePattern(t1));
559  else if (isBoxTGroup(box, label, t1)) return boxTGroup(label, preparePattern(t1));
560 
561  else if (isBoxHBargraph(box, label, min, max)) return box;
562  else if (isBoxVBargraph(box, label, min, max)) return box;
563  else if (isBoxNumEntry(box, label, cur, min, max, step)) return box;
564 
565  else if (isNil(box)) return box;
566  else if (isList(box)) return lmap(preparePattern, box);
567  else if (isBoxEnvironment(box)) return box;
568  /* not expected
569  else if (isClosure(box, abstr, genv, vis, lenv)) {
570  fout << "closure[" << boxpp(abstr)
571  << ", genv = " << envpp(genv)
572  << ", lenv = " << envpp(lenv)
573  << "]";
574  }
575  */
576  else if (isBoxComponent(box, label)) return box;
577  else if (isBoxAccess(box, t1, t2)) return box;
578 
579  /* not expected
580  else if (isImportFile(box, label)) {
581  fout << "import("
582  << tree2str(label) << ')';
583  }
584  */
585 
586 
587  else if (isBoxSlot(box, &id)) return box;
588  else if (isBoxSymbolic(box, slot, body)) return box;
589 
590  // Pattern Matching Extensions
591  else if (isBoxCase(box, rules)) return box;
592  else if (isBoxPatternVar(box, ident)) return box;
593 
594 
595  // None of the previous tests succeded, then it is not a valid box
596  else {
597  cerr << "Error in preparePattern() : " << *box << " is not a valid box" << endl;
598  exit(1);
599  }
600 
601 
602  return box;
603 }
604 
605 static Tree prepareRule(Tree rule)
606 {
607  return cons (lmap(preparePattern,hd(rule)), tl(rule));
608 }
609 
610 static Tree prepareRules(Tree rules) {
611  return lmap(prepareRule, rules);
612 }
613 
614 Tree boxCaseInternal (Tree rules) { return tree(BOXCASE, rules); }
615 Tree boxCase (Tree rules) { return boxCaseInternal(prepareRules(rules)); }
616 
617 bool isBoxCase (Tree s) { Tree rules; return isTree(s, BOXCASE, rules); }
618 bool isBoxCase (Tree s, Tree& rules) { return isTree(s, BOXCASE, rules); }
619 
620 
621 Tree boxPatternVar (Tree id) { return tree(BOXPATVAR, id); }
622 bool isBoxPatternVar(Tree s, Tree& id) { return isTree(s, BOXPATVAR, id); }
623 
624 
625 Tree boxPatternMatcher (Automaton* a, int state, Tree env, Tree origRules, Tree revParamList)
626 {
627  return tree(BOXPATMATCHER, tree((void*)a), tree(state), env, origRules, revParamList);
628 }
629 
631 {
632  Tree ta, ts, env, orig, rpl;
633  return isTree(s, BOXPATMATCHER, ta, ts, env, orig, rpl);
634 }
635 
636 bool isBoxPatternMatcher (Tree s, Automaton*& a, int& state, Tree& env, Tree& origRules, Tree& revParamList)
637 {
638  Tree ta, ts;
639  if (isTree(s, BOXPATMATCHER, ta, ts, env, origRules, revParamList)) {
640  a = (Automaton*)tree2ptr(ta);
641  state = tree2int(ts);
642  return true;
643  } else {
644  return false;
645  }
646 }
647 
648 
bool isBoxPrim1(Tree s)
Definition: boxes.cpp:315
bool isBoxPrim5(Tree s)
Definition: boxes.cpp:335
Sym BOXPRIM3
Definition: boxes.cpp:323
Tree(* prim2)(Tree x, Tree y)
Definition: boxes.hh:210
Sym BOXPRIM0
Definition: boxes.cpp:308
Sym BOXWAVEFORM
Definition: boxes.cpp:90
bool isBoxIPar(Tree t, Tree &x, Tree &y, Tree &z)
Definition: boxes.cpp:169
Tree boxFVar(Tree type, Tree name, Tree file)
Definition: boxes.cpp:355
bool isBoxWire(Tree t)
Definition: boxes.cpp:109
bool isImportFile(Tree s, Tree &filename)
Definition: boxes.cpp:301
Tree boxSymbolic(Tree slot, Tree body)
Definition: boxes.cpp:125
Sym BOXBUTTON
Definition: boxes.cpp:364
bool isBoxSeq(Tree t, Tree &x, Tree &y)
Definition: boxes.cpp:136
bool isBoxWithLocalDef(Tree t, Tree &body, Tree &ldef)
Definition: boxes.cpp:265
Sym BOXPRIM5
Definition: boxes.cpp:333
bool isBoxInt(Tree t)
Definition: boxes.cpp:78
Tree boxPrim5(prim5 foo)
Definition: boxes.cpp:334
Class Node = (type x (int + double + Sym + void*))
Definition: node.hh:75
bool isBoxIdent(Tree t)
Definition: boxes.cpp:58
Tree boxCheckbox(Tree lbl)
Definition: boxes.cpp:371
Sym BOXLIBRARY
Definition: boxes.cpp:292
Tree boxHBargraph(Tree lbl, Tree min, Tree max)
Definition: boxes.cpp:454
Tree boxSlot(int id)
Definition: boxes.cpp:118
bool isBoxOutputs(Tree t, Tree &x)
Definition: boxes.cpp:186
Tree cons(Tree a, Tree b)
Definition: list.hh:124
bool isBoxIProd(Tree t, Tree &x, Tree &y, Tree &z)
Definition: boxes.cpp:172
Sym BOXFFUN
Definition: boxes.cpp:342
bool isBoxPatternMatcher(Tree s)
Definition: boxes.cpp:630
int tree2int(Tree t)
if t has a node of type int, return it otherwise error
Definition: tree.cpp:230
Tree boxRec(Tree x, Tree y)
Definition: boxes.cpp:143
Sym BOXCASE
Definition: boxes.cpp:469
Tree boxPrim0(prim0 foo)
Definition: boxes.cpp:309
bool isBoxSplit(Tree t, Tree &x, Tree &y)
Definition: boxes.cpp:148
Tree boxNumEntry(Tree lbl, Tree cur, Tree min, Tree max, Tree step)
Definition: boxes.cpp:416
Tree boxEnvironment()
Definition: boxes.cpp:284
Sym BOXENVIRONMENT
Definition: boxes.cpp:282
Sym BOXIDENT
Definition: boxes.cpp:55
bool isBoxComponent(Tree s, Tree &filename)
Definition: boxes.cpp:290
Tree boxISeq(Tree x, Tree y, Tree z)
Definition: boxes.cpp:165
Sym IMPORTFILE
Definition: boxes.cpp:298
static Tree prepareRules(Tree rules)
Definition: boxes.cpp:610
Tree(* prim4)(Tree w, Tree x, Tree y, Tree z)
Definition: boxes.hh:212
Tree boxPrim1(prim1 foo)
Definition: boxes.cpp:314
vector< Tree > tvec
Definition: tree.hh:90
Tree boxFFun(Tree ff)
Definition: boxes.cpp:343
Tree boxPrim3(prim3 foo)
Definition: boxes.cpp:324
Tree lmap(tfun f, Tree l)
Definition: list.cpp:247
bool isBoxFFun(Tree s)
Definition: boxes.cpp:344
Tree boxISum(Tree x, Tree y, Tree z)
Definition: boxes.cpp:166
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
Sym BOXCOMPONENT
Definition: boxes.cpp:287
Sym BOXERROR
Definition: boxes.cpp:196
bool isBoxCut(Tree t)
Definition: boxes.cpp:105
Sym BOXWITHLOCALDEF
Definition: boxes.cpp:262
bool isBoxAppl(Tree t)
Definition: boxes.cpp:203
Sym BOXCHECKBOX
Definition: boxes.cpp:370
Sym BOXHGROUP
Definition: boxes.cpp:435
Tree boxVSlider(Tree lbl, Tree cur, Tree min, Tree max, Tree step)
Definition: boxes.cpp:397
Tree boxIdent(const char *name)
Definition: boxes.cpp:57
Tree hd(Tree l)
Definition: list.hh:133
bool isBoxAccess(Tree t, Tree &exp, Tree &id)
Definition: boxes.cpp:256
bool isBoxPrim0(Tree s)
Definition: boxes.cpp:310
bool isBoxPrim4(Tree s)
Definition: boxes.cpp:330
const Node & node() const
return the content of the tree
Definition: tree.hh:143
bool isBoxHGroup(Tree s)
Definition: boxes.cpp:437
Tree list4(Tree a, Tree b, Tree c, Tree d)
Definition: list.hh:130
bool isBoxHBargraph(Tree s)
Definition: boxes.cpp:455
Tree boxHSlider(Tree lbl, Tree cur, Tree min, Tree max, Tree step)
Definition: boxes.cpp:377
Sym BOXFVAR
Definition: boxes.cpp:354
Sym BOXSEQ
Definition: boxes.cpp:134
Sym BOXMODIFLOCALDEF
Definition: boxes.cpp:271
Tree boxCaseInternal(Tree rules)
Definition: boxes.cpp:614
Sym BOXISEQ
Definition: boxes.cpp:160
bool isBoxRec(Tree t, Tree &x, Tree &y)
Definition: boxes.cpp:144
Tree boxInt(int n)
Definition: boxes.cpp:75
double max(double x, double y)
Definition: interval.hh:60
Sym BOXVSLIDER
Definition: boxes.cpp:396
bool isBoxAbstr(Tree t)
Definition: boxes.cpp:202
Tree(* prim3)(Tree x, Tree y, Tree z)
Definition: boxes.hh:211
Tree boxWire()
Definition: boxes.cpp:108
bool isNil(Tree l)
Definition: list.hh:137
Tree(* prim5)(Tree v, Tree w, Tree x, Tree y, Tree z)
Definition: boxes.hh:213
Tree boxVBargraph(Tree lbl, Tree min, Tree max)
Definition: boxes.cpp:460
Sym BOXSLOT
Definition: boxes.cpp:116
Tree boxPatternMatcher(Automaton *a, int state, Tree env, Tree origRules, Tree revParamList)
Definition: boxes.cpp:625
bool isBoxModifLocalDef(Tree t, Tree &body, Tree &ldef)
Definition: boxes.cpp:275
bool isBoxSymbolic(Tree t)
Definition: boxes.cpp:126
bool isBoxTGroup(Tree s)
Definition: boxes.cpp:449
Tree boxButton(Tree lbl)
Definition: boxes.cpp:365
static Tree preparePattern(Tree box)
Prepare a "pattern" by replacing variables x by special pattern variables ?x.
Definition: boxes.cpp:483
bool isBoxISum(Tree t, Tree &x, Tree &y, Tree &z)
Definition: boxes.cpp:171
bool isBoxEnvironment(Tree s)
Definition: boxes.cpp:285
Sym BOXSPLIT
Definition: boxes.cpp:146
bool isBoxButton(Tree s)
Definition: boxes.cpp:366
bool isBoxReal(Tree t)
Definition: boxes.cpp:79
Sym BOXNUMENTRY
Definition: boxes.cpp:415
Sym BOXAPPL
Definition: boxes.cpp:194
Sym BOXIPAR
Definition: boxes.cpp:159
Tree boxWithLocalDef(Tree body, Tree ldef)
Definition: boxes.cpp:264
Tree boxCut()
Definition: boxes.cpp:104
bool isBoxCase(Tree s)
Definition: boxes.cpp:617
Tree(* prim1)(Tree x)
Definition: boxes.hh:209
Tree boxPatternVar(Tree id)
Definition: boxes.cpp:621
Sym BOXMERGE
Definition: boxes.cpp:150
bool isList(Tree l)
Definition: list.hh:138
bool isBoxPar(Tree t, Tree &x, Tree &y)
Definition: boxes.cpp:140
bool isBoxHSlider(Tree s)
Definition: boxes.cpp:379
Sym BOXCUT
Definition: boxes.cpp:103
Tree boxMerge(Tree x, Tree y)
Definition: boxes.cpp:151
Sym BOXPRIM4
Definition: boxes.cpp:328
bool isDouble(const Node &n)
Definition: node.hh:143
Tree boxAppl(Tree x, Tree y)
Definition: boxes.cpp:200
bool isBoxVBargraph(Tree s)
Definition: boxes.cpp:461
bool isBoxWaveform(Tree s)
Definition: boxes.cpp:97
Sym BOXTGROUP
Definition: boxes.cpp:447
Tree importFile(Tree filename)
Definition: boxes.cpp:300
bool isClosure(Tree t, Tree &abstr, Tree &genv, Tree &vis, Tree &lenv)
Definition: boxes.cpp:239
bool isSym(const Node &n)
Definition: node.hh:199
Sym BOXVBARGRAPH
Definition: boxes.cpp:459
Tree boxError()
Definition: boxes.cpp:244
bool isBoxCheckbox(Tree s)
Definition: boxes.cpp:372
Tree boxReal(double n)
Definition: boxes.cpp:76
Tree boxPrim2(prim2 foo)
Definition: boxes.cpp:319
Sym BOXACCESS
Definition: boxes.cpp:197
Tree boxWaveform(const tvec &br)
Definition: boxes.cpp:92
Tree boxAccess(Tree exp, Tree id)
Definition: boxes.cpp:255
bool isBoxVGroup(Tree s)
Definition: boxes.cpp:443
Tree boxLibrary(Tree filename)
Definition: boxes.cpp:294
Tree boxAbstr(Tree x, Tree y)
Definition: boxes.cpp:199
Sym BOXSYMBOLIC
Definition: boxes.cpp:123
Tree boxSeq(Tree x, Tree y)
Definition: boxes.cpp:135
bool isBoxFVar(Tree s)
Definition: boxes.cpp:356
Sym BOXPRIM1
Definition: boxes.cpp:313
const char * name(Symbol *sym)
Returns the name of a symbol.
Definition: symbol.hh:98
Sym CLOSURE
Definition: boxes.cpp:195
Sym BOXPATMATCHER
Definition: boxes.cpp:470
Tree boxFConst(Tree type, Tree name, Tree file)
Definition: boxes.cpp:349
Tree closure(Tree abstr, Tree genv, Tree vis, Tree lenv)
Definition: boxes.cpp:234
Sym BOXWIRE
Definition: boxes.cpp:107
Tree tree(const Node &n)
Definition: tree.hh:186
void * tree2ptr(Tree t)
if t has a node of type ptr, return it otherwise error
Definition: tree.cpp:288
bool isBoxVSlider(Tree s)
Definition: boxes.cpp:399
Tree boxIPar(Tree x, Tree y, Tree z)
Definition: boxes.cpp:164
Sym BOXIPROD
Definition: boxes.cpp:162
Tree boxHGroup(Tree lbl, Tree x)
Definition: boxes.cpp:436
Sym BOXREC
Definition: boxes.cpp:142
Symbols are unique objects with a name stored in a hash table.
Definition: symbol.hh:53
Sym BOXPATVAR
Definition: boxes.cpp:471
bool isTree(const Tree &t, const Node &n)
Definition: tree.cpp:305
bool isBoxNumEntry(Tree s)
Definition: boxes.cpp:418
Tree boxTGroup(Tree lbl, Tree x)
Definition: boxes.cpp:448
bool isBoxSlot(Tree t)
Definition: boxes.cpp:119
Tree boxPar(Tree x, Tree y)
Definition: boxes.cpp:139
bool isBoxInputs(Tree t, Tree &x)
Definition: boxes.cpp:185
void * getUserData(Symbol *sym)
Returns user data.
Definition: symbol.hh:100
bool isBoxPrim3(Tree s)
Definition: boxes.cpp:325
static Tree prepareRule(Tree rule)
Definition: boxes.cpp:605
Sym BOXPRIM2
Definition: boxes.cpp:318
bool isBoxISeq(Tree t, Tree &x, Tree &y, Tree &z)
Definition: boxes.cpp:170
bool isBoxPatternVar(Tree s, Tree &id)
Definition: boxes.cpp:622
Symbol * symbol(const char *str)
Returns (and creates if new) the symbol of name str.
Definition: symbol.hh:95
bool isBoxLibrary(Tree s, Tree &filename)
Definition: boxes.cpp:295
bool isPointer(const Node &n)
Definition: node.hh:216
Sym BOXINPUTS
Definition: boxes.cpp:179
Tree boxPrim4(prim4 foo)
Definition: boxes.cpp:329
Tree boxModifLocalDef(Tree body, Tree ldef)
Definition: boxes.cpp:274
Sym BOXABSTR
Definition: boxes.cpp:193
Tree buildBoxAbstr(Tree largs, Tree body)
Definition: boxes.cpp:208
Tree boxInputs(Tree x)
Definition: boxes.cpp:182
bool isBoxError(Tree t)
Definition: boxes.cpp:249
Tree boxOutputs(Tree x)
Definition: boxes.cpp:183
bool isBoxMerge(Tree t, Tree &x, Tree &y)
Definition: boxes.cpp:152
bool isInt(const Node &n)
Definition: node.hh:126
Tree boxComponent(Tree filename)
Definition: boxes.cpp:289
bool isBoxFConst(Tree s)
Definition: boxes.cpp:350
double min(double x, double y)
Definition: interval.hh:59
bool isBoxPrim2(Tree s)
Definition: boxes.cpp:320
Tree boxSplit(Tree x, Tree y)
Definition: boxes.cpp:147
Sym BOXPAR
Definition: boxes.cpp:138
Tree boxCase(Tree rules)
Definition: boxes.cpp:615
Sym BOXFCONST
Definition: boxes.cpp:348
Tree tl(Tree l)
Definition: list.hh:134
Tree boxVGroup(Tree lbl, Tree x)
Definition: boxes.cpp:442
Tree nth(Tree l, int i)
Definition: list.cpp:182
Sym BOXISUM
Definition: boxes.cpp:161
Sym BOXHBARGRAPH
Definition: boxes.cpp:453
Tree(* prim0)()
Definition: boxes.hh:208
Sym BOXVGROUP
Definition: boxes.cpp:441
Tree buildBoxAppl(Tree fun, Tree revarglist)
Definition: boxes.cpp:227
Tree boxIProd(Tree x, Tree y, Tree z)
Definition: boxes.cpp:167
Sym BOXHSLIDER
Definition: boxes.cpp:376
Sym BOXOUTPUTS
Definition: boxes.cpp:180