lib/nilac/replace_named_functions.rb in nilac-0.0.4.3.9.6 vs lib/nilac/replace_named_functions.rb in nilac-0.0.4.3.9.7

- old
+ new

@@ -1,111 +1,84 @@ require_relative 'read_file_line_by_line' +require_relative 'rollblocks' +require_relative 'replace_strings' + def replace_named_functions(nila_file_contents, temporary_nila_file) - def extract_array(input_array, start_index, end_index) + final_modified_file_contents = nila_file_contents.clone - return input_array[start_index..end_index] + joined_file_contents = final_modified_file_contents.join - end + modified_file_contents = [] - end_locations = [] + rand_number = [] - key_word_locations = [] + nila_file_contents.each do |content| - start_blocks = [] + if replace_strings(content).include?("def ") - end_blocks = [] + rand_num = rand(1..25) - nila_regexp = /(def )/ + while rand_number.include?(rand_num) - named_code_blocks = [] + rand_num = rand(1..10) - for x in 0...nila_file_contents.length + end - current_row = nila_file_contents[x] + modified_file_contents << content.sub("def ","#{" "*rand_num}def ") - if current_row.index(nila_regexp) != nil + rand_number << rand_num - key_word_locations << x + else - elsif current_row.lstrip.eql?("end\n") || current_row.strip.eql?("end") + modified_file_contents << content - end_locations << x - - end - end - unless key_word_locations.empty? + nila_file_contents = modified_file_contents.clone - modified_file_contents = nila_file_contents.dup + possible_function_blocks = nila_file_contents.reject {|element| !replace_strings(element).include?("def ")} - for y in 0...end_locations.length + function_block_locations = [] - current_location = end_locations[y] + possible_function_blocks.each do |block_start| - current_string = modified_file_contents[current_location] + function_block_locations << nila_file_contents.clone.each_index.select {|index| nila_file_contents[index] == block_start } - finder_location = current_location + end - begin + function_block_locations = function_block_locations.flatten - while current_string.index(nila_regexp) == nil + function_block_locations = [0,function_block_locations,-1].flatten.uniq - finder_location -= 1 + file_remains, code_blocks = extract_blocks(function_block_locations,final_modified_file_contents) - current_string = modified_file_contents[finder_location] + modified_code_blocks = [] - end + code_blocks.each do |block| - code_block_begin = finder_location + modified_code_blocks << block.collect {|element| element.gsub("\n\t\nend\n\t\n","}#@$")} - code_block_end = current_location + end - start_blocks << code_block_begin + code_blocks = modified_code_blocks.clone - end_blocks << code_block_end + named_code_blocks = code_blocks.clone.reject {|element| !replace_strings(element[0]).include?("def ")} - code_block_begin_string_split = modified_file_contents[code_block_begin].split(" ") + modified_code_blocks = [] - code_block_begin_string_split[0] = code_block_begin_string_split[0].reverse + code_blocks.each do |block| - code_block_begin_string = code_block_begin_string_split.join(" ") + modified_code_blocks << block.collect {|element| element.gsub("")} - modified_file_contents[code_block_begin] = code_block_begin_string + end - rescue NoMethodError - - puts "Function compilation failed!" + unless named_code_blocks.empty? - end - - end - - final_modified_file_contents = nila_file_contents.dup - - joined_file_contents = final_modified_file_contents.join - - while start_blocks.length != 0 - - top_most_level = start_blocks.min - - top_most_level_index = start_blocks.index(top_most_level) - - matching_level = end_blocks[top_most_level_index] - - named_code_blocks << extract_array(final_modified_file_contents, top_most_level, matching_level) - - start_blocks.delete_at(top_most_level_index) - - end_blocks.delete(matching_level) - - end - codeblock_counter = 1 named_functions = named_code_blocks.dup nested_functions = [] @@ -146,9 +119,12 @@ file_id.close() line_by_line_contents = read_file_line_by_line(temporary_nila_file) - return line_by_line_contents, named_functions, nested_functions + named_functions = named_functions.reject {|element| element.empty?} + nested_functions = nested_functions.reject {|element| element.empty?} + + return line_by_line_contents, named_functions, nested_functions end \ No newline at end of file