Extend CompositeFIF instead of using FIFBuilder.
This commit is contained in:
+8
-9
@@ -37,7 +37,7 @@ import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternB
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
public final class ArrayFIF {
|
||||
public final class ArrayFIF extends CompositeFIF {
|
||||
|
||||
@NotNull
|
||||
private static final NamePredicate ARRAYS;
|
||||
@@ -85,15 +85,14 @@ public final class ArrayFIF {
|
||||
public static final BuiltInPropertyIntrinsic ARRAY_LENGTH_INTRINSIC = new BuiltInPropertyIntrinsic("length");
|
||||
|
||||
@NotNull
|
||||
public static final FunctionIntrinsicFactory INSTANCE = FIFBuilder.start()
|
||||
.add(pattern(ARRAYS, "get"), GET_INTRINSIC)
|
||||
.add(pattern(ARRAYS, "set"), SET_INTRINSIC)
|
||||
.add(pattern(ARRAYS, "<get-size>"), ARRAY_LENGTH_INTRINSIC)
|
||||
.add(pattern(ARRAYS, "<get-indices>"), new CallStandardMethodIntrinsic("Kotlin.arrayIndices", true, 0))
|
||||
.add(pattern(ARRAYS, "iterator"), new CallStandardMethodIntrinsic("Kotlin.arrayIterator", true, 0))
|
||||
.add(pattern(ARRAYS, "<init>"), new CallStandardMethodIntrinsic("Kotlin.arrayFromFun", false, 2))
|
||||
.build();
|
||||
public static final FunctionIntrinsicFactory INSTANCE = new ArrayFIF();
|
||||
|
||||
private ArrayFIF() {
|
||||
add(pattern(ARRAYS, "get"), GET_INTRINSIC);
|
||||
add(pattern(ARRAYS, "set"), SET_INTRINSIC);
|
||||
add(pattern(ARRAYS, "<get-size>"), ARRAY_LENGTH_INTRINSIC);
|
||||
add(pattern(ARRAYS, "<get-indices>"), new CallStandardMethodIntrinsic("Kotlin.arrayIndices", true, 0));
|
||||
add(pattern(ARRAYS, "iterator"), new CallStandardMethodIntrinsic("Kotlin.arrayIterator", true, 0));
|
||||
add(pattern(ARRAYS, "<init>"), new CallStandardMethodIntrinsic("Kotlin.arrayFromFun", false, 2));
|
||||
}
|
||||
}
|
||||
|
||||
+24
-37
@@ -31,51 +31,38 @@ import java.util.Map;
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
public final class FIFBuilder {
|
||||
public abstract class CompositeFIF implements FunctionIntrinsicFactory {
|
||||
|
||||
@NotNull final Map<DescriptorPredicate, FunctionIntrinsic> patternToIntrinsic = Maps.newHashMap();
|
||||
|
||||
private FIFBuilder() {
|
||||
protected CompositeFIF() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static FIFBuilder start() {
|
||||
return new FIFBuilder();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public FIFBuilder add(@NotNull DescriptorPredicate pattern, @NotNull FunctionIntrinsic intrinsic) {
|
||||
patternToIntrinsic.put(pattern, intrinsic);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public FunctionIntrinsicFactory build() {
|
||||
return new FunctionIntrinsicFactory() {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Predicate<FunctionDescriptor> getPredicate() {
|
||||
Collection<DescriptorPredicate> patterns = patternToIntrinsic.keySet();
|
||||
final DescriptorPredicate[] patterns1 = patterns.toArray(new DescriptorPredicate[patterns.size()]);
|
||||
return new DescriptorPredicate() {
|
||||
@Override
|
||||
public Predicate<FunctionDescriptor> getPredicate() {
|
||||
Collection<DescriptorPredicate> patterns = patternToIntrinsic.keySet();
|
||||
final DescriptorPredicate[] patterns1 = patterns.toArray(new DescriptorPredicate[patterns.size()]);
|
||||
return new DescriptorPredicate() {
|
||||
@Override
|
||||
public boolean apply(@Nullable FunctionDescriptor descriptor) {
|
||||
return Predicates.or(patterns1).apply(descriptor);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public FunctionIntrinsic getIntrinsic(@NotNull FunctionDescriptor descriptor) {
|
||||
for (DescriptorPredicate pattern : patternToIntrinsic.keySet()) {
|
||||
if (pattern.apply(descriptor)) {
|
||||
return patternToIntrinsic.get(pattern);
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("Must have intrinsic for pattern.");
|
||||
public boolean apply(@Nullable FunctionDescriptor descriptor) {
|
||||
return Predicates.or(patterns1).apply(descriptor);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public FunctionIntrinsic getIntrinsic(@NotNull FunctionDescriptor descriptor) {
|
||||
for (DescriptorPredicate pattern : patternToIntrinsic.keySet()) {
|
||||
if (pattern.apply(descriptor)) {
|
||||
return patternToIntrinsic.get(pattern);
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("Must have intrinsic for pattern.");
|
||||
}
|
||||
|
||||
protected void add(@NotNull DescriptorPredicate pattern, @NotNull FunctionIntrinsic intrinsic) {
|
||||
patternToIntrinsic.put(pattern, intrinsic);
|
||||
}
|
||||
}
|
||||
+5
-6
@@ -36,7 +36,7 @@ import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternB
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
public final class NumberConversionFIF {
|
||||
public final class NumberConversionFIF extends CompositeFIF {
|
||||
@NotNull
|
||||
private static final NamePredicate SUPPORTED_CONVERSIONS;
|
||||
|
||||
@@ -74,12 +74,11 @@ public final class NumberConversionFIF {
|
||||
@NotNull
|
||||
public static final String FLOATING_POINT_NUMBER_TYPES = "Float|Double|Number";
|
||||
@NotNull
|
||||
public static final FunctionIntrinsicFactory INSTANCE = FIFBuilder.start()
|
||||
.add(pattern(INTEGER_NUMBER_TYPES, SUPPORTED_CONVERSIONS), RETURN_RECEIVER)
|
||||
.add(pattern(FLOATING_POINT_NUMBER_TYPES, INTEGER_CONVERSIONS), new CallStandardMethodIntrinsic("Math.floor", true, 0))
|
||||
.add(pattern(FLOATING_POINT_NUMBER_TYPES, FLOATING_POINT_CONVERSIONS), RETURN_RECEIVER)
|
||||
.build();
|
||||
public static final FunctionIntrinsicFactory INSTANCE = new NumberConversionFIF();
|
||||
|
||||
private NumberConversionFIF() {
|
||||
add(pattern(INTEGER_NUMBER_TYPES, SUPPORTED_CONVERSIONS), RETURN_RECEIVER);
|
||||
add(pattern(FLOATING_POINT_NUMBER_TYPES, INTEGER_CONVERSIONS), new CallStandardMethodIntrinsic("Math.floor", true, 0));
|
||||
add(pattern(FLOATING_POINT_NUMBER_TYPES, FLOATING_POINT_CONVERSIONS), RETURN_RECEIVER);
|
||||
}
|
||||
}
|
||||
|
||||
+5
-6
@@ -35,7 +35,7 @@ import static org.jetbrains.k2js.translate.utils.JsAstUtils.setQualifier;
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
public final class StringOperationFIF {
|
||||
public final class StringOperationFIF extends CompositeFIF {
|
||||
|
||||
@NotNull
|
||||
private static final DescriptorPredicate GET_PATTERN = pattern("String.get");
|
||||
@@ -57,12 +57,11 @@ public final class StringOperationFIF {
|
||||
};
|
||||
|
||||
@NotNull
|
||||
public static final FunctionIntrinsicFactory INSTANCE = FIFBuilder.start()
|
||||
.add(GET_PATTERN, GET_INTRINSIC)
|
||||
.add(pattern("String.<get-length>"), new BuiltInPropertyIntrinsic("length"))
|
||||
.add(pattern("CharSequence.<get-length>"), new BuiltInPropertyIntrinsic("length"))
|
||||
.build();
|
||||
public static final FunctionIntrinsicFactory INSTANCE = new StringOperationFIF();
|
||||
|
||||
private StringOperationFIF() {
|
||||
add(GET_PATTERN, GET_INTRINSIC);
|
||||
add(pattern("String.<get-length>"), new BuiltInPropertyIntrinsic("length"));
|
||||
add(pattern("CharSequence.<get-length>"), new BuiltInPropertyIntrinsic("length"));
|
||||
}
|
||||
}
|
||||
|
||||
+5
-6
@@ -26,18 +26,17 @@ import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternB
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
public final class TopLevelFIF {
|
||||
public final class TopLevelFIF extends CompositeFIF {
|
||||
|
||||
@NotNull
|
||||
private static final DescriptorPredicate EXT_TO_STRING = pattern("toString");
|
||||
|
||||
@NotNull
|
||||
public static final FunctionIntrinsicFactory INSTANCE = FIFBuilder.start()
|
||||
.add(EXT_TO_STRING, new BuiltInFunctionIntrinsic("toString"))
|
||||
//TODO: add intrinsic for calling equals explicitly
|
||||
.add(pattern("arrayOfNulls"), new CallStandardMethodIntrinsic("Kotlin.nullArray", false, 1))
|
||||
.build();
|
||||
public static final FunctionIntrinsicFactory INSTANCE = new TopLevelFIF();
|
||||
|
||||
private TopLevelFIF() {
|
||||
add(EXT_TO_STRING, new BuiltInFunctionIntrinsic("toString"));
|
||||
//TODO: add intrinsic for calling equals explicitly
|
||||
add(pattern("arrayOfNulls"), new CallStandardMethodIntrinsic("Kotlin.nullArray", false, 1));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user