Skip to content

Commit

Permalink
Fix handling of kwargs in atsign-interpret (fixes #113)
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Mar 7, 2019
1 parent d5fbe82 commit 224caa0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/JuliaInterpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1038,22 +1038,18 @@ end

lower(mod, arg) = false ? expand(arg) : Meta.lower(mod, arg)

# This is a version of gen_call_with_extracted_types, except that is passes back the call expression
# for further processing.
separate_kwargs(args...; kwargs...) = (args, kwargs.data)

# This is a version of InteractiveUtils.gen_call_with_extracted_types, except that is passes back the
# call expression for further processing.
function extract_args(__module__, ex0)
if isa(ex0, Expr)
kws = collect(filter(x->isexpr(x,:kw),ex0.args))
if !isempty(kws)
names = []
values = Tuple(map(x-> begin
push!(names,x.args[1])
x.args[2]
end,kws))
names = Tuple(names)
return Expr(:tuple,:(Core.kwfunc($(ex0.args[1]))),
Expr(:call, NamedTuple{names,typeof(values)}, values),
map(x->isexpr(x, :parameters) ? QuoteNode(x) : x,
filter(x->!isexpr(x, :kw),ex0.args))...)
if any(a->(Meta.isexpr(a, :kw) || Meta.isexpr(a, :parameters)), ex0.args)
return quote
local arg1 = $(ex0.args[1])
local args, kwargs = $separate_kwargs($(ex0.args[2:end]...))
tuple(Core.kwfunc(arg1), kwargs, arg1, args...)
end
elseif ex0.head == :.
return Expr(:tuple, :getproperty, ex0.args...)
elseif ex0.head == :(<:)
Expand Down
4 changes: 4 additions & 0 deletions test/interpret.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ end
@interpret f106c()
@interpret f106d()

# issue #113
f113(;x) = x
@test @interpret(f113(;x=[1,2,3])) == f113(;x=[1,2,3])

# Some expression can appear nontrivial but lower to nothing
@test isa(JuliaInterpreter.prepare_thunk(Main, :(@static if ccall(:jl_get_UNAME, Any, ()) == :NoOS 1+1 end)), Nothing)
@test isa(JuliaInterpreter.prepare_thunk(Main, :(Base.BaseDocs.@kw_str "using")), Nothing)
Expand Down

0 comments on commit 224caa0

Please sign in to comment.