IfStatement added
This commit is contained in:
@@ -121,6 +121,8 @@ public class Converter {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static Statement statementToStatement(PsiStatement t) {
|
public static Statement statementToStatement(PsiStatement t) {
|
||||||
|
if (t == null)
|
||||||
|
return Statement.EMPTY_STATEMENT;
|
||||||
final StatementVisitor statementVisitor = new StatementVisitor();
|
final StatementVisitor statementVisitor = new StatementVisitor();
|
||||||
t.accept(statementVisitor);
|
t.accept(statementVisitor);
|
||||||
System.out.println(t.getClass());
|
System.out.println(t.getClass());
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class Block extends Statement {
|
public class Block extends Statement {
|
||||||
private List<Statement> myStatements;
|
private List<Statement> myStatements;
|
||||||
private boolean myNotEmpty;
|
private boolean myNotEmpty = false;
|
||||||
|
|
||||||
public Block(List<Statement> statements) {
|
public Block(List<Statement> statements) {
|
||||||
myStatements = statements;
|
myStatements = statements;
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
package org.jetbrains.jet.j2k.ast;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author ignatov
|
|
||||||
*/
|
|
||||||
public class EmptyStatement extends Statement {
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public String toKotlin() {
|
|
||||||
return EMPTY;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package org.jetbrains.jet.j2k.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author ignatov
|
||||||
|
*/
|
||||||
|
public class IfStatement extends Statement {
|
||||||
|
private final Expression myCondition;
|
||||||
|
private final Statement myThenStatement;
|
||||||
|
private final Statement myElseStatement;
|
||||||
|
|
||||||
|
public IfStatement(Expression condition, Statement thenStatement, Statement elseStatement) {
|
||||||
|
myCondition = condition;
|
||||||
|
myThenStatement = thenStatement;
|
||||||
|
myElseStatement = elseStatement;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toKotlin() {
|
||||||
|
String result = "if" + SPACE + "(" + myCondition.toKotlin() + ")" + N + myThenStatement.toKotlin() + N;
|
||||||
|
|
||||||
|
if (myElseStatement != Statement.EMPTY_STATEMENT)
|
||||||
|
return result +
|
||||||
|
"else" + N +
|
||||||
|
myElseStatement.toKotlin();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,21 @@
|
|||||||
package org.jetbrains.jet.j2k.ast;
|
package org.jetbrains.jet.j2k.ast;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author ignatov
|
* @author ignatov
|
||||||
*/
|
*/
|
||||||
public abstract class Statement extends Element {
|
public abstract class Statement extends Element {
|
||||||
|
public static Statement EMPTY_STATEMENT = new EmptyStatement();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author ignatov
|
||||||
|
*/
|
||||||
|
private static class EmptyStatement extends Statement {
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public String toKotlin() {
|
||||||
|
return EMPTY;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import org.jetbrains.jet.j2k.ast.*;
|
|||||||
* @author ignatov
|
* @author ignatov
|
||||||
*/
|
*/
|
||||||
public class StatementVisitor extends ElementVisitor implements Visitor {
|
public class StatementVisitor extends ElementVisitor implements Visitor {
|
||||||
private Statement myResult = new EmptyStatement();
|
private Statement myResult = Statement.EMPTY_STATEMENT;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Statement getStatement() {
|
public Statement getStatement() {
|
||||||
@@ -24,6 +24,10 @@ public class StatementVisitor extends ElementVisitor implements Visitor {
|
|||||||
@Override
|
@Override
|
||||||
public void visitBlockStatement(PsiBlockStatement statement) {
|
public void visitBlockStatement(PsiBlockStatement statement) {
|
||||||
super.visitBlockStatement(statement);
|
super.visitBlockStatement(statement);
|
||||||
|
myResult = new Block(
|
||||||
|
Converter.statementsToStatementList(statement.getCodeBlock().getStatements()),
|
||||||
|
true
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -83,6 +87,11 @@ public class StatementVisitor extends ElementVisitor implements Visitor {
|
|||||||
@Override
|
@Override
|
||||||
public void visitIfStatement(PsiIfStatement statement) {
|
public void visitIfStatement(PsiIfStatement statement) {
|
||||||
super.visitIfStatement(statement);
|
super.visitIfStatement(statement);
|
||||||
|
myResult = new IfStatement(
|
||||||
|
Converter.expressionToExpression(statement.getCondition()),
|
||||||
|
Converter.statementToStatement(statement.getThenBranch()),
|
||||||
|
Converter.statementToStatement(statement.getElseBranch())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -26,11 +26,9 @@ public class EnumTest extends JetTestCaseBase {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
public void testOverrideToString() throws Exception {
|
public void testOverrideToString() throws Exception {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
classToKotlin(
|
classToKotlin(
|
||||||
|
|
||||||
"enum Color {" +
|
"enum Color {" +
|
||||||
" WHITE, BLACK, RED, YELLOW, BLUE;" +
|
" WHITE, BLACK, RED, YELLOW, BLUE;" +
|
||||||
"@Override String toString() {" +
|
"@Override String toString() {" +
|
||||||
@@ -109,4 +107,4 @@ public class EnumTest extends JetTestCaseBase {
|
|||||||
// ""
|
// ""
|
||||||
// );
|
// );
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package org.jetbrains.jet.j2k.ast;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
import org.jetbrains.jet.j2k.JetTestCaseBase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author ignatov
|
||||||
|
*/
|
||||||
|
public class IfStatementTest extends JetTestCaseBase {
|
||||||
|
public void testIfStatementWithOneLineBlocks() throws Exception {
|
||||||
|
Assert.assertEquals(
|
||||||
|
statementToKotlin("if (true) return 1; else return 0;"),
|
||||||
|
"if (true)\n" +
|
||||||
|
"return 1\n" +
|
||||||
|
"else\n" +
|
||||||
|
"return 0"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testIfStatementWithMultilineBlocks() throws Exception {
|
||||||
|
Assert.assertEquals(
|
||||||
|
statementToKotlin("if (1 > 0) {int n = 1; return n;} else {return 0;}"),
|
||||||
|
"if ((1 > 0))\n" +
|
||||||
|
"{\n" +
|
||||||
|
"var n : Int = 1\n" +
|
||||||
|
"return n\n" +
|
||||||
|
"}\n" +
|
||||||
|
"else\n" +
|
||||||
|
"{\n" +
|
||||||
|
"return 0\n" +
|
||||||
|
"}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testIfStatementWithoutElse() throws Exception {
|
||||||
|
Assert.assertEquals(
|
||||||
|
statementToKotlin("if (1 > 0) {int n = 1; return n;}"),
|
||||||
|
"if ((1 > 0))\n" +
|
||||||
|
"{\n" +
|
||||||
|
"var n : Int = 1\n" +
|
||||||
|
"return n\n" +
|
||||||
|
"}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testIfStatementWithEmptyBlocks() throws Exception {
|
||||||
|
Assert.assertEquals(
|
||||||
|
statementToKotlin("if (1 > 0) {} else {}"),
|
||||||
|
"if ((1 > 0))\n" +
|
||||||
|
"{\n" +
|
||||||
|
"}\n" +
|
||||||
|
"else\n" +
|
||||||
|
"{\n" +
|
||||||
|
"}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user