lib/to_source/visitor.rb in to_source-0.2.4 vs lib/to_source/visitor.rb in to_source-0.2.5
- old
+ new
@@ -146,10 +146,14 @@
#
# @api private
#
def break(node)
emit('break')
+ if node.value.class != Rubinius::AST::NilLiteral
+ emit(' ')
+ dispatch(node.value)
+ end
end
# Emit next
#
# @param [Rubinius::AST::Node] node
@@ -1045,24 +1049,53 @@
else
false
end
end
+ UNARY_OPERATORS = %w(
+ ! ~ -@ +@
+ ).map(&:to_sym).to_set.freeze
+
+ UNARY_MAPPING = {
+ :-@ => :-,
+ :+@ => :+,
+ }.freeze
+
+ # Emit unary operator
+ #
+ # @param [Rubinius::AST::Node] node
+ #
+ # @return [true]
+ # if node was emitted
+ #
+ # @return [false]
+ # otherwise
+ #
+ # @api private
+ #
+ def unary_operator(node)
+ name = node.name
+
+ if UNARY_OPERATORS.include?(name)
+ emit(UNARY_MAPPING.fetch(name, name))
+ dispatch(node.receiver)
+ return true
+ end
+
+ false
+ end
+
# Emit send node
#
# @param [Rubinius::AST::Node] node
#
# @return [undefined]
#
# @api private
#
def send(node)
- if node.name == :'!'
- emit('!')
- dispatch(node.receiver)
- return
- end
+ return if unary_operator(node)
if receiver(node)
emit('.')
end
@@ -1139,14 +1172,11 @@
# @return [undefined]
#
# @api private
#
def element_reference(node)
- unless node.receiver.is_a?(Rubinius::AST::Self)
- dispatch(node.receiver)
- end
-
+ dispatch(node.receiver)
arguments(node,'[',']')
end
# Emit send with arguments
#
@@ -1376,13 +1406,15 @@
# @return [undefined]
#
# @api private
#
def and(node)
+ emit('(')
dispatch(node.left)
emit(' && ')
dispatch(node.right)
+ emit(')')
end
# Emit or
#
# @param [Rubinius::AST::Node] node
@@ -1390,13 +1422,15 @@
# @return [undefined]
#
# @api private
#
def or(node)
+ emit('(')
dispatch(node.left)
emit(' || ')
dispatch(node.right)
+ emit(')')
end
# Emit and operation with assignment
#
# @param [Rubinius::AST::Node] node
@@ -1609,10 +1643,11 @@
empty = required.empty?
if defaults
emit(', ') unless empty
dispatch(node.defaults)
+ empty = false
end
if splat
emit(', ') unless empty
emit('*')
@@ -1805,14 +1840,15 @@
return unless OPERATORS.include?(name)
return if node.arguments.array.length != 1
operand = node.arguments.array[0]
+ emit('(')
dispatch(node.receiver)
emit(" #{name.to_s} ")
dispatch(operand)
- self
+ emit(')')
end
end
end