You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2019/05/05 09:50:11 UTC
[plc4x] 02/02: Extended IfStatement.
This is an automated email from the ASF dual-hosted git repository.
jfeinauer pushed a commit to branch feature/code-gen
in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit 77ae145347e6a7fb0c6c0fb2c0842b3fd302d6b6
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Sun May 5 11:49:57 2019 +0200
Extended IfStatement.
---
.../org/apache/plc4x/codegen/ast/IfStatement.java | 27 +++++++++++-----------
.../apache/plc4x/codegen/ast/JavaGenerator.java | 15 ++++++++----
.../apache/plc4x/codegen/ast/PythonGenerator.java | 26 +++++++++++++++++----
.../plc4x/codegen/ast/JavaGeneratorTest.java | 21 +++++++++++++++++
.../plc4x/codegen/ast/PythonGeneratorTest.java | 23 ++++++++++++++++++
5 files changed, 91 insertions(+), 21 deletions(-)
diff --git a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/IfStatement.java b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/IfStatement.java
index 24949d2..373da67 100644
--- a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/IfStatement.java
+++ b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/IfStatement.java
@@ -1,28 +1,29 @@
package org.apache.plc4x.codegen.ast;
+import java.util.Arrays;
+import java.util.List;
+
public class IfStatement extends Statement {
- private Expression condition;
- private Block body;
- private Block orElse;
+ private List<Expression> condition;
+ private List<Block> blocks;
public IfStatement(Expression condition, Block body, Block orElse) {
- // Check condition returns Binary
- this.condition = condition;
- this.body = body;
- this.orElse = orElse;
+ this(Arrays.asList(condition), Arrays.asList(body, orElse));
}
- public Expression getCondition() {
- return condition;
+ public IfStatement(List<Expression> condition, List<Block> blocks) {
+ assert condition.size() == blocks.size() || condition.size() == (blocks.size() -1);
+ this.condition = condition;
+ this.blocks = blocks;
}
- public Block getBody() {
- return body;
+ public List<Expression> getConditions() {
+ return condition;
}
- public Block getOrElse() {
- return orElse;
+ public List<Block> getBlocks() {
+ return blocks;
}
@Override public <T> T accept(NodeVisitor<T> visitor) {
diff --git a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/JavaGenerator.java b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/JavaGenerator.java
index 875fa1c..2672e81 100644
--- a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/JavaGenerator.java
+++ b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/JavaGenerator.java
@@ -38,12 +38,19 @@ public class JavaGenerator implements Generator {
@Override public void generate(IfStatement ifStatement) {
writer.startLine("if (");
- ifStatement.getCondition().write(this);
+ ifStatement.getConditions().get(0).write(this);
writer.write(") {\n");
- writeBlock(ifStatement.getBody());
- if (ifStatement.getOrElse() != null) {
+ writeBlock(ifStatement.getBlocks().get(0));
+ // For each remaining condition to an else if
+ for (int i = 1; i < ifStatement.getConditions().size(); i++) {
+ writer.startLine("} else if (");
+ ifStatement.getConditions().get(i).write(this);
+ writer.write(") {\n");
+ writeBlock(ifStatement.getBlocks().get(i));
+ }
+ if (ifStatement.getBlocks().size() == ifStatement.getConditions().size() + 1) {
writer.writeLine("} else {");
- writeBlock(ifStatement.getOrElse());
+ writeBlock(ifStatement.getBlocks().get(ifStatement.getBlocks().size() - 1));
}
writer.writeLine("}");
}
diff --git a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/PythonGenerator.java b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/PythonGenerator.java
index 898cefb..5faedfb 100644
--- a/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/PythonGenerator.java
+++ b/sandbox/code-gen/src/main/java/org/apache/plc4x/codegen/ast/PythonGenerator.java
@@ -39,13 +39,31 @@ public class PythonGenerator implements Generator {
@Override public void generate(IfStatement ifStatement) {
writer.startLine("if ");
- ifStatement.getCondition().write(this);
+ ifStatement.getConditions().get(0).write(this);
writer.write(":\n");
- writeBlock(ifStatement.getBody());
- if (ifStatement.getOrElse() != null) {
+ writeBlock(ifStatement.getBlocks().get(0));
+ // For each remaining condition to an else if
+ for (int i = 1; i < ifStatement.getConditions().size(); i++) {
+ writer.startLine("elif ");
+ ifStatement.getConditions().get(i).write(this);
+ writer.write(":\n");
+ writeBlock(ifStatement.getBlocks().get(i));
+ }
+ if (ifStatement.getBlocks().size() == ifStatement.getConditions().size() + 1) {
writer.writeLine("else:");
- writeBlock(ifStatement.getOrElse());
+ writeBlock(ifStatement.getBlocks().get(ifStatement.getBlocks().size() - 1));
}
+
+
+
+// writer.startLine("if ");
+// ifStatement.getConditions().get(0).write(this);
+// writer.write(":\n");
+// writeBlock(ifStatement.getBlocks().get(0));
+// if (ifStatement.getBlocks().size() == 2 && ifStatement.getBlocks().get(1) != null) {
+// writer.writeLine("else:");
+// writeBlock(ifStatement.getBlocks().get(1));
+// }
}
@Override public void writeBlock(Block statements) {
diff --git a/sandbox/code-gen/src/test/java/org/apache/plc4x/codegen/ast/JavaGeneratorTest.java b/sandbox/code-gen/src/test/java/org/apache/plc4x/codegen/ast/JavaGeneratorTest.java
index 329b10f..f4b0934 100644
--- a/sandbox/code-gen/src/test/java/org/apache/plc4x/codegen/ast/JavaGeneratorTest.java
+++ b/sandbox/code-gen/src/test/java/org/apache/plc4x/codegen/ast/JavaGeneratorTest.java
@@ -272,4 +272,25 @@ public class JavaGeneratorTest {
" }\n" +
"}\n", code);
}
+
+ @Test
+ public void ifMultipleElse() {
+ final IfStatement stmt = new IfStatement(
+ Arrays.asList(
+ new BinaryExpression(Primitive.DOUBLE, new ParameterExpression(Primitive.DOUBLE, "a"), new ConstantNode(10.0), BinaryExpression.Operation.EQ),
+ new BinaryExpression(Primitive.DOUBLE, new ParameterExpression(Primitive.DOUBLE, "a"), new ConstantNode(5.0), BinaryExpression.Operation.EQ)
+ ),
+ Arrays.asList(
+ new Block(),
+ new Block(),
+ new Block()
+ ));
+
+ stmt.write(generator);
+ final String code = writer.getCode();
+ assertEquals("if (a == 10.0) {\n" +
+ "} else if (a == 5.0) {\n" +
+ "} else {\n" +
+ "}\n", code);
+ }
}
\ No newline at end of file
diff --git a/sandbox/code-gen/src/test/java/org/apache/plc4x/codegen/ast/PythonGeneratorTest.java b/sandbox/code-gen/src/test/java/org/apache/plc4x/codegen/ast/PythonGeneratorTest.java
index f7ed106..f5278fd 100644
--- a/sandbox/code-gen/src/test/java/org/apache/plc4x/codegen/ast/PythonGeneratorTest.java
+++ b/sandbox/code-gen/src/test/java/org/apache/plc4x/codegen/ast/PythonGeneratorTest.java
@@ -55,4 +55,27 @@ public class PythonGeneratorTest {
" pass\n" +
" \n", code);
}
+
+ @Test
+ public void ifMultipleElse() {
+ final IfStatement stmt = new IfStatement(
+ Arrays.asList(
+ new BinaryExpression(Primitive.DOUBLE, new ParameterExpression(Primitive.DOUBLE, "a"), new ConstantNode(10.0), BinaryExpression.Operation.EQ),
+ new BinaryExpression(Primitive.DOUBLE, new ParameterExpression(Primitive.DOUBLE, "a"), new ConstantNode(5.0), BinaryExpression.Operation.EQ)
+ ),
+ Arrays.asList(
+ new Block(),
+ new Block(),
+ new Block()
+ ));
+
+ stmt.write(generator);
+ final String code = writer.getCode();
+ assertEquals("if a == 10.0:\n" +
+ " pass\n" +
+ "elif a == 5.0:\n" +
+ " pass\n" +
+ "else:\n" +
+ " pass\n", code);
+ }
}
\ No newline at end of file