Added support for multiple cases in one when entry.
This commit is contained in:
@@ -3,6 +3,7 @@ package org.jetbrains.k2js.translate;
|
|||||||
import com.google.dart.compiler.backend.js.ast.*;
|
import com.google.dart.compiler.backend.js.ast.*;
|
||||||
import com.google.dart.compiler.util.AstUtil;
|
import com.google.dart.compiler.util.AstUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.psi.*;
|
import org.jetbrains.jet.lang.psi.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -105,13 +106,35 @@ public class WhenTranslator extends AbstractTranslator {
|
|||||||
//TODO: ask what these conditions mean
|
//TODO: ask what these conditions mean
|
||||||
@NotNull
|
@NotNull
|
||||||
private JsExpression translateConditions(@NotNull JetWhenEntry entry) {
|
private JsExpression translateConditions(@NotNull JetWhenEntry entry) {
|
||||||
JetWhenCondition[] conditions = entry.getConditions();
|
List<JsExpression> conditions = new ArrayList<JsExpression>();
|
||||||
// for (JetWhenCondition condition : conditions) {
|
for (JetWhenCondition condition : entry.getConditions()) {
|
||||||
// translateCondition(condition);
|
conditions.add(translateCondition(condition));
|
||||||
// }
|
}
|
||||||
return translateCondition(conditions[0]);
|
return anyOfThemIsTrue(conditions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private JsExpression anyOfThemIsTrue(List<JsExpression> conditions) {
|
||||||
|
assert !conditions.isEmpty() : "When entry (not else) should have at least one condition";
|
||||||
|
JsExpression current = null;
|
||||||
|
for (JsExpression condition : conditions) {
|
||||||
|
current = addCaseCondition(current, condition);
|
||||||
|
}
|
||||||
|
assert current != null;
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private JsExpression addCaseCondition(@Nullable JsExpression current, @NotNull JsExpression condition) {
|
||||||
|
if (current == null) {
|
||||||
|
current = condition;
|
||||||
|
} else {
|
||||||
|
current = new JsBinaryOperation(JsBinaryOperator.OR, current, condition);
|
||||||
|
}
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private JsExpression translateCondition(@NotNull JetWhenCondition condition) {
|
private JsExpression translateCondition(@NotNull JetWhenCondition condition) {
|
||||||
if (condition instanceof JetWhenConditionIsPattern) {
|
if (condition instanceof JetWhenConditionIsPattern) {
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ public final class PatternMatchingTest extends IncludeLibraryTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void multipleCases() throws Exception {
|
public void multipleCases() throws Exception {
|
||||||
testFooBoxIsTrue("multipleCases.kt");
|
testFunctionOutput("multipleCases.kt", "foo", "box", 2.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
namespace foo
|
namespace foo
|
||||||
|
|
||||||
fun box() : Boolean {
|
fun box() : Int {
|
||||||
val c = 3
|
val c = 3
|
||||||
val d = 5
|
val d = 5
|
||||||
var z = 0
|
var z = 0
|
||||||
@@ -13,5 +13,5 @@ fun box() : Boolean {
|
|||||||
is 5, is 3 => z++;
|
is 5, is 3 => z++;
|
||||||
else => z = -1000;
|
else => z = -1000;
|
||||||
}
|
}
|
||||||
return c == 2
|
return z
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user