a weird problem when performing transformation
seekamoon opened this issue · 3 comments
Hi there,
It is a great project, which makes up the transformation ability that the original tree-sitter haven't had yet.
Here I would like to perform a transformation that removing the type descriptor of the cast expression.
For example, given the input snippet as follows (which has two nested cast expressions in the corresponding AST):
void foo(byte *param)
{
sVar = (char *)xmalloc((int)param + 8);
}
, the expected output should be
void foo(byte *param)
{
sVar = xmalloc(param + 8);
}
To this end, I write the code as follows:
import asts
code = \
"""
void foo(byte *param)
{
sVar = (char *)xmalloc((int)param + 8);
}
"""
def remove_cast(ast: asts.AST):
if isinstance(ast, asts.CCastExpression):
print('TRANS:', ast.source_text, '->', ast.value.source_text)
return ast.value
def main():
root = asts.AST.from_string(code, asts.ASTLanguage.C, deepest=True)
new_root = asts.AST.transform(root, remove_cast)
print('New Code:\n', new_root.source_text)
if __name__ == '__main__':
main()
But the output is
TRANS: (char *)xmalloc((int)param + 8) -> xmalloc((int)param + 8)
TRANS: (int)param -> param
New Code:
param
, there is only an identifier in the generated code, which is unexpected.
After some trials, I found it can work well under the situation that a cast expression doesn't contain other cast expressions.
For example, if the input is
void foo(byte *param)
{
sVar = (char *)xmalloc(param + 8);
}
the output is
void foo(byte *param)
{
sVar = xmalloc(param + 8);
}
, which is expected.
Now I have no idea about how to deal with this problem... Can you help me please?
Thanks!
Hi @seekamoon - thanks for the very detailed report! I am taking a look and will report back when a solution is found.
Hi @seekamoon - I think the issue should be fixed now in the latest (0.6.1) release of the python ASTs package. Please let us know if this issue persists or if something else is found. Thanks!