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

Multi-column assignments #342

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
38 changes: 37 additions & 1 deletion src/parsing_astable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ function is_column_assigment(ex::Expr)
ex.head == :(=) && (get_column_expr(ex.args[1]) !== nothing)
end

is_multi_column_assignment(ex) = false
function is_multi_column_assignment(ex::Expr)
if ex.head == :(=)
exarg = ex.args[1]
if exarg isa Expr && exarg.head == :tuple
return all(!isnothing, get_column_expr.(exarg.args))
else
return false
end
else
return false
end
end

# Taken from MacroTools.jl
# No docstring so assumed unstable
block(ex) = isexpr(ex, :block) ? ex : :($ex;)
Expand All @@ -68,7 +82,7 @@ function get_source_fun_astable(ex; exprflags = deepcopy(DEFAULT_FLAGS))
if is_column_assigment(arg)
lhs = get_column_expr(arg.args[1])
rhs = arg.args[2]
new_ex = replace_syms_astable!(inputs_to_function, lhs_assignments, arg.args[2])
new_ex = replace_syms_astable!(inputs_to_function, lhs_assignments, rhs)
if haskey(inputs_to_function, lhs)
new_lhs = inputs_to_function[lhs]
lhs_assignments[lhs] = new_lhs
Expand All @@ -77,6 +91,28 @@ function get_source_fun_astable(ex; exprflags = deepcopy(DEFAULT_FLAGS))
end

Expr(:(=), new_lhs, new_ex)
elseif is_multi_column_assignment(arg)
@show exarg = arg.args[1]
lhss = get_column_expr.(exarg.args)
rhs = arg.args[2]
new_ex = replace_syms_astable!(inputs_to_function, lhs_assignments, rhs)
new_lhss = Expr(:tuple,)
for lhs in lhss
@show lhs
if haskey(inputs_to_function, lhs)
new_lhs = inputs_to_function[lhs]
lhs_assignments[lhs] = new_lhs
else
new_lhs = addkey!(lhs_assignments, lhs)
end
push!(new_lhss.args, new_lhs)
end
@show new_lhs
@show new_ex
@show new_lhss
t = Expr(:(=), new_lhss, new_ex)
@show MacroTools.prettify(t)
t
else
replace_syms_astable!(inputs_to_function, lhs_assignments, arg)
end
Expand Down