Skip to content

Commit b5263bd

Browse files
authored
Merge pull request #61 from jlumpe/builtins
Minor changes for builtin types
2 parents e6470d5 + 5bf394f commit b5263bd

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

src/AbstractTrees.jl

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,7 @@ include("implicitstacks.jl")
5050
include("printing.jl")
5151
include("indexing.jl")
5252
include("iteration.jl")
53-
54-
55-
## Special cases
56-
57-
# Types which are iterable but shouldn't be considered tree-iterable
58-
children(x::Number) = ()
59-
children(x::Char) = ()
60-
children(x::Task) = ()
61-
children(x::AbstractString) = ()
62-
63-
# Define this here, there isn't really a good canonical package to define this
64-
# elsewhere
65-
children(x::Expr) = x.args
66-
67-
# For AbstractDicts
68-
69-
printnode(io::IO, kv::Pair{K,V}) where {K,V} = printnode(io,kv[1])
70-
children(kv::Pair{K,V}) where {K,V} = (kv[2],)
71-
72-
# For potentially-large containers, just show the type
73-
74-
printnode(io::IO, ::T) where T <: Union{AbstractArray, AbstractDict} = print(io, T)
53+
include("builtins.jl")
7554

7655

7756
end # module

src/builtins.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Tree API implementations for builtin types
2+
3+
4+
# Types which are iterable but shouldn't be considered tree-iterable
5+
children(x::Number) = ()
6+
children(x::Char) = ()
7+
children(x::Task) = ()
8+
children(x::AbstractString) = ()
9+
10+
11+
# Expr
12+
children(x::Expr) = x.args
13+
14+
function printnode(io::IO, x::Expr)
15+
print(io, "Expr(")
16+
show(io, x.head)
17+
print(io, ")")
18+
end
19+
20+
21+
# AbstractDict
22+
printnode(io::IO, kv::Pair{K,V}) where {K,V} = printnode(io,kv[1])
23+
children(kv::Pair{K,V}) where {K,V} = (kv[2],)
24+
25+
26+
# For potentially-large containers, just show the type
27+
printnode(io::IO, ::T) where T <: Union{AbstractArray, AbstractDict} = print(io, T)

test/builtins.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,14 @@
1515
tree2 = Any[Any[1,2],Any[3,4]]
1616
@test collect(PreOrderDFS(tree2)) == Any[tree2,Any[1,2],1,2,Any[3,4],3,4]
1717
end
18+
19+
20+
@testset "Expr" begin
21+
expr = :(foo(x^2 + 3))
22+
23+
@test children(expr) == expr.args
24+
25+
@test repr_tree(expr) == "Expr(:call)\n├─ :foo\n└─ Expr(:call)\n ├─ :+\n ├─ Expr(:call)\n │ ├─ :^\n │ ├─ :x\n │ └─ 2\n └─ 3\n"
26+
27+
@test collect(Leaves(expr)) == [:foo, :+, :^, :x, 2, 3]
28+
end

0 commit comments

Comments
 (0)