Sha256: 6c2196e588d06e45cd5c2126115246dddceda5e12d0f0c6897b3bfb7d1957766

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

# encoding: utf-8

class AbstractMapper

  # Optimizes the immutable abstract syntax tree (AST) using comfigurable
  # collection of applicable rules.
  #
  # @api private
  #
  class Optimizer

    # @!attribute [r] rules
    #
    # @return [AbstractMapper::Rules]
    #   The collection of applicable optimization rules
    #
    attr_reader :rules

    # @!scope class
    # @!method new(rules = Rules.new)
    # Creates the optimizer
    #
    # @param [AbstractMapper::Rules] rules
    #   The collection of applicable optimization rules
    #
    # @return [AbstractMapper::Optimizer]

    # @private
    def initialize(rules)
      @rules = rules
      IceNine.deep_freeze(self)
    end

    # Recursively optimizes the AST from root to leafs
    #
    # @param [AbstractMapper::Branch] tree
    #
    # @return [AbstractMapper::Branch]
    #
    def update(tree)
      return tree unless tree.is_a? Branch
      new_tree = tree.update { rules[tree] }
      new_tree.update { new_tree.map(&method(:update)) }
    end

  end # class Optimizer

end # class AbstractMapper

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
abstract_mapper-0.0.2 lib/abstract_mapper/optimizer.rb