forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.jl
93 lines (85 loc) · 2.89 KB
/
repl.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# called to show a REPL result
repl_show(v::ANY) = repl_show(OUTPUT_STREAM, v)
function repl_show(io::IO, v::ANY)
if !(isa(v,Function) && isgeneric(v))
if isa(v,AbstractVector) && !isa(v,Ranges)
print(io, summary(v))
if !isempty(v)
println(io, ":")
print_matrix(io, reshape(v,(length(v),1)))
end
else
show(io, v)
end
end
if isgeneric(v) && !isa(v,DataType)
show(io, v.env)
end
end
# showing exception objects as descriptive error messages
error_show(io::IO, e) = show(io, e)
function error_show(io::IO, e::TypeError)
ctx = isempty(e.context) ? "" : "in $(e.context), "
if e.expected === Bool
print(io, "type: non-boolean ($(typeof(e.got))) ",
"used in boolean context")
else
if isa(e.got,Type)
tstr = "Type{$(e.got)}"
else
tstr = string(typeof(e.got))
end
print(io, "type: $(e.func): ",
"$(ctx)expected $(e.expected), ",
"got $tstr")
end
end
function error_show(io::IO, e, bt)
error_show(io, e)
show_backtrace(io, bt)
end
error_show(io::IO, e::LoadError) = error_show(io, e, {})
function error_show(io::IO, e::LoadError, bt)
error_show(io, e.error, bt)
print(io, "\nat $(e.file):$(e.line)")
end
error_show(io::IO, e::SystemError) = print(io, "$(e.prefix): $(strerror(e.errnum))")
error_show(io::IO, ::DivideByZeroError) = print(io, "integer divide by zero")
error_show(io::IO, ::StackOverflowError) = print(io, "stack overflow")
error_show(io::IO, ::UndefRefError) = print(io, "access to undefined reference")
error_show(io::IO, ::EOFError) = print(io, "read: end of file")
error_show(io::IO, e::ErrorException) = print(io, e.msg)
error_show(io::IO, e::KeyError) = print(io, "key not found: $(e.key)")
error_show(io::IO, e::InterruptException) = print(io, "interrupt")
function error_show(io::IO, e::MethodError)
name = e.f.env.name
if is(e.f,convert) && length(e.args)==2
print(io, "no method $(name)(Type{$(e.args[1])},$(typeof(e.args[2])))")
else
print(io, "no method $(name)$(typeof(e.args))")
end
end
function show_backtrace(io::IO, t)
# we may not declare :eval_user_input
# directly so that we get a compile error
# in case its name changes in the future
const eval_function = try
symbol(string(eval_user_input))
catch
:(:) #for when client.jl is not yet defined
end
for i = 1:3:length(t)
if i == 1 && t[i] == :error; continue; end
if t[i] == eval_function; break; end
print(io, "\n")
lno = t[i+2]
print(io, " in ", t[i], " at ", t[i+1])
if lno >= 1
try
print(io, ":", lno)
catch
print(io, '?') #for when dec is not yet defined
end
end
end
end