https://github.com/JuliaLang/julia
Revision 4d8ca6b8d8c1b6e0b62f6dba5be4a71dca1c5f7b authored by Amit Murthy on 17 August 2015, 10:07:10 UTC, committed by Amit Murthy on 17 August 2015, 10:07:10 UTC
2 parent s d97d4fb + de8c7da
Raw File
Tip revision: 4d8ca6b8d8c1b6e0b62f6dba5be4a71dca1c5f7b authored by Amit Murthy on 17 August 2015, 10:07:10 UTC
Merge pull request #12658 from peter1000/patch-2
Tip revision: 4d8ca6b
meta.jl
# 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 length(ex.args) == 0; print(io, ",)")
    else print(io, (ex.head === :block ? "\n"*" "^indent : ""), ')')
    end
end

end # module
back to top