Search in sources :

Example 1 with FunExpr

use of suite.jdk.gen.FunExpression.FunExpr in project suite by stupidsing.

the class FunFactory method seq.

public FunExpr seq(FunExpr... fes) {
    int i = fes.length;
    if (0 < i) {
        FunExpr fe = fes[--i];
        while (0 < i) {
            SeqFunExpr expr = new SeqFunExpr();
            expr.left = fes[--i];
            expr.right = fe;
            fe = expr;
        }
        return fe;
    } else
        return _void();
}
Also used : SeqFunExpr(suite.jdk.gen.FunExprM.SeqFunExpr) PlaceholderFunExpr(suite.jdk.gen.FunExprK.PlaceholderFunExpr) InvokeLambdaFunExpr(suite.jdk.gen.FunExprL.InvokeLambdaFunExpr) NewFunExpr(suite.jdk.gen.FunExprM.NewFunExpr) AssignLocalFunExpr(suite.jdk.gen.FunExprM.AssignLocalFunExpr) LocalFunExpr(suite.jdk.gen.FunExprM.LocalFunExpr) Declare2ParameterFunExpr(suite.jdk.gen.FunExprK.Declare2ParameterFunExpr) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) ArrayFunExpr(suite.jdk.gen.FunExprM.ArrayFunExpr) FieldInjectFunExpr(suite.jdk.gen.FunExprL.FieldInjectFunExpr) Declare0ParameterFunExpr(suite.jdk.gen.FunExprK.Declare0ParameterFunExpr) IfNonNullFunExpr(suite.jdk.gen.FunExprM.IfNonNullFunExpr) VoidFunExpr(suite.jdk.gen.FunExprM.VoidFunExpr) BlockContFunExpr(suite.jdk.gen.FunExprM.BlockContFunExpr) FunExpr(suite.jdk.gen.FunExpression.FunExpr) BlockBreakFunExpr(suite.jdk.gen.FunExprM.BlockBreakFunExpr) ObjectFunExpr(suite.jdk.gen.FunExprL.ObjectFunExpr) DeclareLocalFunExpr(suite.jdk.gen.FunExprL.DeclareLocalFunExpr) If2FunExpr(suite.jdk.gen.FunExprM.If2FunExpr) ConstantFunExpr(suite.jdk.gen.FunExprM.ConstantFunExpr) If1FunExpr(suite.jdk.gen.FunExprM.If1FunExpr) BlockFunExpr(suite.jdk.gen.FunExprM.BlockFunExpr) Declare1ParameterFunExpr(suite.jdk.gen.FunExprK.Declare1ParameterFunExpr) BinaryFunExpr(suite.jdk.gen.FunExprM.BinaryFunExpr) SeqFunExpr(suite.jdk.gen.FunExprM.SeqFunExpr) InvokeMethodFunExpr(suite.jdk.gen.FunExprM.InvokeMethodFunExpr)

Example 2 with FunExpr

use of suite.jdk.gen.FunExpression.FunExpr in project suite by stupidsing.

the class FunExpand method weight.

