arrays supported more correclty
This commit is contained in:
@@ -20,9 +20,9 @@ public class ArrayInitializerExpression extends Expression {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private static String createArrayFunction(@NotNull final Type type) {
|
private static String createArrayFunction(@NotNull final Type type) {
|
||||||
String sType = innerTypeStr(type);
|
String sType = innerTypeStr(type);
|
||||||
if (!sType.equals("any") && PRIMITIVE_TYPES.contains(sType))
|
if (PRIMITIVE_TYPES.contains(sType))
|
||||||
return sType + "Array";
|
return sType + "Array"; // intArray
|
||||||
return AstUtil.lowerFirstCharacter(type.convertedToNotNull().toKotlin());
|
return AstUtil.lowerFirstCharacter(type.convertedToNotNull().toKotlin()); // array<Foo?>
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
@@ -12,6 +12,16 @@ public class ArrayType extends Type {
|
|||||||
myType = type;
|
myType = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Type getInnerType() {
|
||||||
|
return myType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public Kind getKind() {
|
||||||
|
return Kind.ARRAY_TYPE;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String toKotlin() {
|
public String toKotlin() {
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package org.jetbrains.jet.j2k.ast;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.j2k.util.AstUtil;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author ignatov
|
||||||
|
*/
|
||||||
|
public class ArrayWithoutInitializationExpression extends Expression {
|
||||||
|
private final Type myType;
|
||||||
|
private final List<Expression> myExpressions;
|
||||||
|
|
||||||
|
public ArrayWithoutInitializationExpression(Type type, List<Expression> expressions) {
|
||||||
|
myType = type;
|
||||||
|
myExpressions = expressions;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public String toKotlin() {
|
||||||
|
if (myType.getKind() == Kind.ARRAY_TYPE) {
|
||||||
|
if (myExpressions.size() == 1)
|
||||||
|
return oneDim(myType, myExpressions.get(0));
|
||||||
|
if (myExpressions.size() == 2) {
|
||||||
|
Type innerType = ((ArrayType) myType).getInnerType();
|
||||||
|
return oneDim(myType, myExpressions.get(0), "{" + SPACE +
|
||||||
|
oneDim(innerType, myExpressions.get(1)) + SPACE + "}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return getConstructorName(myType);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String oneDim(Type type, Expression size) {
|
||||||
|
return oneDim(type, size, EMPTY);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String oneDim(Type type, Expression size, String init) {
|
||||||
|
String commaWithInit = init.isEmpty() ? EMPTY : COMMA_WITH_SPACE + init;
|
||||||
|
return getConstructorName(type) + "(" + size.toKotlin() + commaWithInit + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getConstructorName(Type type) {
|
||||||
|
// if (type instanceof ArrayType) {
|
||||||
|
// String innerTypeStr = ((ArrayType) type).getInnerType().toKotlin().toLowerCase();
|
||||||
|
// if (PRIMITIVE_TYPES.contains(innerTypeStr))
|
||||||
|
// return innerTypeStr + "Array";
|
||||||
|
// }
|
||||||
|
return AstUtil.replaceLastQuest(type.toKotlin());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,6 +13,6 @@ public interface INode {
|
|||||||
public Kind getKind();
|
public Kind getKind();
|
||||||
|
|
||||||
public enum Kind {
|
public enum Kind {
|
||||||
UNDEFINED, TYPE, CONSTRUCTOR, BREAK, CONTINUE, VARARG, TRAIT, ASSIGNMENT_EXPRESSION, CALL_CHAIN, LITERAL,
|
UNDEFINED, TYPE, CONSTRUCTOR, BREAK, CONTINUE, VARARG, TRAIT, ASSIGNMENT_EXPRESSION, CALL_CHAIN, LITERAL, ARRAY_TYPE,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,4 +87,10 @@ public class AstUtil {
|
|||||||
return map.get(e);
|
return map.get(e);
|
||||||
return orElse;
|
return orElse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String replaceLastQuest(String str) {
|
||||||
|
if (str.endsWith("?"))
|
||||||
|
return str.substring(0, str.length() - 1);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -214,11 +214,10 @@ public class ExpressionVisitor extends StatementVisitor {
|
|||||||
@Override
|
@Override
|
||||||
public void visitNewExpression(@NotNull PsiNewExpression expression) {
|
public void visitNewExpression(@NotNull PsiNewExpression expression) {
|
||||||
super.visitNewExpression(expression);
|
super.visitNewExpression(expression);
|
||||||
|
if (expression.getArrayInitializer() != null) // new Foo[] {Foo(1), Foo(2)}
|
||||||
if (expression.getArrayInitializer() != null) // new Foo[] {}
|
|
||||||
myResult = createNewEmptyArray(expression);
|
myResult = createNewEmptyArray(expression);
|
||||||
else if (expression.getArrayDimensions().length > 0) { // new Foo[5]
|
else if (expression.getArrayDimensions().length > 0) { // new Foo[5]
|
||||||
myResult = createNewNonEmptyArray(expression);
|
myResult = createNewEmptyArrayWithoutInitialization(expression);
|
||||||
} else { // new Class(): common case
|
} else { // new Class(): common case
|
||||||
myResult = createNewClassExpression(expression);
|
myResult = createNewClassExpression(expression);
|
||||||
}
|
}
|
||||||
@@ -254,12 +253,10 @@ public class ExpressionVisitor extends StatementVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static NewClassExpression createNewNonEmptyArray(@NotNull PsiNewExpression expression) {
|
private static Expression createNewEmptyArrayWithoutInitialization(@NotNull PsiNewExpression expression) {
|
||||||
final List<Expression> callExpression = expressionsToExpressionList(expression.getArrayDimensions());
|
return new ArrayWithoutInitializationExpression(
|
||||||
callExpression.add(new IdentifierImpl("{null}")); // TODO: remove
|
typeToType(expression.getType(), true),
|
||||||
return new NewClassExpression(
|
expressionsToExpressionList(expression.getArrayDimensions())
|
||||||
typeToType(expression.getType()),
|
|
||||||
callExpression
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
var d2 : Array<IntArray?>? = Array<IntArray?>?(5, {null})
|
var d2 : Array<IntArray?>? = Array<IntArray?>(5)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
int [][] d2 = new int[5][5];
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
String [][] ss = new String[5][5];
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
var array : IntArray? = IntArray?(10, {null})
|
var array : IntArray? = IntArray(10)
|
||||||
for (i in 0..10) {
|
for (i in 0..10) {
|
||||||
array[i] = i
|
array[i] = i
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
var array : IntArray? = IntArray?(10, {null})
|
var array : IntArray? = IntArray(10)
|
||||||
for (i in 0..(10 - 1)) {
|
for (i in 0..(10 - 1)) {
|
||||||
array[i] = i
|
array[i] = i
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
var array : IntArray? = IntArray?(10, {null})
|
var array : IntArray? = IntArray(10)
|
||||||
for (i in 0..(10 - 1)) {
|
for (i in 0..(10 - 1)) {
|
||||||
array[i] = i
|
array[i] = i
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user