Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Else if conversion fails #70

Open
ferchault opened this issue Dec 4, 2020 · 0 comments
Open

Else if conversion fails #70

ferchault opened this issue Dec 4, 2020 · 0 comments

Comments

@ferchault
Copy link

When converting an existing code base I found that else-if apparently is not supported. Here's an example:

   public static void main(String[] args) {
        int a = 0;
        if (a == 1) {
                System.out.println("a=1");
        } else if (a == 2) {
                System.out.println("a=2");
        }
    }

which fails with the output

$ j2py HelloWorld.java
Traceback (most recent call last):
  File "/usr/local/bin/j2py", line 258, in <module>
    sys.exit(runMain(configScript(sys.argv[1:])))
  File "/usr/local/bin/j2py", line 57, in runMain
    return runOneOrMany(options)
  File "/usr/local/bin/j2py", line 82, in runOneOrMany
    return runTransform(options)
  File "/usr/local/bin/j2py", line 134, in runTransform
    module.walk(tree)
  File "/usr/local/lib/python2.7/dist-packages/java2python/compiler/visitor.py", line 86, in walk
    visitor.walk(child, memo)
  File "/usr/local/lib/python2.7/dist-packages/java2python/compiler/visitor.py", line 86, in walk
    visitor.walk(child, memo)
  File "/usr/local/lib/python2.7/dist-packages/java2python/compiler/visitor.py", line 86, in walk
    visitor.walk(child, memo)
  File "/usr/local/lib/python2.7/dist-packages/java2python/compiler/visitor.py", line 86, in walk
    visitor.walk(child, memo)
  File "/usr/local/lib/python2.7/dist-packages/java2python/compiler/visitor.py", line 86, in walk
    visitor.walk(child, memo)
  File "/usr/local/lib/python2.7/dist-packages/java2python/compiler/visitor.py", line 83, in walk
    visitor = self.accept(tree, memo)
  File "/usr/local/lib/python2.7/dist-packages/java2python/compiler/visitor.py", line 43, in accept
    return call(node, memo)
  File "/usr/local/lib/python2.7/dist-packages/java2python/compiler/visitor.py", line 474, in acceptIf
    nextNode = nextNode.children[2]

With the obvious workaround of separating the if statements, the conversion is successful:

    public static void main(String[] args) {
        int a = 0;
        if (a == 1) {
                System.out.println("a=1");
        }
        if (a == 2) {
                System.out.println("a=2");
        }
    }

converts to

#!/usr/bin/env python
""" generated source for module HelloWorld """
class HelloWorld(object):
    """ generated source for class HelloWorld """
    @classmethod
    def main(cls, args):
        """ generated source for method main """
        a = 0
        if a == 1:
            print "a=1"
        if a == 2:
            print "a=2"


if __name__ == '__main__':
    import sys
    HelloWorld.main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant