lib/ruby2ruby.rb in ruby2ruby-2.0.6 vs lib/ruby2ruby.rb in ruby2ruby-2.0.7
- old
+ new
@@ -27,17 +27,17 @@
##
# Generate ruby code from a sexp.
class Ruby2Ruby < SexpProcessor
- VERSION = "2.0.6" # :nodoc:
+ VERSION = "2.0.7" # :nodoc:
# cutoff for one-liners
LINE_LENGTH = 78
# binary operation messages
- BINARY = [:<=>, :==, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :**]
+ BINARY = [:<=>, :==, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :**, :'!=']
##
# Nodes that represent assignment and probably need () around them.
#
# TODO: this should be replaced with full precedence support :/
@@ -53,10 +53,11 @@
:op_asgn2,
:op_asgn_and,
:op_asgn_or,
:return,
:if, # HACK
+ :rescue,
]
def initialize # :nodoc:
super
@indent = " "
@@ -81,11 +82,14 @@
end
def process_arglist(exp) # custom made node # :nodoc:
code = []
until exp.empty? do
- code << process(exp.shift)
+ arg = exp.shift
+ to_wrap = arg.first == :rescue
+ arg_code = process arg
+ code << (to_wrap ? "(#{arg_code})" : arg_code)
end
code.join ', '
end
def process_args(exp) # :nodoc:
@@ -98,10 +102,12 @@
args << arg
when Sexp then
case arg.first
when :lasgn then
args << process(arg)
+ when :masgn then
+ args << process(arg)
else
raise "unknown arg type #{arg.first.inspect}"
end
else
raise "unknown arg type #{arg.inspect}"
@@ -595,43 +601,46 @@
def process_lvar(exp) # :nodoc:
exp.shift.to_s
end
def process_masgn(exp) # :nodoc:
- lhs = exp.shift
- rhs = exp.empty? ? nil : exp.shift
+ # s(:masgn, s(:array, s(:lasgn, :var), ...), s(:to_ary, <val>, ...))
+ # s(:iter, <call>, s(:args, s(:masgn, :a, :b)), <body>)
- case lhs.first
- when :array then
- lhs.shift
- lhs = lhs.map do |l|
- case l.first
- when :masgn then
- "(#{process(l)})"
- else
- process(l)
+ 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
def process_match(exp) # :nodoc:
"#{process(exp.shift)}"