PolyadicExpression supported
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
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 PolyadicExpression extends Expression {
|
||||
private List<Expression> myExpressions;
|
||||
private String myToken;
|
||||
|
||||
public PolyadicExpression(List<Expression> expressions, String token) {
|
||||
super();
|
||||
myExpressions = expressions;
|
||||
myToken = token;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String toKotlin() {
|
||||
return "(" + AstUtil.joinNodes(myExpressions, SPACE + myToken + SPACE) + ")";
|
||||
}
|
||||
}
|
||||
@@ -85,15 +85,31 @@ public class ExpressionVisitor extends StatementVisitor implements Visitor {
|
||||
}
|
||||
}
|
||||
|
||||
private String getOperatorString(PsiJavaToken op) {
|
||||
if (op.getTokenType() == JavaTokenType.GTGT) return "shr";
|
||||
if (op.getTokenType() == JavaTokenType.LTLT) return "shl";
|
||||
if (op.getTokenType() == JavaTokenType.XOR) return "xor";
|
||||
if (op.getTokenType() == JavaTokenType.AND) return "and";
|
||||
if (op.getTokenType() == JavaTokenType.OR) return "or";
|
||||
if (op.getTokenType() == JavaTokenType.OR) return "or";
|
||||
@NotNull
|
||||
private String getOperatorString(@NotNull IElementType tokenType) {
|
||||
if (tokenType == JavaTokenType.PLUS) return "+";
|
||||
if (tokenType == JavaTokenType.MINUS) return "-";
|
||||
if (tokenType == JavaTokenType.ASTERISK) return "*";
|
||||
if (tokenType == JavaTokenType.DIV) return "/";
|
||||
if (tokenType == JavaTokenType.PERC) return "%";
|
||||
if (tokenType == JavaTokenType.GTGT) return "shr";
|
||||
if (tokenType == JavaTokenType.LTLT) return "shl";
|
||||
if (tokenType == JavaTokenType.XOR) return "xor";
|
||||
if (tokenType == JavaTokenType.AND) return "and";
|
||||
if (tokenType == JavaTokenType.OR) return "or";
|
||||
if (tokenType == JavaTokenType.OR) return "or";
|
||||
if (tokenType == JavaTokenType.GT) return ">";
|
||||
if (tokenType == JavaTokenType.LT) return "<";
|
||||
if (tokenType == JavaTokenType.GE) return ">=";
|
||||
if (tokenType == JavaTokenType.LE) return "<=";
|
||||
if (tokenType == JavaTokenType.NE) return "!=";
|
||||
if (tokenType == JavaTokenType.ANDAND) return "&&";
|
||||
if (tokenType == JavaTokenType.OROR) return "||";
|
||||
if (tokenType == JavaTokenType.PLUSPLUS) return "++";
|
||||
if (tokenType == JavaTokenType.MINUSMINUS) return "--";
|
||||
|
||||
return op.getText();
|
||||
System.out.println("UNSUPPORTED TOKEN TYPE: " + tokenType.toString());
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -110,7 +126,7 @@ public class ExpressionVisitor extends StatementVisitor implements Visitor {
|
||||
new BinaryExpression(
|
||||
expressionToExpression(expression.getLOperand()),
|
||||
expressionToExpression(expression.getROperand()),
|
||||
getOperatorString(expression.getOperationSign())
|
||||
getOperatorString(expression.getOperationSign().getTokenType())
|
||||
);
|
||||
}
|
||||
|
||||
@@ -200,27 +216,22 @@ public class ExpressionVisitor extends StatementVisitor implements Visitor {
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String getPlusOrMinus(@NotNull PsiJavaToken token) {
|
||||
if (token.getTokenType() == JavaTokenType.PLUSPLUS) return "++";
|
||||
if (token.getTokenType() == JavaTokenType.MINUSMINUS) return "--";
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPostfixExpression(PsiPostfixExpression expression) {
|
||||
super.visitPostfixExpression(expression);
|
||||
myResult = new PostfixOperator(
|
||||
getPlusOrMinus(expression.getOperationSign()),
|
||||
expressionToExpression(expression.getOperand()));
|
||||
getOperatorString(expression.getOperationSign().getTokenType()),
|
||||
expressionToExpression(expression.getOperand())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPrefixExpression(PsiPrefixExpression expression) {
|
||||
super.visitPrefixExpression(expression);
|
||||
myResult = new PrefixOperator(
|
||||
getPlusOrMinus(expression.getOperationSign()),
|
||||
expressionToExpression(expression.getOperand()));
|
||||
getOperatorString(expression.getOperationSign().getTokenType()),
|
||||
expressionToExpression(expression.getOperand())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -255,5 +266,13 @@ public class ExpressionVisitor extends StatementVisitor implements Visitor {
|
||||
@Override
|
||||
public void visitPolyadicExpression(PsiPolyadicExpression expression) {
|
||||
super.visitPolyadicExpression(expression);
|
||||
if (expression.getOperationTokenType() != JavaTokenType.GTGTGT)
|
||||
myResult = new PolyadicExpression(
|
||||
expressionsToExpressionList(expression.getOperands()),
|
||||
getOperatorString(expression.getOperationTokenType())
|
||||
);
|
||||
else {
|
||||
// TODO: support GTGTGT
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ public class EnumTest extends JetTestCaseBase {
|
||||
);
|
||||
}
|
||||
|
||||
public void testFields() throws Exception {
|
||||
public void testFieldsWithPrimaryPrivateConstructor() throws Exception {
|
||||
Assert.assertEquals(
|
||||
classToKotlin(
|
||||
"enum Color {\n" +
|
||||
@@ -56,20 +56,23 @@ public class EnumTest extends JetTestCaseBase {
|
||||
"\n" +
|
||||
" private int code;\n" +
|
||||
"\n" +
|
||||
" private Color(int c) {\n" + // TODO: private constructor, WTF?
|
||||
" private Color(int c) {\n" +
|
||||
" code = c;\n" +
|
||||
" }\n" +
|
||||
"\n" +
|
||||
" public int getCode() {\n" +
|
||||
" return code;\n" +
|
||||
" }"),
|
||||
"enum Color(c : Int) {\n" +
|
||||
"enum Color {\n" +
|
||||
"WHITE(21)\n" +
|
||||
"BLACK(22)\n" +
|
||||
"RED(23)\n" +
|
||||
"YELLOW(24)\n" +
|
||||
"BLUE(25)\n" +
|
||||
"private var code : Int\n" +
|
||||
"private (c : Int) {\n" +
|
||||
"code = c\n" +
|
||||
"}\n" +
|
||||
"public fun getCode() : Int {\n" +
|
||||
"return code\n" +
|
||||
"}\n" +
|
||||
@@ -85,11 +88,11 @@ public class EnumTest extends JetTestCaseBase {
|
||||
}
|
||||
|
||||
public void testEnumWithNameField() throws Exception {
|
||||
Assert.assertEquals(
|
||||
classToKotlin("enum E { I; private String name; }"),
|
||||
"enum E {\nI\nprivate var name : String?\n}"
|
||||
);
|
||||
}
|
||||
Assert.assertEquals(
|
||||
classToKotlin("enum E { I; private String name; }"),
|
||||
"enum E {\nI\nprivate var name : String?\n}"
|
||||
);
|
||||
}
|
||||
|
||||
public void testEnumImplementsSeveralInterfaces() throws Exception {
|
||||
Assert.assertEquals(
|
||||
@@ -129,19 +132,28 @@ public class EnumTest extends JetTestCaseBase {
|
||||
// "" // TODO: will fail
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// public void testInterfaceImplementation() throws Exception {
|
||||
// Assert.assertEquals(
|
||||
// classToKotlin(
|
||||
// "enum Color implements Runnable {\n" +
|
||||
// " WHITE, BLACK, RED, YELLOW, BLUE;\n" +
|
||||
// "\n" +
|
||||
// " public void run() {\n" +
|
||||
// " System.out.println(\"name()=\" + name() +\n" +
|
||||
// " \", toString()=\" + toString());\n" +
|
||||
// " }\n" +
|
||||
// "}"),
|
||||
// ""
|
||||
// );
|
||||
// }
|
||||
|
||||
public void testRunnableImplementation() throws Exception {
|
||||
Assert.assertEquals(
|
||||
classToKotlin(
|
||||
"enum Color implements Runnable {\n" +
|
||||
" WHITE, BLACK, RED, YELLOW, BLUE;\n" +
|
||||
"\n" +
|
||||
" public void run() {\n" +
|
||||
" System.out.println(\"name()=\" + name() +\n" +
|
||||
" \", toString()=\" + toString());\n" +
|
||||
" }\n" +
|
||||
"}"),
|
||||
"enum Color : Runnable {\n" +
|
||||
"WHITE\n" +
|
||||
"BLACK\n" +
|
||||
"RED\n" +
|
||||
"YELLOW\n" +
|
||||
"BLUE\n" +
|
||||
"override public fun run() : Unit {\n" +
|
||||
"System.out.println((\"name()=\" + name() + \", toString()=\" + toString()))\n" +
|
||||
"}\n" +
|
||||
"}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.jetbrains.jet.j2k.ast;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.jetbrains.jet.j2k.JetTestCaseBase;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class PolyadicExpressionTest extends JetTestCaseBase {
|
||||
public void testMultiply() throws Exception {
|
||||
Assert.assertEquals(expressionToKotlin("1 * 2 * 3"), "(1 * 2 * 3)");
|
||||
}
|
||||
|
||||
public void testDivide() throws Exception {
|
||||
Assert.assertEquals(expressionToKotlin("1 / 2 / 3"), "(1 / 2 / 3)");
|
||||
}
|
||||
|
||||
public void testRemainder() throws Exception {
|
||||
Assert.assertEquals(expressionToKotlin("1 % 2 % 3"), "(1 % 2 % 3)");
|
||||
}
|
||||
|
||||
public void testPlus() throws Exception {
|
||||
Assert.assertEquals(expressionToKotlin("1 + 2 + 3"), "(1 + 2 + 3)");
|
||||
}
|
||||
|
||||
public void testMinus() throws Exception {
|
||||
Assert.assertEquals(expressionToKotlin("1 - 2 - 3"), "(1 - 2 - 3)");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user