-
Notifications
You must be signed in to change notification settings - Fork 2
/
for-patterns.jl
47 lines (46 loc) · 1.76 KB
/
for-patterns.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
using MLStyle
using MLStyle.MatchCore: mangle
using MLStyle.Infras: @format
def_pattern(Main,
predicate = (@λ begin
:[$_ for $_ in $_] -> true
_ -> false
end),
rewrite = (tag, case, mod) -> begin
@match case begin
:[$expr for $iter in $seq] => begin
function (body)
iter_var = mangle(mod)
produced_elt_var = mangle(mod)
inner_test_var = mangle(mod)
seq_var = mangle(mod)
decons_elt = mk_pattern(iter_var, expr, mod)(iter)
decons_seq = mk_pattern(seq_var, seq, mod)(body)
@format [
seq_var, iter_var, inner_test_var, produced_elt_var,
body, tag, decons_elt, decons_seq, push!] quote
if tag isa Vector
let seq_var = [], inner_test_var = true
for iter_var in tag
produced_elt_var = decons_elt
if produced_elt_var === failed
inner_test_var = false
break
end
push!(seq_var, produced_elt_var)
end
if inner_test_var
decons_seq
else
failed
end
end
else
failed
end
end
end
end
end
end
)