Sha256: b48717776fc76524bd8837abdc91add422bffbf449ca827db47975437d5b5e2c

Contents?: true

Size: 1.79 KB

Versions: 4

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, :lambda),
          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

4 entries across 4 versions & 1 rubygems

Version Path
ruby2js-2.0.6 lib/ruby2js/converter/block.rb
ruby2js-2.0.5 lib/ruby2js/converter/block.rb
ruby2js-2.0.4 lib/ruby2js/converter/block.rb
ruby2js-2.0.3 lib/ruby2js/converter/block.rb