forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.jl
266 lines (213 loc) · 6.94 KB
/
base.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
const NonTupleType = Union(DataType,UnionType,TypeConstructor)
typealias Callable Union(Function,DataType)
const Bottom = Union()
# constructors for Core types in boot.jl
call(T::Type{BoundsError}) = Core.call(T)
call(T::Type{DivideError}) = Core.call(T)
call(T::Type{DomainError}) = Core.call(T)
call(T::Type{OverflowError}) = Core.call(T)
call(T::Type{InexactError}) = Core.call(T)
call(T::Type{MemoryError}) = Core.call(T)
call(T::Type{StackOverflowError}) = Core.call(T)
call(T::Type{UndefRefError}) = Core.call(T)
call(T::Type{UndefVarError}, var::Symbol) = Core.call(T, var)
call(T::Type{InterruptException}) = Core.call(T)
call(T::Type{SymbolNode}, name::Symbol, t::ANY) = Core.call(T, name, t)
call(T::Type{GetfieldNode}, value, name::Symbol, typ) = Core.call(T, value, name, typ)
call(T::Type{ASCIIString}, d::Array{UInt8,1}) = Core.call(T, d)
call(T::Type{UTF8String}, d::Array{UInt8,1}) = Core.call(T, d)
call(T::Type{TypeVar}, args...) = Core.call(T, args...)
call(T::Type{TypeConstructor}, args...) = Core.call(T, args...)
call(T::Type{Expr}, args::ANY...) = _expr(args...)
call(T::Type{LineNumberNode}, n::Int) = Core.call(T, n)
call(T::Type{LabelNode}, n::Int) = Core.call(T, n)
call(T::Type{GotoNode}, n::Int) = Core.call(T, n)
call(T::Type{QuoteNode}, x::ANY) = Core.call(T, x)
call(T::Type{NewvarNode}, s::Symbol) = Core.call(T, s)
call(T::Type{TopNode}, s::Symbol) = Core.call(T, s)
call(T::Type{Module}, args...) = Core.call(T, args...)
call(T::Type{Task}, f::ANY) = Core.call(T, f)
call{T}(::Type{T}, args...) = convert(T, args...)::T
convert{T}(::Type{T}, x::T) = x
convert(::(), ::()) = ()
convert(::Type{Tuple}, x::Tuple) = x
argtail(x, rest...) = rest
tail(x::Tuple) = argtail(x...)
convert(T::(Type, Type...), x::(Any, Any...)) =
tuple(convert(T[1],x[1]), convert(tail(T), tail(x))...)
convert(T::(Any, Any...), x::(Any, Any...)) =
tuple(convert(T[1],x[1]), convert(tail(T), tail(x))...)
convert{T}(::Type{(T...)}, x::Tuple) = cnvt_all(T, x...)
cnvt_all(T) = ()
cnvt_all(T, x, rest...) = tuple(convert(T,x), cnvt_all(T, rest...)...)
ptr_arg_convert{T}(::Type{Ptr{T}}, x) = convert(T, x)
ptr_arg_convert(::Type{Ptr{Void}}, x) = x
# conversion used by ccall
cconvert(T, x) = convert(T, x)
# use the code in ccall.cpp to safely allocate temporary pointer arrays
cconvert{T}(::Type{Ptr{Ptr{T}}}, a::Array) = a
# convert strings to ByteString to pass as pointers
cconvert{P<:Union(Int8,UInt8)}(::Type{Ptr{P}}, s::AbstractString) = bytestring(s)
reinterpret{T,S}(::Type{T}, x::S) = box(T,unbox(S,x))
abstract IO
type ErrorException <: Exception
msg::AbstractString
end
type SystemError <: Exception
prefix::AbstractString
errnum::Int32
SystemError(p::AbstractString, e::Integer) = new(p, int32(e))
SystemError(p::AbstractString) = new(p, errno())
end
type TypeError <: Exception
func::Symbol
context::AbstractString
expected::Type
got
end
type ParseError <: Exception
msg::AbstractString
end
type ArgumentError <: Exception
msg::AbstractString
end
#type UnboundError <: Exception
# var::Symbol
#end
type KeyError <: Exception
key
end
type LoadError <: Exception
file::AbstractString
line::Int
error
end
type MethodError <: Exception
f
args
end
type EOFError <: Exception end
type DimensionMismatch <: Exception
msg::AbstractString
end
DimensionMismatch() = DimensionMismatch("")
type WeakRef
value
WeakRef() = WeakRef(nothing)
WeakRef(v::ANY) = ccall(:jl_gc_new_weakref, Any, (Any,), v)::WeakRef
end
ccall(:jl_get_system_hooks, Void, ())
int(x) = convert(Int, x)
int(x::Int) = x
uint(x) = convert(UInt, x)
uint(x::UInt) = x
# index colon
type Colon
end
const (:) = Colon()
==(w::WeakRef, v::WeakRef) = isequal(w.value, v.value)
==(w::WeakRef, v) = isequal(w.value, v)
==(w, v::WeakRef) = isequal(w, v.value)
function finalizer(o::ANY, f::Union(Function,Ptr))
if isimmutable(o)
error("objects of type ", typeof(o), " cannot be finalized")
end
ccall(:jl_gc_add_finalizer, Void, (Any,Any), o, f)
end
finalize(o::ANY) = ccall(:jl_finalize, Void, (Any,), o)
gc() = ccall(:jl_gc_collect, Void, ())
gc_enable() = ccall(:jl_gc_enable, Void, ())
gc_disable() = ccall(:jl_gc_disable, Void, ())
bytestring(str::ByteString) = str
identity(x) = x
function append_any(xs...)
# used by apply() and quote
# must be a separate function from append(), since apply() needs this
# exact function.
out = Array(Any, 4)
l = 4
i = 1
for x in xs
for y in x
if i > l
ccall(:jl_array_grow_end, Void, (Any, UInt), out, 16)
l += 16
end
arrayset(out, y, i)
i += 1
end
end
ccall(:jl_array_del_end, Void, (Any, UInt), out, l-i+1)
out
end
# used by { } syntax
function cell_1d(xs::ANY...)
n = length(xs)
a = Array(Any,n)
for i=1:n
arrayset(a,xs[i],i)
end
a
end
function cell_2d(nr, nc, xs::ANY...)
a = Array(Any,nr,nc)
for i=1:(nr*nc)
arrayset(a,xs[i],i)
end
a
end
# simple Array{Any} operations needed for bootstrap
setindex!(A::Array{Any}, x::ANY, i::Real) = arrayset(A, x, to_index(i))
function length_checked_equal(args...)
n = length(args[1])
for i=2:length(args)
if length(args[i]) != n
error("argument dimensions must match")
end
end
n
end
map(f::Callable, a::Array{Any,1}) = Any[ f(a[i]) for i=1:length(a) ]
macro thunk(ex); :(()->$(esc(ex))); end
macro L_str(s); s; end
function precompile(f::ANY, args::Tuple)
if isa(f,DataType)
args = tuple(Type{f}, args...)
f = f.name.module.call
end
if isgeneric(f)
ccall(:jl_compile_hint, Void, (Any, Any), f, args)
end
end
esc(e::ANY) = Expr(:escape, e)
macro boundscheck(yesno,blk)
# hack: use this syntax since it avoids introducing line numbers
:($(Expr(:boundscheck,yesno));
$(esc(blk));
$(Expr(:boundscheck,:pop)))
end
macro inbounds(blk)
:(@boundscheck false $(esc(blk)))
end
macro label(name::Symbol)
Expr(:symboliclabel, name)
end
macro goto(name::Symbol)
Expr(:symbolicgoto, name)
end
Array{T,N}(::Type{T}, d::NTuple{N,Int}) =
ccall(:jl_new_array, Array{T,N}, (Any,Any), Array{T,N}, d)
Array{T}(::Type{T}, m::Int) =
ccall(:jl_alloc_array_1d, Array{T,1}, (Any,Int), Array{T,1}, m)
Array{T}(::Type{T}, m::Int,n::Int) =
ccall(:jl_alloc_array_2d, Array{T,2}, (Any,Int,Int), Array{T,2}, m,n)
Array{T}(::Type{T}, m::Int,n::Int,o::Int) =
ccall(:jl_alloc_array_3d, Array{T,3}, (Any,Int,Int,Int), Array{T,3}, m,n,o)
Array(T::Type, d::Int...) = Array(T, d)
Array(T::Type, d::Integer...) = Array(T, convert((Int...), d))
Array{T}(::Type{T}, m::Integer) =
ccall(:jl_alloc_array_1d, Array{T,1}, (Any,Int), Array{T,1}, m)
Array{T}(::Type{T}, m::Integer,n::Integer) =
ccall(:jl_alloc_array_2d, Array{T,2}, (Any,Int,Int), Array{T,2}, m, n)
Array{T}(::Type{T}, m::Integer,n::Integer,o::Integer) =
ccall(:jl_alloc_array_3d, Array{T,3}, (Any,Int,Int,Int), Array{T,3}, m, n, o)