Sha256: fc4a8190e141ef6d8988075ec39dfc293735a24c61489ffb7de6f914abab63f8

Contents?: true

Size: 1.25 KB

Versions: 24

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module Unparser
  class Emitter

    # Emitter for postconditions
    class Post < self
      children :condition, :body

      MAP = {
        while_post: 'while',
        until_post: 'until'
      }.freeze

      handle(*MAP.keys)

    private

      def dispatch
        visit(body)
        write(' ', MAP.fetch(node.type), ' ')
        visit(condition)
      end
    end

    # Emitter for while and until nodes
    class Repetition < self
      MAP = {
        while: 'while',
        until: 'until'
      }.freeze

      handle(*MAP.keys)

      children :condition, :body

    private

      def dispatch
        if postcontrol?
          emit_postcontrol
        else
          emit_normal
        end
      end

      def postcontrol?
        body && local_variable_scope.first_assignment_in?(body, condition)
      end

      def emit_keyword
        write(MAP.fetch(node.type), ' ')
      end

      def emit_normal
        emit_keyword
        visit(condition)
        if body
          emit_body(body)
        else
          nl
        end
        k_end
      end

      def emit_postcontrol
        visit(body)
        ws
        emit_keyword
        visit(condition)
      end

    end # Repetition
  end # Emitter
end # Unparser

Version data entries

24 entries across 24 versions & 2 rubygems

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