diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d960230bdc0..eb6634b14b8 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,10 +6,8 @@ - - - - + + @@ -37,7 +35,7 @@ - + @@ -852,57 +860,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -917,15 +874,6 @@ - - - - - - - - - @@ -933,13 +881,6 @@ - - - - - - - @@ -947,6 +888,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -954,9 +930,46 @@ - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/translator/src/org/jetbrains/k2js/translate/ExpressionVisitor.java b/translator/src/org/jetbrains/k2js/translate/ExpressionVisitor.java index 3be265225f8..77608dbbd0f 100644 --- a/translator/src/org/jetbrains/k2js/translate/ExpressionVisitor.java +++ b/translator/src/org/jetbrains/k2js/translate/ExpressionVisitor.java @@ -28,7 +28,8 @@ public final class ExpressionVisitor extends TranslatorVisitor { public JsNode visitConstantExpression(@NotNull JetConstantExpression expression, @NotNull TranslationContext context) { JsExpression result = null; Object value; - CompileTimeConstant compileTimeValue = context.bindingContext().get(BindingContext.COMPILE_TIME_VALUE, expression); + CompileTimeConstant compileTimeValue = + context.bindingContext().get(BindingContext.COMPILE_TIME_VALUE, expression); assert compileTimeValue != null; value = compileTimeValue.getValue(); if (value instanceof Integer) { @@ -194,7 +195,7 @@ public final class ExpressionVisitor extends TranslatorVisitor { } @NotNull - private JsStatement translateNullableExpressionToNotNullStatement(@NotNull JetExpression expression, + private JsStatement translateNullableExpressionToNotNullStatement(@Nullable JetExpression expression, @NotNull TranslationContext context) { return AstUtil.convertToStatement(translateNullableExpression(expression, context)); } @@ -235,6 +236,30 @@ public final class ExpressionVisitor extends TranslatorVisitor { return result; } + @Override + @NotNull + public JsNode visitStringTemplateExpression(@NotNull JetStringTemplateExpression expression, + @NotNull TranslationContext context) { + JsStringLiteral stringLiteral = resolveAsStringConstant(expression, context); + if (stringLiteral != null) { + return stringLiteral; + } + throw new AssertionError("String templates not supported!"); + } + + @Nullable + private JsStringLiteral resolveAsStringConstant(@NotNull JetStringTemplateExpression expression, + @NotNull TranslationContext context) { + CompileTimeConstant compileTimeValue = + context.bindingContext().get(BindingContext.COMPILE_TIME_VALUE, expression); + if (compileTimeValue != null) { + Object value = compileTimeValue.getValue(); + assert value instanceof String : "Compile time constant template should be a String constant"; + String constantString = (String)value; + return context.program().getStringLiteral(constantString); + } + return null; + } } diff --git a/translator/test/org/jetbrains/k2js/test/FunctionTest.java b/translator/test/org/jetbrains/k2js/test/FunctionTest.java index 8904119e983..9655c098ab4 100644 --- a/translator/test/org/jetbrains/k2js/test/FunctionTest.java +++ b/translator/test/org/jetbrains/k2js/test/FunctionTest.java @@ -4,6 +4,9 @@ import org.junit.Test; /** * @author Talanov Pavel + * + * This class tests basic language features and constructs such as constants, local variables, simple loops, + * conditional clauses etc. */ public final class FunctionTest extends TranslationTest { @@ -57,6 +60,13 @@ public final class FunctionTest extends TranslationTest { testFooBoxIsTrue("while2.kt"); } + @Test + public void stringConstant() throws Exception { + testFooBoxIsTrue("stringConstant.kt"); + } - + @Test + public void stringAssignment() throws Exception { + testFooBoxIsTrue("stringAssignment.kt"); + } } diff --git a/translator/test_files/test_cases/stringAssignment.kt b/translator/test_files/test_cases/stringAssignment.kt new file mode 100644 index 00000000000..3bbdcd3cabb --- /dev/null +++ b/translator/test_files/test_cases/stringAssignment.kt @@ -0,0 +1,10 @@ +namespace foo + +fun box() : Boolean { + + val a = "bar"; + var b = "foo"; + b = a; + return (b == "bar"); +} + diff --git a/translator/test_files/test_cases/stringConstant.kt b/translator/test_files/test_cases/stringConstant.kt new file mode 100644 index 00000000000..f9faa1c1bfb --- /dev/null +++ b/translator/test_files/test_cases/stringConstant.kt @@ -0,0 +1,8 @@ +namespace foo + +fun box() : Boolean { + + val a = "String"; + return true; +} +