From 858cfefc0efb60683a43758be28939bc15cd1a85 Mon Sep 17 00:00:00 2001 From: Igor Rossinski Date: Tue, 9 Aug 2022 18:19:13 +0300 Subject: [PATCH 1/2] Translation of 'While' and 'Do-While' statements Signed-off-by: Igor Rossinski --- .../ohos/migrator/java/JavaTransformer.java | 47 ++++++++++++++++++ .../staticTS/writer/StaticTSWriter.java | 4 +- migrator/test/java/do_statement.java | 47 ++++++++++++++++++ migrator/test/java/do_statement.java.sts | 49 +++++++++++++++++++ migrator/test/java/empty_statement.java.sts | 8 +++ migrator/test/java/while_statement.java | 46 +++++++++++++++++ migrator/test/java/while_statement.java.sts | 44 +++++++++++++++++ 7 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 migrator/test/java/do_statement.java create mode 100644 migrator/test/java/do_statement.java.sts create mode 100644 migrator/test/java/while_statement.java create mode 100644 migrator/test/java/while_statement.java.sts diff --git a/migrator/src/com/ohos/migrator/java/JavaTransformer.java b/migrator/src/com/ohos/migrator/java/JavaTransformer.java index b0eb95d5a..fd8cfaaee 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 13b5c09ff..53d5b9559 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 000000000..1e24c4c23 --- /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 000000000..1bf68b3ac --- /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 efe573a47..66c256492 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 000000000..a02725b36 --- /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 000000000..1e95cf586 --- /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; + } + } + } +} + -- Gitee From 7414601c40e4b07dcfa81e1e155b91f726ecce49 Mon Sep 17 00:00:00 2001 From: Igor Rossinski Date: Thu, 11 Aug 2022 11:57:58 +0300 Subject: [PATCH 2/2] some improvement of 'while' and 'do' statements translation Signed-off-by: Igor Rossinski --- .../ohos/migrator/java/JavaTransformer.java | 17 +++------- .../staticTS/writer/StaticTSWriter.java | 2 -- migrator/test/java/do_statement.java | 4 +-- migrator/test/java/do_statement.java.sts | 32 +++++++++---------- migrator/test/java/empty_statement.java.sts | 4 +-- migrator/test/java/while_statement.java | 4 +-- migrator/test/java/while_statement.java.sts | 2 +- 7 files changed, 27 insertions(+), 38 deletions(-) diff --git a/migrator/src/com/ohos/migrator/java/JavaTransformer.java b/migrator/src/com/ohos/migrator/java/JavaTransformer.java index fd8cfaaee..5330fda52 100644 --- a/migrator/src/com/ohos/migrator/java/JavaTransformer.java +++ b/migrator/src/com/ohos/migrator/java/JavaTransformer.java @@ -1595,10 +1595,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { @Override public boolean visit(WhileStatement javaWhileStmt) { - IterationStatementContext stsIterStmt = new IterationStatementContext(stsCurrent, 0); - WhileStatementContext stsWhileStmt = new WhileStatementContext(stsIterStmt); - pushStatement(stsIterStmt); - pushCurrent(stsWhileStmt); + pushCurrent(new WhileStatementContext(pushIterationStatement())); stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.While)); Expression javaExpr = javaWhileStmt.getExpression(); @@ -1609,19 +1606,14 @@ public class JavaTransformer extends ASTVisitor implements Transformer { assert(javaLoopBody != null); javaLoopBody.accept(this); - popCurrent(); - popStatement(); + popIterationStatement(); // WhileStatementContext return false; } @Override public boolean visit(DoStatement javaDoStmt) { - IterationStatementContext stsIterStmt = new IterationStatementContext(stsCurrent, 0); - DoStatementContext stsDoStmt = new DoStatementContext(stsIterStmt); - pushStatement(stsIterStmt); - pushCurrent(stsDoStmt); - + pushCurrent(new DoStatementContext(pushIterationStatement())); stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Do)); Statement javaLoopBody = javaDoStmt.getBody(); @@ -1634,8 +1626,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { assert(javaExpr != null); javaExpr.accept(this); - popCurrent(); - popStatement(); + popIterationStatement(); // DoStatementContext return false; } diff --git a/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java b/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java index 53d5b9559..adaadd53a 100644 --- a/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java +++ b/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java @@ -882,11 +882,9 @@ public class StaticTSWriter extends StaticTSParserBaseVisitor { doNeededIndent(); 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 index 1e24c4c23..831fe0021 100644 --- a/migrator/test/java/do_statement.java +++ b/migrator/test/java/do_statement.java @@ -15,7 +15,7 @@ package com.ohos.migrator.test.java; -class WhileStatements { +class do_statement { public static void Test() { final boolean Flag = true; int i = 0; @@ -44,4 +44,4 @@ class WhileStatements { 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 index 1bf68b3ac..081747e64 100644 --- a/migrator/test/java/do_statement.java.sts +++ b/migrator/test/java/do_statement.java.sts @@ -14,35 +14,35 @@ */ package com.ohos.migrator.test.java; -open class WhileStatements { +open class do_statement { public static Test(): void { const Flag : boolean = true; let i : int = 0; let j : int = 0; do - break; + break; while(Flag); do - { - break; - } + { + break; + } while(Flag); do - { - i++; - if (i == 2) continue; - if (i == 5) break; - } + { + i++; + if (i == 2) continue; + if (i == 5) break; + } while(i < 10); outerLoop: do + { + innerLoop: do { - innerLoop: do - { - if (j == 2) continue innerLoop; - if (i * j == 20) break outerLoop; - } - while(j < 5); + 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 66c256492..4423bbee9 100644 --- a/migrator/test/java/empty_statement.java.sts +++ b/migrator/test/java/empty_statement.java.sts @@ -23,8 +23,8 @@ open class empty_statement { { } do - { - } + { + } while(i > 3); for (; i < 10; ++i) { } diff --git a/migrator/test/java/while_statement.java b/migrator/test/java/while_statement.java index a02725b36..3915019cf 100644 --- a/migrator/test/java/while_statement.java +++ b/migrator/test/java/while_statement.java @@ -15,7 +15,7 @@ package com.ohos.migrator.test.java; -class WhileStatements { +class while_statement { public static void Test() { final boolean Flag = true; int i = 0; @@ -43,4 +43,4 @@ class WhileStatements { } } } -} \ No newline at end of file +} diff --git a/migrator/test/java/while_statement.java.sts b/migrator/test/java/while_statement.java.sts index 1e95cf586..aa29799b7 100644 --- a/migrator/test/java/while_statement.java.sts +++ b/migrator/test/java/while_statement.java.sts @@ -14,7 +14,7 @@ */ package com.ohos.migrator.test.java; -open class WhileStatements { +open class while_statement { public static Test(): void { const Flag : boolean = true; let i : int = 0; -- Gitee