Ok, I tried to do that:
import Base.show
function show(e::Expr)
if e.head == :macro
e.head = :function
print("macro")
print(@sprintf("%s", stripmeta(e))[9:end])
e.head = :macro
nothing
elseif e.head == :$
print("\$(")
for a in e.args
show(a)
end
print(")")
else
show(e)
end
end
I thought that show would be defined recursively on functions, but it seems
that's not the case, since trying to print :(macro m(x) :(x) end) gives me
$(Expr(:quote, :($(Expr(:$, :x))))).
How would I get around this?
On Monday, January 18, 2016 at 6:14:42 AM UTC-8, Ismael Venegas Castelló
wrote:
> That's because in the case of expressions, we are interested in the AST,
> show representation is just an abstraction of that, there is also dump and
> xdump:
>
> julia> ex = :(:($x))
> :($(Expr(:quote, :($(Expr(:$, :x))))))
>
> julia> Meta.show_sexpr(ex); println()
> (:quote, (:$, :x))
>
> julia> dump(ex)
>
> Expr
> head: Symbol quote
> args: Array(Any,(1,))
> 1: Expr
> head: Symbol $
> args: Array(Any,(1,))
> 1: Symbol x
> typ: Any
> typ: Any
>
> julia> ex.args[1].args[1] = :y
> :y
>
> julia> ex
> :($(Expr(:quote, :($(Expr(:$, :y))))))
>
> julia> Meta.show_sexpr(ex); println()
> (:quote, (:$, :y))
>
> julia> dump(ex)
> Expr
> head: Symbol quote
> args: Array(Any,(1,))
> 1: Expr
> head: Symbol $
> args: Array(Any,(1,))
> 1: Symbol y
> typ: Any
> typ: Any
>
> You could overwrite those methods or write your own that prints them the
> way you want to.
>
> El domingo, 17 de enero de 2016, 1:50:05 (UTC-6), [email protected]
> escribió:
>>
>> Hi!
>>
>> Was messing around with exceptions, and trying to see under the hood and
>> construct macro expressions.
>>
>> Eg, Meta.show_sexpr(:(:(+ 1 2))) -> (:quote, (:call, :+, 1, 2))
>>
>> but how do you build an unquote expression?
>>
>> Meta.show_sexpr(:(:($(x+5) + 1))) -> (:quote, (:call, :+, (:$, (:call,
>> :+, :x, 5)), 1))
>>
>>
>> But Expr(:quote, Expr(:call, :+, Expr(:$, Expr(:call, :+, :x, 5)), 1))
>> -> :($(Expr(:quote, :($(Expr(:$, :(x + 5))) + 1))))
>>
>> In other words, it's not processing into the appropriate expression, and
>> there's some weird intermediate syntax going on.
>>
>>
>> How does one build an unquote expression? I.e, an expression that would
>> eval to unquoting a variable?
>>
>