FAUST compiler  0.9.9.6b8
boxes.cpp
Go to the documentation of this file.
00001 /************************************************************************
00002  ************************************************************************
00003     FAUST compiler
00004     Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
00005     ---------------------------------------------------------------------
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  ************************************************************************
00020  ************************************************************************/
00021 
00022 
00023 
00024 /*****************************************************************************
00025 ******************************************************************************
00026 
00027 
00028                                 The Box Language
00029 
00030 
00031 ******************************************************************************
00032 *****************************************************************************/
00033 
00034 
00044 #include <stdio.h>
00045 #include <string.h>
00046 #include "boxes.hh"
00047 #include "ppbox.hh"
00048 #include "prim2.hh"
00049 #include "xtended.hh"
00050 
00051 
00052 /*****************************************************************************
00053                                     Identifiers
00054 *****************************************************************************/
00055 Sym BOXIDENT = symbol ("BoxIdent");
00056 
00057 Tree boxIdent(const char* name)     { return tree(BOXIDENT, tree(symbol(name)) ); }
00058 bool isBoxIdent(Tree t)             { return t->node() == Node(BOXIDENT); }
00059 bool isBoxIdent(Tree t0, const char** str)
00060 {
00061     Tree t1; Sym s;
00062     if ( isTree(t0, BOXIDENT, t1) && isSym(t1->node(), &s) ) {
00063         *str = name(s);
00064         return true;
00065     } else {
00066         return false;
00067     }
00068 }
00069 
00070 
00071 /*****************************************************************************
00072                                     Numbers
00073 *****************************************************************************/
00074 
00075 Tree boxInt(int n)                  { return tree(n);   }
00076 Tree boxReal(double n)              { return tree(n);   }
00077 
00078 bool isBoxInt(Tree t)               { return isInt(t->node());  }
00079 bool isBoxReal(Tree t)              { return isDouble(t->node()); }
00080 
00081 bool isBoxInt(Tree t, int* i)       { return isInt(t->node(), i);   }
00082 bool isBoxReal(Tree t, double* r)   { return isDouble(t->node(), r); }
00083 
00084 
00085 /*****************************************************************************
00086                                 Wire and Cut
00087 *****************************************************************************/
00088 
00089 Sym BOXCUT = symbol ("BoxCut");
00090 Tree boxCut()                       { return tree(BOXCUT); }
00091 bool isBoxCut(Tree t)               { return isTree(t, BOXCUT); }
00092 
00093 Sym BOXWIRE = symbol ("BoxWire");
00094 Tree boxWire()                      { return tree(BOXWIRE); }
00095 bool isBoxWire(Tree t)              { return isTree(t, BOXWIRE); }
00096 
00097 
00098 /*****************************************************************************
00099                         Symbolic Boxes with symbolic slots
00100 *****************************************************************************/
00101 
00102 Sym BOXSLOT = symbol ("BoxSlot");
00103 
00104 Tree boxSlot(int id)                { return tree(BOXSLOT,tree(id)); }
00105 bool isBoxSlot(Tree t)              { Tree w; return isTree(t, BOXSLOT,w); }
00106 bool isBoxSlot(Tree t, int* id)     { Tree w; return isTree(t, BOXSLOT,w) && isInt(w->node(),id); }
00107 
00108 
00109 Sym BOXSYMBOLIC = symbol ("BoxSymbolic");
00110 
00111 Tree boxSymbolic(Tree slot, Tree body)              { return tree(BOXSYMBOLIC,slot, body); }
00112 bool isBoxSymbolic(Tree t)                          { Tree slot, body; return isTree(t, BOXSYMBOLIC, slot, body); }
00113 bool isBoxSymbolic(Tree t, Tree& slot, Tree& body)  { return isTree(t, BOXSYMBOLIC, slot, body); }
00114 
00115 
00116 /*****************************************************************************
00117                               Composition of Boxes
00118 *****************************************************************************/
00119 
00120 Sym BOXSEQ = symbol ("BoxSeq");
00121 Tree boxSeq(Tree x, Tree y)                 { return tree(BOXSEQ, x, y);        }
00122 bool isBoxSeq(Tree t, Tree& x, Tree& y)     { return isTree(t, BOXSEQ, x, y);   }
00123 
00124 Sym BOXPAR = symbol ("BoxPar");
00125 Tree boxPar(Tree x, Tree y)                 { return tree(BOXPAR, x, y);        }
00126 bool isBoxPar(Tree t, Tree& x, Tree& y)     { return isTree(t, BOXPAR, x, y);   }
00127 
00128 Sym BOXREC = symbol ("BoxRec");
00129 Tree boxRec(Tree x, Tree y)                 { return tree(BOXREC, x, y);        }
00130 bool isBoxRec(Tree t, Tree& x, Tree& y)     { return isTree(t, BOXREC, x, y);   }
00131 
00132 Sym BOXSPLIT = symbol ("BoxSplit");
00133 Tree boxSplit(Tree x, Tree y)               { return tree(BOXSPLIT, x, y);      }
00134 bool isBoxSplit(Tree t, Tree& x, Tree& y)   { return isTree(t, BOXSPLIT, x, y); }
00135 
00136 Sym BOXMERGE = symbol ("BoxMerge");
00137 Tree boxMerge(Tree x, Tree y)               { return tree(BOXMERGE, x, y); }
00138 bool isBoxMerge(Tree t, Tree& x, Tree& y)   { return isTree(t, BOXMERGE, x, y); }
00139 
00140 
00141 /*****************************************************************************
00142                         Algorithmic Composition of Boxes
00143 *****************************************************************************/
00144 
00145 Sym BOXIPAR = symbol ("BoxIPar");
00146 Sym BOXISEQ = symbol ("BoxISeq");
00147 Sym BOXISUM = symbol ("BoxISum");
00148 Sym BOXIPROD = symbol ("BoxIProd");
00149 
00150 Tree boxIPar(Tree x, Tree y, Tree z)                    { return tree(BOXIPAR, x, y, z);        }
00151 Tree boxISeq(Tree x, Tree y, Tree z)                    { return tree(BOXISEQ, x, y, z);        }
00152 Tree boxISum(Tree x, Tree y, Tree z)                    { return tree(BOXISUM, x, y, z);        }
00153 Tree boxIProd(Tree x, Tree y, Tree z)                   { return tree(BOXIPROD, x, y, z);       }
00154 
00155 bool isBoxIPar(Tree t, Tree& x, Tree& y, Tree& z)       { return isTree(t, BOXIPAR,  x, y, z);   }
00156 bool isBoxISeq(Tree t, Tree& x, Tree& y, Tree& z)       { return isTree(t, BOXISEQ,  x, y, z);   }
00157 bool isBoxISum(Tree t, Tree& x, Tree& y, Tree& z)       { return isTree(t, BOXISUM,  x, y, z);   }
00158 bool isBoxIProd(Tree t, Tree& x, Tree& y, Tree& z)      { return isTree(t, BOXIPROD, x, y, z);   }
00159 
00160 
00161 
00162 /*****************************************************************************
00163                               Lambda-Calculus of Boxes
00164 *****************************************************************************/
00165 Sym BOXABSTR    = symbol ("BoxAbstr");
00166 Sym BOXAPPL     = symbol ("BoxAppl");
00167 Sym CLOSURE     = symbol ("Closure");
00168 Sym BOXERROR    = symbol ("BoxError");
00169 Sym BOXACCESS   = symbol ("BoxAccess");
00170 
00171 Tree boxAbstr   (Tree x, Tree y)            { return tree(BOXABSTR, x, y); }
00172 Tree boxAppl    (Tree x, Tree y)            { return tree(BOXAPPL, x, y); }
00173 
00174 bool isBoxAbstr (Tree t)                    { return t->node() == Node(BOXABSTR); }
00175 bool isBoxAppl  (Tree t)                    { return t->node() == Node(BOXAPPL); }
00176 
00177 bool isBoxAbstr (Tree t, Tree& x, Tree& y)  { return isTree(t, BOXABSTR, x, y); }
00178 bool isBoxAppl  (Tree t, Tree& x, Tree& y)  { return isTree(t, BOXAPPL, x, y); }
00179 
00180 Tree buildBoxAbstr  (Tree largs, Tree body)
00181 {
00182     if (isNil(largs)) {
00183         return body;
00184     } else {
00185         return buildBoxAbstr(tl(largs), boxAbstr(hd(largs), body));
00186     }
00187 }
00188 #if 0
00189 Tree buildBoxAppl   (Tree fun, Tree revarglist)
00190 {
00191     if (isNil(revarglist)) {
00192         return fun;
00193     } else {
00194         return  boxAppl(buildBoxAppl(fun, tl(revarglist)), hd(revarglist));
00195     }
00196 }
00197 #else
00198 Tree buildBoxAppl   (Tree fun, Tree revarglist)
00199 {
00200     if (isNil (revarglist)) exit(1); // a revoir !!!!!!
00201     return  boxAppl(fun, revarglist);
00202 }
00203 #endif
00204 
00205 Tree closure (Tree abstr, Tree genv, Tree vis, Tree lenv)
00206 {
00207     return  tree(CLOSURE, abstr, genv, vis, lenv);
00208 }
00209 
00210 bool isClosure  (Tree t, Tree& abstr, Tree& genv, Tree& vis, Tree& lenv)
00211 {
00212     return isTree(t, CLOSURE, abstr, genv, vis, lenv);
00213 }
00214 
00215 Tree boxError()
00216 {
00217     return  tree(BOXERROR);
00218 }
00219 
00220 bool isBoxError(Tree t)
00221 {
00222     return isTree(t, BOXERROR);
00223 }
00224 
00225 
00226 Tree boxAccess (Tree exp, Tree id)              { return tree(BOXACCESS, exp, id); }
00227 bool isBoxAccess(Tree t, Tree& exp, Tree& id)   { return isTree(t, BOXACCESS, exp, id); }
00228 
00229 
00230 /*****************************************************************************
00231                         Boxes with local definitions
00232 *****************************************************************************/
00233 Sym BOXWITHLOCALDEF     = symbol ("BoxWithLocalDef");
00234 
00235 Tree boxWithLocalDef (Tree body, Tree ldef)                 { return tree(BOXWITHLOCALDEF, body, ldef); }
00236 bool isBoxWithLocalDef (Tree t, Tree& body, Tree& ldef)     { return isTree(t, BOXWITHLOCALDEF, body, ldef); }
00237 
00238 
00239 /*****************************************************************************
00240                         Boxes modif local definitions
00241 *****************************************************************************/
00242 Sym BOXMODIFLOCALDEF    = symbol ("BoxModifLocalDef");
00243 
00244 
00245 Tree boxModifLocalDef (Tree body, Tree ldef)                { return tree(BOXMODIFLOCALDEF, body, ldef); }
00246 bool isBoxModifLocalDef (Tree t, Tree& body, Tree& ldef)    { return isTree(t, BOXMODIFLOCALDEF, body, ldef); }
00247 
00248 
00249 /*****************************************************************************
00250                              Modules
00251 *****************************************************************************/
00252 
00253 Sym BOXENVIRONMENT  = symbol ("BoxEnvironment");
00254 
00255 Tree boxEnvironment ()                                      { return tree(BOXENVIRONMENT); }
00256 bool isBoxEnvironment (Tree s)                              { return isTree(s, BOXENVIRONMENT); }
00257 
00258 Sym BOXCOMPONENT    = symbol ("BoxComponent");
00259 
00260 Tree boxComponent (Tree filename)                           { return tree(BOXCOMPONENT, filename); }
00261 bool isBoxComponent (Tree s, Tree& filename)                { return isTree(s, BOXCOMPONENT, filename); }
00262 
00263 Sym BOXLIBRARY      = symbol ("BoxLibrary");
00264 
00265 Tree boxLibrary (Tree filename)                             { return tree(BOXLIBRARY, filename); }
00266 bool isBoxLibrary (Tree s, Tree& filename)                  { return isTree(s, BOXLIBRARY, filename); }
00267 
00268 
00269 Sym IMPORTFILE      = symbol ("ImportFile");
00270 
00271 Tree importFile(Tree filename)                              { return tree(IMPORTFILE, filename); }
00272 bool isImportFile(Tree s, Tree& filename)                   { return isTree(s, IMPORTFILE, filename); }
00273 
00274 
00275 /*****************************************************************************
00276                             External Primitive Boxes (n -> 1)
00277 *****************************************************************************/
00278 
00279 Sym BOXPRIM0 = symbol ("BoxPrim0");
00280 Tree boxPrim0(prim0 foo)                    { return tree(BOXPRIM0, tree((void*)foo)); }
00281 bool isBoxPrim0 (Tree s)                    { Tree t; return isTree(s, BOXPRIM0, t);    }
00282 bool isBoxPrim0 (Tree s, prim0* p)          { Tree t; return isTree(s, BOXPRIM0, t) && isPointer(t->node(),(void**)p);  }
00283 
00284 Sym BOXPRIM1 = symbol ("BoxPrim1");
00285 Tree boxPrim1(prim1 foo)                    { return tree(BOXPRIM1, tree((void*)foo)); }
00286 bool isBoxPrim1 (Tree s)                    { Tree t; return isTree(s, BOXPRIM1, t);    }
00287 bool isBoxPrim1 (Tree s, prim1* p)          { Tree t; return isTree(s, BOXPRIM1, t) && isPointer(t->node(),(void**)p);  }
00288 
00289 Sym BOXPRIM2 = symbol ("BoxPrim2");
00290 Tree boxPrim2(prim2 foo)                    { return tree(BOXPRIM2, tree((void*)foo)); }
00291 bool isBoxPrim2 (Tree s)                    { Tree t; return isTree(s, BOXPRIM2, t);    }
00292 bool isBoxPrim2 (Tree s, prim2* p)          { Tree t; return isTree(s, BOXPRIM2, t) && isPointer(t->node(),(void**)p);  }
00293 
00294 Sym BOXPRIM3 = symbol ("BoxPrim3");
00295 Tree boxPrim3(prim3 foo)                    { return tree(BOXPRIM3, tree((void*)foo)); }
00296 bool isBoxPrim3 (Tree s)                    { Tree t; return isTree(s, BOXPRIM3, t);    }
00297 bool isBoxPrim3 (Tree s, prim3* p)          { Tree t; return isTree(s, BOXPRIM3, t) && isPointer(t->node(),(void**)p);  }
00298 
00299 Sym BOXPRIM4 = symbol ("BoxPrim4");
00300 Tree boxPrim4(prim4 foo)                    { return tree(BOXPRIM4, tree((void*)foo)); }
00301 bool isBoxPrim4 (Tree s)                    { Tree t; return isTree(s, BOXPRIM4, t);    }
00302 bool isBoxPrim4 (Tree s, prim4* p)          { Tree t; return isTree(s, BOXPRIM4, t) && isPointer(t->node(),(void**)p);  }
00303 
00304 Sym BOXPRIM5 = symbol ("BoxPrim5");
00305 Tree boxPrim5(prim5 foo)                    { return tree(BOXPRIM5, tree((void*)foo)); }
00306 bool isBoxPrim5 (Tree s)                    { Tree t; return isTree(s, BOXPRIM5, t);    }
00307 bool isBoxPrim5 (Tree s, prim5* p)          { Tree t; return isTree(s, BOXPRIM5, t) && isPointer(t->node(),(void**)p);  }
00308 
00309 /*****************************************************************************
00310                              Foreign Functions
00311 *****************************************************************************/
00312 
00313 Sym BOXFFUN = symbol ("BoxFFun");
00314 Tree boxFFun (Tree ff)                      { return tree(BOXFFUN, ff);                 }
00315 bool isBoxFFun  (Tree s)                    { Tree ff; return isTree(s, BOXFFUN, ff);   }
00316 bool isBoxFFun  (Tree s, Tree& ff)          { return isTree(s, BOXFFUN, ff);            }
00317 
00318 
00319 Sym BOXFCONST = symbol ("BoxFConst");
00320 Tree boxFConst      (Tree type, Tree name, Tree file)               { return tree(BOXFCONST, type, name, file);             }
00321 bool isBoxFConst    (Tree s)                                        { Tree t,n,f; return isTree(s, BOXFCONST, t, n, f); }
00322 bool isBoxFConst    (Tree s, Tree& type, Tree& name, Tree& file)    { return isTree(s, BOXFCONST,type, name, file);     }
00323 
00324 
00325 Sym BOXFVAR = symbol ("BoxFVar");
00326 Tree boxFVar      (Tree type, Tree name, Tree file)                 { return tree(BOXFVAR, type, name, file);             }
00327 bool isBoxFVar    (Tree s)                                          { Tree t,n,f; return isTree(s, BOXFVAR, t, n, f); }
00328 bool isBoxFVar    (Tree s, Tree& type, Tree& name, Tree& file)      { return isTree(s, BOXFVAR,type, name, file);     }
00329 
00330 
00331 /*****************************************************************************
00332                              User Interface Elements
00333 *****************************************************************************/
00334 
00335 Sym BOXBUTTON = symbol ("BoxButton");
00336 Tree boxButton   (Tree lbl)                 { return tree(BOXBUTTON, lbl);                  }
00337 bool isBoxButton (Tree s)                   { Tree lbl; return isTree(s, BOXBUTTON, lbl);   }
00338 bool isBoxButton (Tree s, Tree& lbl)        { return isTree(s, BOXBUTTON, lbl);             }
00339 
00340 
00341 Sym BOXCHECKBOX = symbol ("BoxCheckbox");
00342 Tree boxCheckbox   (Tree lbl)               { return tree(BOXCHECKBOX, lbl);                    }
00343 bool isBoxCheckbox (Tree s)                 { Tree lbl; return isTree(s, BOXCHECKBOX, lbl); }
00344 bool isBoxCheckbox (Tree s, Tree& lbl)      { return isTree(s, BOXCHECKBOX, lbl);               }
00345 
00346 
00347 Sym BOXHSLIDER = symbol ("BoxHSlider");
00348 Tree boxHSlider   (Tree lbl, Tree cur, Tree min, Tree max, Tree step)
00349                                             { return tree(BOXHSLIDER, lbl, list4(cur,min,max,step));        }
00350 bool isBoxHSlider (Tree s)                  { Tree lbl, params; return isTree(s, BOXHSLIDER, lbl, params);  }
00351 
00352 bool isBoxHSlider (Tree s, Tree& lbl, Tree& cur, Tree& min, Tree& max, Tree& step)
00353 {
00354     Tree params;
00355     if (isTree(s, BOXHSLIDER, lbl, params)) {
00356         cur = nth(params, 0);
00357         min = nth(params, 1);
00358         max = nth(params, 2);
00359         step= nth(params, 3);
00360         return true;
00361     } else {
00362         return false;
00363     }
00364 }
00365 
00366 
00367 Sym BOXVSLIDER = symbol ("BoxVSlider");
00368 Tree boxVSlider   (Tree lbl, Tree cur, Tree min, Tree max, Tree step)
00369                                             { return tree(BOXVSLIDER, lbl, list4(cur,min,max,step));        }
00370 bool isBoxVSlider (Tree s)                  { Tree lbl, params; return isTree(s, BOXVSLIDER, lbl, params);  }
00371 
00372 bool isBoxVSlider (Tree s, Tree& lbl, Tree& cur, Tree& min, Tree& max, Tree& step)
00373 {
00374     Tree params;
00375     if (isTree(s, BOXVSLIDER, lbl, params)) {
00376         cur = nth(params, 0);
00377         min = nth(params, 1);
00378         max = nth(params, 2);
00379         step= nth(params, 3);
00380         return true;
00381     } else {
00382         return false;
00383     }
00384 }
00385 
00386 Sym BOXNUMENTRY = symbol ("BoxNumEntry");
00387 Tree boxNumEntry   (Tree lbl, Tree cur, Tree min, Tree max, Tree step)
00388                                             { return tree(BOXNUMENTRY, lbl, list4(cur,min,max,step));       }
00389 bool isBoxNumEntry (Tree s)                 { Tree lbl, params; return isTree(s, BOXNUMENTRY, lbl, params); }
00390 
00391 bool isBoxNumEntry (Tree s, Tree& lbl, Tree& cur, Tree& min, Tree& max, Tree& step)
00392 {
00393     Tree params;
00394     if (isTree(s, BOXNUMENTRY, lbl, params)) {
00395         cur = nth(params, 0);
00396         min = nth(params, 1);
00397         max = nth(params, 2);
00398         step= nth(params, 3);
00399         return true;
00400     } else {
00401         return false;
00402     }
00403 }
00404 
00405 
00406 Sym BOXHGROUP = symbol ("BoxHGroup");
00407 Tree boxHGroup   (Tree lbl, Tree x)             { return tree(BOXHGROUP, lbl, x);               }
00408 bool isBoxHGroup (Tree s)                       { Tree lbl, x; return isTree(s, BOXHGROUP, lbl, x); }
00409 bool isBoxHGroup (Tree s, Tree& lbl, Tree& x)   { return isTree(s, BOXHGROUP, lbl, x);              }
00410 
00411 
00412 Sym BOXVGROUP = symbol ("BoxVGroup");
00413 Tree boxVGroup   (Tree lbl, Tree x)             { return tree(BOXVGROUP, lbl, x);               }
00414 bool isBoxVGroup (Tree s)                       { Tree lbl, x; return isTree(s, BOXVGROUP, lbl, x); }
00415 bool isBoxVGroup (Tree s, Tree& lbl, Tree& x)   { return isTree(s, BOXVGROUP, lbl, x);              }
00416 
00417 
00418 Sym BOXTGROUP = symbol ("BoxTGroup");
00419 Tree boxTGroup   (Tree lbl, Tree x)             { return tree(BOXTGROUP, lbl, x);               }
00420 bool isBoxTGroup (Tree s)                       { Tree lbl, x; return isTree(s, BOXTGROUP, lbl, x); }
00421 bool isBoxTGroup (Tree s, Tree& lbl, Tree& x)   { return isTree(s, BOXTGROUP, lbl, x);              }
00422 
00423 
00424 Sym BOXHBARGRAPH = symbol ("BoxHBargraph");
00425 Tree boxHBargraph(Tree lbl, Tree min, Tree max)                 { return tree(BOXHBARGRAPH, lbl, min, max);     }
00426 bool isBoxHBargraph (Tree s)                                    { Tree lbl, min, max; return isTree(s, BOXHBARGRAPH, lbl, min, max);    }
00427 bool isBoxHBargraph (Tree s, Tree& lbl, Tree& min, Tree& max)   { return isTree(s, BOXHBARGRAPH, lbl, min, max); }
00428 
00429 
00430 Sym BOXVBARGRAPH = symbol ("BoxVBargraph");
00431 Tree boxVBargraph(Tree lbl, Tree min, Tree max)                 { return tree(BOXVBARGRAPH, lbl, min, max);     }
00432 bool isBoxVBargraph (Tree s)                                    { Tree lbl, min, max; return isTree(s, BOXVBARGRAPH, lbl, min, max);    }
00433 bool isBoxVBargraph (Tree s, Tree& lbl, Tree& min, Tree& max)   { return isTree(s, BOXVBARGRAPH, lbl, min, max); }
00434 
00435 
00436 /*****************************************************************************
00437                              pattern lmatching case
00438 *****************************************************************************/
00439 
00440 Sym BOXCASE         = symbol ("BoxCase");
00441 Sym BOXPATMATCHER   = symbol ("BoxPatMatcher");
00442 Sym BOXPATVAR       = symbol ("BoxPatVar");
00443 
00454 static Tree preparePattern(Tree box)
00455 {
00456 //    cerr << "preparePattern(" << boxpp(box) << ")" << endl;
00457 
00458         int     id;
00459         double  r;
00460         prim0   p0;
00461         prim1   p1;
00462         prim2   p2;
00463         prim3   p3;
00464         prim4   p4;
00465         prim5   p5;
00466 
00467         Tree    t1, t2, t3, ff, label, cur, min, max, step, type, name, file, arg,
00468                 body, fun, args, ldef, slot,
00469                 ident, rules;
00470 
00471         xtended* xt = (xtended*) getUserData(box);
00472 
00473 
00474         // primitive elements
00475              if (xt)                        return box;
00476         else if (isBoxIdent(box))           return boxPatternVar(box);
00477         else if (isBoxAppl(box, fun, args)) {
00478                 if (isBoxIdent(fun))        return boxAppl( fun, lmap(preparePattern,args));
00479                 else                        return boxAppl( preparePattern(fun), lmap(preparePattern,args));
00480             }
00481         else if (isBoxAbstr(box,arg,body))  return box;
00482         else if (isBoxInt(box))             return box;
00483         else if (isBoxReal(box, &r))        return box;
00484         else if (isBoxCut(box))             return box;
00485         else if (isBoxWire(box))            return box;
00486         else if (isBoxPrim0(box, &p0))      return box;
00487         else if (isBoxPrim1(box, &p1))      return box;
00488         else if (isBoxPrim2(box, &p2))      return box;
00489         else if (isBoxPrim3(box, &p3))      return box;
00490         else if (isBoxPrim4(box, &p4))      return box;
00491         else if (isBoxPrim5(box, &p5))      return box;
00492 
00493         else if (isBoxWithLocalDef(box, body, ldef))    return boxWithLocalDef(preparePattern(body), ldef);
00494 
00495 
00496         // foreign elements
00497         else if (isBoxFFun(box, ff))        return box;
00498         else if (isBoxFConst(box, type, name, file))
00499                                             return box;
00500         else if (isBoxFVar(box, type, name, file))
00501                                             return box;
00502 
00503         // block diagram binary operator
00504         else if (isBoxSeq(box, t1, t2))     return boxSeq( preparePattern(t1), preparePattern(t2) );
00505         else if (isBoxSplit(box, t1, t2))   return boxSplit( preparePattern(t1), preparePattern(t2) );
00506         else if (isBoxMerge(box, t1, t2))   return boxMerge( preparePattern(t1), preparePattern(t2) );
00507         else if (isBoxPar(box, t1, t2))     return boxPar( preparePattern(t1), preparePattern(t2) );
00508         else if (isBoxRec(box, t1, t2))     return boxRec( preparePattern(t1), preparePattern(t2) );
00509 
00510         // iterative block diagram construction
00511         else if (isBoxIPar(box, t1, t2, t3))    return boxIPar ( t1, t2, preparePattern(t3) );
00512         else if (isBoxISeq(box, t1, t2, t3))    return boxISeq ( t1, t2, preparePattern(t3) );
00513         else if (isBoxISum(box, t1, t2, t3))    return boxISum ( t1, t2, preparePattern(t3) );
00514         else if (isBoxIProd(box, t1, t2, t3))   return boxIProd( t1, t2, preparePattern(t3) );
00515 
00516         // user interface
00517         else if (isBoxButton(box, label))       return box;
00518         else if (isBoxCheckbox(box, label))     return box;
00519 
00520         else if (isBoxVSlider(box, label, cur, min, max, step))     return box;
00521         else if (isBoxHSlider(box, label, cur, min, max, step))     return box;
00522 
00523         else if (isBoxVGroup(box, label, t1))   return boxVGroup(label, preparePattern(t1));
00524         else if (isBoxHGroup(box, label, t1))   return boxHGroup(label, preparePattern(t1));
00525         else if (isBoxTGroup(box, label, t1))   return boxTGroup(label, preparePattern(t1));
00526 
00527         else if (isBoxHBargraph(box, label, min, max))              return box;
00528         else if (isBoxVBargraph(box, label, min, max))              return box;
00529         else if (isBoxNumEntry(box, label, cur, min, max, step))    return box;
00530 
00531         else if (isNil(box))                    return box;
00532         else if (isList(box))                   return lmap(preparePattern, box);
00533         else if (isBoxEnvironment(box))         return box;
00534         /* not expected
00535         else if (isClosure(box, abstr, genv, vis, lenv)) {
00536             fout << "closure[" << boxpp(abstr)
00537                 << ", genv = " << envpp(genv)
00538                 << ", lenv = " << envpp(lenv)
00539                 << "]";
00540         }
00541         */
00542         else if (isBoxComponent(box, label))        return box;
00543         else if (isBoxAccess(box, t1, t2))          return box;
00544 
00545         /* not expected
00546         else if (isImportFile(box, label)) {
00547             fout << "import("
00548                 << tree2str(label) << ')';
00549         }
00550         */
00551 
00552 
00553         else if (isBoxSlot(box, &id))               return box;
00554         else if (isBoxSymbolic(box, slot, body))    return box;
00555 
00556         // Pattern Matching Extensions
00557         else if (isBoxCase(box, rules))             return box;
00558         else if (isBoxPatternVar(box, ident))       return box;
00559 
00560 
00561         // None of the previous tests succeded, then it is not a valid box
00562         else {
00563             cerr << "Error in preparePattern() : " << *box << " is not a valid box" << endl;
00564             exit(1);
00565         }
00566 
00567 
00568    return box;
00569 }
00570 
00571 static Tree prepareRule(Tree rule)
00572 {
00573     return cons (lmap(preparePattern,hd(rule)), tl(rule));
00574 }
00575 
00576 static Tree prepareRules(Tree rules) {
00577     return lmap(prepareRule, rules);
00578 }
00579 
00580 Tree boxCaseInternal     (Tree rules)       { return tree(BOXCASE, rules);                  }
00581 Tree boxCase    (Tree rules)                { return boxCaseInternal(prepareRules(rules));  }
00582 
00583 bool isBoxCase (Tree s)                     { Tree rules; return isTree(s, BOXCASE, rules); }
00584 bool isBoxCase (Tree s, Tree& rules)        { return isTree(s, BOXCASE, rules);             }
00585 
00586 
00587 Tree boxPatternVar  (Tree id)               { return tree(BOXPATVAR, id);                   }
00588 bool isBoxPatternVar(Tree s, Tree& id)      { return isTree(s, BOXPATVAR, id);              }
00589 
00590 
00591 Tree boxPatternMatcher      (Automaton* a, int state, Tree env, Tree origRules, Tree revParamList)  
00592 { 
00593     return tree(BOXPATMATCHER, tree((void*)a), tree(state), env, origRules, revParamList); 
00594 }                   
00595 
00596 bool isBoxPatternMatcher    (Tree s)
00597 {
00598     Tree ta, ts, env, orig, rpl;
00599     return isTree(s, BOXPATMATCHER, ta, ts, env, orig, rpl);
00600 }
00601 
00602 bool isBoxPatternMatcher    (Tree s, Automaton*& a, int& state, Tree& env, Tree& origRules, Tree& revParamList)
00603 {
00604     Tree ta, ts;
00605     if (isTree(s, BOXPATMATCHER, ta, ts, env, origRules, revParamList)) {
00606         a = (Automaton*)tree2ptr(ta);
00607         state = tree2int(ts);
00608         return true;
00609     } else {
00610         return false;
00611     }
00612 }
00613 
00614