File: boxes.cpp

package info (click to toggle)
faust 2.81.10%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 431,496 kB
  • sloc: cpp: 283,941; ansic: 116,215; javascript: 18,529; sh: 14,356; vhdl: 14,052; java: 5,900; python: 5,091; objc: 3,852; makefile: 2,725; cs: 1,672; lisp: 1,146; ruby: 954; yacc: 586; xml: 471; lex: 247; awk: 111; tcl: 26
file content (1184 lines) | stat: -rw-r--r-- 31,586 bytes parent folder | download | duplicates (2)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
/************************************************************************
 ************************************************************************
    FAUST compiler
    Copyright (C) 2003-2018 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 Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.

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

/*****************************************************************************
******************************************************************************

                                The Box Language

******************************************************************************
*****************************************************************************/

/**\file boxes.cpp
 * \author Yann Orlarey
 * \version 1.0
 * \date 2003
 * \brief Implementation of block diagram expressions.
 *  Boxes are created using five main connection operations : sequential (:),
 *  parallel (,), split (<:), merge (:>), and recursive (~).
 */

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

using namespace std;

/*****************************************************************************
                                    Identifiers
*****************************************************************************/

Tree boxIdent(const char* name)
{
    return tree(gGlobal->BOXIDENT, tree(symbol(name)));
}
LIBFAUST_API bool isBoxIdent(Tree t)
{
    return t->node() == Node(gGlobal->BOXIDENT);
}
LIBFAUST_API bool isBoxIdent(Tree t0, const char** str)
{
    Tree t1;
    Sym  s;
    if (isTree(t0, gGlobal->BOXIDENT, t1) && isSym(t1->node(), &s)) {
        *str = name(s);
        return true;
    } else {
        return false;
    }
}

/*****************************************************************************
                                    Numbers
*****************************************************************************/

LIBFAUST_API Tree boxInt(int n)
{
    return tree(n);
}
LIBFAUST_API Tree boxReal(double n)
{
    return tree(n);
}

LIBFAUST_API bool isBoxInt(Tree t)
{
    return isInt(t->node());
}
LIBFAUST_API bool isBoxReal(Tree t)
{
    return isDouble(t->node());
}

LIBFAUST_API bool isBoxInt(Tree t, int* i)
{
    return isInt(t->node(), i);
}
LIBFAUST_API bool isBoxReal(Tree t, double* r)
{
    return isDouble(t->node(), r);
}

/*****************************************************************************
                             Waveform
*****************************************************************************/

LIBFAUST_API Tree boxWaveform(const tvec& br)
{
    return tree(gGlobal->BOXWAVEFORM, br);
}

LIBFAUST_API bool isBoxWaveform(Tree s)
{
    return isTree(s, gGlobal->BOXWAVEFORM);
}

/*****************************************************************************
                             Route
*****************************************************************************/

LIBFAUST_API Tree boxRoute(Tree n, Tree m, Tree r)
{
    return tree(gGlobal->BOXROUTE, n, m, r);
}

LIBFAUST_API bool isBoxRoute(Tree s, Tree& n, Tree& m, Tree& r)
{
    return isTree(s, gGlobal->BOXROUTE, n, m, r);
}

/*****************************************************************************
                                Wire and Cut
*****************************************************************************/

LIBFAUST_API Tree boxCut()
{
    return tree(gGlobal->BOXCUT);
}
LIBFAUST_API bool isBoxCut(Tree t)
{
    return isTree(t, gGlobal->BOXCUT);
}

LIBFAUST_API Tree boxWire()
{
    return tree(gGlobal->BOXWIRE);
}
LIBFAUST_API bool isBoxWire(Tree t)
{
    return isTree(t, gGlobal->BOXWIRE);
}

/*****************************************************************************
                        Symbolic Boxes with symbolic slots
*****************************************************************************/

Tree boxSlot(int id)
{
    return tree(gGlobal->BOXSLOT, tree(id));
}
LIBFAUST_API bool isBoxSlot(Tree t)
{
    Tree w;
    return isTree(t, gGlobal->BOXSLOT, w);
}
LIBFAUST_API bool isBoxSlot(Tree t, int* id)
{
    Tree w;
    return isTree(t, gGlobal->BOXSLOT, w) && isInt(w->node(), id);
}

Tree boxSymbolic(Tree slot, Tree body)
{
    return tree(gGlobal->BOXSYMBOLIC, slot, body);
}
LIBFAUST_API bool isBoxSymbolic(Tree t)
{
    Tree slot, body;
    return isTree(t, gGlobal->BOXSYMBOLIC, slot, body);
}
LIBFAUST_API bool isBoxSymbolic(Tree t, Tree& slot, Tree& body)
{
    return isTree(t, gGlobal->BOXSYMBOLIC, slot, body);
}

