lib/to_source/visitor.rb in to_source-0.2.7 vs lib/to_source/visitor.rb in to_source-0.2.8

- old
+ new

@@ -147,12 +147,13 @@ # @api private # def break(node) emit('break') if node.value.class != Rubinius::AST::NilLiteral - emit(' ') + emit('(') dispatch(node.value) + emit(')') end end # Emit next # @@ -1413,31 +1414,68 @@ # @return [undefined] # # @api private # def and(node) + binary(node, '&&') + end + + # Call block while emitting parantheses + # + # @return [undefined] + # + # @api private + # + def parantheses emit('(') - dispatch(node.left) - emit(' && ') - dispatch(node.right) + yield emit(')') end + # Emit binary operation + # + # @param [Rubnius::AST::Node] node + # + # @param [Symbol] symbol + # the operation symbol + # + # @api private + # + def binary(node, symbol) + parantheses do + parantheses { dispatch(node.left) } + emit(" #{symbol} ") + parantheses { dispatch(node.right) } + end + end + + # Emit binary shortcut + # + # @param [Rubinius::AST::Node] node + # + # @param [Symbol] symbol + # + # @api private + # + def binary_shortcut(node, symbol) + parantheses do + dispatch(node.left) + emit(" #{symbol} ") + parantheses { dispatch(node.right.value) } + end + end + # Emit or # # @param [Rubinius::AST::Node] node # # @return [undefined] # # @api private # def or(node) - emit('(') - dispatch(node.left) - emit(' || ') - dispatch(node.right) - emit(')') + binary(node, '||') end # Emit and operation with assignment # # @param [Rubinius::AST::Node] node @@ -1445,13 +1483,11 @@ # @return [undefined] # # @api private # def op_assign_and(node) - dispatch(node.left) - emit(' && ') - dispatch(node.right) + binary_shortcut(node, :'&&=') end # Emit or operation with assignment # # @param [Rubinius::AST::Node] node @@ -1459,13 +1495,11 @@ # @return [undefined] # # @api private # def op_assign_or(node) - dispatch(node.left) - emit(' || ') - dispatch(node.right) + binary_shortcut(node, :'||=') end alias_method :op_assign_or19, :op_assign_or # Emit toplevel constant # @@ -1843,19 +1877,16 @@ # @api private # def process_binary_operator(node) name = node.name 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) - - emit(')') + parantheses do + parantheses { dispatch(node.receiver) } + emit(" #{name.to_s} ") + parantheses { dispatch(operand) } + end end end end