Browse files

+ Support more op_asgn nodes. (presidentbeef)

[git-p4: depot-paths = "//src/ruby2ruby/dev/": change = 10686]
  • Loading branch information...
1 parent ef35341 commit 22cd6da3ac0c49da775131b55c65a7db0fe3a39d @zenspider zenspider committed Jun 2, 2016
Showing with 64 additions and 0 deletions.
  1. +31 −0 lib/ruby2ruby.rb
  2. +33 −0 test/test_ruby2ruby.rb
View
31 lib/ruby2ruby.rb
@@ -736,6 +736,16 @@ def process_nth_ref(exp) # :nodoc:
"$#{exp.shift}"
end
+ def process_op_asgn(exp) # :nodoc:
+ # [[:lvar, :x], [:call, nil, :z, [:lit, 1]], :y, :"||"]
+ lhs = process(exp.shift)
+ rhs = process(exp.shift)
+ index = exp.shift
+ op = exp.shift
+
+ "#{lhs}.#{index} #{op}= #{rhs}"
+ end
+
def process_op_asgn1(exp) # :nodoc:
# [[:lvar, :b], [:arglist, [:lit, 1]], :"||", [:lit, 10]]
lhs = process(exp.shift)
@@ -856,6 +866,27 @@ def process_safe_attrasgn(exp) # :nodoc:
end
end
+ def process_safe_op_asgn(exp) # :nodoc:
+ # [[:lvar, :x], [:call, nil, :z, [:lit, 1]], :y, :"||"]
+ lhs = process(exp.shift)
+ rhs = process(exp.shift)
+ index = exp.shift
+ op = exp.shift
+
+ "#{lhs}&.#{index} #{op}= #{rhs}"
+ end
+
+ def process_safe_op_asgn2(exp) # :nodoc:
+ # [[:lvar, :c], :var=, :"||", [:lit, 20]]
+ lhs = process(exp.shift)
+ index = exp.shift.to_s[0..-2]
+ msg = exp.shift
+
+ rhs = process(exp.shift)
+
+ "#{lhs}&.#{index} #{msg}= #{rhs}"
+ end
+
def process_sclass(exp) # :nodoc:
"class << #{process(exp.shift)}\n#{indent(process_block(exp))}\nend"
end
View
33 test/test_ruby2ruby.rb
@@ -412,6 +412,28 @@ def test_safe_call_binary
util_compare inn, out
end
+ def test_safe_op_asgn
+ inn = s(:safe_op_asgn,
+ s(:call, nil, :x),
+ s(:call, nil, :z, s(:lit, 1)),
+ :y,
+ :+)
+
+ out = "x&.y += z(1)"
+ util_compare inn, out
+ end
+
+ def test_safe_op_asgn2
+ inn = s(:safe_op_asgn2,
+ s(:call, nil, :x),
+ :y=,
+ :"||",
+ s(:lit, 1))
+
+ out = "x&.y ||= 1"
+ util_compare inn, out
+ end
+
def test_splat_call
inn = s(:call, nil, :x,
s(:splat,
@@ -599,6 +621,17 @@ def test_nested_rescue_exception2
util_compare inn, out
end
+ def test_op_asgn
+ inn = s(:op_asgn,
+ s(:call, nil, :x),
+ s(:call, nil, :z, s(:lit, 1)),
+ :y,
+ :+)
+
+ out = "x.y += z(1)"
+ util_compare inn, out
+ end
+
def test_rescue_block
inn = s(:rescue,
s(:call, nil, :alpha),

0 comments on commit 22cd6da

Please sign in to comment.