diff --git a/migrator/src/com/ohos/migrator/java/JavaTransformer.java b/migrator/src/com/ohos/migrator/java/JavaTransformer.java index b0eb95d5a4462a485122bc9190497df411651e8c..fd8cfaaeecd03133091cda21086911b8c67e9cd4 100644 --- a/migrator/src/com/ohos/migrator/java/JavaTransformer.java +++ b/migrator/src/com/ohos/migrator/java/JavaTransformer.java @@ -1593,6 +1593,53 @@ public class JavaTransformer extends ASTVisitor implements Transformer { return false; } + @Override + public boolean visit(WhileStatement javaWhileStmt) { + IterationStatementContext stsIterStmt = new IterationStatementContext(stsCurrent, 0); + WhileStatementContext stsWhileStmt = new WhileStatementContext(stsIterStmt); + pushStatement(stsIterStmt); + pushCurrent(stsWhileStmt); + + stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.While)); + Expression javaExpr = javaWhileStmt.getExpression(); + assert(javaExpr != null); + javaExpr.accept(this); + + Statement javaLoopBody = javaWhileStmt.getBody(); + assert(javaLoopBody != null); + javaLoopBody.accept(this); + + popCurrent(); + popStatement(); + return false; + } + + + @Override + public boolean visit(DoStatement javaDoStmt) { + IterationStatementContext stsIterStmt = new IterationStatementContext(stsCurrent, 0); + DoStatementContext stsDoStmt = new DoStatementContext(stsIterStmt); + pushStatement(stsIterStmt); + pushCurrent(stsDoStmt); + + stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Do)); + + Statement javaLoopBody = javaDoStmt.getBody(); + assert(javaLoopBody != null); + javaLoopBody.accept(this); + + stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.While)); + + Expression javaExpr = javaDoStmt.getExpression(); + assert(javaExpr != null); + javaExpr.accept(this); + + popCurrent(); + popStatement(); + return false; + } + + // Java tree: // Initializer: // [ static ] Block diff --git a/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java b/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java index 13b5c09ff97a4b80cf61a7c6e6f23c2f8b988148..53d5b95599bfe8ef3a58f4af23ad22bac9b9bef0 100644 --- a/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java +++ b/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java @@ -880,11 +880,13 @@ public class StaticTSWriter extends StaticTSParserBaseVisitor { @Override public Void visitDoStatement(DoStatementContext stsDoStatement) { doNeededIndent(); - sb.append(stsDoStatement.Do().getText()); + sb.append(stsDoStatement.Do().getText() + "\n"); + indentIncrement(); StatementContext stsStmt = stsDoStatement.statement(); assert(stsStmt != null); visitStatement(stsStmt); + indentDecrement(); sb.append(indentCurrent).append(stsDoStatement.While().getText()).append('('); diff --git a/migrator/test/java/do_statement.java b/migrator/test/java/do_statement.java new file mode 100644 index 0000000000000000000000000000000000000000..1e24c4c237262e7c5665abd35aa6501dbd4b7168 --- /dev/null +++ b/migrator/test/java/do_statement.java @@ -0,0 +1,47 @@ +/* + * 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.java; + +class WhileStatements { + public static void Test() { + final boolean Flag = true; + int i = 0; + int j = 0; + + do break; while (Flag); + + do { + break; + } while( Flag ); + + do { + i++; + if (i == 2) continue; + if (i == 5) break; + } while ( i < 10 ); + + outerLoop: + do { + + innerLoop: + do { + if (j == 2) continue innerLoop; + if (i * j == 20) break outerLoop; + } + while ( j < 5 ); + } while ( i < 5 ); + } +} \ No newline at end of file diff --git a/migrator/test/java/do_statement.java.sts b/migrator/test/java/do_statement.java.sts new file mode 100644 index 0000000000000000000000000000000000000000..1bf68b3acdf563b1d211d867c8aa84ba4e2634d2 --- /dev/null +++ b/migrator/test/java/do_statement.java.sts @@ -0,0 +1,49 @@ +/* + * 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.java; + +open class WhileStatements { + public static Test(): void { + const Flag : boolean = true; + let i : int = 0; + let j : int = 0; + do + break; + while(Flag); + do + { + break; + } + while(Flag); + do + { + i++; + if (i == 2) continue; + if (i == 5) break; + } + while(i < 10); + outerLoop: do + { + innerLoop: do + { + if (j == 2) continue innerLoop; + if (i * j == 20) break outerLoop; + } + while(j < 5); + } + while(i < 5); + } +} + diff --git a/migrator/test/java/empty_statement.java.sts b/migrator/test/java/empty_statement.java.sts index efe573a472663eeea307d5929fe083bf7cada565..66c2564925d56ddae084c8237e1cd933f01aa540 100644 --- a/migrator/test/java/empty_statement.java.sts +++ b/migrator/test/java/empty_statement.java.sts @@ -19,6 +19,13 @@ open class empty_statement { public open Test(): void { a = 10; let i : int = 0; + while(i < 2) + { + } + do + { + } + while(i > 3); for (; i < 10; ++i) { } for (let s : int = 1; s < 2; ++s) { @@ -36,3 +43,4 @@ open class empty_statement { public open Run(): void { } } + diff --git a/migrator/test/java/while_statement.java b/migrator/test/java/while_statement.java new file mode 100644 index 0000000000000000000000000000000000000000..a02725b36b3ed18de4b2cae7e29504b920d6d5c2 --- /dev/null +++ b/migrator/test/java/while_statement.java @@ -0,0 +1,46 @@ +/* + * 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.java; + +class WhileStatements { + public static void Test() { + final boolean Flag = true; + int i = 0; + int j = 0; + + while (Flag) break; + + while (Flag) { + break; + } + + while ( i< 10 ) { + i++; + if (i == 2) continue; + if (i == 5) break; + } + + outerLoop: + while (i < 5) { + + innerLoop: + while ( j < 5 ) { + if (j == 2) continue innerLoop; + if (i * j == 20) break outerLoop; + } + } + } +} \ No newline at end of file diff --git a/migrator/test/java/while_statement.java.sts b/migrator/test/java/while_statement.java.sts new file mode 100644 index 0000000000000000000000000000000000000000..1e95cf58657ff06821efbf4475b12d006209cce1 --- /dev/null +++ b/migrator/test/java/while_statement.java.sts @@ -0,0 +1,44 @@ +/* + * 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.java; + +open class WhileStatements { + public static Test(): void { + const Flag : boolean = true; + let i : int = 0; + let j : int = 0; + while(Flag) + break; + while(Flag) + { + break; + } + while(i < 10) + { + i++; + if (i == 2) continue; + if (i == 5) break; + } + outerLoop: while(i < 5) + { + innerLoop: while(j < 5) + { + if (j == 2) continue innerLoop; + if (i * j == 20) break outerLoop; + } + } + } +} +