Sha256: c120ad96c2073ef37f6891345e70da7dbfea91a4793a25ff2bc7e9c8f09df479

Contents?: true

Size: 1.72 KB

Versions: 27

Compression:

Stored size: 1.72 KB

Contents

module Ruby2JS
  class Converter

    # (return
    #   (int 1))

    handle :return do |value=nil|
      if value
        put 'return '; parse value
      else
        put 'return'
      end
    end

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

    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

      return if block == []
      if EXPRESSIONS.include? block.last.type 
        block.push @ast.updated(: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, cstatements| 
            node = s(:if, condition, cstatements, 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

27 entries across 27 versions & 1 rubygems

Version Path
ruby2js-2.1.22 lib/ruby2js/converter/return.rb
ruby2js-2.1.21 lib/ruby2js/converter/return.rb
ruby2js-2.1.20 lib/ruby2js/converter/return.rb
ruby2js-2.1.19 lib/ruby2js/converter/return.rb
ruby2js-2.1.18 lib/ruby2js/converter/return.rb
ruby2js-2.1.17 lib/ruby2js/converter/return.rb
ruby2js-2.1.16 lib/ruby2js/converter/return.rb
ruby2js-2.1.15 lib/ruby2js/converter/return.rb
ruby2js-2.1.14 lib/ruby2js/converter/return.rb
ruby2js-2.1.13 lib/ruby2js/converter/return.rb
ruby2js-2.1.12 lib/ruby2js/converter/return.rb
ruby2js-2.1.11 lib/ruby2js/converter/return.rb
ruby2js-2.1.10 lib/ruby2js/converter/return.rb
ruby2js-2.1.9 lib/ruby2js/converter/return.rb
ruby2js-2.1.8 lib/ruby2js/converter/return.rb
ruby2js-2.1.7 lib/ruby2js/converter/return.rb
ruby2js-2.1.6 lib/ruby2js/converter/return.rb
ruby2js-2.1.5 lib/ruby2js/converter/return.rb
ruby2js-2.1.4 lib/ruby2js/converter/return.rb
ruby2js-2.1.3 lib/ruby2js/converter/return.rb