Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 127 lines (117 sloc) 3.023 kb
fd4c9e3 @vtjnash example code: type tree graphic using names(::Module)
vtjnash authored
1 ##
2 #
34a9cdb @vtjnash add header comment for typetree.jl
vtjnash authored
3 # (spaghetti) Code to create a text graphic of the Julia type tree
4 # Used to generate https://github.com/JuliaLang/julia/wiki/Types-Hierarchy
fd4c9e3 @vtjnash example code: type tree graphic using names(::Module)
vtjnash authored
5 #
6 ##
7
8 function list_all(T::Type, modules::Array{Module}, f::Function)
9 a = Dict{String, T}()
10 for m = modules
11 for s = names(m)
12 try
13 t = eval(s)
14 if f(t)
15 a[string(s)] = t
16 end
17 end
18 end
19 end
20 a
21 end
22
23 function to_array{T}(s::Set{T})
24 a = Array(T, length(s))
25 i = 1
26 for ele = s
27 a[i] = ele
28 i += 1
29 end
30 a
31 end
32
33 function mk_tree()
34 for (x,t) in all_types
35 add_type(x,t,true)
36 end
37 end
38 extra = Dict{String, String}()
39 function insert_type(m,s,x,ex)
40 try
41 ms = m[s]
42 add(ms, x)
43 catch
44 m[s] = Set{String}(x)
45 end
46 extra[x] = ex
47 end
48
49 function add_type(x,t,sup)
50 if isa(t, AbstractKind) ||
51 isa(t, BitsKind) ||
52 isa(t, CompositeKind)
53 s = sup ? super(t) : t
54 s_param = ""
55 if x != string(t.name)
56 s_param *= " = " * string(t.name)
57 elseif length(s.parameters) != 0
58 s_param *= " (=" * string(s.name) * "{" * join(s.parameters, ", ") * "})"
59 end
60 insert_type(children_map, string(s.name), x, s_param)
61 elseif isa(t, UnionKind)
62 for c in t.types
63 add_type(x, c, false)
64 end
65 insert_type(children_map, "UnionKind", x, " = " * string(t))
66 elseif isa(t, TypeConstructor)
67 add_type(x, t.body, false)
68 #println(typeof(t.body))
69 insert_type(children_map, "TypeConstructor", x, " = " * string(t))
70 else
71 try
72 print("unknown -> ")
73 print(x)
74 print(" ")
75 print(typeof(t))
76 print(" ")
77 print(t.name)
78 end
79 println(t)
80 end
81 end
82
83 function show_tree(root, prefix, norm_ex)
84 r = root
85 t = all_types[root]
86 try
87 if length(t.parameters) != 0
88 root *= "{" * join(t.parameters, ", ") * "}"
89 end
90 end
91 ex = extra[r]
92 if ex != norm_ex
93 root *= ex
94 end
95 println(prefix[1:end-1] * "+- " * root)
96 norm_ex = ""
97 try
98 norm_ex = " (=" * r * "{" * join(t.parameters, ", ") * "})"
99 end
100 children = get(children_map, r, None)
101 if children != None
102 ch = to_array(children)
103 sort!(ch)
104 del(children_map, r)
105 iter = start(ch)
106 cpre = prefix * " +"
107 dpre = prefix * " "
108 while !done(ch, iter)
109 c,iter = next(ch, iter)
110 if c != root
111 show_tree(c, done(ch, iter) ? dpre : cpre, norm_ex)
112 end
113 end
114 end
115 end
116
117 show_tree(root) = show_tree(root, "", "")
118
119 ## main ##
120 all_types = list_all(Type, [Core, Base], (x)->isa(x, Type))
121 children_map = Dict{String, Set{String}}(length(all_types))
122 mk_tree()
123 println("\n\nType Tree:")
124 show_tree("Any") #todo: generalize this to be every item without a parent in all_types
125
126
Something went wrong with that request. Please try again.