diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 3bd7ed243b0..237b96ffdaa 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,13 +6,13 @@ - - - - - + + - + + + + @@ -37,7 +37,7 @@ - + - + - + - + + + + + + + + + + @@ -180,34 +189,14 @@ - - - - - - - - - - - - - - - - - - - - - + @@ -215,55 +204,87 @@ - - + + - - - - - - - - - - - - + - - + + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - - - - - - - - - @@ -280,15 +301,9 @@ @@ -356,6 +377,24 @@ - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + - - - - - + + + + + localhost @@ -761,9 +794,9 @@ - + - + @@ -801,7 +834,7 @@ @@ -826,124 +860,122 @@ - + - + - + - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - - - - - - - - - - + - + - - - - - - - - - - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/js/src/com/google/dart/compiler/backend/js/ast/JsScope.java b/js/src/com/google/dart/compiler/backend/js/ast/JsScope.java index b7b6e2fc8b2..0a4053960b8 100644 --- a/js/src/com/google/dart/compiler/backend/js/ast/JsScope.java +++ b/js/src/com/google/dart/compiler/backend/js/ast/JsScope.java @@ -297,4 +297,12 @@ public class JsScope implements Serializable { public JsName findExistingNameNoRecurse(String ident) { return names.get(ident); } + + /** + * + * Checks whether this scope owns this name. + */ + public boolean ownsName(JsName name) { + return names.containsValue(name); + } } diff --git a/translator/src/org/jetbrains/k2js/translate/ExpressionVisitor.java b/translator/src/org/jetbrains/k2js/translate/ExpressionVisitor.java index 0420f222221..a8932aec233 100644 --- a/translator/src/org/jetbrains/k2js/translate/ExpressionVisitor.java +++ b/translator/src/org/jetbrains/k2js/translate/ExpressionVisitor.java @@ -94,25 +94,19 @@ public final class ExpressionVisitor extends TranslatorVisitor { return new JsBinaryOperation(OperationTranslator.getBinaryOperator(jetOperationToken), left, right); } - //TODO think about recursive/non-recursive name look-up //TODO correct look-up logic @Override @NotNull public JsNode visitSimpleNameExpression(JetSimpleNameExpression expression, TranslationContext context) { String referencedName = expression.getReferencedName(); - JsName jsName = context.enclosingScope().findExistingNameNoRecurse(referencedName); - if (jsName != null) { - return jsName.makeRef(); + JsName jsName = context.enclosingScope().findExistingName(referencedName); + if (jsName == null) { + throw new AssertionError("Unindentified name " + expression.getReferencedName()); } - jsName = context.classScope().findExistingNameNoRecurse(referencedName); - if (jsName != null) { + if (context.namespaceScope().ownsName(jsName)) { return context.getNamespaceQualifiedReference(jsName); } - jsName = context.namespaceScope().findExistingNameNoRecurse(referencedName); - if (jsName != null) { - return context.getNamespaceQualifiedReference(jsName); - } - throw new AssertionError("Unindentified name " + expression.getReferencedName()); + return jsName.makeRef(); } @Override @@ -170,6 +164,7 @@ public final class ExpressionVisitor extends TranslatorVisitor { return jsArguments; } + @NotNull private JsExpression translateArgument(@NotNull TranslationContext context, @NotNull ValueArgument argument) { JetExpression jetExpression = argument.getArgumentExpression(); if (jetExpression == null) { @@ -178,4 +173,48 @@ public final class ExpressionVisitor extends TranslatorVisitor { return AstUtil.convertToExpression(jetExpression.accept(this, context)); } + @Override + @NotNull + public JsNode visitIfExpression(@NotNull JetIfExpression expression, @NotNull TranslationContext context) { + JsIf result = new JsIf(); + result.setIfExpr(translateIfExpression(expression, context)); + result.setThenStmt(translateThenStatement(expression, context)); + result.setElseStmt(translateElseStatement(expression, context)); + return result; + } + + @NotNull + private JsExpression translateIfExpression(@NotNull JetIfExpression expression, + @NotNull TranslationContext context) { + JsNode jsCondition = translateNullableExpression(expression.getCondition(), context); + if (jsCondition == context.program().getEmptyStmt()) { + throw new AssertionError("Empty condition in if clause!"); + } + return AstUtil.convertToExpression(jsCondition); + } + + @NotNull + private JsStatement translateThenStatement(@NotNull JetIfExpression expression, + @NotNull TranslationContext context) { + return AstUtil.convertToStatement(translateNullableExpression(expression.getThen(), context)); + } + + @Nullable + private JsStatement translateElseStatement(@NotNull JetIfExpression expression, + @NotNull TranslationContext context) { + if (expression.getElse() == null) { + return null; + } + return AstUtil.convertToStatement(translateNullableExpression(expression.getElse(), context)); + } + + @NotNull + private JsNode translateNullableExpression(@Nullable JetExpression expression, + @NotNull TranslationContext context) { + if (expression == null) { + return context.program().getEmptyStmt(); + } + return expression.accept(this, context); + } + } diff --git a/translator/test/org/jetbrains/k2js/test/FunctionTest.java b/translator/test/org/jetbrains/k2js/test/FunctionTest.java index e32171bdf33..96c697eeb22 100644 --- a/translator/test/org/jetbrains/k2js/test/FunctionTest.java +++ b/translator/test/org/jetbrains/k2js/test/FunctionTest.java @@ -27,4 +27,14 @@ public final class FunctionTest extends TranslationTest { testFooBoxIsTrue("comparison.kt"); } + @Test + public void ifElseTest() throws Exception { + performTest("if.kt", "foo", "box", 5); + } + + @Test + public void ifElseIfTest() throws Exception { + performTest("elseif.kt", "foo", "box", 5); + } + } diff --git a/translator/test/org/jetbrains/k2js/test/TranslationTest.java b/translator/test/org/jetbrains/k2js/test/TranslationTest.java index ae4ca0d61b4..26fba591a5c 100644 --- a/translator/test/org/jetbrains/k2js/test/TranslationTest.java +++ b/translator/test/org/jetbrains/k2js/test/TranslationTest.java @@ -38,7 +38,7 @@ public class TranslationTest { Scriptable scope = cx.initStandardObjects(); cx.evaluateReader(scope, reader, "test case", 1, null); Object result = extractAndCallFunctionObject(namespaceName, functionName, cx, scope); - assertTrue(result.equals(expectedResult)); + assertTrue("Result is not what expected!", result.equals(expectedResult)); String report = namespaceName + "." + functionName + "() = " + Context.toString(result); System.out.println(report); diff --git a/translator/test_files/test_cases/elseif.kt b/translator/test_files/test_cases/elseif.kt new file mode 100644 index 00000000000..d8a99a9f07a --- /dev/null +++ b/translator/test_files/test_cases/elseif.kt @@ -0,0 +1,16 @@ +namespace foo + +fun box() : Int { + val a = 2; + val b = 3; + var c = 4; + if (a < 2) { + return a; + } else if (a > 2) { + return b; + } else if (a == c) { + return c; + } else { + return 5; + } +} diff --git a/translator/test_files/test_cases/if.kt b/translator/test_files/test_cases/if.kt new file mode 100644 index 00000000000..ca634df5730 --- /dev/null +++ b/translator/test_files/test_cases/if.kt @@ -0,0 +1,19 @@ +namespace foo + +fun box() : Int { + val a = 2; + val b = 3; + var c = 4; + if (a < 2) { + return a; + } + if (a > 2) { + return b; + } + if (a == c) { + return c; + } else { + return 5; + } +} +