Sha256: 7bfbfa89e6f9aa12c66ccb219c32bac1691f091332272adcd28e8db86364b91d

Contents?: true

Size: 1.46 KB

Versions: 64

Compression:

Stored size: 1.46 KB

Contents

module Ruby2JS
  class Converter

    # (and
    #   (...)
    #   (...))

    # (or
    #   (...)
    #   (...))

    # Note: not handled below
    #   (...))

    handle :and, :or do |left, right|
      type = @ast.type
      op_index = operator_index type

      lgroup   = LOGICAL.include?( left.type ) && 
        op_index < operator_index( left.type )
      lgroup = true if left and left.type == :begin

      rgroup = LOGICAL.include?( right.type ) && 
        op_index < operator_index( right.type )
      rgroup = true if right.type == :begin

      put '(' if lgroup; parse left; put ')' if lgroup
      put (type==:and ? ' && ' : ' || ')
      put '(' if rgroup; parse right; put ')' if rgroup
    end

    # (not
    #   (...))

    handle :not do |expr|

      if expr.type == :send and INVERT_OP.include? expr.children[1]
        parse(s(:send, expr.children[0], INVERT_OP[expr.children[1]],
          expr.children[2]))
      elsif expr.type == :defined?
        parse s(:undefined?, *expr.children)
      elsif expr.type == :or
        parse s(:and, s(:not, expr.children[0]), s(:not, expr.children[1]))
      elsif expr.type == :and
        parse s(:or, s(:not, expr.children[0]), s(:not, expr.children[1]))
      else
        group   = LOGICAL.include?( expr.type ) && 
          operator_index( :not ) < operator_index( expr.type )
        group = true if expr and expr.type == :begin

        put '!'; put '(' if group; parse expr; put ')' if group
      end
    end
  end
end

Version data entries

64 entries across 64 versions & 1 rubygems

Version Path
ruby2js-3.2.0 lib/ruby2js/converter/logical.rb
ruby2js-3.1.2 lib/ruby2js/converter/logical.rb
ruby2js-3.1.1 lib/ruby2js/converter/logical.rb
ruby2js-3.1.0 lib/ruby2js/converter/logical.rb
ruby2js-3.0.15 lib/ruby2js/converter/logical.rb
ruby2js-3.0.14 lib/ruby2js/converter/logical.rb
ruby2js-3.0.13 lib/ruby2js/converter/logical.rb
ruby2js-3.0.12 lib/ruby2js/converter/logical.rb
ruby2js-3.0.11 lib/ruby2js/converter/logical.rb
ruby2js-3.0.10 lib/ruby2js/converter/logical.rb
ruby2js-3.0.9 lib/ruby2js/converter/logical.rb
ruby2js-3.0.8 lib/ruby2js/converter/logical.rb
ruby2js-3.0.7 lib/ruby2js/converter/logical.rb
ruby2js-3.0.6 lib/ruby2js/converter/logical.rb
ruby2js-3.0.5 lib/ruby2js/converter/logical.rb
ruby2js-3.0.4 lib/ruby2js/converter/logical.rb
ruby2js-3.0.3 lib/ruby2js/converter/logical.rb
ruby2js-3.0.2 lib/ruby2js/converter/logical.rb
ruby2js-3.0.1 lib/ruby2js/converter/logical.rb
ruby2js-3.0.0 lib/ruby2js/converter/logical.rb