forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.jl
50 lines (35 loc) · 737 Bytes
/
meta.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
# test meta-expressions that annotate blocks of code
module MetaTest
using Base.Test
function f(x)
y = x+5
z = y*y
q = z/y
m = q-3
end
@inline function f_inlined(x)
y = x+5
z = y*y
q = z/y
m = q-3
end
g(x) = f(2x)
g_inlined(x) = f_inlined(2x)
@test g(3) == g_inlined(3)
@test f(3) == f_inlined(3)
using Base.pushmeta!, Base.popmeta!
macro attach(val, ex)
esc(_attach(val, ex))
end
_attach(val, ex) = pushmeta!(ex, :test, val)
@attach 42 function dummy()
false
end
asts = code_lowered(dummy, ())
@assert length(asts) == 1
ast = asts[1]
body = Expr(:block)
body.args = ast.args[3].args
@test popmeta!(body, :test) == (true, [42])
@test popmeta!(body, :nonexistent) == (false, [])
end