<commit_before>
require 'shin/utils/mangler'

module Shin
  class FastMutator
    include Shin::Utils::Mangler
    include Shin::Utils::Mimic

    DEBUG = ENV['FAST_MUTATOR_DEBUG']

    def initialize(compiler, mod)
      @compiler = compiler
      @mod = mod
    end

    def expand(invoc, info, context)
      debug "Expanding #{invoc}" if DEBUG

      deps = @compiler.collect_deps(info[:module])

      all_in_cache = deps.keys.all? { |slug| @compiler.modules.include?(slug) }
      unless all_in_cache
        raise "Not all deps in cache: #{deps.keys}"
      end

      deps.each do |slug, dep|
        Shin::NsParser.new(dep).parse unless dep.ns
        Shin::Mutator.new(@compiler, dep).mutate unless dep.ast2
        Shin::Translator.new(@compiler, dep).translate unless dep.jst
        Shin::Generator.new(dep).generate unless dep.code
      end

      deps.each do |slug, dep|
        context.load(slug)
      end

      all_loaded = deps.keys.all? { |slug| context.spec_loaded?(context.parse_spec(slug)) }
      unless all_loaded
        raise "Not all loaded!"
      end

      macro_slug = info[:module].slug
      debug "macro_slug: #{macro_slug}" if DEBUG

      macro_name = invoc.inner.first.value
      debug "macro_name: #{macro_name}" if DEBUG

      macro_sexp = info[:macro]
      debug "macro_sexp: #{macro_sexp}" if DEBUG

      macro_func = context.context['$kir']['modules'][macro_slug]['exports'][mangle(macro_name)]
      unless macro_func
        raise "Could not retrieve macro_func"
      end
      debug "macro_func: #{macro_func}" if DEBUG

      macro_args = invoc.inner.drop(1).to_a
      debug "macro_args: #{macro_args.join(", ")}" if DEBUG

      macro_gifted_args = macro_args.map { |arg| unwrap(arg) }
      debug "macro_gifted_args: #{macro_gifted_args.join(", ")}" if DEBUG

      macro_ret = macro_func.call(*macro_gifted_args)
      debug "macro_ret: #{macro_ret}" if DEBUG

      macro_ret_unquoted = unquote(macro_ret, invoc.token)
      debug "unquoted macro_ret: #{macro_ret_unquoted}" if DEBUG
<commit_msg>Use eval to retrieve macro_func in one fell swoop.<commit_after>
require 'shin/utils/mangler'

module Shin
  class FastMutator
    include Shin::Utils::Mangler
    include Shin::Utils::Mimic

    DEBUG = ENV['FAST_MUTATOR_DEBUG']

    def initialize(compiler, mod)
      @compiler = compiler
      @mod = mod
    end

    def expand(invoc, info, context)
      debug "Expanding #{invoc}" if DEBUG

      deps = @compiler.collect_deps(info[:module])
      all_in_cache = deps.keys.all? { |slug| @compiler.modules.include?(slug) }
      unless all_in_cache
        raise "Not all deps in cache: #{deps.keys}"
      end

      deps.each do |slug, dep|
        Shin::NsParser.new(dep).parse unless dep.ns
        Shin::Mutator.new(@compiler, dep).mutate unless dep.ast2
        Shin::Translator.new(@compiler, dep).translate unless dep.jst
        Shin::Generator.new(dep).generate unless dep.code
      end

      deps.each do |slug, dep|
        context.load(slug)
      end

      all_loaded = deps.keys.all? { |slug| context.spec_loaded?(context.parse_spec(slug)) }
      unless all_loaded
        raise "Not all loaded!"
      end

      macro_slug = info[:module].slug
      debug "macro_slug: #{macro_slug}" if DEBUG

      macro_name = invoc.inner.first.value
      debug "macro_name: #{macro_name}" if DEBUG

      macro_sexp = info[:macro]
      debug "macro_sexp: #{macro_sexp}" if DEBUG

      macro_func = context.eval("$kir.modules['#{macro_slug}'].exports.#{mangle(macro_name)}")
      unless macro_func
        raise "Could not retrieve macro_func"
      end
      debug "macro_func: #{macro_func}" if DEBUG

      macro_args = invoc.inner.drop(1).to_a
      debug "macro_args: #{macro_args.join(", ")}" if DEBUG

      macro_gifted_args = macro_args.map { |arg| unwrap(arg) }
      debug "macro_gifted_args: #{macro_gifted_args.join(", ")}" if DEBUG

      macro_ret = macro_func.call(*macro_gifted_args)
      debug "macro_ret: #{macro_ret}" if DEBUG

      macro_ret_unquoted = unquote(macro_ret, invoc.token)
      debug "unquoted macro_ret: #{macro_ret_unquoted}" if DEBUG

{'fraction_non_alphanumeric': 0.11915098890496864, 'fraction_numerical': 0.000964785335262904, 'mean_word_length': 3.0106382978723403, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3677354', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1387, 'n_words': 308}