/*****************************************************************************
                              Composition of Boxes
*****************************************************************************/

LIBFAUST_API Tree boxSeq(Tree x, Tree y)
{
    return tree(gGlobal->BOXSEQ, x, y);
}
LIBFAUST_API bool isBoxSeq(Tree t, Tree& x, Tree& y)
{
    return isTree(t, gGlobal->BOXSEQ, x, y);
}

LIBFAUST_API Tree boxPar(Tree x, Tree y)
{
    return tree(gGlobal->BOXPAR, x, y);
}
LIBFAUST_API bool isBoxPar(Tree t, Tree& x, Tree& y)
{
    return isTree(t, gGlobal->BOXPAR, x, y);
}

LIBFAUST_API Tree boxRec(Tree x, Tree y)
{
    return tree(gGlobal->BOXREC, x, y);
}
LIBFAUST_API bool isBoxRec(Tree t, Tree& x, Tree& y)
{
    return isTree(t, gGlobal->BOXREC, x, y);
}

LIBFAUST_API Tree boxSplit(Tree x, Tree y)
{
    return tree(gGlobal->BOXSPLIT, x, y);
}
LIBFAUST_API bool isBoxSplit(Tree t, Tree& x, Tree& y)
{
    return isTree(t, gGlobal->BOXSPLIT, x, y);
}

LIBFAUST_API Tree boxMerge(Tree x, Tree y)
{
    return tree(gGlobal->BOXMERGE, x, y);
}
LIBFAUST_API bool isBoxMerge(Tree t, Tree& x, Tree& y)
{
    return isTree(t, gGlobal->BOXMERGE, x, y);
}

/*****************************************************************************
                        Algorithmic Composition of Boxes
*****************************************************************************/

Tree boxIPar(Tree x, Tree y, Tree z)
{
    return tree(gGlobal->BOXIPAR, x, y, z);
}
Tree boxISeq(Tree x, Tree y, Tree z)
{
    return tree(gGlobal->BOXISEQ, x, y, z);
}
Tree boxISum(Tree x, Tree y, Tree z)
{
    return tree(gGlobal->BOXISUM, x, y, z);
}
Tree boxIProd(Tree x, Tree y, Tree z)
{
    return tree(gGlobal->BOXIPROD, x, y, z);
}

LIBFAUST_API bool isBoxIPar(Tree t, Tree& x, Tree& y, Tree& z)
{
    return isTree(t, gGlobal->BOXIPAR, x, y, z);
}
LIBFAUST_API bool isBoxISeq(Tree t, Tree& x, Tree& y, Tree& z)
{
    return isTree(t, gGlobal->BOXISEQ, x, y, z);
}
LIBFAUST_API bool isBoxISum(Tree t, Tree& x, Tree& y, Tree& z)
{
    return isTree(t, gGlobal->BOXISUM, x, y, z);
}
LIBFAUST_API bool isBoxIProd(Tree t, Tree& x, Tree& y, Tree& z)
{
    return isTree(t, gGlobal->BOXIPROD, x, y, z);
}

/*****************************************************************************
                        Static information on Boxes
*****************************************************************************/

Tree boxInputs(Tree x)
{
    return tree(gGlobal->BOXINPUTS, x);
}
Tree boxOutputs(Tree x)
{
    return tree(gGlobal->BOXOUTPUTS, x);
}

LIBFAUST_API bool isBoxInputs(Tree t, Tree& x)
{
    return isTree(t, gGlobal->BOXINPUTS, x);
}
LIBFAUST_API bool isBoxOutputs(Tree t, Tree& x)
{
    return isTree(t, gGlobal->BOXOUTPUTS, x);
}

/*****************************************************************************
                              Lambda-Calculus of Boxes
*****************************************************************************/

Tree boxAbstr(Tree x, Tree y)
{
    return tree(gGlobal->BOXABSTR, x, y);
}
Tree boxModulation(Tree x, Tree y)
{
    return tree(gGlobal->BOXMODULATION, x, y);
}
Tree boxAppl(Tree x, Tree y)
{
    return tree(gGlobal->BOXAPPL, x, y);
}

LIBFAUST_API bool isBoxAbstr(Tree t)
{
    return t->node() == Node(gGlobal->BOXABSTR);
}

LIBFAUST_API bool isBoxModulation(Tree t)
{
    return t->node() == Node(gGlobal->BOXMODULATION);
}
LIBFAUST_API bool isBoxAppl(Tree t)
{
    return t->node() == Node(gGlobal->BOXAPPL);
}

LIBFAUST_API bool isBoxAbstr(Tree t, Tree& x, Tree& y)
{
    return isTree(t, gGlobal->BOXABSTR, x, y);
}

