I'm trying to transform List Comprehensions to For Loops with the help of ast.NodeTransformer. After this, I would do a type analysis for Python.
I understand that when an ast node ast.ListComp is encountered, it has to be transformed to two ast nodes: the first node is ast.Assign and the second is ast.For. But according to the document ast.NodeTransformer, ast.ListComp can only be converted into one ast node rather than two, which means we can not insert ast.Assign and ast.For into AST together.
For now I can make an equivalent for loop. But I also need an assignment statement to initialize a list to []. How can I circumvent this problem?
An example may help:
a=[x for x in [1,2,3]] # One node in AST
to
a=[] # One node in AST
for x in [1,2,3]: # Another one in AST
a.append(x)