-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
staged.jl
137 lines (121 loc) · 3.48 KB
/
staged.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
# This file is a part of Julia. License is MIT: http:https://julialang.org/license
@generated function staged_t1(a,b)
if a == Int
return :(a+b)
else
return :(a*b)
end
end
@test staged_t1(1,2) == 3
@test staged_t1(1.0,0.5) == 0.5
@test staged_t1(1,0.5) == 1.5
tinline(a,b) = staged_t1(a,b)
@test !isa(tinline(1,2),Expr)
@test tinline(1,0.5) == 1.5
@generated function splat(a,b...)
:( ($a,$b,a,b) )
end
@test splat(1,2,3) == (Int,(Int,Int),1,(2,3))
stagediobuf = IOBuffer()
@generated function splat2(a...)
print(stagediobuf, a)
:(nothing)
end
const intstr = @sprintf("%s", Int)
splat2(1)
@test takebuf_string(stagediobuf) == "($intstr,)"
splat2(1,3)
@test takebuf_string(stagediobuf) == "($intstr,$intstr)"
splat2(5,2)
@test takebuf_string(stagediobuf) == ""
splat2(1:3,5.2)
@test takebuf_string(stagediobuf) == "(UnitRange{$intstr},Float64)"
splat2(3,5:2:7)
@test takebuf_string(stagediobuf) == "($intstr,StepRange{$intstr,$intstr})"
splat2(1,2,3,4)
@test takebuf_string(stagediobuf) == "($intstr,$intstr,$intstr,$intstr)"
splat2(1,2,3)
@test takebuf_string(stagediobuf) == "($intstr,$intstr,$intstr)"
splat2(1:5, 3, 3:3)
@test takebuf_string(stagediobuf) == "(UnitRange{$intstr},$intstr,UnitRange{$intstr})"
splat2(1:5, 3, 3:3)
@test takebuf_string(stagediobuf) == ""
splat2(1:5, 3:3, 3)
@test takebuf_string(stagediobuf) == "(UnitRange{$intstr},UnitRange{$intstr},$intstr)"
splat2(1:5, 3:3)
@test takebuf_string(stagediobuf) == "(UnitRange{$intstr},UnitRange{$intstr})"
splat2(3, 3:5)
@test takebuf_string(stagediobuf) == "($intstr,UnitRange{$intstr})"
# varargs specialization with parametric @generated functions (issue #8944)
@generated function splat3{T,N}(A::AbstractArray{T,N}, indx::RangeIndex...)
print(stagediobuf, indx)
:(nothing)
end
A = rand(5,5,3);
splat3(A, 1:2, 1:2, 1)
@test takebuf_string(stagediobuf) == "(UnitRange{$intstr},UnitRange{$intstr},$intstr)"
splat3(A, 1:2, 1, 1:2)
@test takebuf_string(stagediobuf) == "(UnitRange{$intstr},$intstr,UnitRange{$intstr})"
B = slice(A, 1:3, 2, 1:3);
@generated function mygetindex(S::SubArray, indexes::Real...)
T, N, A, I = S.parameters
if N != length(indexes)
error("Wrong number of indexes supplied")
end
Ip = I.parameters
NP = length(Ip)
indexexprs = Array(Expr, NP)
j = 1
for i = 1:NP
if Ip[i] == Int
indexexprs[i] = :(S.indexes[$i])
else
indexexprs[i] = :(S.indexes[$i][indexes[$j]])
j += 1
end
end
ex = :(S.parent[$(indexexprs...)])
ex
end
@test mygetindex(B,2,2) == A[2,2,2]
# issue #8497
module MyTest8497
internalfunction(x) = x+1
@generated function h(x)
quote
internalfunction(x)
end
end
end
@test MyTest8497.h(3) == 4
# static parameters (issue #8505)
@generated function foo1{N,T}(a::Array{T,N})
"N = $N, T = $T"
end
@generated function foo2{T,N}(a::Array{T,N})
"N = $N, T = $T"
end
@test foo1(randn(3,3)) == "N = 2, T = Float64"
@test foo2(randn(3,3)) == "N = 2, T = Float64"
# issue #9088
@generated function f9088(x, a=5)
:(x+a)
end
@test f9088(7) == 12
# issue #10502
@generated function f10502(x...)
:($x)
end
f10502() = ()
@test f10502(1) == (Int,)
@test f10502(1,2) == (Int,Int)
@test f10502(1,2,3) == (Int,Int,Int)
# One-line @generated functions
@generated oneliner(x,y) = :($x, x, $y, y)
@test oneliner(1, 2.) == (Int, 1, Float64, 2.)
# issue #11982
@generated function f11982(T)
string(T.parameters[1])
end
@test f11982(Float32) == "Float32"
@test f11982(Int32) == "Int32"