Skip to content

Commit

Permalink
refactor create_function
Browse files Browse the repository at this point in the history
  • Loading branch information
LilithHafner committed Nov 18, 2024
1 parent 20f8277 commit d8bdce1
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/macro_tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@ create_first_function(x) = Returns(x)
create_first_function(x::QuoteNode) = Returns(x.value)
create_first_function(body::Expr) = :(() -> $body)
function create_function(f)
f === :_ && return identity
f === :_ && return nothing
var = gensym()
new, changed = substitute_underscores(f, var)
changed ? :($var -> $new) : f
end
function process_args(exprs)
@nospecialize
first = true
in_kw = false
parameters = Any[]
args = Any[benchmark, exprarray(:parameters, parameters)]
Expand All @@ -70,18 +69,21 @@ function process_args(exprs)
push!(parameters, Expr(:kw, ex.args...))
elseif in_kw
error("Positional argument after keyword argument")
elseif ex === :_
push!(args, nothing)
else
ex2 = extract_interpolations!(interpolations, ex)
if first
push!(args, create_first_function(ex2))
first = false
else
push!(args, create_function(ex2))
end
push!(args, extract_interpolations!(interpolations, ex))
end
end
i = 3
while args[i] === :_ && i <= lastindex(args)
args[i] = nothing
i += 1
end
args[i] = create_first_function(args[i])
i += 1
while i <= lastindex(args)
args[i] = create_function(args[i])
i += 1
end
call = exprarray(:call, args)
esc(isempty(interpolations) ? call : Expr(:let, exprarray(:block, interpolations), Expr(:block, call)))
end

0 comments on commit d8bdce1

Please sign in to comment.