forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta.jl
49 lines (38 loc) · 1.32 KB
/
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
# This file is a part of Julia. License is MIT: http://julialang.org/license
module Meta
#
# convenience functions for metaprogramming
#
export quot,
isexpr,
show_sexpr
quot(ex) = Expr(:quote, ex)
isexpr(ex::Expr, head) = ex.head === head
isexpr(ex::Expr, heads::Set) = in(ex.head, heads)
isexpr(ex::Expr, heads::Vector) = in(ex.head, heads)
isexpr(ex, head) = false
isexpr(ex, head, n::Int) = isexpr(ex, head) && length(ex.args) == n
# ---- show_sexpr: print an AST as an S-expression ----
show_sexpr(ex) = show_sexpr(STDOUT, ex)
show_sexpr(io::IO, ex) = show_sexpr(io, ex, 0)
show_sexpr(io::IO, ex, indent::Int) = show(io, ex)
const sexpr_indent_width = 2
function show_sexpr(io::IO, ex::QuoteNode, indent::Int)
inner = indent + sexpr_indent_width
print(io, "(:quote, #QuoteNode\n", " "^inner)
show_sexpr(io, ex.value, inner)
print(io, '\n', " "^indent, ')')
end
function show_sexpr(io::IO, ex::Expr, indent::Int)
inner = indent + sexpr_indent_width
print(io, '(')
show_sexpr(io, ex.head, inner)
for arg in ex.args
print(io, ex.head === :block ? ",\n"*" "^inner : ", ")
show_sexpr(io, arg, inner)
end
if isempty(ex.args); print(io, ",)")
else print(io, (ex.head === :block ? "\n"*" "^indent : ""), ')')
end
end
end # module