<commit_before>module RbPlusPlus
  module Writers
    # Writer that takes a builder and writes out the code in
    # one single file
    class SingleFileWriter < Base

      def initialize(builder, working_dir)
        super

        @includes = []
        @declarations = []
        @registrations = []

        # Keep track of the code generated by the global nodes
        @global_hpp = []
        @global_cpp = []
      end

      def write
        process_code(builder)

#        if Builders::TypesManager.prototypes.length > 0
#          builder.declarations << Builders::TypesManager.prototypes
#        end
#
#        if Builders::TypesManager.body.length > 0
#          # Handle to_from_ruby constructions
#          builder.declarations << Builders::TypesManager.body
#        end

        filename = builder.name
        cpp_file = File.join(working_dir, "#{filename}.rb.cpp")

        File.open(cpp_file, "w+") do |cpp|

          cpp.puts @includes.flatten.compact.uniq.sort.reverse.join("\n")
          cpp.puts @global_hpp.flatten.compact.join("\n")
          cpp.puts @declarations.flatten.compact.join("\n")
          cpp.puts @global_cpp.flatten.compact.join("\n")
          cpp.puts @registrations.flatten.compact.join("\n")
          cpp.puts "}" # Yeah, need to figure this one out

        end
      end

      protected

      # What we do here is to go through the builder heirarchy
      # and push all the code from children up to the parent, 
      # ending up with all the code in the top-level builder
      def process_code(builder)
        builder.write

        @includes << builder.includes
        @declarations << builder.declarations
        @registrations << builder.registrations
        
        builder.global_nodes.each do |g|
          g.write
          @includes << g.includes
          @global_hpp << g.declarations
          @global_cpp << g.registrations
        end

        if builder.has_children?
          builder.nodes.each do |b|
            process_code(b)
          end
        end
      end

    end
  end
end<commit_msg>Don't think I was handlign global nodes correctly, got rid of cruft comments<commit_after>module RbPlusPlus
  module Writers
    # Writer that takes a builder and writes out the code in
    # one single file
    class SingleFileWriter < Base

      def initialize(builder, working_dir)
        super

        @includes = []
        @declarations = []
        @registrations = []

        # Keep track of the code generated by the global nodes
        @global_hpp = []
        @global_cpp = []
      end

      def write
        process_code(builder)

        filename = builder.name
        cpp_file = File.join(working_dir, "#{filename}.rb.cpp")

        File.open(cpp_file, "w+") do |cpp|

          cpp.puts @includes.flatten.compact.uniq.sort.reverse.join("\n")
          cpp.puts @global_hpp.flatten.compact.join("\n")
          cpp.puts @declarations.flatten.compact.join("\n")
          cpp.puts @global_cpp.flatten.compact.join("\n")
          cpp.puts @registrations.flatten.compact.join("\n")
          cpp.puts "}" # Yeah, need to figure this one out

        end
      end

      protected

      # What we do here is to go through the builder heirarchy
      # and push all the code from children up to the parent, 
      # ending up with all the code in the top-level builder
      def process_code(builder)
        builder.write

        @includes << builder.includes
        @declarations << builder.declarations
        @registrations << builder.registrations
        
        process_globals(builder)

        if builder.has_children?
          builder.nodes.each do |b|
            process_code(b)
          end
        end
      end

      def process_globals(builder)
        builder.global_nodes.each do |g|
          process_global_node(g)
        end
      end

      def process_global_node(node)
        node.write
        node.nodes.each do |n|
          process_global_node(n)
        end

        @includes << node.includes
        @global_hpp << node.declarations
        @global_cpp << node.registrations
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.08814296063752716, 'fraction_numerical': 0.0004829751267809708, 'mean_word_length': 1.964924838940587, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 5, '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': '1877394', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1161, 'n_words': 315}