From 858cfefc0efb60683a43758be28939bc15cd1a85 Mon Sep 17 00:00:00 2001 From: Igor Rossinski Date: Tue, 9 Aug 2022 18:19:13 +0300 Subject: [PATCH] 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