Sha256: 8ccc72ed5503fbf225e9270610005ac0de11396de2bfe78e09b60940bd816f17

Contents?: true

Size: 1.76 KB

Versions: 8

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module Unparser
  module Writer
    class Binary
      include Writer, Adamantium::Flat

      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

8 entries across 8 versions & 1 rubygems

Version Path
unparser-0.5.7 lib/unparser/writer/binary.rb
unparser-0.5.6 lib/unparser/writer/binary.rb
unparser-0.5.5 lib/unparser/writer/binary.rb
unparser-0.5.4 lib/unparser/writer/binary.rb
unparser-0.5.3 lib/unparser/writer/binary.rb
unparser-0.5.2 lib/unparser/writer/binary.rb
unparser-0.5.1 lib/unparser/writer/binary.rb
unparser-0.5.0 lib/unparser/writer/binary.rb