Sha256: b18e11741839547d445604d012b9542685e71553dc2cd745ed3da5bcfb5d13b2

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 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
      freeze
    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.rebuild { rules[tree] }
      new_tree.rebuild { 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.1 lib/abstract_mapper/optimizer.rb