LIBFAUST_API bool isBoxModulation(Tree t, Tree& x, Tree& y)
{
    return isTree(t, gGlobal->BOXMODULATION, x, y);
}
LIBFAUST_API bool isBoxAppl(Tree t, Tree& x, Tree& y)
{
    return isTree(t, gGlobal->BOXAPPL, x, y);
}

Tree buildBoxAbstr(Tree largs, Tree body)
{
    if (isNil(largs)) {
        return body;
    } else {
        return buildBoxAbstr(tl(largs), boxAbstr(hd(largs), body));
    }
}

Tree buildBoxModulation(Tree largs, Tree body)
{
    if (isNil(largs)) {
        return body;
    } else {
        return buildBoxModulation(tl(largs), boxModulation(hd(largs), body));
    }
}

#if 0
Tree buildBoxAppl(Tree fun, Tree revarglist)
{
	if (isNil(revarglist)) {
		return fun;
	} else {
		return boxAppl(buildBoxAppl(fun, tl(revarglist)), hd(revarglist));
	}
}
#else

Tree buildBoxAppl(Tree fun, Tree revarglist)
{
    if (isNil(revarglist)) {
        cerr << "ASSERT : buildBoxAppl called with null revarglist\n";
        faustassert(false);
    }
    return boxAppl(fun, revarglist);
}
#endif

Tree closure(Tree abstr, Tree genv, Tree vis, Tree lenv)
{
    return tree(gGlobal->CLOSURE, abstr, genv, vis, lenv);
}

bool isClosure(Tree t, Tree& abstr, Tree& genv, Tree& vis, Tree& lenv)
{
    return isTree(t, gGlobal->CLOSURE, abstr, genv, vis, lenv);
}

Tree boxError()
{
    return tree(gGlobal->BOXERROR);
}

LIBFAUST_API bool isBoxError(Tree t)
{
    return isTree(t, gGlobal->BOXERROR);
}

Tree boxAccess(Tree exp, Tree id)
{
    return tree(gGlobal->BOXACCESS, exp, id);
}
LIBFAUST_API bool isBoxAccess(Tree t, Tree& exp, Tree& id)
{
    return isTree(t, gGlobal->BOXACCESS, exp, id);
}

/*****************************************************************************
                        Boxes with local definitions
*****************************************************************************/

Tree boxWithLocalDef(Tree body, Tree ldef)
{
    return tree(gGlobal->BOXWITHLOCALDEF, body, ldef);
}
LIBFAUST_API bool isBoxWithLocalDef(Tree t, Tree& body, Tree& ldef)
{
    return isTree(t, gGlobal->BOXWITHLOCALDEF, body, ldef);
}

/*****************************************************************************
                        Boxes with recursive definitions
 *****************************************************************************/

// def2names transforms a liste of definition (name.expression) into a list of name
static Tree def2names(Tree ldef)
{
    if (isNil(ldef)) {
        return gGlobal->nil;
    } else {
        Tree def = hd(ldef);
        return cons(hd(def), def2names(tl(ldef)));
    }
}

// def2exp transforms a list of definition (name.expression) into a list of expressions
static Tree def2exp(Tree ldef)
{
    if (isNil(ldef)) {
        return gGlobal->nil;
    } else {
        Tree def = hd(ldef);
        return cons(tl(def), def2exp(tl(ldef)));
    }
}

// makeBus(3) => "_,_,_"
static Tree makeBus(int n)
{
    return (n <= 1) ? boxWire() : boxPar(boxWire(), makeBus(n - 1));
}

// makeParList((a,b,d)) => "a,b,c"
static Tree makeParList(Tree lexp)
{
    Tree l2 = tl(lexp);
    if (isNil(l2)) {
        return hd(lexp);
    } else {
        return boxPar(hd(lexp), makeParList(l2));
    }
}

// makeBoxAbstr(largs,body) => \(lnames).(body)
static Tree makeBoxAbstr(Tree largs, Tree body)
{
    if (isNil(largs)) {
        return body;
    } else {
        return boxAbstr(hd(largs), makeBoxAbstr(tl(largs), body));
    }
}

// makeSelector(5,2) => "!,!,_,!,!"
static Tree makeSelector(int n, int i)
{
    Tree op = (i == 0) ? boxWire() : boxCut();
    return (n == 1) ? op : boxPar(op, makeSelector(n - 1, i - 1));
}

// defines each symbol si of lnames as => "si = RECURSIVEBODY : select(n,i);"
static Tree makeRecProjectionsList(int n, int i, Tree lnames, Tree ldef)
{
    if (i == n) {
        return ldef;
    } else {
        Tree sel = boxSeq(gGlobal->LETRECBODY, makeSelector(n, i));
        return cons(cons(hd(lnames), sel), makeRecProjectionsList(n, i + 1, tl(lnames), ldef));
    }
}

