<commit_before>module Atomy
  module Code
    class Block
      def initialize(body, args = [], block = nil)
        @body = body
        @arguments = args
        @block = block
      end

      def bytecode(gen, mod)
        blk = build_block(gen.state.scope, mod)

        gen.push_cpath_top
        gen.find_const :Proc
        gen.create_block(blk)
        gen.send(:__from_block__, 1)
        gen.dup
        gen.send(:lambda_style!, 0)
        gen.pop
      end

      private

      def build_block(scope, mod)
        Atomy::Compiler.generate(mod.file) do |blk|
          # close over the outer scope
          blk.state.scope.parent = scope

          # for now, only allow a fixed set of arguments
          blk.required_args = blk.total_args = @arguments.size

          # this bubbles up to Proc#arity and BlockEnvironment, though it
          # doesn't appear to change actual behavior of the block
          blk.arity = @arguments.size

          # discard extra arguments
          blk.splat_index = @arguments.size

          # create a local for each argument name
          @arguments.each.with_index do |a, i|
            blk.state.scope.new_local(:"arg:#{i}")
          end

          # local for discarded splat args
          blk.state.scope.new_local(:"arg:extra")

          # pattern-match all args
          @arguments.each.with_index do |a, i|
            Assign.new(a, Variable.new(:"arg:#{i}")).bytecode(blk, mod)
          end

          # pattern-match the block arg
          if @block
            Assign.new(@block, PushProc.new).bytecode(blk, mod)
          end

          # build the block's body
          mod.compile(blk, @body)
        end
      end

      class PushProc
        def initialize
        end

        def bytecode(gen, mod)
          gen.push_proc
        end
      end
    end
  end
end<commit_msg>terminology<commit_after>module Atomy
  module Code
    class Block
      def initialize(body, args = [], proc_argument = nil)
        @body = body
        @arguments = args
        @proc_argument = proc_argument
      end

      def bytecode(gen, mod)
        blk = build_block(gen.state.scope, mod)

        gen.push_cpath_top
        gen.find_const :Proc
        gen.create_block(blk)
        gen.send(:__from_block__, 1)
        gen.dup
        gen.send(:lambda_style!, 0)
        gen.pop
      end

      private

      def build_block(scope, mod)
        Atomy::Compiler.generate(mod.file) do |blk|
          # close over the outer scope
          blk.state.scope.parent = scope

          # for now, only allow a fixed set of arguments
          blk.required_args = blk.total_args = @arguments.size

          # this bubbles up to Proc#arity and BlockEnvironment, though it
          # doesn't appear to change actual behavior of the block
          blk.arity = @arguments.size

          # discard extra arguments
          blk.splat_index = @arguments.size

          # create a local for each argument name
          @arguments.each.with_index do |a, i|
            blk.state.scope.new_local(:"arg:#{i}")
          end

          # local for discarded splat args
          blk.state.scope.new_local(:"arg:extra")

          # pattern-match all args
          @arguments.each.with_index do |a, i|
            Assign.new(a, Variable.new(:"arg:#{i}")).bytecode(blk, mod)
          end

          # pattern-match the proc arg
          if @proc_argument
            Assign.new(@proc_argument, PushProc.new).bytecode(blk, mod)
          end

          # build the block's body
          mod.compile(blk, @body)
        end
      end

      class PushProc
        def initialize
        end

        def bytecode(gen, mod)
          gen.push_proc
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08656158161902218, 'fraction_numerical': 0.0010686615014694097, 'mean_word_length': 1.7032490974729242, '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': '7355678', 'n_tokens_mistral': 1118, 'n_tokens_neox': 1051, 'n_words': 295}