Sha256: 4704e39de550c7be3d797a486cf8c9363dd4aee02beec66ec9ee5d38b03065f7

Contents?: true

Size: 1.79 KB

Versions: 5

Compression:

Stored size: 1.79 KB

Contents

module Ruby2JS
  class Converter

    # (block
    #   (send nil :x)
    #   (args
    #     (arg :a))
    #   (lvar :a))

    handle :block do |call, args, block|

      if
        @state == :statement and args.children.length == 1 and
        call.children.first and call.children.first.type == :begin and
        call.children[1] == :step and
        [:irange, :erange].include? call.children.first.children.first.type
      then
        # convert combinations of range, step and block to a for loop
        var = args.children.first
        expression = call.children.first.children.first
        comp = (expression.type == :irange ? '<=' : '<')
        put "for (var "; parse var; put " = "; parse expression.children.first
        put "; "; parse var; put " #{comp} "; parse expression.children.last
        put "; "; parse var; put " += "; parse call.children[2]; puts ") {"
        scope block
        sput "}"

      elsif
        call.children[0] == nil and call.children[1] == :function and
        call.children[2..-1].all? do |child|
          child.type == :lvar or (child.type == :send and
            child.children.length == 2 and child.children[0] == nil and 
            Symbol === child.children[1])
        end
      then
        # accommodate javascript style syntax: convert function blocks with
        # simple arguments into an anonymous function
        args = call.children[2..-1].map {|arg| s(:arg, arg.children.last)}
        parse @ast.updated(:block, [s(:send, nil, :proc),
          s(:args, *args), block])

      else
        # convert blocks into method calls with an additional argument
        # consisting of an anonymous function
        block ||= s(:begin)
        function = @ast.updated(:def, [nil, args, block])
        parse s(:send, *call.children, function)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ruby2js-2.0.11 lib/ruby2js/converter/block.rb
ruby2js-2.0.10 lib/ruby2js/converter/block.rb
ruby2js-2.0.9 lib/ruby2js/converter/block.rb
ruby2js-2.0.8 lib/ruby2js/converter/block.rb
ruby2js-2.0.7 lib/ruby2js/converter/block.rb