Repository: incubator-systemml Updated Branches: refs/heads/master 936dcdd42 -> 01184e3f0
[SYSTEMML-296] Add elif (else if) to PyDML Add the elif case to parser and validator. Add tests of if/elif/else statement in PyDML. Closes #151. Project: http://git-wip-us.apache.org/repos/asf/incubator-systemml/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-systemml/commit/01184e3f Tree: http://git-wip-us.apache.org/repos/asf/incubator-systemml/tree/01184e3f Diff: http://git-wip-us.apache.org/repos/asf/incubator-systemml/diff/01184e3f Branch: refs/heads/master Commit: 01184e3f0a559aebae051bb66f502c234591e818 Parents: 936dcdd Author: Tatsuya.Nishiyama <[email protected]> Authored: Tue May 17 11:44:38 2016 -0700 Committer: Deron Eriksson <[email protected]> Committed: Tue May 17 11:44:38 2016 -0700 ---------------------------------------------------------------------- .../java/org/apache/sysml/parser/pydml/Pydml.g4 | 10 +- .../parser/pydml/PydmlSyntacticValidator.java | 37 ++++++- .../test/integration/functions/misc/IfTest.java | 107 +++++++++++++++++++ src/test/scripts/functions/misc/IfTest.pydml | 26 +++++ src/test/scripts/functions/misc/IfTest2.pydml | 28 +++++ src/test/scripts/functions/misc/IfTest3.pydml | 28 +++++ src/test/scripts/functions/misc/IfTest4.pydml | 30 ++++++ src/test/scripts/functions/misc/IfTest5.pydml | 30 ++++++ src/test/scripts/functions/misc/IfTest6.pydml | 32 ++++++ .../functions/misc/ZPackageSuite.java | 1 + 10 files changed, 326 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/01184e3f/src/main/java/org/apache/sysml/parser/pydml/Pydml.g4 ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/parser/pydml/Pydml.g4 b/src/main/java/org/apache/sysml/parser/pydml/Pydml.g4 index 5fb4951..47280ed 100644 --- a/src/main/java/org/apache/sysml/parser/pydml/Pydml.g4 +++ b/src/main/java/org/apache/sysml/parser/pydml/Pydml.g4 @@ -178,7 +178,7 @@ statement returns [ org.apache.sysml.parser.common.StatementInfo info ] | targetList=dataIdentifier '=' source=expression NEWLINE # AssignmentStatement // IfStatement // | 'if' OPEN_PAREN predicate=expression CLOSE_PAREN (ifBody+=statement ';'* | NEWLINE INDENT (ifBody+=statement)+ DEDENT ) ('else' (elseBody+=statement ';'* | '{' (elseBody+=statement ';'*)* '}'))? # IfStatement - | 'if' (OPEN_PAREN predicate=expression CLOSE_PAREN | predicate=expression) ':' NEWLINE INDENT (ifBody+=statement)+ DEDENT ('else' ':' NEWLINE INDENT (elseBody+=statement)+ DEDENT )? # IfStatement + | 'if' (OPEN_PAREN predicate=expression CLOSE_PAREN | predicate=expression) ':' NEWLINE INDENT (ifBody+=statement)+ DEDENT (elifBranches += elifBranch)* ('else' ':' NEWLINE INDENT (elseBody+=statement)+ DEDENT )? # IfStatement // ------------------------------------------ // ForStatement & ParForStatement | 'for' (OPEN_PAREN iterVar=ID 'in' iterPred=iterablePredicate (',' parForParams+=strictParameterizedExpression)* CLOSE_PAREN | iterVar=ID 'in' iterPred=iterablePredicate (',' parForParams+=strictParameterizedExpression)* ) ':' NEWLINE INDENT (body+=statement)+ DEDENT # ForStatement @@ -189,6 +189,14 @@ statement returns [ org.apache.sysml.parser.common.StatementInfo info ] | NEWLINE #IgnoreNewLine ; +elifBranch returns [ org.apache.sysml.parser.common.StatementInfo info ] + @init { + // This actions occurs regardless of how many alternatives in this rule + $info = new org.apache.sysml.parser.common.StatementInfo(); + } : + 'elif' (OPEN_PAREN predicate=expression CLOSE_PAREN | predicate=expression) ':' NEWLINE INDENT (elifBody+=statement)+ DEDENT +; + iterablePredicate returns [ org.apache.sysml.parser.common.ExpressionInfo info ] @init { // This actions occurs regardless of how many alternatives in this rule http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/01184e3f/src/main/java/org/apache/sysml/parser/pydml/PydmlSyntacticValidator.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/parser/pydml/PydmlSyntacticValidator.java b/src/main/java/org/apache/sysml/parser/pydml/PydmlSyntacticValidator.java index 6bd9985..b4803ee 100644 --- a/src/main/java/org/apache/sysml/parser/pydml/PydmlSyntacticValidator.java +++ b/src/main/java/org/apache/sysml/parser/pydml/PydmlSyntacticValidator.java @@ -91,6 +91,7 @@ import org.apache.sysml.parser.pydml.PydmlParser.FunctionCallAssignmentStatement import org.apache.sysml.parser.pydml.PydmlParser.FunctionCallMultiAssignmentStatementContext; import org.apache.sysml.parser.pydml.PydmlParser.FunctionStatementContext; import org.apache.sysml.parser.pydml.PydmlParser.IfStatementContext; +import org.apache.sysml.parser.pydml.PydmlParser.ElifBranchContext; import org.apache.sysml.parser.pydml.PydmlParser.IfdefAssignmentStatementContext; import org.apache.sysml.parser.pydml.PydmlParser.IgnoreNewLineContext; import org.apache.sysml.parser.pydml.PydmlParser.ImportStatementContext; @@ -1230,11 +1231,20 @@ public class PydmlSyntacticValidator extends CommonSyntacticValidator implements ifStmt.mergeStatementBlocksIfBody(); } + IfStatement tailIfStmt = ifStmt; + + if(ctx.elifBranches.size() > 0) { + for(ElifBranchContext elifCtx : ctx.elifBranches) { + tailIfStmt.addStatementBlockElseBody(getStatementBlock(elifCtx.info.stmt)); + tailIfStmt = (IfStatement) elifCtx.info.stmt; + } + } + if(ctx.elseBody.size() > 0) { for(StatementContext stmtCtx : ctx.elseBody) { - ifStmt.addStatementBlockElseBody(getStatementBlock(stmtCtx.info.stmt)); + tailIfStmt.addStatementBlockElseBody(getStatementBlock(stmtCtx.info.stmt)); } - ifStmt.mergeStatementBlocksElseBody(); + tailIfStmt.mergeStatementBlocksElseBody(); } ctx.info.stmt = ifStmt; @@ -1242,6 +1252,27 @@ public class PydmlSyntacticValidator extends CommonSyntacticValidator implements } @Override + public void exitElifBranch(ElifBranchContext ctx) { + IfStatement elifStmt = new IfStatement(); + ConditionalPredicate predicate = new ConditionalPredicate(ctx.predicate.info.expr); + elifStmt.setConditionalPredicate(predicate); + String fileName = currentFile; + int line = ctx.start.getLine(); + int col = ctx.start.getCharPositionInLine(); + elifStmt.setAllPositions(fileName, line, col, line, col); + + if(ctx.elifBody.size() > 0) { + for (StatementContext stmtCtx : ctx.elifBody) { + elifStmt.addStatementBlockIfBody(getStatementBlock(stmtCtx.info.stmt)); + } + elifStmt.mergeStatementBlocksIfBody(); + } + + ctx.info.stmt = elifStmt; + setFileLineColumn(ctx.info.stmt, ctx); + } + + @Override public void exitWhileStatement(WhileStatementContext ctx) { WhileStatement whileStmt = new WhileStatement(); ConditionalPredicate predicate = new ConditionalPredicate(ctx.predicate.info.expr); @@ -1652,6 +1683,8 @@ public class PydmlSyntacticValidator extends CommonSyntacticValidator implements @Override public void enterIfStatement(IfStatementContext ctx) {} + @Override public void enterElifBranch(ElifBranchContext ctx) {} + @Override public void enterIgnoreNewLine(IgnoreNewLineContext ctx) {} @Override public void enterConstDoubleIdExpression(ConstDoubleIdExpressionContext ctx) {} http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/01184e3f/src/test/java/org/apache/sysml/test/integration/functions/misc/IfTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/sysml/test/integration/functions/misc/IfTest.java b/src/test/java/org/apache/sysml/test/integration/functions/misc/IfTest.java new file mode 100644 index 0000000..82635ae --- /dev/null +++ b/src/test/java/org/apache/sysml/test/integration/functions/misc/IfTest.java @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.sysml.test.integration.functions.misc; + + +import org.apache.sysml.test.integration.AutomatedTestBase; +import org.apache.sysml.test.integration.TestConfiguration; +import org.junit.Test; + +public class IfTest extends AutomatedTestBase +{ + + private final static String TEST_DIR = "functions/misc/"; + private final static String TEST_NAME1 = "IfTest"; + private final static String TEST_NAME2 = "IfTest2"; + private final static String TEST_NAME3 = "IfTest3"; + private final static String TEST_NAME4 = "IfTest4"; + private final static String TEST_NAME5 = "IfTest5"; + private final static String TEST_NAME6 = "IfTest6"; + private final static String TEST_CLASS_DIR = TEST_DIR + IfTest.class.getSimpleName() + "/"; + + @Override + public void setUp() + { + addTestConfiguration(TEST_NAME1, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME1, new String[] {})); + addTestConfiguration(TEST_NAME2, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME2, new String[] {})); + addTestConfiguration(TEST_NAME3, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME3, new String[] {})); + addTestConfiguration(TEST_NAME4, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME4, new String[] {})); + addTestConfiguration(TEST_NAME5, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME5, new String[] {})); + addTestConfiguration(TEST_NAME6, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME6, new String[] {})); + } + + @Test + public void testIf() { runTest(TEST_NAME1, 1); } + + @Test + public void testIfElse() { + runTest(TEST_NAME2, 1); + runTest(TEST_NAME2, 2); + } + + @Test + public void testIfElif() { + runTest(TEST_NAME3, 1); + runTest(TEST_NAME3, 2); + } + + @Test + public void testIfElifElse() { + runTest(TEST_NAME4, 1); + runTest(TEST_NAME4, 2); + runTest(TEST_NAME4, 3); + } + + @Test + public void testIfElifElif() { + runTest(TEST_NAME5, 1); + runTest(TEST_NAME5, 2); + runTest(TEST_NAME5, 3); + } + + @Test + public void testIfElifElifElse() { + runTest(TEST_NAME6, 1); + runTest(TEST_NAME6, 2); + runTest(TEST_NAME6, 3); + runTest(TEST_NAME6, 4); + } + + private void runTest( String testName, int val ) + { + TestConfiguration config = getTestConfiguration(testName); + loadTestConfiguration(config); + + String HOME = SCRIPT_DIR + TEST_DIR; + fullDMLScriptName = HOME + testName + ".pydml"; + programArgs = new String[]{"-python","-nvargs","val=" + Integer.toString(val)}; + + if (val == 1) + setExpectedStdOut("A"); + else if (val == 2) + setExpectedStdOut("B"); + else if (val == 3) + setExpectedStdOut("C"); + else + setExpectedStdOut("D"); + + runTest(true, false, null, -1); + } +} http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/01184e3f/src/test/scripts/functions/misc/IfTest.pydml ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/misc/IfTest.pydml b/src/test/scripts/functions/misc/IfTest.pydml new file mode 100644 index 0000000..253824c --- /dev/null +++ b/src/test/scripts/functions/misc/IfTest.pydml @@ -0,0 +1,26 @@ +#------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# +#------------------------------------------------------------- + +def ifTest(x: int): + if (x == 1): + print('A') + +z = ifTest($val) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/01184e3f/src/test/scripts/functions/misc/IfTest2.pydml ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/misc/IfTest2.pydml b/src/test/scripts/functions/misc/IfTest2.pydml new file mode 100644 index 0000000..a4943a7 --- /dev/null +++ b/src/test/scripts/functions/misc/IfTest2.pydml @@ -0,0 +1,28 @@ +#------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# +#------------------------------------------------------------- + +def ifTest(x: int): + if (x == 1): + print('A') + else: + print('B') + +z = ifTest($val) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/01184e3f/src/test/scripts/functions/misc/IfTest3.pydml ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/misc/IfTest3.pydml b/src/test/scripts/functions/misc/IfTest3.pydml new file mode 100644 index 0000000..7c17a69 --- /dev/null +++ b/src/test/scripts/functions/misc/IfTest3.pydml @@ -0,0 +1,28 @@ +#------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# +#------------------------------------------------------------- + +def ifTest(x: int): + if (x == 1): + print('A') + elif (x == 2): + print('B') + +z = ifTest($val) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/01184e3f/src/test/scripts/functions/misc/IfTest4.pydml ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/misc/IfTest4.pydml b/src/test/scripts/functions/misc/IfTest4.pydml new file mode 100644 index 0000000..5f79151 --- /dev/null +++ b/src/test/scripts/functions/misc/IfTest4.pydml @@ -0,0 +1,30 @@ +#------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# +#------------------------------------------------------------- + +def ifTest(x: int): + if (x == 1): + print('A') + elif (x == 2): + print('B') + else : + print('C') + +z = ifTest($val) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/01184e3f/src/test/scripts/functions/misc/IfTest5.pydml ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/misc/IfTest5.pydml b/src/test/scripts/functions/misc/IfTest5.pydml new file mode 100644 index 0000000..d90816a --- /dev/null +++ b/src/test/scripts/functions/misc/IfTest5.pydml @@ -0,0 +1,30 @@ +#------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# +#------------------------------------------------------------- + +def ifTest(x: int): + if (x == 1): + print('A') + elif (x == 2): + print('B') + elif (x == 3): + print('C') + +z = ifTest($val) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/01184e3f/src/test/scripts/functions/misc/IfTest6.pydml ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/misc/IfTest6.pydml b/src/test/scripts/functions/misc/IfTest6.pydml new file mode 100644 index 0000000..5aebb48 --- /dev/null +++ b/src/test/scripts/functions/misc/IfTest6.pydml @@ -0,0 +1,32 @@ +#------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# +#------------------------------------------------------------- + +def ifTest(x: int): + if (x == 1): + print('A') + elif (x == 2): + print('B') + elif (x == 3): + print('C') + else : + print('D') + +z = ifTest($val) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/01184e3f/src/test_suites/java/org/apache/sysml/test/integration/functions/misc/ZPackageSuite.java ---------------------------------------------------------------------- diff --git a/src/test_suites/java/org/apache/sysml/test/integration/functions/misc/ZPackageSuite.java b/src/test_suites/java/org/apache/sysml/test/integration/functions/misc/ZPackageSuite.java index dce345b..f24660f 100644 --- a/src/test_suites/java/org/apache/sysml/test/integration/functions/misc/ZPackageSuite.java +++ b/src/test_suites/java/org/apache/sysml/test/integration/functions/misc/ZPackageSuite.java @@ -31,6 +31,7 @@ import org.junit.runners.Suite; DataTypeChangeTest.class, FunctionInliningTest.class, FunctionNamespaceTest.class, + IfTest.class, InvalidFunctionAssignmentTest.class, InvalidFunctionSignatureTest.class, IPALiteralReplacementTest.class,
