From 5e52788e2902c272ee0e404be8b1ccd93d2ad8b2 Mon Sep 17 00:00:00 2001 From: Evgeniy Okolnov Date: Mon, 17 Oct 2022 17:37:38 +0300 Subject: [PATCH 1/2] Kotlin. Local variable declaration, constant literals. Change-Id: Ibf3967d0f2e0cce7430b5e557421b91c74b47941 Signed-off-by: Evgeniy Okolnov --- .../migrator/kotlin/KotlinTransformer.java | 104 ++++++++++++++++-- .../com/ohos/migrator/kotlin/NodeBuilder.java | 6 + migrator/test/kotlin/imports.kt.sts | 5 +- migrator/test/kotlin/literals.kt | 59 ++++++++++ migrator/test/kotlin/literals.kt.sts | 48 ++++++++ migrator/test/kotlin/var_declaration.kt | 27 +++++ migrator/test/kotlin/var_declaration.kt.sts | 26 +++++ 7 files changed, 266 insertions(+), 9 deletions(-) create mode 100644 migrator/test/kotlin/literals.kt create mode 100644 migrator/test/kotlin/literals.kt.sts create mode 100644 migrator/test/kotlin/var_declaration.kt create mode 100644 migrator/test/kotlin/var_declaration.kt.sts diff --git a/migrator/src/com/ohos/migrator/kotlin/KotlinTransformer.java b/migrator/src/com/ohos/migrator/kotlin/KotlinTransformer.java index 05400fc2b..e52398ae5 100644 --- a/migrator/src/com/ohos/migrator/kotlin/KotlinTransformer.java +++ b/migrator/src/com/ohos/migrator/kotlin/KotlinTransformer.java @@ -15,6 +15,7 @@ package com.ohos.migrator.kotlin; +import com.intellij.psi.tree.IElementType; import com.ohos.migrator.Main; import com.ohos.migrator.ResultCode; import com.ohos.migrator.Transformer; @@ -26,6 +27,7 @@ import org.antlr.v4.runtime.tree.TerminalNode; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; +import org.jetbrains.kotlin.KtNodeTypes; import org.jetbrains.kotlin.analyzer.AnalysisResult; import org.jetbrains.kotlin.descriptors.ClassifierDescriptor; import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; @@ -303,9 +305,7 @@ public class KotlinTransformer extends KtVisitor implem translateFunction(ktNamedFunction, stsMethod); - ClassMemberContext stsClassMember = new ClassMemberContext(null, 0); - AccessibilityModifierContext stsAccessMod = NodeBuilder.accessibilityModifier(ktNamedFunction); - if (stsAccessMod != null) stsClassMember.addChild(stsAccessMod).setParent(stsClassMember); + ClassMemberContext stsClassMember = NodeBuilder.classMember(ktNamedFunction); stsClassMember.addChild(stsClassMethod).setParent(stsClassMember); return stsClassMember; @@ -422,21 +422,109 @@ public class KotlinTransformer extends KtVisitor implem BlockContext stsBlock = new BlockContext(null, 0); for (KtExpression ktExpr : ktBlockExpression.getStatements()) { - // TODO: translate statement expressions. + ParserRuleContext stsResult = ktExpr.accept(this, data); + + StatementOrLocalDeclarationContext stsStmtOrLocalDecl = new StatementOrLocalDeclarationContext(stsBlock, 0); + stsStmtOrLocalDecl.addChild(stsResult).setParent(stsStmtOrLocalDecl); + stsBlock.addChild(stsStmtOrLocalDecl); } return stsBlock; } @Override - public ParserRuleContext visitExpression(KtExpression ktBlockExpression, Void data) { + public ParserRuleContext visitProperty(KtProperty ktProperty, Void data) { + if (ktProperty.isTopLevel()) { + // TODO: Translate top-level property. + } + else if (ktProperty.isMember()) { + // TODO: Translate class member property. + } + else { + return translateLocalVariableDeclaration(ktProperty); + } + + return null; + } + + private ParserRuleContext translateLocalVariableDeclaration(KtProperty ktProperty) { + VariableOrConstantDeclarationContext stsVarOrConstDecl = new VariableOrConstantDeclarationContext(null, 0); + + // Note: Kotlin allows read-only variables declared without initializer. + // STS doesn't allow const variables without initializer, thus translate + // such variables without const modifier. + KtExpression ktInitExpr = ktProperty.getInitializer(); + boolean isConst = !ktProperty.isVar() && ktInitExpr != null; + ParserRuleContext stsDeclList; + if (isConst) { + stsVarOrConstDecl.addChild(NodeBuilder.terminalNode(StaticTSParser.Const)); + stsDeclList = new ConstantDeclarationListContext(stsVarOrConstDecl, 0); + } else { + stsVarOrConstDecl.addChild(NodeBuilder.terminalNode(StaticTSParser.Let)); + stsDeclList = new VariableDeclarationListContext(stsVarOrConstDecl, 0); + } + stsVarOrConstDecl.addChild(stsDeclList); + + ParserRuleContext stsDecl; + if (isConst) { + stsDecl = new ConstantDeclarationContext(stsVarOrConstDecl, 0); + } else { + stsDecl = new VariableDeclarationContext(stsVarOrConstDecl, 0); + } + stsDeclList.addChild(stsDecl); + + stsDecl.addChild(NodeBuilder.terminalIdentifier(ktProperty.getName())); + + KtTypeReference ktTypeRef = ktProperty.getTypeReference(); + if (ktTypeRef != null) { + TypeReferenceContext stsTypeRef = (TypeReferenceContext) ktTypeRef.accept(this, null); + stsDecl.addChild(NodeBuilder.typeAnnotation(stsTypeRef)).setParent(stsDecl); + } + + if (ktInitExpr != null) { + InitializerContext stsInit = new InitializerContext(stsDecl, 0); + stsInit.addChild(ktInitExpr.accept(this, null)).setParent(stsInit); + stsDecl.addChild(stsInit); + } + + return stsVarOrConstDecl; + } + + @Override + public SingleExpressionContext visitConstantExpression(KtConstantExpression ktConstantExpression, Void data) { + SingleExpressionContext stsConstExpr; + + String ktConstText = ktConstantExpression.getText(); + IElementType ktElementType = ktConstantExpression.getNode().getElementType(); + if (ktElementType == KtNodeTypes.BOOLEAN_CONSTANT) { + stsConstExpr = NodeBuilder.boolLiteral(Boolean.parseBoolean(ktConstText)); + } + else if (ktElementType == KtNodeTypes.INTEGER_CONSTANT || ktElementType == KtNodeTypes.FLOAT_CONSTANT) { + stsConstExpr = NodeBuilder.numericLiteral(ktConstText); + } + else if (ktElementType == KtNodeTypes.CHARACTER_CONSTANT) { + stsConstExpr = NodeBuilder.charLiteral(ktConstText); + } + else if (ktElementType == KtNodeTypes.NULL) { + stsConstExpr = NodeBuilder.nullLiteral(); + } + else { + reportError("Unknown constant expression kind", ktConstantExpression); + stsConstExpr = NodeBuilder.untranslatedExpression(ktConstantExpression); + } + + return stsConstExpr; + } + + @Override + public ParserRuleContext visitExpression(KtExpression ktExpression, Void data) { // For the moment, return untranslated expression/statement in order // to avoid possible NullPointerExceptions while developing the translator. // TODO: Remove this code, once all expressions are supported. - if (KtPsiUtil.isStatement(ktBlockExpression)) - return NodeBuilder.untranslatedStatement(ktBlockExpression); + if (KtPsiUtil.isStatement(ktExpression)) + return NodeBuilder.untranslatedStatement(ktExpression); else - return NodeBuilder.untranslatedExpression(ktBlockExpression); + return NodeBuilder.untranslatedExpression(ktExpression); } } \ No newline at end of file diff --git a/migrator/src/com/ohos/migrator/kotlin/NodeBuilder.java b/migrator/src/com/ohos/migrator/kotlin/NodeBuilder.java index 0848a300c..a0365b8fc 100644 --- a/migrator/src/com/ohos/migrator/kotlin/NodeBuilder.java +++ b/migrator/src/com/ohos/migrator/kotlin/NodeBuilder.java @@ -53,4 +53,10 @@ public class NodeBuilder extends NodeBuilderBase { return stsStatement; } + public static ClassMemberContext classMember(KtModifierListOwner ktModifierListOwner) { + ClassMemberContext stsClassMember = new ClassMemberContext(null, 0); + AccessibilityModifierContext stsAccessMod = NodeBuilder.accessibilityModifier(ktModifierListOwner); + if (stsAccessMod != null) stsClassMember.addChild(stsAccessMod).setParent(stsClassMember); + return stsClassMember; + } } diff --git a/migrator/test/kotlin/imports.kt.sts b/migrator/test/kotlin/imports.kt.sts index 2a78c4379..5a75e8f44 100644 --- a/migrator/test/kotlin/imports.kt.sts +++ b/migrator/test/kotlin/imports.kt.sts @@ -17,6 +17,9 @@ package com.ohos.migrator.test.kotlin; import kotlin.random.Random; import kotlin.math.*; -import kotlin.reflect.KClass as Class; +import kotlin.reflect.KClass as Class; export function main(): kotlin.Unit { + __untranslated_statement(/* println(PI) */); + let rnd : Random = __untranslated_expression(/* Random(5) */); + let kk : Class ; } \ No newline at end of file diff --git a/migrator/test/kotlin/literals.kt b/migrator/test/kotlin/literals.kt new file mode 100644 index 000000000..f1ed4e6a2 --- /dev/null +++ b/migrator/test/kotlin/literals.kt @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ohos.migrator.test.kotlin; + +public fun main() { + // Integer + val integer = 50; + val integer2 = 1_2_3_4; + val longVal = 100L; + val longVal2 = 50_60_70L; + val hex = 0x5A; + val hex2 = 0X1_B_C; + val binary = 0b10; + val binary2 = 0B01_01_101; + + // Floating-point + val floatVal = 1f; + val floatVal2 = 2_3.4_5F; + val floatVal3 = 10e2_0f; + val floatVal4 = 45E+3F; + + val doubleVal = 1.25; + val doubleVal2 = 2.5E-10; + val doubleVal3 = .45_67; + val doubleVal4 = 3e+5; + + // Boolean + val trueVal = true; + val falseVal = false; + + // Character + val letter = 'a'; + val digit = '5'; + val cr = '\r'; + val lf = '\n'; + val tab = '\t'; + val backspace = '\b'; + val quote = '\''; + val doubleQuote = '\"'; + val backslash = '\b'; + val dollar = '\$'; + val unicode = '\u01FA' + + // Null + val nullVal = null +} \ No newline at end of file diff --git a/migrator/test/kotlin/literals.kt.sts b/migrator/test/kotlin/literals.kt.sts new file mode 100644 index 000000000..c80481de3 --- /dev/null +++ b/migrator/test/kotlin/literals.kt.sts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.ohos.migrator.test.kotlin; + +export function main(): kotlin.Unit { + const integer = 50; + const integer2 = 1_2_3_4; + const longVal = 100; + const longVal2 = 50_60_70; + const hex = 0x5A; + const hex2 = 0X1_B_C; + const binary = 0b10; + const binary2 = 0B01_01_101; + const floatVal = 1; + const floatVal2 = 2_3.4_5; + const floatVal3 = 10e2_0; + const floatVal4 = 45E+3; + const doubleVal = 1.25; + const doubleVal2 = 2.5E-10; + const doubleVal3 = .45_67; + const doubleVal4 = 3e+5; + const trueVal = true; + const falseVal = false; + const letter = 'a'; + const digit = '5'; + const cr = '\r'; + const lf = '\n'; + const tab = '\t'; + const backspace = '\b'; + const quote = '\''; + const doubleQuote = '\"'; + const backslash = '\b'; + const dollar = '\$'; + const unicode = '\u01FA'; + const nullVal = null; +} diff --git a/migrator/test/kotlin/var_declaration.kt b/migrator/test/kotlin/var_declaration.kt new file mode 100644 index 000000000..5ec38b1cd --- /dev/null +++ b/migrator/test/kotlin/var_declaration.kt @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2022-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ohos.migrator.test.kotlin; + +public fun main() { + var a : Int; + var b : Double = 5.6; + var c = null; + + val d : Char = 'e'; + val e = 100 + val f : Int; + f = 5; +} \ No newline at end of file diff --git a/migrator/test/kotlin/var_declaration.kt.sts b/migrator/test/kotlin/var_declaration.kt.sts new file mode 100644 index 000000000..6e1f4e45e --- /dev/null +++ b/migrator/test/kotlin/var_declaration.kt.sts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2022-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ohos.migrator.test.kotlin; + +export function main(): kotlin.Unit { + let a : Int ; + let b : Double = 5.6; + let c = null; + const d : Char = 'e'; + const e = 100; + let f : Int ; + __untranslated_statement(/* f = 5 */); +} -- Gitee From 2960365357aa690a1c5f3b0d62828685a1b96857 Mon Sep 17 00:00:00 2001 From: Evgeniy Okolnov Date: Fri, 21 Oct 2022 15:29:48 +0300 Subject: [PATCH 2/2] Kotlin. Translation of assignments, string literals. Change-Id: Ic479f00188cf1e4e631456d797d58b1dbede7ede Signed-off-by: Evgeniy Okolnov --- .../migrator/kotlin/KotlinTransformer.java | 76 +++++++++++++++++-- .../com/ohos/migrator/kotlin/NodeBuilder.java | 24 ++++++ .../migrator/staticTS/NodeBuilderBase.java | 9 +++ migrator/test/kotlin/assignments.kt | 26 +++++++ migrator/test/kotlin/assignments.kt.sts | 26 +++++++ migrator/test/kotlin/strings.kt | 26 +++++++ migrator/test/kotlin/strings.kt.sts | 22 ++++++ migrator/test/kotlin/var_declaration.kt.sts | 2 +- 8 files changed, 205 insertions(+), 6 deletions(-) create mode 100644 migrator/test/kotlin/assignments.kt create mode 100644 migrator/test/kotlin/assignments.kt.sts create mode 100644 migrator/test/kotlin/strings.kt create mode 100644 migrator/test/kotlin/strings.kt.sts diff --git a/migrator/src/com/ohos/migrator/kotlin/KotlinTransformer.java b/migrator/src/com/ohos/migrator/kotlin/KotlinTransformer.java index e52398ae5..43caf01ba 100644 --- a/migrator/src/com/ohos/migrator/kotlin/KotlinTransformer.java +++ b/migrator/src/com/ohos/migrator/kotlin/KotlinTransformer.java @@ -15,7 +15,6 @@ package com.ohos.migrator.kotlin; -import com.intellij.psi.tree.IElementType; import com.ohos.migrator.Main; import com.ohos.migrator.ResultCode; import com.ohos.migrator.Transformer; @@ -23,16 +22,18 @@ import com.ohos.migrator.staticTS.parser.StaticTSParser; import com.ohos.migrator.staticTS.parser.StaticTSParser.*; import org.antlr.v4.runtime.ParserRuleContext; -import org.antlr.v4.runtime.tree.TerminalNode; import com.intellij.openapi.util.TextRange; +import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiElement; +import com.intellij.psi.tree.IElementType; import org.jetbrains.kotlin.KtNodeTypes; import org.jetbrains.kotlin.analyzer.AnalysisResult; import org.jetbrains.kotlin.descriptors.ClassifierDescriptor; import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor; import org.jetbrains.kotlin.diagnostics.PsiDiagnosticUtils; +import org.jetbrains.kotlin.lexer.KtSingleValueToken; import org.jetbrains.kotlin.lexer.KtTokens; import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.psi.*; @@ -422,10 +423,16 @@ public class KotlinTransformer extends KtVisitor implem BlockContext stsBlock = new BlockContext(null, 0); for (KtExpression ktExpr : ktBlockExpression.getStatements()) { - ParserRuleContext stsResult = ktExpr.accept(this, data); + ParserRuleContext stsStmtResult = ktExpr.accept(this, data); + + // If an expression is used as a statement, wrap it up + // with a statement context. + if (stsStmtResult.getRuleIndex() == StaticTSParser.RULE_singleExpression) { + stsStmtResult = NodeBuilder.expressionStatement((SingleExpressionContext) stsStmtResult); + } StatementOrLocalDeclarationContext stsStmtOrLocalDecl = new StatementOrLocalDeclarationContext(stsBlock, 0); - stsStmtOrLocalDecl.addChild(stsResult).setParent(stsStmtOrLocalDecl); + stsStmtOrLocalDecl.addChild(stsStmtResult).setParent(stsStmtOrLocalDecl); stsBlock.addChild(stsStmtOrLocalDecl); } @@ -444,7 +451,7 @@ public class KotlinTransformer extends KtVisitor implem return translateLocalVariableDeclaration(ktProperty); } - return null; + return super.visitProperty(ktProperty, data); } private ParserRuleContext translateLocalVariableDeclaration(KtProperty ktProperty) { @@ -516,6 +523,65 @@ public class KotlinTransformer extends KtVisitor implem return stsConstExpr; } + public ParserRuleContext visitBinaryExpression(KtBinaryExpression ktBinaryExpr, Void data) { + KtOperationReferenceExpression ktOpRef = ktBinaryExpr.getOperationReference(); + KtSingleValueToken ktOpToken = ktOpRef.getOperationSignTokenType(); + + if (KtPsiUtil.isAssignment(ktBinaryExpr)) { + SingleExpressionContext stsExpr = new SingleExpressionContext(null, 0); + + ParserRuleContext stsAssignExpr; + if(ktOpToken == KtTokens.EQ) { + stsAssignExpr = new AssignmentExpressionContext(stsExpr); + } else { + stsAssignExpr = new AssignmentOperatorExpressionContext(stsExpr); + } + stsExpr.addChild(stsAssignExpr).setParent(stsExpr); + + ParserRuleContext stsLeftExpr = ktBinaryExpr.getLeft().accept(this, data); + stsAssignExpr.addChild(stsLeftExpr).setParent(stsAssignExpr); + + if(ktOpToken == KtTokens.EQ) { + stsAssignExpr.addChild(NodeBuilder.terminalNode(StaticTSParser.Assign)); + } else { + AssignmentOperatorContext stsAssignOp = NodeBuilder.assignmentOperator(ktOpToken); + stsAssignExpr.addChild(stsAssignOp).setParent(stsAssignExpr); + } + + ParserRuleContext stsRightExpr = ktBinaryExpr.getRight().accept(this, data); + stsAssignExpr.addChild(stsRightExpr).setParent(stsAssignExpr); + + return stsExpr; + } else { + // TODO: Translate binary expressions. + + return super.visitBinaryExpression(ktBinaryExpr, data); + } + } + + @Override + public ParserRuleContext visitSimpleNameExpression(KtSimpleNameExpression ktSimpleName, Void data) { + return NodeBuilder.identifierExpression(ktSimpleName.getReferencedName()); + } + + @Override + public ParserRuleContext visitStringTemplateExpression(KtStringTemplateExpression ktStringTemplateExpr, Void data) { + if (ktStringTemplateExpr.hasInterpolation()) { + // TODO: Translate string interpolation. + return super.visitStringTemplateExpression(ktStringTemplateExpr, data); + } else { + // Collect all string template entries into one string literal. + StringBuilder literal = new StringBuilder(); + for (KtStringTemplateEntry ktTemplateEntry : ktStringTemplateExpr.getEntries()) { + literal.append(ktTemplateEntry.getText()); + } + + // Escape line-terminating characters that might appear from the multi-line string. + literal = StringUtil.escapeStringCharacters(literal.length(), literal.toString(), null, false, false, new StringBuilder()); + return NodeBuilder.stringLiteral('\"' + literal.toString() + '\"'); + } + } + @Override public ParserRuleContext visitExpression(KtExpression ktExpression, Void data) { // For the moment, return untranslated expression/statement in order diff --git a/migrator/src/com/ohos/migrator/kotlin/NodeBuilder.java b/migrator/src/com/ohos/migrator/kotlin/NodeBuilder.java index a0365b8fc..c37d03f17 100644 --- a/migrator/src/com/ohos/migrator/kotlin/NodeBuilder.java +++ b/migrator/src/com/ohos/migrator/kotlin/NodeBuilder.java @@ -18,6 +18,9 @@ package com.ohos.migrator.kotlin; import com.ohos.migrator.staticTS.NodeBuilderBase; import com.ohos.migrator.staticTS.parser.StaticTSParser; import com.ohos.migrator.staticTS.parser.StaticTSParser.*; +import org.eclipse.jdt.core.dom.Assignment; +import org.jetbrains.kotlin.lexer.KtSingleValueToken; +import org.jetbrains.kotlin.lexer.KtTokens; import org.jetbrains.kotlin.psi.KtElement; import org.jetbrains.kotlin.psi.KtModifierListOwner; import org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt; @@ -59,4 +62,25 @@ public class NodeBuilder extends NodeBuilderBase { if (stsAccessMod != null) stsClassMember.addChild(stsAccessMod).setParent(stsClassMember); return stsClassMember; } + + public static AssignmentOperatorContext assignmentOperator(KtSingleValueToken ktAssignOpToken) { + int stsOperatorCode = -1; + + if (ktAssignOpToken == KtTokens.PLUSEQ) + stsOperatorCode = StaticTSParser.PlusAssign; + else if (ktAssignOpToken == KtTokens.MINUSEQ) + stsOperatorCode = StaticTSParser.MinusAssign; + else if (ktAssignOpToken == KtTokens.MULTEQ) + stsOperatorCode = StaticTSParser.MultiplyAssign; + else if (ktAssignOpToken == KtTokens.DIVEQ) + stsOperatorCode = StaticTSParser.DivideAssign; + else if (ktAssignOpToken == KtTokens.PERCEQ) + stsOperatorCode = StaticTSParser.ModulusAssign; + + if (stsOperatorCode == -1) return null; + + AssignmentOperatorContext stsAssignOp = new AssignmentOperatorContext(null, 0); + stsAssignOp.addChild(terminalNode(stsOperatorCode)); + return stsAssignOp; + } } diff --git a/migrator/src/com/ohos/migrator/staticTS/NodeBuilderBase.java b/migrator/src/com/ohos/migrator/staticTS/NodeBuilderBase.java index 86f4f9495..bb5336a1e 100644 --- a/migrator/src/com/ohos/migrator/staticTS/NodeBuilderBase.java +++ b/migrator/src/com/ohos/migrator/staticTS/NodeBuilderBase.java @@ -394,4 +394,13 @@ public class NodeBuilderBase { stsType.getRuleIndex() == StaticTSParser.RULE_typeReference || stsType.getRuleIndex() == StaticTSParser.RULE_wildcardType; } + + public static StatementContext expressionStatement(SingleExpressionContext stsSingleExpr) { + ExpressionStatementContext stsExprStmt = new ExpressionStatementContext(null, 0); + stsExprStmt.addChild(stsSingleExpr).setParent(stsExprStmt); + StatementContext stsStmt = new StatementContext(null, 0); + stsStmt.addChild(stsExprStmt).setParent(stsStmt); + + return stsStmt; + } } diff --git a/migrator/test/kotlin/assignments.kt b/migrator/test/kotlin/assignments.kt new file mode 100644 index 000000000..e6e1ca939 --- /dev/null +++ b/migrator/test/kotlin/assignments.kt @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2022-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ohos.migrator.test.kotlin; + +public fun main() { + var a : Int; + a = 0; + a += 1; + a -= 2; + a *= 3; + a /= 4; + a %= 5; +} \ No newline at end of file diff --git a/migrator/test/kotlin/assignments.kt.sts b/migrator/test/kotlin/assignments.kt.sts new file mode 100644 index 000000000..52507ffc6 --- /dev/null +++ b/migrator/test/kotlin/assignments.kt.sts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2022-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ohos.migrator.test.kotlin; + +export function main(): kotlin.Unit { + let a : Int ; + a = 0; + a += 1; + a -= 2; + a *= 3; + a /= 4; + a %= 5; +} diff --git a/migrator/test/kotlin/strings.kt b/migrator/test/kotlin/strings.kt new file mode 100644 index 000000000..d26cee818 --- /dev/null +++ b/migrator/test/kotlin/strings.kt @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2022-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ohos.migrator.test.kotlin; + +fun main() { + val empty = "" + val escapedChars = "\"Quoted text\"; C:\\Program Files\\Program; \r\n New line"; + val multiline = """ + One + Two + Three + """ +} \ No newline at end of file diff --git a/migrator/test/kotlin/strings.kt.sts b/migrator/test/kotlin/strings.kt.sts new file mode 100644 index 000000000..57c31c5b6 --- /dev/null +++ b/migrator/test/kotlin/strings.kt.sts @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2022-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ohos.migrator.test.kotlin; + +export function main(): kotlin.Unit { + const empty = ""; + const escapedChars = "\"Quoted text\"; C:\\Program Files\\Program; \r\n New line"; + const multiline = "\n One\n Two\n Three\n "; +} diff --git a/migrator/test/kotlin/var_declaration.kt.sts b/migrator/test/kotlin/var_declaration.kt.sts index 6e1f4e45e..d240f1d5d 100644 --- a/migrator/test/kotlin/var_declaration.kt.sts +++ b/migrator/test/kotlin/var_declaration.kt.sts @@ -22,5 +22,5 @@ export function main(): kotlin.Unit { const d : Char = 'e'; const e = 100; let f : Int ; - __untranslated_statement(/* f = 5 */); + f = 5; } -- Gitee