removed accidentally commited files.
This commit is contained in:
Generated
+477
-371
File diff suppressed because it is too large
Load Diff
@@ -327,4 +327,9 @@ public class AstUtil {
|
||||
public static JsExpression equalsTrue(JsExpression expression, JsProgram program) {
|
||||
return equals(expression, program.getTrueLiteral());
|
||||
}
|
||||
|
||||
|
||||
public static JsBinaryOperation sum(JsExpression left, JsExpression right) {
|
||||
return new JsBinaryOperation(JsBinaryOperator.ADD, left, right);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,9 +30,16 @@ public final class StandardClasses {
|
||||
declareIterator(standardClasses, standardLibrary);
|
||||
declareJavaArrayList(standardClasses);
|
||||
declareJavaSystem(standardClasses);
|
||||
declareInteger(standardClasses);
|
||||
return standardClasses;
|
||||
}
|
||||
|
||||
private static void declareInteger(@NotNull StandardClasses standardClasses) {
|
||||
String integerFQName = "<java_root>.java.lang.Integer";
|
||||
standardClasses.declareStandardTopLevelObject(integerFQName, "Integer");
|
||||
declareMethods(standardClasses, integerFQName, "parseInt");
|
||||
}
|
||||
|
||||
private static void declareJavaSystem(@NotNull StandardClasses standardClasses) {
|
||||
String systemFQName = "<java_root>.java.lang.System";
|
||||
standardClasses.declareStandardTopLevelObject(systemFQName, "System");
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.jetbrains.k2js.translate.utils.BindingUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.k2js.translate.utils.BindingUtils.getCompileTimeValue;
|
||||
import static org.jetbrains.k2js.translate.utils.TranslationUtils.notNullCheck;
|
||||
import static org.jetbrains.k2js.translate.utils.TranslationUtils.translateInitializerForProperty;
|
||||
|
||||
@@ -217,21 +218,25 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
|
||||
if (stringLiteral != null) {
|
||||
return stringLiteral;
|
||||
}
|
||||
throw new AssertionError("String templates not supported!");
|
||||
return resolveAsTemplate(expression, context);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JsNode resolveAsTemplate(@NotNull JetStringTemplateExpression expression,
|
||||
@NotNull TranslationContext context) {
|
||||
return StringTemplateTranslator.translate(expression, context);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private JsStringLiteral resolveAsStringConstant(@NotNull JetStringTemplateExpression expression,
|
||||
private JsStringLiteral resolveAsStringConstant(@NotNull JetExpression 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);
|
||||
Object value = getCompileTimeValue(context.bindingContext(), expression);
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
assert value instanceof String : "Compile time constant template should be a String constant.";
|
||||
String constantString = (String) value;
|
||||
return context.program().getStringLiteral(constantString);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package org.jetbrains.k2js.translate.expression;
|
||||
|
||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||
import com.google.dart.compiler.backend.js.ast.JsStringLiteral;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
||||
import org.jetbrains.k2js.translate.general.Translation;
|
||||
|
||||
import static com.google.dart.compiler.util.AstUtil.sum;
|
||||
|
||||
/**
|
||||
* @author Talanov Pavel
|
||||
*/
|
||||
public final class StringTemplateTranslator extends AbstractTranslator {
|
||||
|
||||
@NotNull
|
||||
public static JsExpression translate(@NotNull JetStringTemplateExpression expression,
|
||||
@NotNull TranslationContext context) {
|
||||
return (new StringTemplateTranslator(expression, context).translate());
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
private final JetStringTemplateExpression expression;
|
||||
|
||||
private StringTemplateTranslator(@NotNull JetStringTemplateExpression expression,
|
||||
@NotNull TranslationContext context) {
|
||||
super(context);
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JsExpression translate() {
|
||||
assert expression.getEntries().length != 0 : "String template must have one or more entries.";
|
||||
EntryVisitor entryVisitor = new EntryVisitor();
|
||||
for (JetStringTemplateEntry entry : expression.getEntries()) {
|
||||
entry.accept(entryVisitor);
|
||||
}
|
||||
return entryVisitor.getResultingExpression();
|
||||
}
|
||||
|
||||
private final class EntryVisitor extends JetVisitorVoid {
|
||||
|
||||
@Nullable
|
||||
private JsExpression resultingExpression = null;
|
||||
|
||||
void append(@NotNull JsExpression expression) {
|
||||
if (resultingExpression == null) {
|
||||
resultingExpression = expression;
|
||||
} else {
|
||||
resultingExpression = sum(resultingExpression, expression);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitStringTemplateEntryWithExpression(@NotNull JetStringTemplateEntryWithExpression entry) {
|
||||
JetExpression entryExpression = entry.getExpression();
|
||||
assert entryExpression != null :
|
||||
"JetStringTemplateEntryWithExpression must have not null entry expression.";
|
||||
append(Translation.translateAsExpression(entryExpression, context()));
|
||||
}
|
||||
|
||||
//TODO: duplication
|
||||
@Override
|
||||
public void visitLiteralStringTemplateEntry(@NotNull JetLiteralStringTemplateEntry entry) {
|
||||
JsStringLiteral stringConstant = context().program().getStringLiteral(entry.getText());
|
||||
append(stringConstant);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitEscapeStringTemplateEntry(@NotNull JetEscapeStringTemplateEntry entry) {
|
||||
JsStringLiteral escapedValue = context().program().getStringLiteral(entry.getUnescapedValue());
|
||||
append(escapedValue);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JsExpression getResultingExpression() {
|
||||
assert resultingExpression != null;
|
||||
return resultingExpression;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,9 @@ public class WhenTranslator extends AbstractTranslator {
|
||||
List<JsStatement> entries = translateEntries();
|
||||
resultingFor.setBody(AstUtil.newBlock(entries));
|
||||
return resultingFor;
|
||||
// JsFunction dummyFunction = JsFunction.getAnonymousFunctionWithScope(context().jsScope());
|
||||
// dummyFunction.setBody(AstUtil.convertToBlock(resultingFor));
|
||||
// return AstUtil.newInvocation(dummyFunction);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
|
||||
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
|
||||
import org.jetbrains.jet.lang.types.JetStandardClasses;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
|
||||
@@ -239,4 +240,13 @@ public final class BindingUtils {
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Object getCompileTimeValue(@NotNull BindingContext context, @NotNull JetExpression expression) {
|
||||
CompileTimeConstant<?> compileTimeValue = context.get(BindingContext.COMPILE_TIME_VALUE, expression);
|
||||
if (compileTimeValue != null) {
|
||||
return compileTimeValue.getValue();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,8 +25,8 @@ public class ConditionalTest extends AbstractExpressionTest {
|
||||
}
|
||||
|
||||
//TODO: test fails due to isStatement issue, include when issue is solved or implement another solution
|
||||
// @Test
|
||||
// public void ifElseIf() throws Exception {
|
||||
// testFunctionOutput("elseif.kt", "foo", "box", 5);
|
||||
// }
|
||||
@Test
|
||||
public void ifElseIf() throws Exception {
|
||||
testFunctionOutput("elseif.kt", "foo", "box", 5);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,15 +55,8 @@ public final class PatternMatchingTest extends TranslationTest {
|
||||
testFooBoxIsTrue("matchNullableType.kt");
|
||||
}
|
||||
|
||||
//TODO: tests not actual due to drop of conditionCall feature
|
||||
// @Test
|
||||
// public void whenConditionMethodCall() throws Exception {
|
||||
// testFooBoxIsTrue("whenConditionMethodCall.kt");
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void whenConditionPropertyAccess() throws Exception {
|
||||
// testFooBoxIsTrue("whenConditionPropertyAccess.kt");
|
||||
// }
|
||||
|
||||
@Test
|
||||
public void whenAsExpression() throws Exception {
|
||||
testFooBoxIsTrue("whenAsExpression.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,13 +41,13 @@ public final class RhinoSystemOutputChecker implements RhinoResultChecker {
|
||||
|
||||
@NotNull
|
||||
private String execMain() {
|
||||
String constructArguments = "var args = new Kotlin.Array(" + arguments.size() + ");\n";
|
||||
String constructArguments = "var args = Kotlin.array(" + arguments.size() + ");\n";
|
||||
int index = 0;
|
||||
for (String argument : arguments) {
|
||||
constructArguments = constructArguments + "args[" + index + "] = \"" + argument + "\";\n";
|
||||
constructArguments = constructArguments + "args.set(" + index + ", \"" + argument + "\");\n";
|
||||
index++;
|
||||
}
|
||||
String callMain = "foo.main(args)\n";
|
||||
String callMain = "Anonymous.main(args);\n";
|
||||
return constructArguments + callMain;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,4 +23,19 @@ public class StringTest extends AbstractExpressionTest {
|
||||
public void stringAssignment() throws Exception {
|
||||
testFooBoxIsTrue("stringAssignment.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void intInTemplate() throws Exception {
|
||||
testFunctionOutput("intInTemplate.kt", "foo", "box", "my age is 3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringInTemplate() throws Exception {
|
||||
testFunctionOutput("stringInTemplate.kt", "foo", "box", "oHelloo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleExpressionInTemplate() throws Exception {
|
||||
testFunctionOutput("multipleExpressionsInTemplate.kt", "foo", "box", "left = 3\nright = 2\nsum = 5\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,5 +27,25 @@ public final class SystemTest extends JavaClassesTest {
|
||||
checkOutput("systemPrint.kt", "Hello, world!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void printArg() throws Exception {
|
||||
checkOutput("printArg.kt", "Hello, world!", "Hello, world!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whileLoop() throws Exception {
|
||||
checkOutput("whileLoop.kt", "guest1\nguest2\nguest3\nguest4\n", "guest1", "guest2", "guest3", "guest4");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifAsExpression() throws Exception {
|
||||
checkOutput("ifAsExpression.kt", "20\n", "10", "20");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiLanguageHello() throws Exception {
|
||||
checkOutput("multiLanguageHello.kt", "Salut!\n", "FR");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.jetbrains.k2js.test;
|
||||
|
||||
import org.jetbrains.k2js.K2JSTranslator;
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace foo
|
||||
|
||||
fun box() : String {
|
||||
var number = 3
|
||||
return ("my age is $number");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace foo
|
||||
|
||||
fun box() : String {
|
||||
var right = 2
|
||||
var left = 3
|
||||
return ("left = $left\nright = $right\nsum = ${left + right}\n");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace foo
|
||||
|
||||
fun box() : String {
|
||||
var name = "Hello"
|
||||
return ("o${name}o");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
fun main(args : Array<String>) {
|
||||
System.out?.println(max(Integer.parseInt(args[0]), Integer.parseInt(args[1])))
|
||||
}
|
||||
|
||||
fun max(a : Int, b : Int) = if (a > b) a else b
|
||||
@@ -0,0 +1,9 @@
|
||||
fun main(args : Array<String>) {
|
||||
val language = if (args.size == 0) "EN" else args[0]
|
||||
System.out?.println(when (language) {
|
||||
"EN" => "Hello!"
|
||||
"FR" => "Salut!"
|
||||
"IT" => "Ciao!"
|
||||
else => "Sorry, I can't greet you in $language yet"
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun main(args : Array<String>) {
|
||||
System.out?.print(args[0]);
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
namespace foo
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
System.out?.print("Hello, world!");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
fun main(args : Array<String>) {
|
||||
var i = 0
|
||||
while (i < args.size)
|
||||
System.out?.println(args[i++])
|
||||
}
|
||||
@@ -540,6 +540,16 @@ Kotlin.ArrayIterator = Class.create({
|
||||
}
|
||||
});
|
||||
|
||||
Kotlin.Integer = function () {
|
||||
|
||||
return {
|
||||
parseInt:function (str) {
|
||||
return parseInt(str);
|
||||
}
|
||||
}
|
||||
|
||||
}();
|
||||
|
||||
Kotlin.System = function () {
|
||||
var output = "";
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace foo
|
||||
|
||||
|
||||
fun box() : Boolean {
|
||||
|
||||
return (when(1) {
|
||||
is 2 => 3
|
||||
is 1 => 1
|
||||
else => 5
|
||||
} == 1)
|
||||
|
||||
}
|
||||
@@ -7,13 +7,13 @@
|
||||
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/examples" isTestSource="true" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="IDEA IC-112.121" jdkType="IDEA JDK" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module" module-name="js" />
|
||||
<orderEntry type="module" module-name="frontend" />
|
||||
<orderEntry type="module" module-name="frontend.java" />
|
||||
<orderEntry type="library" name="libs" level="project" />
|
||||
<orderEntry type="module" module-name="backend" />
|
||||
<orderEntry type="library" name="lib" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user