Sha256: 96c887357643fa78e9e3907989f252c820fbab513cc5da4fb6ae4e27994bcfb8

Contents?: true

Size: 1.75 KB

Versions: 16

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module Unparser
  module Writer
    class Binary
      include Writer, Adamantium

      children :left, :right

      OPERATOR_TOKENS =
        {
          and: '&&',
          or:  '||'
        }.freeze

      KEYWORD_TOKENS =
        {
          and: 'and',
          or:  'or'
        }.freeze

      KEYWORD_SYMBOLS =
        {
          and: :kAND,
          or:  :kOR
        }.freeze

      OPERATOR_SYMBOLS =
        {
          and: :tANDOP,
          or:  :tOROP
        }.freeze

      MAP =
        {
          kAND:   'and',
          kOR:    'or',
          tOROP:  '||',
          tANDOP: '&&'
        }.freeze

      NEED_KEYWORD = %i[return break next].freeze

      private_constant(*constants(false))

      def emit_operator
        emit_with(OPERATOR_TOKENS)
      end

      def symbol_name
        true
      end

      def dispatch
        left_emitter.write_to_buffer
        write(' ', MAP.fetch(effective_symbol), ' ')
        visit(right)
      end

    private

      def effective_symbol
        if NEED_KEYWORD.include?(right.type) || NEED_KEYWORD.include?(left.type)
          return keyword_symbol
        end

        unless left_emitter.symbol_name
          return operator_symbol
        end

        keyword_symbol
      end

      def emit_with(map)
        visit(left)
        write(' ', map.fetch(node.type), ' ')
        visit(right)
      end

      def keyword_symbol
        KEYWORD_SYMBOLS.fetch(node.type)
      end

      def operator_symbol
        OPERATOR_SYMBOLS.fetch(node.type)
      end

      def left_emitter
        emitter(left)
      end
      memoize :left_emitter

      def right_emitter
        emitter(right)
      end
      memoize :right_emitter
    end # Binary
  end # Writer
end # Unparser

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
unparser-0.6.15 lib/unparser/writer/binary.rb
unparser-0.6.14 lib/unparser/writer/binary.rb
unparser-0.6.13 lib/unparser/writer/binary.rb
unparser-0.6.12 lib/unparser/writer/binary.rb
unparser-0.6.10 lib/unparser/writer/binary.rb
unparser-0.6.9 lib/unparser/writer/binary.rb
unparser-0.6.8 lib/unparser/writer/binary.rb
devcycle-ruby-server-sdk-2.0.0 vendor/bundle/ruby/3.0.0/gems/unparser-0.6.7/lib/unparser/writer/binary.rb
unparser-0.6.7 lib/unparser/writer/binary.rb
unparser-0.6.6 lib/unparser/writer/binary.rb
unparser-0.6.5 lib/unparser/writer/binary.rb
unparser-0.6.4 lib/unparser/writer/binary.rb
unparser-0.6.3 lib/unparser/writer/binary.rb
unparser-0.6.2 lib/unparser/writer/binary.rb
unparser-0.6.1 lib/unparser/writer/binary.rb
unparser-0.6.0 lib/unparser/writer/binary.rb