Search in sources :

Example 6 with FunExpr

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

the class FunCreatorTest method testObject.

@Test
public void testObject() {
    Int_Int inc = i -> i + 1;
    Iterate<FunExpr> fun = i -> f.object(inc).invoke("apply", i);
    assertEquals(3, LambdaInstance.of(Int_Int.class, fun).newFun().apply(2));
}
Also used : Reference(suite.node.Reference) Suite(suite.Suite) Flt_Flt(suite.primitive.Flt_Flt) Dump(suite.inspect.Dump) PrintlnFunExpr(suite.jdk.gen.FunExprM.PrintlnFunExpr) Source(suite.util.FunUtil.Source) Assert.assertTrue(org.junit.Assert.assertTrue) TermOp(suite.node.io.TermOp) Test(org.junit.Test) Fun(suite.util.FunUtil.Fun) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) Tree(suite.node.Tree) Node(suite.node.Node) IntSource(suite.primitive.IntPrimitives.IntSource) Iterate(suite.util.FunUtil.Iterate) BiPredicate(java.util.function.BiPredicate) LambdaInstance(suite.jdk.lambda.LambdaInstance) Map(java.util.Map) Type(org.apache.bcel.generic.Type) FunExpr(suite.jdk.gen.FunExpression.FunExpr) Int(suite.node.Int) LambdaInterface(suite.jdk.lambda.LambdaInterface) Int_Int(suite.primitive.Int_Int) Assert.assertEquals(org.junit.Assert.assertEquals) Int_Int(suite.primitive.Int_Int) PrintlnFunExpr(suite.jdk.gen.FunExprM.PrintlnFunExpr) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) FunExpr(suite.jdk.gen.FunExpression.FunExpr) Test(org.junit.Test)

Example 7 with FunExpr

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

the class FunCreatorTest method testProfile.

@Test
public void testProfile() {
    Iterate<FunExpr> fun = i -> (ProfileFunExpr) f.profile(f.int_(1));
    IntSource instance = LambdaInstance.of(IntSource.class, fun).newFun();
    assertEquals(1, instance.source());
    Dump.out(instance);
}
Also used : Reference(suite.node.Reference) Suite(suite.Suite) Flt_Flt(suite.primitive.Flt_Flt) Dump(suite.inspect.Dump) PrintlnFunExpr(suite.jdk.gen.FunExprM.PrintlnFunExpr) Source(suite.util.FunUtil.Source) Assert.assertTrue(org.junit.Assert.assertTrue) TermOp(suite.node.io.TermOp) Test(org.junit.Test) Fun(suite.util.FunUtil.Fun) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) Tree(suite.node.Tree) Node(suite.node.Node) IntSource(suite.primitive.IntPrimitives.IntSource) Iterate(suite.util.FunUtil.Iterate) BiPredicate(java.util.function.BiPredicate) LambdaInstance(suite.jdk.lambda.LambdaInstance) Map(java.util.Map) Type(org.apache.bcel.generic.Type) FunExpr(suite.jdk.gen.FunExpression.FunExpr) Int(suite.node.Int) LambdaInterface(suite.jdk.lambda.LambdaInterface) Int_Int(suite.primitive.Int_Int) Assert.assertEquals(org.junit.Assert.assertEquals) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) IntSource(suite.primitive.IntPrimitives.IntSource) PrintlnFunExpr(suite.jdk.gen.FunExprM.PrintlnFunExpr) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) FunExpr(suite.jdk.gen.FunExpression.FunExpr) Test(org.junit.Test)

Example 8 with FunExpr

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

the class FunFactory method and.

public FunExpr and(FunExpr... exprs) {
    if (0 < exprs.length) {
        List<FunExpr> list = Read.from(exprs).reverse().toList();
        FunExpr expr = list.get(0);
        for (int i = 1; i < exprs.length; i++) expr = if_(list.get(i), expr, _false());
        return expr;
    } else
        return _true();
}
Also used : 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 9 with FunExpr

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

the class FunExpand method expand_.

