forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supersedes and closes JuliaLang#1755. Closes JuliaLang#1576.
- Loading branch information
Showing
3 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,8 @@ export | |
Pkg, | ||
Operators, | ||
Errno, | ||
|
||
Meta, | ||
|
||
# Types | ||
AbstractMatrix, | ||
AbstractSparseMatrix, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters