-
Notifications
You must be signed in to change notification settings - Fork 38
/
Pervasives.jl
217 lines (197 loc) · 6.29 KB
/
Pervasives.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
module Pervasives
if isdefined(Base, :Experimental)
@eval Base.Experimental.@compiler_options optimize=0 compile=min infer=no
end
using MLStyle
using MLStyle.AbstractPatterns
using MLStyle.AbstractPatterns
struct Many end
struct Do end
struct GuardBy end
export Many, Do, GuardBy
@nospecialize
function MLStyle.pattern_uncall(
::typeof(:),
self::Function,
tparams::AbstractArray,
targs::AbstractArray,
args::AbstractArray,
)
isempty(tparams) || error("A (:) pattern requires no type params.")
isempty(targs) || error("A (:) pattern requires no type arguments.")
guard() do target, scope, _
rng = Expr(:call, :, args...)
see_captured_vars(:($target in $rng), scope)
end
end
@specialize
function MLStyle.pattern_uncall(
::Type{Dict},
self::Function,
tparams::AbstractArray,
targs::AbstractArray,
args::AbstractArray,
)
isempty(tparams) || return begin
call = Expr(:call, Dict, args...)
ann = Expr(:curly, Dict, targs...)
self(Where(call, ann, tparams))
end
pairs = Pair[]
for arg in args
@switch arg begin
@case :($a => $b)
push!(pairs, a => b)
continue
@case _
error(
"A Dict pattern's sub-pattern should be the form of `(a::Symbol) => b`.",
)
end
end
function dict_extract(expr::Any, i::Int, scope::ChainDict{Symbol, Symbol}, ::Any)
# cannot avoid performance overhead due to
# https://discourse.julialang.org/t/distinguish-dictionary-lookup-from-nothing-and-not-found/38654
k, v = pairs[i]
if k isa Union{Expr, Symbol}
# how to reduce the generate code size?
# most of the cases, see_captured_vars is unnecessary.
k = see_captured_vars(k, scope)
end
:(haskey($expr, $k) ? Some($expr[$k]) : nothing)
end
tchk = isempty(targs) ? P_type_of(Dict) : self(:(::$Dict{$(targs...)}))
decomp =
decons(dict_extract, [self(Expr(:call, Some, pair.second)) for pair in pairs])
and([tchk, decomp])
end
function _allow_assignment!(expr::Expr)
if expr.head === :kw || expr.head === :(=)
expr.head = :(=)
@assert expr.args[1] isa Symbol
end
end
function MLStyle.pattern_unref(::Type{E}, self::Function, args::AbstractArray) where E
self(:([$(args...)] :: $AbstractVector{$E}))
end
function MLStyle.pattern_unref(::Type{Do}, self::Function, args::AbstractArray)
foreach(_allow_assignment!, args)
effect() do target, scope, ln
ret = Expr(:block)
for arg in args
@switch arg begin
@case :($sym = $value) && if sym isa Symbol
end
sym′ = get(scope, sym) do
nothing
end
bound = true
if sym′ === nothing
sym′ = sym
bound = false
elseif sym′ !== sym
mlstyle_add_deprecation_msg!(
"Deprecated use of pattern Do($sym=$value, ...): \n" *
"Chaning a variable $sym captured during pattern matching.\n" *
"This is dangerous and prevents optimizations, hence got deprecated.\n" *
"There might be similar cases that we couldn't detect from your code.\n" *
"Plesae avoid it! And remember not to change the variable bound during pattern matching, " *
"instead, mutate outer variables.",
)
end
assignment = Expr(:(=), sym′, see_captured_vars(value, scope))
push!(ret.args, assignment)
if !bound
scope[sym] = sym′
end
continue
@case _
push!(ret.args, see_captured_vars(arg, scope))
continue
end
end
ret
end
end
@nospecialize
function MLStyle.pattern_uncall(
::Type{Do},
self::Function,
tparams::AbstractArray,
targs::AbstractArray,
args::AbstractArray,
)
isempty(tparams) || error("A (:) pattern requires no type params.")
isempty(targs) || error("A (:) pattern requires no type arguments.")
MLStyle.pattern_unref(Do, self, args)
end
function MLStyle.pattern_uncall(
::Type{GuardBy},
self::Function,
tparams::AbstractArray,
targs::AbstractArray,
args::AbstractArray,
)
isempty(tparams) || error("A (:) pattern requires no type params.")
isempty(targs) || error("A (:) pattern requires no type arguments.")
@assert length(args) === 1
guard() do target, _, _
:($(args[1])($target))
end
end
function MLStyle.pattern_unref(::Type{Many}, self::Function, args::AbstractArray)
@assert length(args) === 1
arg = args[1]
foreach(_allow_assignment!, args)
let_pat = Expr(:let, Expr(:block, args...), Expr(:block))
old = repr(Expr(:call, :Do, args...))
new = repr(let_pat)
guard() do target, scope, ln
token = gensym("loop token")
iter = gensym("loop iter")
mk_case(x) = Expr(:macrocall, Symbol("@case"), ln, x)
switch_body = quote
$(mk_case(arg))
continue
$(mk_case(:_))
$token = false
break
end
switch_stmt =
Expr(:macrocall, GlobalRef(MLStyle, Symbol("@switch")), ln, iter, switch_body)
final = quote
$token = true
for $iter in $target
$switch_stmt
end
$token
end
see_captured_vars!(final, scope)
end
end
function MLStyle.pattern_uncall(
::Type{Many},
self::Function,
tparams::AbstractArray,
targs::AbstractArray,
args::AbstractArray,
)
isempty(tparams) || error("A (:) pattern requires no type params.")
isempty(targs) || error("A (:) pattern requires no type arguments.")
MLStyle.pattern_unref(Many, self, args)
end
function MLStyle.pattern_unmacrocall(
r_str::typeof(@eval $(Symbol("@", "r_str"))),
self::Function,
args::AbstractArray,
)
@switch args begin
@case [ln, m, s::String]
end
regex = r_str(ln, m, s)
guard() do target, _, _
:($match($regex, $target) !== nothing)
end
end
@specialize
end