reference icon indicating copy to clipboard operation
reference copied to clipboard

Macros-by-example `expr` metavariable substitution behaves as if parenthesized

Open 8573 opened this issue 2 years ago • 2 comments

While reviewing some code, I saw a macro-by-example using a metavariable of type expr in such a way that the code would be wrong if an expression were given to the macro that needed to be parenthesized — so I thought. Testing in the Playground, I found that substituted expressions appeared to be parenthesized automatically. I checked the Reference's section on macros-by-example and saw nothing about this; I looked at the Book's section on macros-by-example and saw nothing about this, but the Book suggested for further reading https://veykril.github.io/tlborm/, which explains that expressions are substituted as AST nodes rather than as tokens and so behave as if parenthesized. I suggest that this be mentioned in the Reference.

8573 avatar Jun 26 '23 22:06 8573

The section at Forwarding a matched fragment mentions that fragments are treated as opaque AST elements. Does that cover what you are looking for?

For the most part, expansion is only very partially documented. Eventually I would expect it to have a dedicated chapter (or five 😛).

ehuss avatar Jun 26 '23 22:06 ehuss

The section at Forwarding a matched fragment mentions that fragments are treated as opaque AST elements. Does that cover what you are looking for?

Maybe I don't have enough reference-level understanding to see it, but I don't think so; I think that only addresses matching of the fragment by further macros, rather than how a fragment substituted into the macro's output is treated when the final, post-all-macro-expansion code is processed by the rest of the compiler.

8573 avatar Jun 27 '23 02:06 8573