Skip to content

Commit

Permalink
Merge branch 'with-meta'
Browse files Browse the repository at this point in the history
Supersedes and closes JuliaLang#1755.
Closes JuliaLang#1576.
  • Loading branch information
pao committed Feb 14, 2013
2 parents e9263b7 + 78c2494 commit 7b3e46e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
3 changes: 2 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export
Pkg,
Operators,
Errno,

Meta,

# Types
AbstractMatrix,
AbstractSparseMatrix,
Expand Down
47 changes: 47 additions & 0 deletions base/meta.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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) = has(heads, ex.head)
isexpr(ex::Expr, heads::Vector) = contains(heads, ex.head)
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(OUTPUT_STREAM, 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
3 changes: 2 additions & 1 deletion base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,13 @@ include("combinatorics.jl")
include("darray2.jl")
include("mmap.jl")

# utilities - version, timing, help, edit
# utilities - version, timing, help, edit, metaprogramming
include("version.jl")
include("datafmt.jl")
include("deepcopy.jl")
include("util.jl")
include("test.jl")
include("meta.jl")

# linear algebra
include("blas.jl")
Expand Down

0 comments on commit 7b3e46e

Please sign in to comment.