private int weight(FunExpr e0) {
    if (e0 instanceof CastFunExpr) {
        CastFunExpr e1 = (CastFunExpr) e0;
        return weight(e1.expr);
    } else
        return // 
        Read.from(// 
        inspect.fields(e0.getClass())).toInt(Obj_Int.sum(field -> {
            Object e1 = Rethrow.ex(() -> field.get(e0));
            if (e1 instanceof FunExpr)
                return weight_(e1);
            else if (e1 instanceof Iterable<?>) {
                Iterable<?> iter = (Iterable<?>) e1;
                int sum = 0;
                for (Object e2 : iter) sum += weight_(e2);
                return sum;
            } else
                return 0;
        })) + 1;
}
Also used : DeclareLocalFunExpr(suite.jdk.gen.FunExprL.DeclareLocalFunExpr) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) Inspect(suite.inspect.Inspect) ConstantFunExpr(suite.jdk.gen.FunExprM.ConstantFunExpr) If1FunExpr(suite.jdk.gen.FunExprM.If1FunExpr) InvokeLambdaFunExpr(suite.jdk.gen.FunExprL.InvokeLambdaFunExpr) Declare2ParameterFunExpr(suite.jdk.gen.FunExprK.Declare2ParameterFunExpr) ApplyFunExpr(suite.jdk.gen.FunExprL.ApplyFunExpr) LambdaImplementation(suite.jdk.lambda.LambdaImplementation) FieldInjectFunExpr(suite.jdk.gen.FunExprL.FieldInjectFunExpr) Obj_Int(suite.primitive.IntPrimitives.Obj_Int) String_(suite.util.String_) Declare0ParameterFunExpr(suite.jdk.gen.FunExprK.Declare0ParameterFunExpr) Declare1ParameterFunExpr(suite.jdk.gen.FunExprK.Declare1ParameterFunExpr) Rethrow(suite.util.Rethrow) LambdaInstance(suite.jdk.lambda.LambdaInstance) Type(org.apache.bcel.generic.Type) CastFunExpr(suite.jdk.gen.FunExprM.CastFunExpr) FunExpr(suite.jdk.gen.FunExpression.FunExpr) FunFactory(suite.jdk.gen.FunFactory) LambdaInterface(suite.jdk.lambda.LambdaInterface) CastFunExpr(suite.jdk.gen.FunExprM.CastFunExpr) DeclareLocalFunExpr(suite.jdk.gen.FunExprL.DeclareLocalFunExpr) ConstantFunExpr(suite.jdk.gen.FunExprM.ConstantFunExpr) If1FunExpr(suite.jdk.gen.FunExprM.If1FunExpr) InvokeLambdaFunExpr(suite.jdk.gen.FunExprL.InvokeLambdaFunExpr) Declare2ParameterFunExpr(suite.jdk.gen.FunExprK.Declare2ParameterFunExpr) ApplyFunExpr(suite.jdk.gen.FunExprL.ApplyFunExpr) FieldInjectFunExpr(suite.jdk.gen.FunExprL.FieldInjectFunExpr) Declare0ParameterFunExpr(suite.jdk.gen.FunExprK.Declare0ParameterFunExpr) Declare1ParameterFunExpr(suite.jdk.gen.FunExprK.Declare1ParameterFunExpr) CastFunExpr(suite.jdk.gen.FunExprM.CastFunExpr) FunExpr(suite.jdk.gen.FunExpression.FunExpr)

Example 3 with FunExpr

use of suite.jdk.gen.FunExpression.FunExpr in project suite by stupidsing.

the class CompileBinderImpl method binder.

public Bind_ binder(Node node) {
    FunCreator<Bind_> fc = FunCreator.of(Bind_.class, false);
    return fc.create(new BinOp<>() {

        private FunExpr env, trail, b;

        public FunExpr apply(FunExpr bindEnv, FunExpr target) {
            this.env = bindEnv.field("env");
            this.trail = bindEnv.field("trail");
            return f.declare(ok, b -> {
                this.b = b;
                return compile_(node, target);
            });
        }

        private FunExpr compile_(Node node, FunExpr target) {
            FunExpr br = bind(f.object_(node, Node.class), target);
            FunExpr brc = bindClone(node, target);
            return new // 
            SwitchNode<FunExpr>(// 
            node).applyIf(Atom.class, n -> {
                return f.ifEquals(target, f.object(node), ok, br);
            }).applyIf(Int.class, n -> {
                return f.ifInstance(Int.class, target, i -> f.ifEquals(i.field("number"), f.int_(n.number), ok, fail), br);
            }).applyIf(Reference.class, n -> {
                FunExpr ref = env.field("refs").index(f.int_(mapper().computeIndex(n)));
                return f.invokeStatic(Binder.class, "bind", target, ref.cast_(Node.class), trail);
            }).applyIf(Str.class, n -> {
                return f.ifInstance(Str.class, target, s -> f.object(n.value).invoke("equals", s.field("value").cast_(Object.class)), br);
            }).applyIf(Tree.class, tree -> {
                return f.declare(f.invokeStatic(Tree.class, "decompose", target, f.object(tree.getOperator())), t -> {
                    Node lt = tree.getLeft();
                    Node rt = tree.getRight();
                    return f.ifNonNull(t, compile_(lt, t.invoke("getLeft"), compile_(rt, t.invoke("getRight"), ok)), brc);
                });
            }).applyIf(Tuple.class, n -> {
                return f.ifInstance(Tuple.class, target, tuple -> f.declare(tuple.field("nodes"), targets -> {
                    Node[] nodes = n.nodes;
                    FunExpr fe = ok;
                    for (int i = 0; i < nodes.length; i++) fe = compile_(nodes[i], targets.index(f.int_(i)), fe);
                    return f.if_(targets.length(), fe, brc);
                }), brc);
            }).applyIf(Node.class, n -> {
                Clone_ cloner = cloner(n);
                return f.invokeStatic(Binder.class, "bind", target, f.object(cloner).invoke("apply", env), trail);
            }).nonNullResult();
        }

        private FunExpr compile_(Node node, FunExpr target, FunExpr cps) {
            return f.assign(b, compile_(node, target), f.if_(b, cps, fail));
        }

        private FunExpr bindClone(Node node, FunExpr target) {
            if (isBindTrees)
                return bind(f.object(cloner(node)).apply(env), target);
            else
                return fail;
        }

        private FunExpr bind(FunExpr node, FunExpr target) {
            return f.ifInstanceAnd(Reference.class, target, ref -> f.seq(trail.invoke("addBind", ref, node), ok));
        }
    }).apply(Map.ofEntries());
}
Also used : Reference(suite.node.Reference) BinderFactory(suite.lp.doer.BinderFactory) Tree(suite.node.Tree) BinOp(suite.util.FunUtil2.BinOp) Node(suite.node.Node) Atom(suite.node.Atom) Map(java.util.Map) FunCreator(suite.jdk.gen.FunCreator) Binder(suite.lp.doer.Binder) SwitchNode(suite.node.io.SwitchNode) FunExpr(suite.jdk.gen.FunExpression.FunExpr) FunFactory(suite.jdk.gen.FunFactory) Int(suite.node.Int) Tuple(suite.node.Tuple) Str(suite.node.Str) Binder(suite.lp.doer.Binder) Reference(suite.node.Reference) Node(suite.node.Node) SwitchNode(suite.node.io.SwitchNode) Tree(suite.node.Tree) BinOp(suite.util.FunUtil2.BinOp) SwitchNode(suite.node.io.SwitchNode) FunExpr(suite.jdk.gen.FunExpression.FunExpr) Atom(suite.node.Atom)

