<commit_before>module Alf
  module Engine
    class Compilable

      def initialize(cog)
        @cog = cog
        @parser = Lang::Lispy.new
      end

      def expr
        @cog.expr
      end

      ### main

      def to_cog(expr)
        send(expr.class.rubycase_name, expr)
      end

      ### non relational

      def autonum(expr, traceability = expr)
        Autonum.new(@cog, expr.as, traceability)
      end

      def clip(expr, traceability = expr)
        Clip.new(@cog, expr.attributes, expr.allbut, traceability)
      end

      def coerce(expr, traceability = expr)
        Coerce.new(@cog, expr.coercions, traceability)
      end

      def compact(expr, traceability = expr)
        Compact.new(@cog, traceability)
      end

      def defaults(expr, traceability = expr)
        compiled = Defaults.new(@cog, expr.defaults, traceability)
        if expr.strict
          clipping = @parser.clip(expr, expr.defaults.to_attr_list)
          compiled = compiled.to_compilable.clip(clipping, traceability)
        end
        compiled
      end

      def generator(expr, traceability = expr)
        Generator.new(expr.as, 1, 1, expr.size, traceability)
      end

      def sort(expr, traceability = expr)
        compiled = @cog
        unless @cog.orderedby?(expr.ordering)
          compiled = Sort.new(compiled, expr.ordering, traceability)
        end
        compiled
      end

      def type_safe(expr, traceability = expr)
        checker = TypeCheck.new(expr.heading, expr.strict)
        TypeSafe.new(@cog, checker, traceability)
      end

      ### relational

      def extend(expr, traceability = expr)
        SetAttr.new(@cog, expr.ext, traceability)
      end

      def frame(expr, traceability = expr)
        ordering = expr.full_ordering rescue expr.ordering
        #
        compiled = sort(@parser.sort(expr, ordering))
        compiled = Take.new(compiled, expr.offset, expr.limit, traceability)
        #
        compiled
      end

      def group(expr, traceability = expr)
        Group::Hash.new(@cog, expr.attributes, expr.as, expr.allbut, traceability)
      end

      def infer_heading(expr, traceability = expr)
        InferHeading.new(@cog, traceability)
      end

      def intersect(expr, traceability = expr)
        Join::Hash.new(@cog, expr.right.to_cog, traceability)
      end

      def join(expr, traceability = expr)
        Join::Hash.new(@cog, expr.right.to_cog, traceability)
      end

      def matching(expr, traceability = expr)
        Semi::Hash.new(@cog, expr.right.to_cog, true, traceability)
      end

      def minus(expr, traceability = expr)
        Semi::Hash.new(@cog, expr.right.to_cog, false, traceability)
      end

      def not_matching(expr, traceability = expr)
        Semi::Hash.new(@cog, expr.right.to_cog, false, traceability)
      end

      def page(expr, traceability = expr)
        index, size = expr.page_index, expr.page_size
        #
        ordering = expr.full_ordering rescue expr.ordering
        ordering = ordering.reverse if index < 0
        #
        compiled = sort(@parser.sort(expr, ordering))
        compiled = Take.new(compiled, (index.abs - 1) * size, size, traceability)
        #
        compiled
      end

      def project(expr, traceability = expr)
        preserving = expr.key_preserving? rescue false
        #
        compiled = Clip.new(@cog, expr.attributes, expr.allbut, traceability)
        compiled = Compact.new(compiled, traceability) unless preserving
        #
        compiled
      end

      def quota(expr, traceability = expr)
        compiled = sort(@parser.sort(expr, expr.by.to_ordering + expr.order))
        compiled = Quota::Cesure.new(compiled, expr.by, expr.summarization, traceability)
        #
        compiled
      end

      def rank(expr, traceability = expr)
        compiled = sort(@parser.sort(expr, expr.order))
        compiled = Rank::Cesure.new(compiled, expr.order, expr.as, traceability)
        #
        compiled
      end

      def rename(expr, traceability = expr)
        Rename.new(@cog, expr.renaming, traceability)
      end

      def restrict(expr, traceability = expr)
        Filter.new(@cog, expr.predicate, traceability)
      end

      def summarize(expr, traceability = expr)
        clazz = expr.allbut ? Summarize::Hash : Summarize::Cesure
        #
        compiled = @cog
        unless expr.allbut
          compiled = compiled.to_compilable.sort(@parser.sort(expr, expr.by.to_ordering))
        end
        compiled = clazz.new(compiled, expr.by, expr.summarization, expr.allbut, traceability)
        #
        compiled
      end

      def ungroup(expr, traceability = expr)
        Ungroup.new(@cog, expr.attribute, traceability)
      end

      def union(expr, traceability = expr)
        compiled = Concat.new([@cog, expr.right.to_cog], traceability)
        compiled = Compact.new(compiled, expr)
        #
        compiled
      end

      def unwrap(expr, traceability = expr)
        Unwrap.new(@cog, expr.attribute, traceability)
      end

      def wrap(expr, traceability = expr)
        Wrap.new(@cog, expr.attributes, expr.as, expr.allbut, traceability)
      end

    end # class Compilable
  end # module Engine
