@@ -5,82 +5,50 @@ using MLStyle
5
5
6
6
Replace every `f(args...; kwargs..)` with `mycall(f, args...; kwargs...)`
7
7
"""
8
- function callify (mycall , ast)
8
+ function callify (g , ast)
9
9
leaf (x) = x
10
10
function branch (f, head, args)
11
11
default () = Expr (head, map (f, args)... )
12
+
13
+ # Convert `for` to `while`
14
+ if head == :for
15
+ arg1 = args[1 ]
16
+ @assert arg1. head == :(= )
17
+ a, A0 = arg1. args
18
+ A0 = callify (g, A0)
19
+ @gensym temp
20
+ @gensym state
21
+ @gensym A
22
+ return quote
23
+ $ A = $ A0
24
+ $ temp = $ call ($ g, iterate, $ A)
25
+ while $ temp != = nothing
26
+ $ a, $ state = $ temp
27
+ $ (args[2 ])
28
+ $ temp = $ call ($ g, iterate, $ A, $ state)
29
+ end
30
+ end
31
+ end
32
+
12
33
head == :call || return default ()
13
34
14
35
if first (args) == :~ && length (args) == 3
15
36
return default ()
16
37
end
17
38
18
39
# At this point we know it's a function call
19
- length (args) == 1 && return Expr (:call , mycall , first (args))
40
+ length (args) == 1 && return Expr (:call , call, g , first (args))
20
41
21
42
fun = args[1 ]
22
43
arg2 = args[2 ]
23
44
24
45
if arg2 isa Expr && arg2. head == :parameters
25
46
# keyword arguments (try dump(:(f(x,y;a=1, b=2))) to see this)
26
- return Expr (:call , mycall , arg2, fun, map (f, Base. rest (args, 3 ))... )
47
+ return Expr (:call , call, g , arg2, fun, map (f, Base. rest (args, 3 ))... )
27
48
else
28
- return Expr (:call , mycall , map (f, args)... )
49
+ return Expr (:call , call, g , map (f, args)... )
29
50
end
30
51
end
31
52
32
- foldast (leaf, branch)(ast)
53
+ foldast (leaf, branch)(ast) |> MacroTools . flatten
33
54
end
34
-
35
- # struct Provenance{T,S}
36
- # value::T
37
- # sources::S
38
- # end
39
-
40
- # getvalue(p::Provenance) = p.value
41
- # getvalue(x) = x
42
-
43
- # getsources(p::Provenance) = p.sources
44
- # getsources(x) = Set()
45
-
46
- # function trace_provenance(f, args...; kwargs...)
47
- # (newargs, arg_sources) = (getvalue.(args), union(getsources.(args)...))
48
-
49
- # k = keys(kwargs)
50
- # v = values(kwargs)
51
- # newkwargs = NamedTuple{k}(map(getvalue, v))
52
-
53
- # k = keys(kwargs)
54
- # v = values(NamedTuple(kwargs))
55
- # newkwargs = NamedTuple{k}(getvalue.(v))
56
- # kwarg_sources = union(getsources.(args)...)
57
-
58
- # sources = union(arg_sources, kwarg_sources)
59
- # Provenance(f(newargs...; newkwargs), sources)
60
- # end
61
-
62
- # macro call(expr)
63
- # callify(expr)
64
- # end
65
-
66
- # julia> callify(:(f(g(x,y))))
67
- # :(call(f, call(g, x, y)))
68
-
69
- # julia> callify(:(f(x; a=3)))
70
- # :(call(f, x; a = 3))
71
-
72
- # julia> callify(:(a+b))
73
- # :(call(+, a, b))
74
-
75
- # julia> callify(:(call(f,3)))
76
- # :(call(f, 3))
77
-
78
- # f(x) = x+1
79
-
80
- # @call f(2)
81
-
82
- # using SymbolicUtils
83
-
84
- # @syms x::Vector{Float64} i::Int
85
-
86
- # @call getindex(x,i)
0 commit comments