// buildRecursiveBodyDef(n,lnames,lexp) => "RECURSIVEBODY = \(lnames).(lexp) ~ bus(n);"
static Tree buildRecursiveBodyDef(int n, Tree lnames, Tree lexp, Tree ldef2)
{
    if (ldef2 == gGlobal->nil) {
        return cons(gGlobal->LETRECBODY,
                    boxRec(makeBoxAbstr(lnames, makeParList(lexp)), makeBus(n)));
    } else {
        return cons(
            gGlobal->LETRECBODY,
            boxRec(makeBoxAbstr(lnames, boxWithLocalDef(makeParList(lexp), ldef2)), makeBus(n)));
    }
}

//----------------------------------------------------------------------------
// Transform a letrec expression into a with expression. The 'where' part
// is optional. It allows for common local definitions. It is translated into
// a with{} inside the body of the abstraction. Only signals x and y are
// visible outside the letrec. The foo definition after the where is not visible
// outside.
//
// process = x * y letrec {
//  'x = foo(x,y);
//  'y = foo(y,x);
//  where
//    foo(u, v) = u - v;
//  };
//----------------------------------------------------------------------------
Tree boxWithRecDef(Tree body, Tree ldef, Tree ldef2)
{
    // cout << "list of recursive definitions : " << *ldef << endl;
    Tree lnames = def2names(ldef);
    Tree lexp   = def2exp(ldef);
    int  n      = len(ldef);

    Tree rdef = buildRecursiveBodyDef(n, lnames, lexp, ldef2);
    Tree lrp  = makeRecProjectionsList(n, 0, lnames, gGlobal->nil);
    Tree w    = boxWithLocalDef(body, cons(rdef, lrp));
    // cerr << "boxWithRecDef(" << boxpp(body) << ',' << *ldef << ") -> " << boxpp(w) << endl;
    return w;
}

/*****************************************************************************
                        Boxes modif local definitions
*****************************************************************************/

Tree boxModifLocalDef(Tree body, Tree ldef)
{
    return tree(gGlobal->BOXMODIFLOCALDEF, body, ldef);
}
bool isBoxModifLocalDef(Tree t, Tree& body, Tree& ldef)
{
    return isTree(t, gGlobal->BOXMODIFLOCALDEF, body, ldef);
}

/*****************************************************************************
                             Modules
*****************************************************************************/

Tree boxEnvironment()
{
    return tree(gGlobal->BOXENVIRONMENT);
}
LIBFAUST_API bool isBoxEnvironment(Tree s)
{
    return isTree(s, gGlobal->BOXENVIRONMENT);
}

Tree boxComponent(Tree filename)
{
    return tree(gGlobal->BOXCOMPONENT, filename);
}
LIBFAUST_API bool isBoxComponent(Tree s, Tree& filename)
{
    return isTree(s, gGlobal->BOXCOMPONENT, filename);
}

Tree boxLibrary(Tree filename)
{
    return tree(gGlobal->BOXLIBRARY, filename);
}
LIBFAUST_API bool isBoxLibrary(Tree s, Tree& filename)
{
    return isTree(s, gGlobal->BOXLIBRARY, filename);
}

Tree importFile(Tree filename)
{
    return tree(gGlobal->IMPORTFILE, filename);
}
bool isImportFile(Tree s, Tree& filename)
{
    return isTree(s, gGlobal->IMPORTFILE, filename);
}

/*****************************************************************************
                            External Primitive Boxes (n -> 1)
*****************************************************************************/

LIBFAUST_API Tree boxPrim0(prim0 foo)
{
    return tree(gGlobal->BOXPRIM0, tree((void*)foo));
}
LIBFAUST_API bool isBoxPrim0(Tree s)
{
    Tree t;
    return isTree(s, gGlobal->BOXPRIM0, t);
}
LIBFAUST_API bool isBoxPrim0(Tree s, prim0* p)
{
    Tree t;
    return isTree(s, gGlobal->BOXPRIM0, t) && isPointer(t->node(), (void**)p);
}

LIBFAUST_API Tree boxPrim1(prim1 foo)
{
    return tree(gGlobal->BOXPRIM1, tree((void*)foo));
}
LIBFAUST_API bool isBoxPrim1(Tree s)
{
    Tree t;
    return isTree(s, gGlobal->BOXPRIM1, t);
}
LIBFAUST_API bool isBoxPrim1(Tree s, prim1* p)
{
    Tree t;
    return isTree(s, gGlobal->BOXPRIM1, t) && isPointer(t->node(), (void**)p);
}

