CozySynthesizer / cozy

The collection synthesizer
https://cozy.uwplse.org
Apache License 2.0
209 stars 18 forks source link

a sane way to annotate generated code #83

Closed izgzhen closed 5 years ago

izgzhen commented 5 years ago

Why attach a .comment field to the syntax tree? Could we instead add, say, self.comment(text) in the right places during code generation?

Because sometimes I want to add comment to a AST node inside a large AST tree, e.g. the modified part in codege/java.py. I don't usually have access to visitor

Calvin-L commented 5 years ago

The code generators like JavaPrinter in codegen/java.py stream their output to a text I/O handle. Their visit methods do not return values, they write code directly to the handle statefully. You can always just write a comment directly to the handle during code generation when the visitor visits a particular AST node.

Another clean way to do this might be to use SEscape to produce a no-op statement that just contains a comment, if you really need to put a comment in the middle of a big syntax blob that is being passed elsewhere.

izgzhen commented 5 years ago

Another benefit of attaching comment directly to the AST node is that the later optimization pass will automatically get rid of the comment, if the node is removed. They are semantically relevant anyway.