diff --git a/migrator/src/com/ohos/migrator/java/JavaTransformer.java b/migrator/src/com/ohos/migrator/java/JavaTransformer.java index b0eb95d5a4462a485122bc9190497df411651e8c..5330fda5282e71cfdfbbaa046423f05a8b32f880 100644 --- a/migrator/src/com/ohos/migrator/java/JavaTransformer.java +++ b/migrator/src/com/ohos/migrator/java/JavaTransformer.java @@ -1593,6 +1593,44 @@ public class JavaTransformer extends ASTVisitor implements Transformer { return false; } + @Override + public boolean visit(WhileStatement javaWhileStmt) { + pushCurrent(new WhileStatementContext(pushIterationStatement())); + + 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); + + popIterationStatement(); // WhileStatementContext + return false; + } + + + @Override + public boolean visit(DoStatement javaDoStmt) { + pushCurrent(new DoStatementContext(pushIterationStatement())); + 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); + + popIterationStatement(); // DoStatementContext + 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..adaadd53a6d651384c9c34a0fadef94de3da7ee9 100644 --- a/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java +++ b/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java @@ -880,7 +880,7 @@ public class StaticTSWriter extends StaticTSParserBaseVisitor { @Override public Void visitDoStatement(DoStatementContext stsDoStatement) { doNeededIndent(); - sb.append(stsDoStatement.Do().getText()); + sb.append(stsDoStatement.Do().getText() + "\n"); StatementContext stsStmt = stsDoStatement.statement(); assert(stsStmt != null); diff --git a/migrator/test/java/do_statement.java b/migrator/test/java/do_statement.java new file mode 100644 index 0000000000000000000000000000000000000000..831fe0021aa6b6d69f50aff9e89e1e538015d24c --- /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 do_statement { + 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 ); + } +} diff --git a/migrator/test/java/do_statement.java.sts b/migrator/test/java/do_statement.java.sts new file mode 100644 index 0000000000000000000000000000000000000000..081747e643564a8d2364d9e587049bd479827f98 --- /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 do_statement { + 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..4423bbee92bc76fdb9977bc3f34bf504f1845a4e 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..3915019cfcdbcaef5d5dd82b0afdaf054e4f4325 --- /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 while_statement { + 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; + } + } + } +} diff --git a/migrator/test/java/while_statement.java.sts b/migrator/test/java/while_statement.java.sts new file mode 100644 index 0000000000000000000000000000000000000000..aa29799b7830116f7a165fdeb97eeff55e5bc3a7 --- /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 while_statement { + 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; + } + } + } +} +