LIBFAUST_API Tree boxPrim2(prim2 foo)
{
    return tree(gGlobal->BOXPRIM2, tree((void*)foo));
}
LIBFAUST_API bool isBoxPrim2(Tree s)
{
    Tree t;
    return isTree(s, gGlobal->BOXPRIM2, t);
}
LIBFAUST_API bool isBoxPrim2(Tree s, prim2* p)
{
    Tree t;
    return isTree(s, gGlobal->BOXPRIM2, t) && isPointer(t->node(), (void**)p);
}

LIBFAUST_API Tree boxPrim3(prim3 foo)
{
    return tree(gGlobal->BOXPRIM3, tree((void*)foo));
}
LIBFAUST_API bool isBoxPrim3(Tree s)
{
    Tree t;
    return isTree(s, gGlobal->BOXPRIM3, t);
}
LIBFAUST_API bool isBoxPrim3(Tree s, prim3* p)
{
    Tree t;
    return isTree(s, gGlobal->BOXPRIM3, t) && isPointer(t->node(), (void**)p);
}

LIBFAUST_API Tree boxPrim4(prim4 foo)
{
    return tree(gGlobal->BOXPRIM4, tree((void*)foo));
}
LIBFAUST_API bool isBoxPrim4(Tree s)
{
    Tree t;
    return isTree(s, gGlobal->BOXPRIM4, t);
}
LIBFAUST_API bool isBoxPrim4(Tree s, prim4* p)
{
    Tree t;
    return isTree(s, gGlobal->BOXPRIM4, t) && isPointer(t->node(), (void**)p);
}

LIBFAUST_API Tree boxPrim5(prim5 foo)
{
    return tree(gGlobal->BOXPRIM5, tree((void*)foo));
}
LIBFAUST_API bool isBoxPrim5(Tree s)
{
    Tree t;
    return isTree(s, gGlobal->BOXPRIM5, t);
}
LIBFAUST_API bool isBoxPrim5(Tree s, prim5* p)
{
    Tree t;
    return isTree(s, gGlobal->BOXPRIM5, t) && isPointer(t->node(), (void**)p);
}

/*****************************************************************************
                             Foreign Functions
*****************************************************************************/

LIBFAUST_API Tree boxFFun(Tree ff)
{
    return tree(gGlobal->BOXFFUN, ff);
}
LIBFAUST_API bool isBoxFFun(Tree s)
{
    Tree ff;
    return isTree(s, gGlobal->BOXFFUN, ff);
}
LIBFAUST_API bool isBoxFFun(Tree s, Tree& ff)
{
    return isTree(s, gGlobal->BOXFFUN, ff);
}

LIBFAUST_API Tree boxFConst(Tree type, Tree name, Tree file)
{
    return tree(gGlobal->BOXFCONST, type, name, file);
}
LIBFAUST_API bool isBoxFConst(Tree s)
{
    Tree t, n, f;
    return isTree(s, gGlobal->BOXFCONST, t, n, f);
}
LIBFAUST_API bool isBoxFConst(Tree s, Tree& type, Tree& name, Tree& file)
{
    return isTree(s, gGlobal->BOXFCONST, type, name, file);
}

LIBFAUST_API Tree boxFVar(Tree type, Tree name, Tree file)
{
    return tree(gGlobal->BOXFVAR, type, name, file);
}
LIBFAUST_API bool isBoxFVar(Tree s)
{
    Tree t, n, f;
    return isTree(s, gGlobal->BOXFVAR, t, n, f);
}
LIBFAUST_API bool isBoxFVar(Tree s, Tree& type, Tree& name, Tree& file)
{
    return isTree(s, gGlobal->BOXFVAR, type, name, file);
}

/*****************************************************************************
                             User Interface Elements
*****************************************************************************/

LIBFAUST_API Tree boxButton(Tree lbl)
{
    return tree(gGlobal->BOXBUTTON, lbl);
}
LIBFAUST_API bool isBoxButton(Tree s)
{
    Tree lbl;
    return isTree(s, gGlobal->BOXBUTTON, lbl);
}
LIBFAUST_API bool isBoxButton(Tree s, Tree& lbl)
{
    return isTree(s, gGlobal->BOXBUTTON, lbl);
}

LIBFAUST_API Tree boxCheckbox(Tree lbl)
{
    return tree(gGlobal->BOXCHECKBOX, lbl);
}
LIBFAUST_API bool isBoxCheckbox(Tree s)
{
    Tree lbl;
    return isTree(s, gGlobal->BOXCHECKBOX, lbl);
}
LIBFAUST_API bool isBoxCheckbox(Tree s, Tree& lbl)
{
    return isTree(s, gGlobal->BOXCHECKBOX, lbl);
}

