use of suite.jdk.gen.FunExprK.DeclareParameterFunExpr 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();
}
Aggregations