Search in sources :

Example 1 with FunpDontCare

use of suite.funp.P0.FunpDontCare in project suite by stupidsing.

the class P3Optimize method optimize_.

private Funp optimize_(Funp n) {
    return // 
    n.<// 
    Funp>switch_().applyIf(FunpCoerce.class, f -> f.apply((coerce, expr) -> {
        return !(expr instanceof FunpDontCare) ? n : optimize(expr);
    })).applyIf(FunpData.class, f -> f.apply(pairs -> {
        return FunpData.of(Read.from2(pairs).concatMap((expr, range) -> {
            Funp expr1 = optimize(expr);
            int start = range.t0;
            Streamlet<Pair<Funp, IntIntPair>> pairsx = new // 
            Switch<Streamlet<Pair<Funp, IntIntPair>>>(// 
            expr1).applyIf(FunpData.class, g -> g.apply(pairs1 -> {
                return // 
                Read.from2(// 
                pairs1).map((exprc, range1) -> Pair.of(optimize(exprc), IntIntPair.of(start + range1.t0, start + range1.t1)));
            })).result();
            return pairsx != null ? pairsx : Read.each(Pair.of(expr1, range));
        }).toList());
    })).applyIf(FunpDeref.class, f -> f.apply(pointer -> {
        return optimize(pointer).<Funp>switch_().applyIf(FunpReference.class, g -> g.expr).result();
    })).applyIf(FunpIf.class, f -> f.apply((if_, then, else_) -> {
        return // 
        optimize(if_).<Funp>switch_().applyIf(FunpBoolean.class, g -> g.apply(b -> {
            return b ? then : else_;
        })).result();
    })).applyIf(FunpMemory.class, f -> f.apply((pointer, start, end) -> {
        return // 
        optimize(pointer).<Funp>switch_().applyIf(FunpData.class, g -> g.apply(pairs -> {
            for (Pair<Funp, IntIntPair> pair : pairs) {
                IntIntPair range = pair.t1;
                if (start == range.t0 && end == range.t1)
                    return pair.t0;
            }
            return null;
        })).applyIf(FunpReference.class, g -> {
            return FunpTree.of(TermOp.PLUS__, g.expr, FunpNumber.ofNumber(start));
        }).result();
    })).applyIf(FunpReference.class, f -> f.apply(expr -> {
        return optimize(expr).<Funp>switch_().applyIf(FunpMemory.class, g -> g.pointer).result();
    })).applyIf(FunpTree.class, f -> f.apply((operator, lhs, rhs) -> {
        IntInt_Bool iib = TreeUtil.boolOperations.get(operator);
        IntInt_Int iii = TreeUtil.intOperations.get(operator);
        if (iib != null)
            return evaluate(iib, lhs, rhs);
        else if (iii != null)
            return evaluate(iii, lhs, rhs);
        else
            return null;
    })).applyIf(FunpTree2.class, f -> f.apply((operator, lhs, rhs) -> {
        return evaluate(TreeUtil.tupleOperations.get(operator), lhs, rhs);
    })).applyIf(FunpWhile.class, f -> f.apply((while_, do_, expr) -> {
        return // 
        optimize(while_).<Funp>switch_().applyIf(FunpBoolean.class, g -> g.apply(b -> {
            return b ? null : expr;
        })).result();
    })).result();
}
Also used : IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) FunpCoerce(suite.funp.P0.FunpCoerce) IntIntPair(suite.primitive.adt.pair.IntIntPair) FunpBoolean(suite.funp.P0.FunpBoolean) Inspect(suite.inspect.Inspect) FunpTree(suite.funp.P0.FunpTree) TermOp(suite.node.io.TermOp) FunpReference(suite.funp.P0.FunpReference) FunpTree2(suite.funp.P0.FunpTree2) Funp(suite.funp.Funp_.Funp) IntInt_Int(suite.primitive.IntInt_Int) Pair(suite.adt.pair.Pair) Streamlet(suite.streamlet.Streamlet) FunpIf(suite.funp.P0.FunpIf) FunpNumber(suite.funp.P0.FunpNumber) FunpDeref(suite.funp.P0.FunpDeref) FunpMemory(suite.funp.P2.FunpMemory) FunpWhile(suite.funp.P2.FunpWhile) Switch(suite.util.Switch) FunpData(suite.funp.P2.FunpData) TreeUtil(suite.node.util.TreeUtil) FunpDontCare(suite.funp.P0.FunpDontCare) FunpDontCare(suite.funp.P0.FunpDontCare) FunpReference(suite.funp.P0.FunpReference) FunpWhile(suite.funp.P2.FunpWhile) FunpMemory(suite.funp.P2.FunpMemory) FunpBoolean(suite.funp.P0.FunpBoolean) Funp(suite.funp.Funp_.Funp) FunpDeref(suite.funp.P0.FunpDeref) Streamlet(suite.streamlet.Streamlet) FunpCoerce(suite.funp.P0.FunpCoerce) IntInt_Int(suite.primitive.IntInt_Int) FunpTree(suite.funp.P0.FunpTree) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) IntIntPair(suite.primitive.adt.pair.IntIntPair) IntIntPair(suite.primitive.adt.pair.IntIntPair) Pair(suite.adt.pair.Pair)

Aggregations

Pair (suite.adt.pair.Pair)1 Funp (suite.funp.Funp_.Funp)1 FunpBoolean (suite.funp.P0.FunpBoolean)1 FunpCoerce (suite.funp.P0.FunpCoerce)1 FunpDeref (suite.funp.P0.FunpDeref)1 FunpDontCare (suite.funp.P0.FunpDontCare)1 FunpIf (suite.funp.P0.FunpIf)1 FunpNumber (suite.funp.P0.FunpNumber)1 FunpReference (suite.funp.P0.FunpReference)1 FunpTree (suite.funp.P0.FunpTree)1 FunpTree2 (suite.funp.P0.FunpTree2)1 FunpData (suite.funp.P2.FunpData)1 FunpMemory (suite.funp.P2.FunpMemory)1 FunpWhile (suite.funp.P2.FunpWhile)1 Inspect (suite.inspect.Inspect)1 TermOp (suite.node.io.TermOp)1 Singleton (suite.node.util.Singleton)1 TreeUtil (suite.node.util.TreeUtil)1 IntInt_Bool (suite.node.util.TreeUtil.IntInt_Bool)1 IntInt_Int (suite.primitive.IntInt_Int)1