Example 4 with FunExpr

use of suite.jdk.gen.FunExpression.FunExpr in project suite by stupidsing.

the class CompileClonerImpl method cloner.

@Override
public Clone_ cloner(Node node) {
    FunCreator<Clone_> fc = FunCreator.of(Clone_.class, false);
    return fc.create(new Iterate<>() {

        private FunExpr env;

        public FunExpr apply(FunExpr env) {
            this.env = env;
            return compile_(node);
        }

        private FunExpr compile_(Node node_) {
            return new // 
            SwitchNode<FunExpr>(// 
            node_).applyIf(Atom.class, n -> {
                return f.object(node_);
            }).applyIf(Dict.class, n -> {
                FunExpr[] exprs = // 
                Read.from2(// 
                n.map).map(// 
                (key, value) -> f.invokeStatic(Pair.class, "of", compile_(key), compile_(value))).toArray(FunExpr.class);
                return f.invokeStatic(Dict.class, "of", f.array(Pair.class, exprs));
            }).applyIf(Int.class, n -> {
                return f.object(node_);
            }).applyIf(Reference.class, n -> {
                return env.field("refs").index(f.int_(vm.computeIndex(n)));
            }).applyIf(Str.class, n -> {
                return f.object(node_);
            }).applyIf(Tree.class, tree -> {
                FunExpr fe0 = compile_(tree.getLeft()).cast_(Node.class);
                FunExpr fe1 = compile_(tree.getRight()).cast_(Node.class);
                return f.invokeStatic(Tree.class, "of", f.object(tree.getOperator()), fe0, fe1);
            }).applyIf(Tuple.class, n -> {
                FunExpr[] exprs = Read.from(n.nodes).map(this::compile_).toArray(FunExpr.class);
                return f.invokeStatic(Tuple.class, "of", f.array(Node.class, exprs));
            }).nonNullResult();
        }
    }).apply(Map.ofEntries());
}
Also used : Reference(suite.node.Reference) Read(suite.streamlet.Read) Tree(suite.node.Tree) Node(suite.node.Node) Iterate(suite.util.FunUtil.Iterate) Pair(suite.adt.pair.Pair) VariableMapper(suite.lp.sewing.VariableMapper) Atom(suite.node.Atom) Map(java.util.Map) ClonerFactory(suite.lp.doer.ClonerFactory) FunCreator(suite.jdk.gen.FunCreator) SwitchNode(suite.node.io.SwitchNode) FunExpr(suite.jdk.gen.FunExpression.FunExpr) FunFactory(suite.jdk.gen.FunFactory) Int(suite.node.Int) Tuple(suite.node.Tuple) Dict(suite.node.Dict) Str(suite.node.Str) Iterate(suite.util.FunUtil.Iterate) Node(suite.node.Node) SwitchNode(suite.node.io.SwitchNode) SwitchNode(suite.node.io.SwitchNode) Atom(suite.node.Atom) Int(suite.node.Int) Str(suite.node.Str) FunExpr(suite.jdk.gen.FunExpression.FunExpr) Tuple(suite.node.Tuple) Pair(suite.adt.pair.Pair)

