Skip to content

Commit

Permalink
+ Extend process_args to deal with masgn (eg: a.b { |(c, d)| ... }).
Browse files Browse the repository at this point in the history
+ Extend process_masgn to deal with both sexps and var lists.
+ Clean out cruft from process_masgn that I can't reproduce anymore.

[git-p4: depot-paths = "//src/ruby2ruby/dev/": change = 9089]
  • Loading branch information
zenspider committed Dec 14, 2013
1 parent accbc5d commit 7d1e01a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 30 deletions.
65 changes: 35 additions & 30 deletions lib/ruby2ruby.rb
Expand Up @@ -104,6 +104,8 @@ def process_args(exp) # :nodoc:
case arg.first
when :lasgn then
args << process(arg)
when :masgn then
args << process(arg)
else
raise "unknown arg type #{arg.first.inspect}"
end
Expand Down Expand Up @@ -601,40 +603,43 @@ def process_lvar(exp) # :nodoc:
end

def process_masgn(exp) # :nodoc:
lhs = exp.shift
rhs = exp.empty? ? nil : exp.shift

case lhs.first
when :array then
lhs.shift
lhs = lhs.map do |l|
case l.first
when :masgn then
"(#{process(l)})"
else
process(l)
# s(:masgn, s(:array, s(:lasgn, :var), ...), s(:to_ary, <val>, ...))
# s(:iter, <call>, s(:args, s(:masgn, :a, :b)), <body>)

case exp.first
when Sexp then
lhs = exp.shift
rhs = exp.empty? ? nil : exp.shift

case lhs.first
when :array then
lhs.shift # node type
lhs = lhs.map do |l|
case l.first
when :masgn then
"(#{process(l)})"
else
process(l)
end
end
else
raise "no clue: #{lhs.inspect}"
end
when :lasgn then
lhs = [ splat(lhs.last) ]
when :splat then
lhs = [ :"*" ]
else
raise "no clue: #{lhs.inspect}"
end

if context[1] == :iter and rhs then
lhs << splat(rhs[1])
rhs = nil
end

unless rhs.nil? then
t = rhs.first
rhs = process rhs
rhs = rhs[1..-2] if t == :array # FIX: bad? I dunno
return "#{lhs.join(", ")} = #{rhs}"
unless rhs.nil? then
t = rhs.first
rhs = process rhs
rhs = rhs[1..-2] if t == :array # FIX: bad? I dunno
return "#{lhs.join(", ")} = #{rhs}"
else
return lhs.join(", ")
end
when Symbol then # block arg list w/ masgn
result = exp.join ", "
exp.clear
"(#{result})"
else
return lhs.join(", ")
raise "unknown masgn: #{exp.inspect}"
end
end

Expand Down
15 changes: 15 additions & 0 deletions test/test_ruby2ruby.rb
Expand Up @@ -222,6 +222,21 @@ def test_call_arglist_if
util_compare inn, out
end

def test_masgn_block_arg
inn = s(:iter,
s(:call,
s(:nil),
:x),
s(:args, s(:masgn, :a, :b)),
s(:dstr, "",
s(:evstr, s(:lvar, :a)),
s(:str, "="),
s(:evstr, s(:lvar, :b))))
out = 'nil.x { |(a, b)| "#{a}=#{b}" }'

util_compare inn, out
end

def test_masgn_wtf
inn = s(:block,
s(:masgn,
Expand Down

0 comments on commit 7d1e01a

Please sign in to comment.