LIBFAUST_API Tree boxHSlider(Tree lbl, Tree cur, Tree min, Tree max, Tree step)
{
    return tree(gGlobal->BOXHSLIDER, lbl, list4(cur, min, max, step));
}
LIBFAUST_API bool isBoxHSlider(Tree s)
{
    Tree lbl, params;
    return isTree(s, gGlobal->BOXHSLIDER, lbl, params);
}

LIBFAUST_API bool isBoxHSlider(Tree s, Tree& lbl, Tree& cur, Tree& min, Tree& max, Tree& step)
{
    Tree params;
    if (isTree(s, gGlobal->BOXHSLIDER, lbl, params)) {
        cur  = nth(params, 0);
        min  = nth(params, 1);
        max  = nth(params, 2);
        step = nth(params, 3);
        return true;
    } else {
        return false;
    }
}

LIBFAUST_API Tree boxVSlider(Tree lbl, Tree cur, Tree min, Tree max, Tree step)
{
    return tree(gGlobal->BOXVSLIDER, lbl, list4(cur, min, max, step));
}
LIBFAUST_API bool isBoxVSlider(Tree s)
{
    Tree lbl, params;
    return isTree(s, gGlobal->BOXVSLIDER, lbl, params);
}

LIBFAUST_API bool isBoxVSlider(Tree s, Tree& lbl, Tree& cur, Tree& min, Tree& max, Tree& step)
{
    Tree params;
    if (isTree(s, gGlobal->BOXVSLIDER, lbl, params)) {
        cur  = nth(params, 0);
        min  = nth(params, 1);
        max  = nth(params, 2);
        step = nth(params, 3);
        return true;
    } else {
        return false;
    }
}

LIBFAUST_API Tree boxNumEntry(Tree lbl, Tree cur, Tree min, Tree max, Tree step)
{
    return tree(gGlobal->BOXNUMENTRY, lbl, list4(cur, min, max, step));
}
LIBFAUST_API bool isBoxNumEntry(Tree s)
{
    Tree lbl, params;
    return isTree(s, gGlobal->BOXNUMENTRY, lbl, params);
}

LIBFAUST_API bool isBoxNumEntry(Tree s, Tree& lbl, Tree& cur, Tree& min, Tree& max, Tree& step)
{
    Tree params;
    if (isTree(s, gGlobal->BOXNUMENTRY, lbl, params)) {
        cur  = nth(params, 0);
        min  = nth(params, 1);
        max  = nth(params, 2);
        step = nth(params, 3);
        return true;
    } else {
        return false;
    }
}

LIBFAUST_API Tree boxHGroup(Tree lbl, Tree x)
{
    return tree(gGlobal->BOXHGROUP, lbl, x);
}
LIBFAUST_API bool isBoxHGroup(Tree s)
{
    Tree lbl, x;
    return isTree(s, gGlobal->BOXHGROUP, lbl, x);
}
LIBFAUST_API bool isBoxHGroup(Tree s, Tree& lbl, Tree& x)
{
    return isTree(s, gGlobal->BOXHGROUP, lbl, x);
}

LIBFAUST_API Tree boxVGroup(Tree lbl, Tree x)
{
    return tree(gGlobal->BOXVGROUP, lbl, x);
}
LIBFAUST_API bool isBoxVGroup(Tree s)
{
    Tree lbl, x;
    return isTree(s, gGlobal->BOXVGROUP, lbl, x);
}
LIBFAUST_API bool isBoxVGroup(Tree s, Tree& lbl, Tree& x)
{
    return isTree(s, gGlobal->BOXVGROUP, lbl, x);
}

LIBFAUST_API Tree boxTGroup(Tree lbl, Tree x)
{
    return tree(gGlobal->BOXTGROUP, lbl, x);
}
LIBFAUST_API bool isBoxTGroup(Tree s)
{
    Tree lbl, x;
    return isTree(s, gGlobal->BOXTGROUP, lbl, x);
}
LIBFAUST_API bool isBoxTGroup(Tree s, Tree& lbl, Tree& x)
{
    return isTree(s, gGlobal->BOXTGROUP, lbl, x);
}

LIBFAUST_API Tree boxHBargraph(Tree lbl, Tree min, Tree max)
{
    return tree(gGlobal->BOXHBARGRAPH, lbl, min, max);
}
LIBFAUST_API bool isBoxHBargraph(Tree s)
{
    Tree lbl, min, max;
    return isTree(s, gGlobal->BOXHBARGRAPH, lbl, min, max);
}
LIBFAUST_API bool isBoxHBargraph(Tree s, Tree& lbl, Tree& min, Tree& max)
{
    return isTree(s, gGlobal->BOXHBARGRAPH, lbl, min, max);
}

