442
441
Sym BOXPATMATCHER = symbol ("BoxPatMatcher");
443
442
Sym BOXPATVAR = symbol ("BoxPatVar");
445
Tree boxCase (Tree rules) { return tree(BOXCASE, rules); }
445
* Prepare a "pattern" by replacing variables x by special
446
* pattern variables ?x.
450
* P[e(f)] -> P[e](P[f])
451
* P[e:f] -> P[e]:P[f]
454
static Tree preparePattern(Tree box)
456
// cerr << "preparePattern(" << boxpp(box) << ")" << endl;
467
Tree t1, t2, t3, ff, label, cur, min, max, step, type, name, file, arg,
468
body, fun, args, ldef, slot,
471
xtended* xt = (xtended*) getUserData(box);
474
// primitive elements
476
else if (isBoxIdent(box)) return boxPatternVar(box);
477
else if (isBoxAppl(box, fun, args)) {
478
if (isBoxIdent(fun)) return boxAppl( fun, lmap(preparePattern,args));
479
else return boxAppl( preparePattern(fun), lmap(preparePattern,args));
481
else if (isBoxAbstr(box,arg,body)) return box;
482
else if (isBoxInt(box)) return box;
483
else if (isBoxReal(box, &r)) return box;
484
else if (isBoxCut(box)) return box;
485
else if (isBoxWire(box)) return box;
486
else if (isBoxPrim0(box, &p0)) return box;
487
else if (isBoxPrim1(box, &p1)) return box;
488
else if (isBoxPrim2(box, &p2)) return box;
489
else if (isBoxPrim3(box, &p3)) return box;
490
else if (isBoxPrim4(box, &p4)) return box;
491
else if (isBoxPrim5(box, &p5)) return box;
493
else if (isBoxWithLocalDef(box, body, ldef)) return boxWithLocalDef(preparePattern(body), ldef);
497
else if (isBoxFFun(box, ff)) return box;
498
else if (isBoxFConst(box, type, name, file))
500
else if (isBoxFVar(box, type, name, file))
503
// block diagram binary operator
504
else if (isBoxSeq(box, t1, t2)) return boxSeq( preparePattern(t1), preparePattern(t2) );
505
else if (isBoxSplit(box, t1, t2)) return boxSplit( preparePattern(t1), preparePattern(t2) );
506
else if (isBoxMerge(box, t1, t2)) return boxMerge( preparePattern(t1), preparePattern(t2) );
507
else if (isBoxPar(box, t1, t2)) return boxPar( preparePattern(t1), preparePattern(t2) );
508
else if (isBoxRec(box, t1, t2)) return boxRec( preparePattern(t1), preparePattern(t2) );
510
// iterative block diagram construction
511
else if (isBoxIPar(box, t1, t2, t3)) return boxIPar ( t1, t2, preparePattern(t3) );
512
else if (isBoxISeq(box, t1, t2, t3)) return boxISeq ( t1, t2, preparePattern(t3) );
513
else if (isBoxISum(box, t1, t2, t3)) return boxISum ( t1, t2, preparePattern(t3) );
514
else if (isBoxIProd(box, t1, t2, t3)) return boxIProd( t1, t2, preparePattern(t3) );
517
else if (isBoxButton(box, label)) return box;
518
else if (isBoxCheckbox(box, label)) return box;
520
else if (isBoxVSlider(box, label, cur, min, max, step)) return box;
521
else if (isBoxHSlider(box, label, cur, min, max, step)) return box;
523
else if (isBoxVGroup(box, label, t1)) return boxVGroup(label, preparePattern(t1));
524
else if (isBoxHGroup(box, label, t1)) return boxHGroup(label, preparePattern(t1));
525
else if (isBoxTGroup(box, label, t1)) return boxTGroup(label, preparePattern(t1));
527
else if (isBoxHBargraph(box, label, min, max)) return box;
528
else if (isBoxVBargraph(box, label, min, max)) return box;
529
else if (isBoxNumEntry(box, label, cur, min, max, step)) return box;
531
else if (isNil(box)) return box;
532
else if (isList(box)) return lmap(preparePattern, box);
533
else if (isBoxEnvironment(box)) return box;
535
else if (isClosure(box, abstr, genv, vis, lenv)) {
536
fout << "closure[" << boxpp(abstr)
537
<< ", genv = " << envpp(genv)
538
<< ", lenv = " << envpp(lenv)
542
else if (isBoxComponent(box, label)) return box;
543
else if (isBoxAccess(box, t1, t2)) return box;
546
else if (isImportFile(box, label)) {
548
<< tree2str(label) << ')';
553
else if (isBoxSlot(box, &id)) return box;
554
else if (isBoxSymbolic(box, slot, body)) return box;
556
// Pattern Matching Extensions
557
else if (isBoxCase(box, rules)) return box;
558
else if (isBoxPatternVar(box, ident)) return box;
561
// None of the previous tests succeded, then it is not a valid box
563
cerr << "Error in preparePattern() : " << *box << " is not a valid box" << endl;
571
static Tree prepareRule(Tree rule)
573
return cons (lmap(preparePattern,hd(rule)), tl(rule));
576
static Tree prepareRules(Tree rules) {
577
return lmap(prepareRule, rules);
580
Tree boxCaseInternal (Tree rules) { return tree(BOXCASE, rules); }
581
Tree boxCase (Tree rules) { return boxCaseInternal(prepareRules(rules)); }
446
583
bool isBoxCase (Tree s) { Tree rules; return isTree(s, BOXCASE, rules); }
447
584
bool isBoxCase (Tree s, Tree& rules) { return isTree(s, BOXCASE, rules); }
449
587
Tree boxPatternVar (Tree id) { return tree(BOXPATVAR, id); }
450
588
bool isBoxPatternVar(Tree s, Tree& id) { return isTree(s, BOXPATVAR, id); }