arrays supported more correclty

This commit is contained in:
Sergey Ignatov
2011-12-14 20:09:03 +04:00
parent 2ee8e1c087
commit a234445cef
12 changed files with 84 additions and 17 deletions
@@ -20,9 +20,9 @@ public class ArrayInitializerExpression extends Expression {
@NotNull
private static String createArrayFunction(@NotNull final Type type) {
String sType = innerTypeStr(type);
if (!sType.equals("any") && PRIMITIVE_TYPES.contains(sType))
return sType + "Array";
return AstUtil.lowerFirstCharacter(type.convertedToNotNull().toKotlin());
if (PRIMITIVE_TYPES.contains(sType))
return sType + "Array"; // intArray
return AstUtil.lowerFirstCharacter(type.convertedToNotNull().toKotlin()); // array<Foo?>
}
@NotNull
@@ -12,6 +12,16 @@ public class ArrayType extends Type {
myType = type;
}
public Type getInnerType() {
return myType;
}
@NotNull
@Override
public Kind getKind() {
return Kind.ARRAY_TYPE;
}
@NotNull
@Override
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());
}
}
+1 -1
View File
@@ -13,6 +13,6 @@ public interface INode {
public Kind getKind();
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 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
public void visitNewExpression(@NotNull PsiNewExpression expression) {
super.visitNewExpression(expression);
if (expression.getArrayInitializer() != null) // new Foo[] {}
if (expression.getArrayInitializer() != null) // new Foo[] {Foo(1), Foo(2)}
myResult = createNewEmptyArray(expression);
else if (expression.getArrayDimensions().length > 0) { // new Foo[5]
myResult = createNewNonEmptyArray(expression);
myResult = createNewEmptyArrayWithoutInitialization(expression);
} else { // new Class(): common case
myResult = createNewClassExpression(expression);
}
@@ -254,12 +253,10 @@ public class ExpressionVisitor extends StatementVisitor {
}
@NotNull
private static NewClassExpression createNewNonEmptyArray(@NotNull PsiNewExpression expression) {
final List<Expression> callExpression = expressionsToExpressionList(expression.getArrayDimensions());
callExpression.add(new IdentifierImpl("{null}")); // TODO: remove
return new NewClassExpression(
typeToType(expression.getType()),
callExpression
private static Expression createNewEmptyArrayWithoutInitialization(@NotNull PsiNewExpression expression) {
return new ArrayWithoutInitializationExpression(
typeToType(expression.getType(), true),
expressionsToExpressionList(expression.getArrayDimensions())
);
}
@@ -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 -1
View File
@@ -1,4 +1,4 @@
var array : IntArray? = IntArray?(10, {null})
var array : IntArray? = IntArray(10)
for (i in 0..10) {
array[i] = i
}
+1 -1
View File
@@ -1,4 +1,4 @@
var array : IntArray? = IntArray?(10, {null})
var array : IntArray? = IntArray(10)
for (i in 0..(10 - 1)) {
array[i] = i
}
+1 -1
View File
@@ -1,4 +1,4 @@
var array : IntArray? = IntArray?(10, {null})
var array : IntArray? = IntArray(10)
for (i in 0..(10 - 1)) {
array[i] = i
}