Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle keyword arguments in bounce macro #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/tail.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ mutable struct Bounce
f::Function
end

function trampoline(f, args...)
val = f(args...)
function trampoline(f, args...; kwargs...)
val = f(args...; kwargs...)
while isa(val, Bounce)
val = val.f()
end
Expand Down Expand Up @@ -116,21 +116,24 @@ Tail recursion that doesn't blow the stack.
For simple cases you probably want the much faster [`@rec`](@ref).
"""
macro bounce(def)
def = macroexpand(@__MODULE__, def)
def = macroexpand(@__MODULE__, prettify(def))
@assert isdef(def)
@assert isexpr(def.args[1].args[1], Symbol) # TODO: handle f{T}() = ...
f = namify(def)
f_tramp = trampname(f)
args = def.args[1].args[2:end]
def.args[1].args[1] = f_tramp

@capture(def.args[1], ff_(args__; kwargs__)) || @capture(def.args[1], ff_(args__))
isnothing(kwargs) && (kwargs = [])

calls = Symbol[]
def.args[2] = tailcalls(ex -> (isexpr(ex, :call) && push!(calls, ex.args[1]);
bounce(ex)),
def.args[2])

quote
$([trampdef(call) for call in calls]...)
$def
$f($(args...)) = Lazy.trampoline($f_tramp, $(args...))
$f($(args...);$(kwargs...)) = trampoline($f_tramp, $(args...); $(kwargs...))
end |> esc

end