-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMyActive.jl
More file actions
150 lines (131 loc) · 3.98 KB
/
Copy pathMyActive.jl
File metadata and controls
150 lines (131 loc) · 3.98 KB
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
# Active patterns
module MyActive
if isdefined(Base, :Experimental) && isdefined(Base.Experimental, Symbol("@compiler_options"))
@eval Base.Experimental.@compiler_options compile=min infer=no optimize=0
end
using MLStyle
using MLStyle.Qualification
using MLStyle.AbstractPatterns
export @active, active_def
@nospecialize
function active_def(P, body, mod::Module, line::LineNumberNode)
inferred_type = @switch P begin
@case :($P::$t_expr)
mod.eval(t_expr)
@case _
Any
end
@switch P begin
@case Expr(
:call,
Expr(:curly, t, type_args...) || t && let type_args = []
end,
arg,
) && if t isa Symbol
end
@case _
error("malformed active pattern definition: $P")
end
# We don't want to redefine the struct
definition = line
parametric = isempty(type_args) ? [] : type_args
prepr = "$P"
token = gensym(prepr)
v_ty = Val{(view, token)}
v_val = Val((view, token))
quote
$definition
(::$v_ty)($(parametric...)) = $arg -> $body
$line
function $MLStyle.pattern_uncall(
t::($t isa Function ? typeof($t) : Type{$t}),
self::Function,
type_params,
type_args,
args,
)
$line
isempty(type_params) || error("A ($t) pattern requires no type params.")
parametric = isempty(type_args) ? [] : type_args
n_args = length(args)
function trans(expr)
Expr(:call, Expr(:call, $v_val, parametric...), expr)
end
function guard2(expr)
if n_args === 0
:($expr isa Bool && $expr)
elseif n_args === 1
expr_s = "$t(x)"
msg =
"invalid use of active patterns: " *
"1-ary view pattern($expr_s) should accept Union{Some{T}, Nothing} " *
"instead of Union{T, Nothing}! " *
"A simple solution is:\n" *
" (@active $expr_s ex) =>\n (@active $expr_s let r=ex; r === nothing? r : Some(r)) end"
:($expr !== nothing && ($expr isa $Some || begin
$error($msg)
end))
else
:($expr isa $Tuple && length($expr) === $n_args)
end
end
extract = if n_args <= 1
function (expr::Any, i::Int, ::Any, ::Any)
expr
end
else
function (expr::Any, i::Int, ::Any, ::Any)
:($expr[$i])
end
end
type_infer(_...) = $inferred_type
comp = $PComp(
$prepr,
type_infer;
view = $SimpleCachablePre(trans),
guard2 = $NoncachablePre(guard2),
)
ps = if n_args === 0
[]
elseif n_args === 1
[self(Expr(:call, Some, args[1]))]
else
[self(e) for e in args]
end
$decons(comp, extract, ps)
end
end
end
"""
Simple active pattern implementation.
You can give a qualifier in the first argument of `@active` to customize its visibility in other modules.
```julia
@active F(x) begin
if x > 0
nothing
else
:ok
end
end
@match -1 begin
F(:ok) => false
_ => true
end # true
@active public IsEven(x) begin
x % 2 === 0
end
@match 4 begin
IsEven() => :ok
_ => :err
end # :ok
```
"""
macro active(qualifier, case, active_body)
deprecate_qualifier_macro(qualifier, __source__)
active_def(case, active_body, __module__, __source__) |> esc
end
macro active(case, active_body)
active_def(case, active_body, __module__, __source__) |> esc
end
@specialize
end