Example 5 with FunExpr

use of suite.jdk.gen.FunExpression.FunExpr in project suite by stupidsing.

the class CompileProverImpl method prover.

@Override
public Prove_ prover(Node node) {
    FunExpr rt = f.input();
    Fun<FunExpr, ProveRt> cf = cps -> FunCreator.of(ProveRt.class, false).create(rt_ -> cps).apply(Map.ofEntries());
    FunExpr compiled = new Object() {

        private FunExpr compile_(Node node, FunExpr cps) {
            return new // 
            SwitchNode<FunExpr>(// 
            node).match(".0, .1", m -> {
                return compile_(m[0], compile_(m[1], cps));
            }).match(".0; .1", m -> {
                FunExpr cps1;
                if (Boolean.TRUE) {
                    ProveRt proveRt_ = cf.apply(cps);
                    cps1 = f.object(proveRt_).invoke("test", rt);
                } else
                    cps1 = cps;
                FunExpr f0 = compile_(m[0], cps1);
                FunExpr f1 = compile_(m[1], cps1);
                return f.seq(f0, f1);
            }).match("fail", m -> {
                return f._void();
            }).match("yes", m -> {
                return cps;
            }).nonNullResult();
        }
    }.compile_(node, rt.fieldSet("ok", ok));
    ProveRt proveRt = cf.apply(compiled);
    return proverConfig -> {
        Runtime_ rt_ = new Runtime_();
        rt_.proverConfig = proverConfig;
        proveRt.test(rt_);
        return rt_.ok;
    };
}
Also used : Map(java.util.Map) FunCreator(suite.jdk.gen.FunCreator) ProverFactory(suite.lp.doer.ProverFactory) SwitchNode(suite.node.io.SwitchNode) FunExpr(suite.jdk.gen.FunExpression.FunExpr) FunFactory(suite.jdk.gen.FunFactory) Fun(suite.util.FunUtil.Fun) ProverConfig(suite.lp.Configuration.ProverConfig) Node(suite.node.Node) SwitchNode(suite.node.io.SwitchNode) Node(suite.node.Node) SwitchNode(suite.node.io.SwitchNode) FunExpr(suite.jdk.gen.FunExpression.FunExpr)

Aggregations

FunExpr (suite.jdk.gen.FunExpression.FunExpr)11 Map (java.util.Map)7 FunFactory (suite.jdk.gen.FunFactory)7 Node (suite.node.Node)6 Type (org.apache.bcel.generic.Type)5 FunCreator (suite.jdk.gen.FunCreator)5 Declare0ParameterFunExpr (suite.jdk.gen.FunExprK.Declare0ParameterFunExpr)5 Declare1ParameterFunExpr (suite.jdk.gen.FunExprK.Declare1ParameterFunExpr)5 Declare2ParameterFunExpr (suite.jdk.gen.FunExprK.Declare2ParameterFunExpr)5 DeclareLocalFunExpr (suite.jdk.gen.FunExprL.DeclareLocalFunExpr)5 FieldInjectFunExpr (suite.jdk.gen.FunExprL.FieldInjectFunExpr)5 InvokeLambdaFunExpr (suite.jdk.gen.FunExprL.InvokeLambdaFunExpr)5 ProfileFunExpr (suite.jdk.gen.FunExprM.ProfileFunExpr)5 Int (suite.node.Int)5 ConstantFunExpr (suite.jdk.gen.FunExprM.ConstantFunExpr)4 If1FunExpr (suite.jdk.gen.FunExprM.If1FunExpr)4 LambdaInstance (suite.jdk.lambda.LambdaInstance)4 LambdaInterface (suite.jdk.lambda.LambdaInterface)4 Reference (suite.node.Reference)4 Tree (suite.node.Tree)4