LIBFAUST_API Tree boxVBargraph(Tree lbl, Tree min, Tree max)
{
    return tree(gGlobal->BOXVBARGRAPH, lbl, min, max);
}
LIBFAUST_API bool isBoxVBargraph(Tree s)
{
    Tree lbl, min, max;
    return isTree(s, gGlobal->BOXVBARGRAPH, lbl, min, max);
}
LIBFAUST_API bool isBoxVBargraph(Tree s, Tree& lbl, Tree& min, Tree& max)
{
    return isTree(s, gGlobal->BOXVBARGRAPH, lbl, min, max);
}

LIBFAUST_API Tree boxSoundfile(Tree lbl, Tree chan)
{
    return tree(gGlobal->BOXSOUNDFILE, lbl, chan);
}

LIBFAUST_API bool isBoxSoundfile(Tree s)
{
    Tree label;
    Tree chan;
    return isTree(s, gGlobal->BOXSOUNDFILE, label, chan);
}

LIBFAUST_API bool isBoxSoundfile(Tree s, Tree& label, Tree& chan)
{
    return isTree(s, gGlobal->BOXSOUNDFILE, label, chan);
}

/*****************************************************************************
                             Metadata (pattern matching)
*****************************************************************************/
Tree boxMetadata(Tree exp, Tree mdlist)
{
    return tree(gGlobal->BOXMETADATA, exp, mdlist);
}

LIBFAUST_API bool isBoxMetadata(Tree s, Tree& exp, Tree& mdlist)
{
    return isTree(s, gGlobal->BOXMETADATA, exp, mdlist);
}

/*****************************************************************************
                             Pattern matching case
*****************************************************************************/

/**
 * Prepare a "pattern" by replacing variables x by special
 * pattern variables ?x.
 *
 * P[x]     -> ?x
 * P[x(e)]  -> x(P[e])
 * P[e(f)]  -> P[e](P[f])
 * P[e:f]   -> P[e]:P[f]
 * etc.
 */
