/
DbgMacro.jl
122 lines (98 loc) · 1.98 KB
/
DbgMacro.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
module DbgMacro
export @dbg, @dumpct, @dumprt, @qn
"""
@dbg <expr1> <expr2> <expr3> ...
Display all the expressions in the same way as `@show` does, each on a separate line,
preceded by the location in the format `module:file:line`.
The output goes to `stderr`.
Useful for debugging.
# Examples
```jldoctest
julia> m = [1 2; 3 4];
julia> @dbg 1+2 "Hello" m
Main:none:1 1 + 2 = 3
Main:none:1 "Hello" = "Hello"
Main:none:1 m = [1 2; 3 4]
```
"""
macro dbg(exprs...)
mod = __module__
file = relpath(string(__source__.file))
line = __source__.line
location = "$mod:$file:$line "
esc(quote
$((:(Base.println(Base.stderr, $location,
$(ex isa String ? repr(ex) : string(ex)),
" = ", Base.repr($ex))) for ex in exprs)...)
end)
end
export @dumpct, @dumprt
"""
@dumpct <expression>
Dump the expression at compile-time.
# Examples
```jldoctest
julia> function foo(x)
@dumpct :x + x + \$x
x
end
Expr
head: Symbol call
args: Array{Any}((4,))
1: Symbol +
2: QuoteNode
value: Symbol x
3: Symbol x
4: Expr
head: Symbol \$
args: Array{Any}((1,))
1: Symbol x
foo (generic function with 1 method)
julia> foo(42)
42
```
"""
macro dumpct(ex)
dump(ex; maxdepth=32)
end
"""
@dumprt <expression>
Dump the expression at run-time.
# Examples
```jldoctest
julia> function foo(x)
@dumprt :x + x + \$x
x
end
foo (generic function with 1 method)
julia> foo(42)
Expr
head: Symbol call
args: Array{Any}((4,))
1: Symbol +
2: QuoteNode
value: Symbol x
3: Symbol x
4: Expr
head: Symbol \$
args: Array{Any}((1,))
1: Symbol x
42
```
"""
macro dumprt(ex)
:(dump($(QuoteNode(ex)); maxdepth=32))
end
"""
@qn <expression>
Return the expression, without \$ interpolation.
# Examples
```jldoctest
julia> @qn foo(1, x, :y, \$z)
:(foo(1, x, :y, \$(Expr(:\$, :z))))
```
"""
macro qn(ex)
QuoteNode(ex)
end
end