Sha256: 3d669d390bec2cc7b0a5419a4bb39ba1ba64a48bd5058c3a7c807093d6b495db

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 KB

Contents

module Ruby2JS
  class Converter

    # (return
    #   (int 1))

    handle :return do |value=nil|
      if value
        "return #{ parse value }"
      else
        "return"
      end
    end

    EXPRESSIONS = [ :array, :float, :hash, :int, :lvar, :nil, :send, :attr,
      :str, :sym, :dstr, :dsym, :cvar, :ivar, :zsuper, :super, :or, :and ]

    handle :autoreturn do |*statements|
      return if statements == [nil]
      block = statements.dup
      while block.length == 1 and block.first.type == :begin
        block = block.first.children.dup
      end

      if EXPRESSIONS.include? block.last.type 
        block.push s(:return, block.pop)
      elsif block.last.type == :if
        node = block.pop
        if node.children[1] and node.children[2] and
          EXPRESSIONS.include? node.children[1].type and
          EXPRESSIONS.include? node.children[2].type
          node = s(:return, node)
        else
          conditions = [[ node.children.first,
            node.children[1] ? s(:autoreturn, node.children[1]) : nil ]]

          while node.children[2] and node.children[2].type == :if
            node = node.children[2]
            conditions.unshift [ node.children.first,
              node.children[1] ? s(:autoreturn, node.children[1]) : nil ]
          end

          node = node.children[2] ? s(:autoreturn, node.children[2]) : nil

          conditions.each do |condition, statements| 
            node = s(:if, condition, statements, node)
          end
        end
        block.push node
      end

      if block.length == 1
        parse block.first, @state
      else
        parse s(:begin, *block), @state
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby2js-1.9.1 lib/ruby2js/converter/return.rb
ruby2js-1.9.0 lib/ruby2js/converter/return.rb
ruby2js-1.8.0 lib/ruby2js/converter/return.rb
ruby2js-1.7.0 lib/ruby2js/converter/return.rb