static Tree preparePattern(Tree box)
{
    //    cerr << "preparePattern(" << boxpp(box) << ")" << endl;

    int    id;
    double r;
    prim0  p0;
    prim1  p1;
    prim2  p2;
    prim3  p3;
    prim4  p4;
    prim5  p5;

    Tree t1, t2, t3, ff, label, cur, min, max, step, type, name, file, arg, body, fun, args, ldef,
        slot, ident, rules;

    xtended* xt = (xtended*)getUserData(box);

    // Primitive elements
    if (xt) {
        return box;
    } else if (isBoxIdent(box)) {
        return boxPatternVar(box);
    } else if (isBoxAppl(box, fun, args)) {
        if (isBoxIdent(fun)) {
            return boxAppl(fun, lmap(preparePattern, args));
        } else {
            return boxAppl(preparePattern(fun), lmap(preparePattern, args));
        }
    } else if (isBoxAbstr(box, arg, body)) {
        return box;
    } else if (isBoxInt(box)) {
        return box;
    } else if (isBoxReal(box, &r)) {
        return box;
    } else if (isBoxWaveform(box)) {
        return box;
    } else if (isBoxCut(box)) {
        return box;
    } else if (isBoxWire(box)) {
        return box;
    } else if (isBoxPrim0(box, &p0)) {
        return box;
    } else if (isBoxPrim1(box, &p1)) {
        return box;
    } else if (isBoxPrim2(box, &p2)) {
        return box;
    } else if (isBoxPrim3(box, &p3)) {
        return box;
    } else if (isBoxPrim4(box, &p4)) {
        return box;
    } else if (isBoxPrim5(box, &p5)) {
        return box;
    }

    else if (isBoxWithLocalDef(box, body, ldef)) {
        return boxWithLocalDef(preparePattern(body), ldef);
    }

    // Foreign elements
    else if (isBoxFFun(box, ff)) {
        return box;
    } else if (isBoxFConst(box, type, name, file)) {
        return box;
    } else if (isBoxFVar(box, type, name, file)) {
        return box;
    }

    // Block diagram binary operator
    else if (isBoxSeq(box, t1, t2)) {
        return boxSeq(preparePattern(t1), preparePattern(t2));
    } else if (isBoxSplit(box, t1, t2)) {
        return boxSplit(preparePattern(t1), preparePattern(t2));
    } else if (isBoxMerge(box, t1, t2)) {
        return boxMerge(preparePattern(t1), preparePattern(t2));
    } else if (isBoxPar(box, t1, t2)) {
        return boxPar(preparePattern(t1), preparePattern(t2));
    } else if (isBoxRec(box, t1, t2)) {
        return boxRec(preparePattern(t1), preparePattern(t2));
    }

    // Iterative block diagram construction
    else if (isBoxIPar(box, t1, t2, t3)) {
        return boxIPar(t1, t2, preparePattern(t3));
    } else if (isBoxISeq(box, t1, t2, t3)) {
        return boxISeq(t1, t2, preparePattern(t3));
    } else if (isBoxISum(box, t1, t2, t3)) {
        return boxISum(t1, t2, preparePattern(t3));
    } else if (isBoxIProd(box, t1, t2, t3)) {
        return boxIProd(t1, t2, preparePattern(t3));
    }

    // Static information
    else if (isBoxInputs(box, t1)) {
        return boxInputs(preparePattern(t1));
    } else if (isBoxOutputs(box, t1)) {
        return boxOutputs(preparePattern(t1));
    }

    // User interface
    else if (isBoxButton(box, label)) {
        return box;
    } else if (isBoxCheckbox(box, label)) {
        return box;
    }

    else if (isBoxVSlider(box, label, cur, min, max, step)) {
        return box;
    } else if (isBoxHSlider(box, label, cur, min, max, step)) {
        return box;
    }

    else if (isBoxVGroup(box, label, t1)) {
        return boxVGroup(label, preparePattern(t1));
    } else if (isBoxHGroup(box, label, t1)) {
        return boxHGroup(label, preparePattern(t1));
    } else if (isBoxTGroup(box, label, t1)) {
        return boxTGroup(label, preparePattern(t1));
    }

    else if (isBoxHBargraph(box, label, min, max)) {
        return box;
    } else if (isBoxVBargraph(box, label, min, max)) {
        return box;
    } else if (isBoxNumEntry(box, label, cur, min, max, step)) {
        return box;
    }

    else if (isNil(box)) {
        return box;
    } else if (isList(box)) {
        return lmap(preparePattern, box);
    } else if (isBoxEnvironment(box)) {
        return box;
    }
    /* not expected
    else if (isClosure(box, abstr, genv, vis, lenv)) {
        fout << "closure[" << boxpp(abstr)
            << ", genv = " << envpp(genv)
            << ", lenv = " << envpp(lenv)
            << "]";
    }
    */
    else if (isBoxComponent(box, label)) {
        return box;
    } else if (isBoxAccess(box, t1, t2)) {
        return box;
    }

    /* not expected
    else if (isImportFile(box, label)) {
        fout << "import("
            << tree2str(label) << ')';
    }
    */

    else if (isBoxSlot(box, &id)) {
        return box;
    } else if (isBoxSymbolic(box, slot, body)) {
        return box;
    }

    // Pattern Matching Extensions
    else if (isBoxCase(box, rules)) {
        return box;
    } else if (isBoxPatternVar(box, ident)) {
        return box;
    }

    // None of the previous tests succeded, then it is not a valid box
    else {
        stringstream error;
        error << "ERROR : preparePattern() : " << *box << " is not a valid box" << endl;
        throw faustexception(error.str());
    }

    return box;
}

static Tree prepareRule(Tree rule)
{
    return cons(lmap(preparePattern, hd(rule)), tl(rule));
}

static Tree prepareRules(Tree rules)
{
    return lmap(prepareRule, rules);
}

Tree boxCaseInternal(Tree rules)
{
    return tree(gGlobal->BOXCASE, rules);
}
Tree boxCase(Tree rules)
{
    return boxCaseInternal(prepareRules(rules));
}

LIBFAUST_API bool isBoxCase(Tree s)
{
    Tree rules;
    return isTree(s, gGlobal->BOXCASE, rules);
}
LIBFAUST_API bool isBoxCase(Tree s, Tree& rules)
{
    return isTree(s, gGlobal->BOXCASE, rules);
}

Tree boxPatternVar(Tree id)
{
    return tree(gGlobal->BOXPATVAR, id);
}
bool isBoxPatternVar(Tree s, Tree& id)
{
    return isTree(s, gGlobal->BOXPATVAR, id);
}

Tree boxPatternMatcher(PM::Automaton* a, int state, Tree env, Tree origRules, Tree revParamList)
{
    return tree(gGlobal->BOXPATMATCHER, tree((void*)a), tree(state), env, origRules, revParamList);
}

bool isBoxPatternMatcher(Tree s)
{
    Tree ta, ts, env, orig, rpl;
    return isTree(s, gGlobal->BOXPATMATCHER, ta, ts, env, orig, rpl);
}

bool isBoxPatternMatcher(Tree s, PM::Automaton*& a, int& state, Tree& env, Tree& origRules,
                         Tree& revParamList)
{
    Tree ta, ts;
    if (isTree(s, gGlobal->BOXPATMATCHER, ta, ts, env, origRules, revParamList)) {
        a     = (PM::Automaton*)tree2ptr(ta);
        state = tree2int(ts);
        return true;
    } else {
        return false;
    }
}