How to show the interpolated expression in metaprogramming


#1

Here is an example given by the docs.

for op = (:+, :*, :&, :|, :$)
  @eval ($op)(a,b,c) = ($op)(($op)(a,b),c)
end

I am using ASTInterpreter to step through and trying to see which op is being defined. But errors are thrown when I typed either

`op 

or

`$op 

How could I see the expression op in debugger? Thanks!!


#2

Not sure about the debugger, but in the ordinary REPL you can do:

for op = (:+, :*, :&, :|, :$)
    @eval println($op)
end

#3

Thanks for the comments. Unfortunately, it doesn’t work. Here is the error:

1|julia > println($op)
ERROR: UndefVarError: op not defined
 in _step_expr(::ASTInterpreter.Interpreter) at /home/chobbes/.julia/v0.5/ASTInterpreter/src/ASTInterpreter.jl:653
 in step_expr at /home/chobbes/.julia/v0.5/ASTInterpreter/src/ASTInterpreter.jl:666 [inlined]
 in #finish!#81(::Bool, ::Bool, ::Function, ::ASTInterpreter.Interpreter) at /home/chobbes/.julia/v0.5/ASTInterpreter/src/ASTInterpreter.jl:1720
 in eval_in_interp(::ASTInterpreter.Interpreter, ::Expr, ::JuliaParser.Tokens.SourceExpr, ::String) at /home/chobbes/.julia/v0.5/ASTInterpreter/src/ASTInterpreter.jl:1298
 in eval_code(::ASTInterpreter.InterpreterState, ::String) at /home/chobbes/.julia/v0.5/ASTInterpreter/src/ASTInterpreter.jl:1529
 in (::ASTInterpreter.##72#78{ASTInterpreter.InterpreterState,Base.LineEdit.Prompt})(::Base.LineEdit.MIState, ::Base.AbstractIOBuffer{Array{UInt8,1}}, ::Bool) at /home/chobbes/.julia/v0.5/ASTInterpreter/src/ASTInterpreter.jl:1654
 in run_interface(::Base.Terminals.TTYTerminal, ::Base.LineEdit.ModalInterface) at ./LineEdit.jl:1579
 in RunDebugREPL(::ASTInterpreter.Interpreter) at /home/chobbes/.julia/v0.5/ASTInterpreter/src/ASTInterpreter.jl:1693

Apparently, interpolation doesn’t work this way in debugger.

I think if `println($op) works, `$op should have worked too.


Weird ASTInterpreter output
#4

Bump.


#5

Bump again. Btw, could this be something missing from the current debugger? A bug??


#6

Metaprogramming happens at parse time, so ASTInterpreter doesn’t have access to it. The easiest way to do that is to put the macro body in a separate function and manually call that on the expression you want to debug.


#7

Thanks for the explanation. Indeed, ASTInterpreter can’t see anything happened in the phase of metaprogramming. No doubt at all. But it’s kind of counter-intuitive since one expects to be able to see the value of every single variable using a debugger, especially when what you’re doing is not debugging your own code but trying to use debugger to step through some other’s code (sometimes it’s a massive package). But my thought may be not very reasonable.

But anyhow I really appreciate your comments!


#8

Here is another thing I come across frequently - when I use back-tick to show the value of some variables, I got the correct value, but wrapped by Core.Box(). For example, I expect `a return me the value of a. But what is returned is something like Core.Box(a). Is this something normal?

I know you’re busy with 0.6. So don’t bother to answer me if you’re hectic. Thanks.