explicit conversion for arguments added in array function
This commit is contained in:
@@ -3,7 +3,7 @@ package org.jetbrains.jet.j2k.ast;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.j2k.util.AstUtil;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
@@ -15,20 +15,55 @@ public class ArrayInitializerExpression extends Expression {
|
||||
public ArrayInitializerExpression(final Type type, List<Expression> initializers) {
|
||||
myType = type;
|
||||
myInitializers = initializers;
|
||||
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String createArrayFunction(@NotNull final Type type) {
|
||||
String sType = type.convertedToNotNull().toKotlin().replace("Array", "").toLowerCase();
|
||||
String sType = innerTypeStr(type);
|
||||
if (!sType.equals("any") && PRIMITIVE_TYPES.contains(sType))
|
||||
return sType + "Array";
|
||||
return AstUtil.lowerFirstCharacter(type.convertedToNotNull().toKotlin());
|
||||
}
|
||||
|
||||
private static String innerTypeStr(final Type type) {
|
||||
return type.convertedToNotNull().toKotlin().replace("Array", "").toLowerCase();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String createInitializers(@NotNull final Type type, @NotNull final List<Expression> initializers) {
|
||||
List<String> arguments = new LinkedList<String>();
|
||||
for (Expression i : initializers)
|
||||
arguments.add(explicitConvertIfNeeded(type, i));
|
||||
return AstUtil.join(arguments, COMMA_WITH_SPACE);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String explicitConvertIfNeeded(final Type type, @NotNull final Expression i) {
|
||||
Set<String> doubleOrFloatTypes = new HashSet<String>(
|
||||
Arrays.asList("double", "float", "java.lang.double", "java.lang.float")
|
||||
);
|
||||
String afterReplace = innerTypeStr(type).replace(">", "").replace("<", "").replace("?", "");
|
||||
if (doubleOrFloatTypes.contains(afterReplace)) {
|
||||
if (i.getKind() == Kind.LITERAL) {
|
||||
if (i.toKotlin().contains("."))
|
||||
return i.toKotlin();
|
||||
return i.toKotlin() + DOT + ZERO;
|
||||
}
|
||||
return "(" + i.toKotlin() + ")" + getConversion(afterReplace);
|
||||
}
|
||||
return i.toKotlin();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String getConversion(@NotNull final String afterReplace) {
|
||||
if (afterReplace.contains("double")) return DOT + "dbl";
|
||||
if (afterReplace.contains("float")) return DOT + "flt";
|
||||
return "";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String toKotlin() {
|
||||
return createArrayFunction(myType) + "(" + AstUtil.joinNodes(myInitializers, COMMA_WITH_SPACE) + ")";
|
||||
return createArrayFunction(myType) + "(" + createInitializers(myType, myInitializers) + ")";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,6 @@ public interface INode {
|
||||
public Kind getKind();
|
||||
|
||||
public enum Kind {
|
||||
UNDEFINED, TYPE, CONSTRUCTOR, BREAK, CONTINUE, VARARG, TRAIT, ASSIGNMENT_EXPRESSION, CALL_CHAIN,
|
||||
UNDEFINED, TYPE, CONSTRUCTOR, BREAK, CONTINUE, VARARG, TRAIT, ASSIGNMENT_EXPRESSION, CALL_CHAIN, LITERAL,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,12 @@ public class LiteralExpression extends Expression {
|
||||
myIdentifier = identifier;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Kind getKind() {
|
||||
return Kind.LITERAL;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String toKotlin() {
|
||||
|
||||
@@ -39,4 +39,5 @@ public abstract class Node implements INode {
|
||||
static final String QUEST = "?";
|
||||
static final String COMMA_WITH_SPACE = "," + SPACE;
|
||||
static final String STAR = "*";
|
||||
public static final String ZERO = "0";
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
double[] a = new double[]{1.0, 2, 3}
|
||||
@@ -0,0 +1 @@
|
||||
var a : DoubleArray? = doubleArray(1.0, 2.0, 3.0)
|
||||
@@ -0,0 +1,2 @@
|
||||
double a = 0, b = 0, c = 0;
|
||||
double ds[] = {a, b, c};
|
||||
@@ -0,0 +1,4 @@
|
||||
var a : Double = 0
|
||||
var b : Double = 0
|
||||
var c : Double = 0
|
||||
var ds : DoubleArray? = doubleArray((a).dbl, (b).dbl, (c).dbl)
|
||||
@@ -0,0 +1 @@
|
||||
float[] a = new float[]{1, 2, 3.0}
|
||||
@@ -0,0 +1 @@
|
||||
var a : FloatArray? = floatArray(1.0, 2.0, 3.0)
|
||||
@@ -0,0 +1 @@
|
||||
java.lang.Double[] a = new java.lang.Double[]{1, 2, 3};
|
||||
@@ -0,0 +1 @@
|
||||
var a : Array<java.lang.Double?>? = array<java.lang.Double?>(1.0, 2.0, 3.0)
|
||||
@@ -0,0 +1 @@
|
||||
java.lang.Float[] a = new java.lang.Float[]{1, 2, 3};
|
||||
@@ -0,0 +1 @@
|
||||
var a : Array<java.lang.Float?>? = array<java.lang.Float?>(1.0, 2.0, 3.0)
|
||||
@@ -1 +0,0 @@
|
||||
double[] a = new double[]{1, 2, 3}
|
||||
@@ -1 +0,0 @@
|
||||
var a : DoubleArray? = doubleArray(1, 2, 3)
|
||||
Reference in New Issue
Block a user