private FunExpr expand_(FunExpr e0, int depth) {
    return // 
    e0.<// 
    FunExpr>switch_().applyIf(ApplyFunExpr.class, e1 -> {
        FunExpr object0 = e1.object;
        FunExpr object1 = object0 instanceof CastFunExpr ? ((CastFunExpr) object0).expr : object0;
        if (object1 instanceof Declare0ParameterFunExpr) {
            Declare0ParameterFunExpr object_ = (Declare0ParameterFunExpr) object1;
            return expand(object_.do_, depth);
        } else if (object1 instanceof Declare1ParameterFunExpr) {
            Declare1ParameterFunExpr object_ = (Declare1ParameterFunExpr) object1;
            return expand(replace(object_.do_, object_.parameter, e1.parameters.get(0)), depth);
        } else if (object1 instanceof Declare2ParameterFunExpr) {
            Declare2ParameterFunExpr object_ = (Declare2ParameterFunExpr) object1;
            FunExpr do0 = object_.do_;
            FunExpr do1 = replace(do0, object_.p0, e1.parameters.get(0));
            FunExpr do2 = replace(do1, object_.p1, e1.parameters.get(1));
            return expand(do2, depth);
        } else
            return null;
    }).applyIf(DeclareLocalFunExpr.class, e1 -> {
        return expand(replace(e1.do_, e1.var, e1.value), depth);
    }).applyIf(InvokeLambdaFunExpr.class, e1 -> {
        if (Boolean.FALSE) {
            LambdaInstance<?> l_inst = e1.lambda;
            LambdaImplementation<?> l_impl = l_inst.lambdaImplementation;
            if (e1.isExpand || weight(l_impl.expr) <= 5) {
                LambdaInterface<?> l_iface = l_impl.lambdaInterface;
                FunExpr fe = l_impl.expr;
                for (String fieldName : l_impl.fieldTypes.keySet()) fe = replaceFieldInject(fe, fieldName, object(l_inst.fieldValues.get(fieldName), l_impl.fieldTypes.get(fieldName)));
                return expand(fe.cast_(l_iface.interfaceClass).apply(e1.parameters), depth - 1);
            } else
                return null;
        } else
            return null;
    }).applyIf(If1FunExpr.class, e1 -> {
        FunExpr if_ = e1.if_;
        if (if_ instanceof ConstantFunExpr) {
            ConstantFunExpr e2 = (ConstantFunExpr) if_;
            if (e2.type == Type.INT)
                return ((Integer) e2.constant).intValue() != 0 ? e1.then : e1.else_;
            else
                return null;
        } else
            return null;
    }).result();
}
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) Declare1ParameterFunExpr(suite.jdk.gen.FunExprK.Declare1ParameterFunExpr) CastFunExpr(suite.jdk.gen.FunExprM.CastFunExpr) Declare2ParameterFunExpr(suite.jdk.gen.FunExprK.Declare2ParameterFunExpr) ConstantFunExpr(suite.jdk.gen.FunExprM.ConstantFunExpr) If1FunExpr(suite.jdk.gen.FunExprM.If1FunExpr) Declare0ParameterFunExpr(suite.jdk.gen.FunExprK.Declare0ParameterFunExpr) DeclareLocalFunExpr(suite.jdk.gen.FunExprL.DeclareLocalFunExpr) 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 10 with FunExpr

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

the class FunRewrite method rewrite_.

