adsharma / decaf2many

Java to Python transpiler
MIT License
14 stars 3 forks source link

visitMethodCall breaks when some argument is not resolved #6

Open panjea opened 1 year ago

panjea commented 1 year ago

Description

translate a very large java file

ERROR

    return tree.accept(self)
  File "/Users/bretthartshorn/lvis/tools/metå/decaf2many/lang/JavaParser.py", line 8264, in accept
    return visitor.visitExpression(self)
  File "/Users/bretthartshorn/lvis/tools/metå/decaf2many/decaf2many.py", line 192, in visitExpression
    right = self.visit(right_ctx)
  File "/opt/homebrew/lib/python3.10/site-packages/antlr4/tree/Tree.py", line 34, in visit
    return tree.accept(self)
  File ".../lang/JavaParser.py", line 7957, in accept
    return visitor.visitMethodCall(self)
  File ".../decaf2many.py", line 135, in visitMethodCall
    args_str = ",".join([self.visit(a) for a in args])
  File ".../decaf2many.py", line 135, in <listcomp>
    args_str = ",".join([self.visit(a) for a in args])
  File "/opt/homebrew/lib/python3.10/site-packages/antlr4/tree/Tree.py", line 34, in visit
    return tree.accept(self)
AttributeError: 'NoneType' object has no attribute 'accept'
panjea commented 1 year ago

my temp workaround:

before:

    def visitMethodCall(self, ctx: JavaParser.MethodCallContext):
        fname = ctx.IDENTIFIER().getText()
        args = ctx.expressionList()
        args = [args.expression(i) for i in range(args.getChildCount())]
        args_str = ",".join([self.visit(a) for a in args])
        return f"{fname}({args_str})"

quick fix:

    def visitMethodCall(self, ctx: JavaParser.MethodCallContext):
        fname = ctx.IDENTIFIER().getText()
        exl = ctx.expressionList()
        if exl is None:
            args_str = ''
        else:
            args = []
            for i in range(exl.getChildCount()):
                a = exl.expression(i)
                if a is not None:
                    args.append( a )
            args_str = ",".join([self.visit(a) for a in args])
        return f"{fname}({args_str})"