end # module Alf<commit_msg>Add cog and parser attr_readers in Engine::Compilable.<commit_after>module Alf
  module Engine
    class Compilable

      def initialize(cog)
        @cog = cog
        @parser = Lang::Lispy.new
      end
      attr_reader :cog
      attr_reader :parser

      def expr
        cog.expr
      end

      ### main

      def to_cog(expr)
        send(expr.class.rubycase_name, expr)
      end

      ### non relational

      def autonum(expr, traceability = expr)
        Autonum.new(self.cog, expr.as, traceability)
      end

      def clip(expr, traceability = expr)
        Clip.new(self.cog, expr.attributes, expr.allbut, traceability)
      end

      def coerce(expr, traceability = expr)
        Coerce.new(self.cog, expr.coercions, traceability)
      end

      def compact(expr, traceability = expr)
        Compact.new(self.cog, traceability)
      end

      def defaults(expr, traceability = expr)
        compiled = Defaults.new(self.cog, expr.defaults, traceability)
        if expr.strict
          clipping = parser.clip(expr, expr.defaults.to_attr_list)
          compiled = compiled.to_compilable.clip(clipping, traceability)
        end
        compiled
      end

      def generator(expr, traceability = expr)
        Generator.new(expr.as, 1, 1, expr.size, traceability)
      end

      def sort(expr, traceability = expr)
        compiled = self.cog
        unless self.cog.orderedby?(expr.ordering)
          compiled = Sort.new(compiled, expr.ordering, traceability)
        end
        compiled
      end

      def type_safe(expr, traceability = expr)
        checker = TypeCheck.new(expr.heading, expr.strict)
        TypeSafe.new(self.cog, checker, traceability)
      end

      ### relational

      def extend(expr, traceability = expr)
        SetAttr.new(self.cog, expr.ext, traceability)
      end

      def frame(expr, traceability = expr)
        ordering = expr.full_ordering rescue expr.ordering
        #
        compiled = sort(parser.sort(expr, ordering))
        compiled = Take.new(compiled, expr.offset, expr.limit, traceability)
        #
        compiled
      end

      def group(expr, traceability = expr)
        Group::Hash.new(self.cog, expr.attributes, expr.as, expr.allbut, traceability)
      end

      def infer_heading(expr, traceability = expr)
        InferHeading.new(self.cog, traceability)
      end

      def intersect(expr, traceability = expr)
        Join::Hash.new(self.cog, expr.right.to_cog, traceability)
      end

      def join(expr, traceability = expr)
        Join::Hash.new(self.cog, expr.right.to_cog, traceability)
      end

      def matching(expr, traceability = expr)
        Semi::Hash.new(self.cog, expr.right.to_cog, true, traceability)
      end

      def minus(expr, traceability = expr)
        Semi::Hash.new(self.cog, expr.right.to_cog, false, traceability)
      end

      def not_matching(expr, traceability = expr)
        Semi::Hash.new(self.cog, expr.right.to_cog, false, traceability)
      end

      def page(expr, traceability = expr)
        index, size = expr.page_index, expr.page_size
        #
        ordering = expr.full_ordering rescue expr.ordering
        ordering = ordering.reverse if index < 0
        #
        compiled = sort(parser.sort(expr, ordering))
        compiled = Take.new(compiled, (index.abs - 1) * size, size, traceability)
        #
        compiled
      end

      def project(expr, traceability = expr)
        preserving = expr.key_preserving? rescue false
        #
        compiled = Clip.new(self.cog, expr.attributes, expr.allbut, traceability)
        compiled = Compact.new(compiled, traceability) unless preserving
        #
        compiled
      end

      def quota(expr, traceability = expr)
        compiled = sort(parser.sort(expr, expr.by.to_ordering + expr.order))
        compiled = Quota::Cesure.new(compiled, expr.by, expr.summarization, traceability)
        #
        compiled
      end

      def rank(expr, traceability = expr)
        compiled = sort(parser.sort(expr, expr.order))
        compiled = Rank::Cesure.new(compiled, expr.order, expr.as, traceability)
        #
        compiled
      end

      def rename(expr, traceability = expr)
        Rename.new(self.cog, expr.renaming, traceability)
      end

      def restrict(expr, traceability = expr)
        Filter.new(self.cog, expr.predicate, traceability)
      end

      def summarize(expr, traceability = expr)
        clazz = expr.allbut ? Summarize::Hash : Summarize::Cesure
        #
        compiled = self.cog
        unless expr.allbut
          compiled = compiled.to_compilable.sort(parser.sort(expr, expr.by.to_ordering))
        end
        compiled = clazz.new(compiled, expr.by, expr.summarization, expr.allbut, traceability)
        #
        compiled
      end

      def ungroup(expr, traceability = expr)
        Ungroup.new(self.cog, expr.attribute, traceability)
      end

      def union(expr, traceability = expr)
        compiled = Concat.new([self.cog, expr.right.to_cog], traceability)
        compiled = Compact.new(compiled, expr)
        #
        compiled
      end

      def unwrap(expr, traceability = expr)
        Unwrap.new(self.cog, expr.attribute, traceability)
      end

      def wrap(expr, traceability = expr)
        Wrap.new(self.cog, expr.attributes, expr.as, expr.allbut, traceability)
      end

    end # class Compilable
  end # module Engine
end # module Alf
{'fraction_non_alphanumeric': 0.09776614636881951, 'fraction_numerical': 0.0007477334330311244, 'mean_word_length': 2.2582216808769795, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '961760', 'n_tokens_mistral': 3302, 'n_tokens_neox': 3163, 'n_words': 739}