private FunExpr rewrite_(FunExpr e0) {
    return // 
    e0.<// 
    FunExpr>switch_().applyIf(ApplyFunExpr.class, e1 -> {
        FunExpr object = rewrite(e1.object);
        FunExpr[] parameters = Read.from(e1.parameters).map(this::rewrite).toArray(FunExpr.class);
        Method method = fti.methodOf(object);
        return object.invoke(method.getName(), parameters);
    }).applyIf(CastFunExpr.class, e1 -> {
        FunExpr e2 = e1.expr;
        if (e2 instanceof DeclareParameterFunExpr) {
            Class<?> interfaceClass = Type_.classOf(e1.type);
            Map<String, Type> fieldTypes = new HashMap<>();
            Map<String, FunExpr> fieldValues = new HashMap<>();
            FunExpr e3 = rewrite(e -> {
                FunExpr fieldValue;
                if (e instanceof FieldStaticFunExpr) {
                    FieldStaticFunExpr e_ = (FieldStaticFunExpr) e;
                    String fieldName = e_.fieldName;
                    Type fieldType = fieldTypes.get(fieldName);
                    fieldTypes.put(fieldName, fieldType);
                    fieldValues.put(fieldName, e_);
                    return e;
                } else if (e instanceof PlaceholderFunExpr && (fieldValue = placeholders.get(e)) != null) {
                    String fieldName = "e" + Util.temp();
                    Type fieldType = fti.typeOf(fieldValue);
                    fieldTypes.put(fieldName, fieldType);
                    fieldValues.put(fieldName, fieldValue);
                    return this_().field(fieldName, fieldType);
                } else
                    return null;
            }, e2);
            FunCreator<?>.CreateClass cc = FunCreator.of(LambdaInterface.of(interfaceClass), fieldTypes).create_(e3);
            Streamlet2<String, FunExpr> fieldValues0 = Read.from2(cc.fieldTypeValues).mapValue(tv -> objectField(tv.t1, tv.t0));
            Streamlet2<String, FunExpr> fieldValues1 = Read.from2(fieldValues);
            NewFunExpr e4 = new NewFunExpr();
            e4.className = cc.className;
            e4.fieldValues = Streamlet2.concat(fieldValues0, fieldValues1).toMap();
            e4.implementationClass = cc.clazz;
            e4.interfaceClass = interfaceClass;
            return e4;
        } else
            return null;
    }).applyIf(DeclareLocalFunExpr.class, e1 -> {
        FunExpr value = rewrite(e1.value);
        FunExpr lfe = local(localTypes.size());
        localTypes.add(fti.typeOf(value));
        AssignLocalFunExpr alfe = new AssignLocalFunExpr();
        alfe.var = lfe;
        alfe.value = value;
        placeholders.put(e1.var, lfe);
        return seq(alfe, rewrite(e1.do_));
    }).applyIf(FieldFunExpr_.class, e1 -> {
        FunExpr set = e1 instanceof FieldSetFunExpr ? ((FieldSetFunExpr) e1).value : null;
        FunExpr object0 = rewrite(e1.object);
        String fieldName = e1.fieldName;
        Class<?> clazz = fti.classOf(object0);
        Field field = Rethrow.ex(() -> clazz.getField(fieldName));
        FunExpr object1 = object0.cast_(field.getDeclaringClass());
        Type fieldType = Type.getType(field.getType());
        return set == null ? object1.field(fieldName, fieldType) : object1.fieldSet(fieldName, fieldType, set);
    }).applyIf(FieldInjectFunExpr.class, e1 -> {
        Type type = fieldTypes.get(e1.fieldName);
        if (type != null)
            return rewrite(this_().field(e1.fieldName, type));
        else
            return Fail.t(e1.fieldName);
    }).applyIf(InvokeLambdaFunExpr.class, e1 -> {
        LambdaInstance<?> l_inst = e1.lambda;
        LambdaImplementation<?> l_impl = l_inst.lambdaImplementation;
        LambdaInterface<?> l_iface = l_impl.lambdaInterface;
        FunExpr object = object_(l_impl.newFun(l_inst.fieldValues), l_iface.interfaceClass);
        return rewrite(object.invoke(l_iface.interfaceClass, l_iface.methodName, e1.parameters));
    }).applyIf(ObjectFunExpr.class, e1 -> {
        return objectField(e1.object, e1.type);
    }).applyIf(PlaceholderFunExpr.class, e1 -> {
        FunExpr e2 = placeholders.get(e1);
        if (e2 != null)
            return e2;
        else
            return Fail.t("cannot resolve placeholder");
    }).applyIf(ProfileFunExpr.class, e1 -> {
        fieldTypeValues.put(e1.counterFieldName, Pair.of(Type.INT, 0));
        return null;
    }).result();
}
Also used : FieldStaticFunExpr(suite.jdk.gen.FunExprM.FieldStaticFunExpr) Read(suite.streamlet.Read) PlaceholderFunExpr(suite.jdk.gen.FunExprK.PlaceholderFunExpr) InvokeLambdaFunExpr(suite.jdk.gen.FunExprL.InvokeLambdaFunExpr) NewFunExpr(suite.jdk.gen.FunExprM.NewFunExpr) AssignLocalFunExpr(suite.jdk.gen.FunExprM.AssignLocalFunExpr) Util(suite.util.Util) HashMap(java.util.HashMap) Type_(suite.jdk.gen.Type_) Declare2ParameterFunExpr(suite.jdk.gen.FunExprK.Declare2ParameterFunExpr) ApplyFunExpr(suite.jdk.gen.FunExprL.ApplyFunExpr) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) ArrayList(java.util.ArrayList) LambdaImplementation(suite.jdk.lambda.LambdaImplementation) FieldInjectFunExpr(suite.jdk.gen.FunExprL.FieldInjectFunExpr) Declare0ParameterFunExpr(suite.jdk.gen.FunExprK.Declare0ParameterFunExpr) FieldSetFunExpr(suite.jdk.gen.FunExprL.FieldSetFunExpr) Rethrow(suite.util.Rethrow) LambdaInstance(suite.jdk.lambda.LambdaInstance) Map(java.util.Map) Type(org.apache.bcel.generic.Type) FunCreator(suite.jdk.gen.FunCreator) CastFunExpr(suite.jdk.gen.FunExprM.CastFunExpr) FunExpr(suite.jdk.gen.FunExpression.FunExpr) FunFactory(suite.jdk.gen.FunFactory) Method(java.lang.reflect.Method) LambdaInterface(suite.jdk.lambda.LambdaInterface) ObjectFunExpr(suite.jdk.gen.FunExprL.ObjectFunExpr) DeclareLocalFunExpr(suite.jdk.gen.FunExprL.DeclareLocalFunExpr) Streamlet2(suite.streamlet.Streamlet2) DeclareParameterFunExpr(suite.jdk.gen.FunExprK.DeclareParameterFunExpr) Field(java.lang.reflect.Field) FieldFunExpr_(suite.jdk.gen.FunExprL.FieldFunExpr_) Pair(suite.adt.pair.Pair) List(java.util.List) Declare1ParameterFunExpr(suite.jdk.gen.FunExprK.Declare1ParameterFunExpr) Fail(suite.util.Fail) ApplyFunExpr(suite.jdk.gen.FunExprL.ApplyFunExpr) HashMap(java.util.HashMap) ObjectFunExpr(suite.jdk.gen.FunExprL.ObjectFunExpr) Field(java.lang.reflect.Field) PlaceholderFunExpr(suite.jdk.gen.FunExprK.PlaceholderFunExpr) FieldSetFunExpr(suite.jdk.gen.FunExprL.FieldSetFunExpr) NewFunExpr(suite.jdk.gen.FunExprM.NewFunExpr) FieldStaticFunExpr(suite.jdk.gen.FunExprM.FieldStaticFunExpr) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) FunCreator(suite.jdk.gen.FunCreator) Method(java.lang.reflect.Method) DeclareParameterFunExpr(suite.jdk.gen.FunExprK.DeclareParameterFunExpr) Type(org.apache.bcel.generic.Type) AssignLocalFunExpr(suite.jdk.gen.FunExprM.AssignLocalFunExpr) DeclareLocalFunExpr(suite.jdk.gen.FunExprL.DeclareLocalFunExpr) FieldStaticFunExpr(suite.jdk.gen.FunExprM.FieldStaticFunExpr) PlaceholderFunExpr(suite.jdk.gen.FunExprK.PlaceholderFunExpr) InvokeLambdaFunExpr(suite.jdk.gen.FunExprL.InvokeLambdaFunExpr) NewFunExpr(suite.jdk.gen.FunExprM.NewFunExpr) AssignLocalFunExpr(suite.jdk.gen.FunExprM.AssignLocalFunExpr) Declare2ParameterFunExpr(suite.jdk.gen.FunExprK.Declare2ParameterFunExpr) ApplyFunExpr(suite.jdk.gen.FunExprL.ApplyFunExpr) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) FieldInjectFunExpr(suite.jdk.gen.FunExprL.FieldInjectFunExpr) Declare0ParameterFunExpr(suite.jdk.gen.FunExprK.Declare0ParameterFunExpr) FieldSetFunExpr(suite.jdk.gen.FunExprL.FieldSetFunExpr) CastFunExpr(suite.jdk.gen.FunExprM.CastFunExpr) FunExpr(suite.jdk.gen.FunExpression.FunExpr) ObjectFunExpr(suite.jdk.gen.FunExprL.ObjectFunExpr) DeclareLocalFunExpr(suite.jdk.gen.FunExprL.DeclareLocalFunExpr) DeclareParameterFunExpr(suite.jdk.gen.FunExprK.DeclareParameterFunExpr) Declare1ParameterFunExpr(suite.jdk.gen.FunExprK.Declare1ParameterFunExpr) FieldInjectFunExpr(suite.jdk.gen.FunExprL.FieldInjectFunExpr)

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