Get `@amacro x 3` to produce `[(:x, x), (:3, 3)]` -- a version of `@show`

question
macros

#1

I want a macro that behaves as follows:

julia> x=7; @amacro x x+x 3
[(:x, 7), (:(x+x), 14), (:3, 3)]

julia> x=7; macroexpand(:( @amacro x x+x 3 ))
[(:x, x), (:(x+x), x+x, (:3, 3)]

I think you can consider this a variant of @show that doesn’t quite print anything but includes all the information.

So far I have the two “orthogonal” pieces working (except for a 3 where there should be a :3)

julia> macro parta(exs...)
           Expr(:vect, [:($(ex), $(ex)) for ex in exs]...)
       end
@parta (macro with 1 method)

julia> macro partb(exs...)
           Expr(:vect, [(ex, ex) for ex in exs]...)
       end
@partb (macro with 1 method)

julia> x = 7; @parta x x+x 3
3-element Array{Tuple{Int64,Int64},1}:
 (7,7)  
 (14,14)
 (3,3)  

julia> x = 7; @partb x x+x 3
3-element Array{Tuple{Any,Any},1}:
 (:x,:x)            
 (:(x + x),:(x + x))
 (3,3)   

But I do not see how to combine these. I’ve tried monkeying a few compositions of Symbol, escs, and $ with no success.

At first I was trying to make this macro look like

macro amacro
    :[(ex, ... stuff) for ex in exs]
end

But the big problem was that I somehow wanted the [] to be quoted, without the for to be quoted. Now I feel like I am experiencing the same issue with the inner tuples.


#2

This isn’t a complete solution, but I think you might find QuoteNode helpful. Here’s my version of parta:

julia> macro parta(exs...)
           Expr(:vect, [Expr(:tuple, QuoteNode(ex), ex) for ex in exs]...)
       end
@parta (macro with 1 method)

julia> x = 5
5

julia> @parta x x+x
2-element Array{Tuple{Any,Int64},1}:
 (:x, 5)
 (:(x + x), 10)