Skip to content

Commit

Permalink
Merge pull request #92 from davidanthoff/fix-macro-bug
Browse files Browse the repository at this point in the history
Fix translation of @vlplot syntax into Dict calls
  • Loading branch information
davidanthoff committed Jun 6, 2018
2 parents 663c57d + c73e399 commit 2e33303
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions src/dsl_vlplot_macro/dsl_vlplot_macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,31 +138,45 @@ function fix_shortcuts(spec::Dict{String,Any}, positional_key::String)
return new_spec
end

macro vlplot(ex...)
ex = :({$(ex...)})
positional_key = gensym()
new_ex = MacroTools.prewalk(ex) do x
# @show x
if x isa Expr && x.head==:(=) && x.args[2] isa Symbol
return :($(string(x.args[1]))=>$(esc(x.args[2])))
elseif x isa Expr && x.head==:(=)
return :($(string(x.args[1]))=>$(x.args[2]))
elseif x isa Expr && x.head==:cell1d
args = Vector{Expr}(length(x.args))
for (i,v) in enumerate(x.args)
if v isa Expr && v.head==:(=)
args[i] = v
else
args[i] = Expr(:(=), positional_key, v)
end
function convert_curly_style_array(exprs, positional_key)
res = Expr(:vect)

for ex in exprs
if ex isa Expr && ex.head==:cell1d
push!(res.args, :( Dict{String,Any}($(convert_curly_style(ex.args, positional_key)...)) ))
else
push!(res.args, ex)
end
end

return res
end

function convert_curly_style(exprs, positional_key)
new_exprs=[]
for ex in exprs
if ex isa Expr && ex.head==:(=)
if ex.args[2] isa Expr && ex.args[2].head==:cell1d
push!(new_exprs, :( $(string(ex.args[1])) => Dict{String,Any}($(convert_curly_style(ex.args[2].args, positional_key)...)) ))
elseif ex.args[2] isa Expr && ex.args[2].head==:vect
push!(new_exprs, :( $(string(ex.args[1])) => $(convert_curly_style_array(ex.args[2].args, positional_key)) ))
else
push!(new_exprs, :( $(string(ex.args[1])) => $(esc(ex.args[2])) ))
end
return :(Dict{String,Any}($(args...)))
else
return x
push!(new_exprs, :( $(string(positional_key)) => $ex ))
end
end

return :( VegaLite.VLSpec{:plot}(fix_shortcuts($new_ex, $(string(positional_key)))) )
return new_exprs
end

macro vlplot(ex...)
positional_key = gensym()

new_ex = convert_curly_style(ex, positional_key)

return :( VegaLite.VLSpec{:plot}(fix_shortcuts(Dict{String,Any}($(new_ex...)), $(string(positional_key)))) )
end

function Base.:+(a::VLSpec{:plot}, b::VLSpec{:plot})
Expand Down

0 comments on commit 2e33303

Please sign in to comment.