Sha256: 6f397b2e85b19f91a35acca5254567f04f7fc0282c1e8996fdcff54cebd47ca4

Contents?: true

Size: 1.54 KB

Versions: 21

Compression:

Stored size: 1.54 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
      left     = parse left
      left     = "(#{ left })" if lgroup

      rgroup = LOGICAL.include?( right.type ) && 
        op_index < operator_index( right.type )
      rgroup = true if right.type == :begin
      right    = parse right
      right    = "(#{ right })" if rgroup

      "#{ left } #{ type==:and ? '&&' : '||' } #{ right }"
    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
        expr     = parse expr
        expr     = "(#{ expr })" if group

        "!#{ expr }"
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
ruby2js-1.15.1 lib/ruby2js/converter/logical.rb
ruby2js-1.15.0 lib/ruby2js/converter/logical.rb
ruby2js-1.14.1 lib/ruby2js/converter/logical.rb
ruby2js-1.14.0 lib/ruby2js/converter/logical.rb
ruby2js-1.13.1 lib/ruby2js/converter/logical.rb
ruby2js-1.13.0 lib/ruby2js/converter/logical.rb
ruby2js-1.12.2 lib/ruby2js/converter/logical.rb
ruby2js-1.12.1 lib/ruby2js/converter/logical.rb
ruby2js-1.12.0 lib/ruby2js/converter/logical.rb
ruby2js-1.11.1 lib/ruby2js/converter/logical.rb
ruby2js-1.11.0 lib/ruby2js/converter/logical.rb
ruby2js-1.10.0 lib/ruby2js/converter/logical.rb
ruby2js-1.9.3 lib/ruby2js/converter/logical.rb
ruby2js-1.9.2 lib/ruby2js/converter/logical.rb
ruby2js-1.9.1 lib/ruby2js/converter/logical.rb
ruby2js-1.9.0 lib/ruby2js/converter/logical.rb
ruby2js-1.8.0 lib/ruby2js/converter/logical.rb
ruby2js-1.7.0 lib/ruby2js/converter/logical.rb
ruby2js-1.6.0 lib/ruby2js/converter/logical.rb
ruby2js-1.5.0 lib/ruby2js/converter/logical.rb