I use the Language.C library Abstract Syntax Tree (AST) to modify C programs using common SYB library transformations . This AST has different types of nodes (data types), each of which is a construction C, i.e. Expressions, statements, definitions, etc. I now need to somehow increase the information that the data contains, i.e. Annotate them. I assumed (maybe I'm wrong) that I cannot change or override the original data type, so I would like to have something like this:
annotateAST anns =
everywhere (mkT (annotateAST_ anns))
annotateAST_ astnode anns
| isStmt astnode = AnnStmt astnode (getAnn astnode anns)
| otherwise = astnode
This way I will have a new ash with annotated statements instead of the original one. Of course, the GHC is about to complain because it everywheremust return the same type that it receives, and that’s not what happens here.
In conclusion, I need to outline the AST in general terms without changing the original data types and in such a way that it is easy to return to the original data structure. I thought about various solutions to this problem, but I am not convinced of any of them, so I decided to share it here.
PS I was told that the SYB library is not very efficient. Given that Language.C language AST only outputs data, do I have a better alternative to general AST traversal and modification?