bin/nilac in nilac-0.0.4.3.6 vs bin/nilac in nilac-0.0.4.3.7

- old
+ new

@@ -419,28 +419,36 @@ def replace_strings(input_string) string_counter = 0 - while input_string.include?("\"") + if input_string.count("\"") % 2 == 0 + while input_string.include?("\"") + string_extract = input_string[input_string.index("\"")..input_string.index("\"",input_string.index("\"")+1)] input_string = input_string.sub(string_extract,"--repstring#{string_counter}") string_counter += 1 + end + end - while input_string.include?("'") + if input_string.count("'") % 2 == 0 - string_extract = input_string[input_string.index("'")..input_string.index("'",input_string.index("'")+1)] + while input_string.include?("'") - input_string = input_string.sub(string_extract,"--repstring#{string_counter}") + string_extract = input_string[input_string.index("'")..input_string.index("'",input_string.index("'")+1)] - string_counter += 1 + input_string = input_string.sub(string_extract,"--repstring#{string_counter}") + string_counter += 1 + + end + end return input_string end @@ -820,10 +828,14 @@ input_file_contents = input_file_contents.collect { |element| element.gsub("%=", "modequal") } input_file_contents = input_file_contents.collect { |element| element.gsub("=~", "matchequal") } + input_file_contents = input_file_contents.collect { |element| element.gsub(">=", "greatequal") } + + input_file_contents = input_file_contents.collect { |element| element.gsub("<=", "lessyequal") } + possible_default_values = input_file_contents.dup.reject { |element| (!element.include?("def")) } possible_default_values = possible_default_values.reject { |element| !element.include?("=") } possible_default_values = possible_default_values.reject {|element| !element.index(reject_regexp) == nil} @@ -874,10 +886,14 @@ line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("notequal", "!=") } line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("matchequal", "=~") } + line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("greatequal", ">=") } + + line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("lessyequal", "<=") } + return line_by_line_contents end def get_variables(input_file_contents, temporary_nila_file, *loop_variables) @@ -926,10 +942,14 @@ input_file_contents = input_file_contents.collect { |element| element.gsub("%=", "modequal") } input_file_contents = input_file_contents.collect { |element| element.gsub("=~", "matchequal") } + input_file_contents = input_file_contents.collect { |element| element.gsub(">=", "greatequal") } + + input_file_contents = input_file_contents.collect { |element| element.gsub("<=", "lessyequal") } + modified_file_contents = input_file_contents.clone input_file_contents = input_file_contents.collect {|element| replace_strings(element)} javascript_regexp = /(if |while |for )/ @@ -959,10 +979,11 @@ current_row_split[0] = current_row_split[0].split(".",2)[0].strip if current_row_split[0].include?(".") variables << current_row_split[0] + end input_file_contents[x] = current_row end @@ -995,18 +1016,10 @@ variables += for_loop_variables variables = variables.flatten - if variables.length > 0 - - variable_declaration_string = "var " + variables.uniq.sort.join(", ") + "\n\n" - - line_by_line_contents = [variable_declaration_string, line_by_line_contents].flatten - - end - line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("plusequal", "+=") } line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("minusequal", "-=") } line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("multiequal", "*=") } @@ -1019,10 +1032,14 @@ line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("notequal", "!=") } line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("matchequal", "=~") } + line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("greatequal", ">=") } + + line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("lessyequal", "<=") } + return variables.uniq, line_by_line_contents end def remove_question_marks(input_file_contents, variable_list, temporary_nila_file) @@ -1170,11 +1187,11 @@ index_start, index_end = range_index.split "..." replacement_string = nil - if index_end.strip == "end" + if index_end.strip == "last" replacement_string = split1 + ".slice(#{index_start},#{split}.length)\n" else @@ -1214,13 +1231,13 @@ index_end = "" if index_end.nil? replacement_string = nil - if index_end.strip == "end" + if index_end.strip == "last" - replacement_string = split1 + ".slice(#{index_start})\n" + replacement_string = split1 + ".slice(#{index_start})" + split3.strip + "\n\n" elsif index_end.strip == "" and index_start.strip == "" replacement_string = split1 + ".slice(0)\n" @@ -1735,10 +1752,30 @@ #This method will pickup and declare all the variables inside a function block. In future, this method will be #merged with the get variables method def replace_strings(input_string) + element = input_string.gsub("==", "equalequal") + + element = element.gsub("!=", "notequal") + + element = element.gsub("+=", "plusequal") + + element = element.gsub("-=", "minusequal") + + element = element.gsub("*=", "multiequal") + + element = element.gsub("/=", "divequal") + + element = element.gsub("%=", "modequal") + + element = element.gsub("=~", "matchequal") + + element = element.gsub(">=", "greatequal") + + input_string = element.gsub("<=", "lessyequal") + string_counter = 0 while input_string.include?("\"") string_extract = input_string[input_string.index("\"")..input_string.index("\"",input_string.index("\"")+1)] @@ -1763,11 +1800,11 @@ end input_function_block = input_function_block.collect {|element| replace_strings(element)} - controlregexp = /(if |Euuf |while |def |function |function\()/ + controlregexp = /(if |Euuf |for |while |def |function |function\()/ variables = [] function_name, parameters = input_function_block[0].split("(") @@ -2218,12 +2255,18 @@ ".strip" => ".replace(/^\\s+|\\s+$/g,'')", ".lstrip" => ".replace(/^\\s+/g,\"\")", - ".rstrip" => ".replace(/\\s+$/g,\"\")" + ".rstrip" => ".replace(/\\s+$/g,\"\")", + ".to_s" => ".toString()", + + ".reverse" => ".reverse()", + + ".empty?" => ".length == 0", + } method_map = method_map_replacement.keys method_map_regex = method_map.collect {|name| name.gsub(".","\\.")} @@ -2252,10 +2295,89 @@ return modified_file_contents end + def compile_special_keywords(input_file_contents) + + # This method compiles some Ruby specific keywords to Javascript to make it easy to port + # Ruby code into Javascript + + def replace_strings(input_string) + + string_counter = 0 + + if input_string.count("\"") % 2 == 0 + + while input_string.include?("\"") + + string_extract = input_string[input_string.index("\"")..input_string.index("\"",input_string.index("\"")+1)] + + input_string = input_string.sub(string_extract,"--repstring#{string_counter}") + + string_counter += 1 + + end + + end + + if input_string.count("'") % 2 == 0 + + while input_string.include?("'") + + string_extract = input_string[input_string.index("'")..input_string.index("'",input_string.index("'")+1)] + + input_string = input_string.sub(string_extract,"--repstring#{string_counter}") + + string_counter += 1 + + end + + end + + return input_string + + end + + keyword_replacement_map = { + + "nil" => "null", + + "Array.new" => "Array()" + + } + + special_keywords = keyword_replacement_map.keys + + keyword_map_regex = special_keywords.collect {|name| name.gsub(".","\\.")} + + keyword_map_regex = Regexp.new(keyword_map_regex.join("|")) + + modified_file_contents = input_file_contents.clone + + input_file_contents.each_with_index do |line, index| + + if replace_strings(line).match(keyword_map_regex) + + method_match = line.match(keyword_map_regex).to_a[0] + + if line.split(keyword_map_regex)[0].include?("=") + + line = line.sub(method_match,keyword_replacement_map[method_match]) + + end + + end + + modified_file_contents[index] = line + + end + + return modified_file_contents + + end + def compile_whitespace_delimited_functions(input_file_contents, function_names, temporary_nila_file) def extract(input_string, pattern_start, pattern_end) def find_all_matching_indices(input_string, pattern) @@ -3757,10 +3879,14 @@ input_file_contents = input_file_contents.collect { |element| element.gsub("%=", "modequal") } input_file_contents = input_file_contents.collect { |element| element.gsub("=~", "matchequal") } + input_file_contents = input_file_contents.collect { |element| element.gsub(">=", "greatequal") } + + input_file_contents = input_file_contents.collect { |element| element.gsub("<=", "lessyequal") } + javascript_regexp = /(if |while |for )/ for x in 0...input_file_contents.length current_row = input_file_contents[x] @@ -3899,10 +4025,12 @@ end times_counter = loop.split(".times")[0].lstrip + times_counter = times_counter[1...-1] if times_counter.include?("(") and times_counter.include?(")") + replacement_string = "for (_i = 0, _j = #{times_counter}; _i < _j; _i += 1) {\n\n#{compiled_block}\n\n}" modified_file_contents[input_file_contents.index(original_loop)] = replacement_string end @@ -3951,11 +4079,11 @@ index_counter = starting_counter = input_file_contents.index(starting_line) line = starting_line - until line.strip.eql?("end") + until line.strip.eql?("end") or line.strip.eql?("end)") index_counter += 1 line = input_file_contents[index_counter] @@ -4029,12 +4157,24 @@ end caller_func = loop.split(" blockky ")[0] - replacement_string = "#{caller_func.rstrip}(#{compiled_block.lstrip})" + unless caller_func.rstrip[-1] == "," + replacement_string = "#{caller_func.rstrip}(#{compiled_block.lstrip})" + + else + + caller_func_split = caller_func.split("(") if caller_func.include?("(") + + caller_func_split = caller_func.split(" ",2) if caller_func.include?(" ") + + replacement_string = "#{caller_func_split[0]}(#{caller_func_split[1].strip + compiled_block.lstrip})" + + end + modified_file_contents[input_file_contents.index(original_loop)] = replacement_string end end @@ -4187,11 +4327,11 @@ line_by_line_contents end - def pretty_print_javascript(javascript_file_contents, temporary_nila_file) + def pretty_print_javascript(javascript_file_contents, temporary_nila_file,declarable_variables) def reset_tabs(input_file_contents) #This method removes all the predefined tabs to avoid problems in #later parts of the beautifying process. @@ -4351,11 +4491,11 @@ def roll_blocks(input_file_contents, code_block_starting_locations) if !code_block_starting_locations.empty? - controlregexp = /(if |for |while |\(function\(|= function\(|((=|:)\s+\{))/ + controlregexp = /(if |for |while |,function\(|\(function\(|= function\(|((=|:)\s+\{))/ code_block_starting_locations = [0, code_block_starting_locations, -1].flatten possible_blocks = [] @@ -4481,11 +4621,11 @@ end def replace_ignored_words(input_string) - ignorable_keywords = [/if/, /while/, /function/] + ignorable_keywords = [/if/, /while/, /function/,/function/] dummy_replacement_words = ["eeuuff", "whaalesskkey", "conffoolotion"] dummy_replacement_words.each_with_index do |word, index| @@ -4497,10 +4637,18 @@ end javascript_regexp = /(if |for |while |\(function\(|= function\(|((=|:)\s+\{))/ + if declarable_variables.length > 0 + + declaration_string = "var " + declarable_variables.flatten.uniq.sort.join(", ") + ";\n\n" + + javascript_file_contents = [declaration_string,javascript_file_contents].flatten + + end + javascript_file_contents = javascript_file_contents.collect { |element| element.sub("Euuf", "if") } javascript_file_contents = javascript_file_contents.collect { |element| element.sub("whaaleskey", "while") } javascript_file_contents = reset_tabs(javascript_file_contents) @@ -4780,18 +4928,22 @@ file_contents,named_functions = compile_arrays(file_contents, named_functions, temp_file) file_contents = compile_strings(file_contents) + list_of_variables, file_contents = get_variables(file_contents, temp_file,loop_vars) + file_contents, function_names = compile_named_functions(file_contents, named_functions, nested_functions, temp_file) - list_of_variables, file_contents = get_variables(file_contents, temp_file,loop_vars+function_names) + func_names = function_names.dup file_contents, ruby_functions = compile_custom_function_map(file_contents) file_contents = compile_ruby_methods(file_contents) + file_contents = compile_special_keywords(file_contents) + function_names << ruby_functions list_of_variables += loop_vars file_contents = compile_whitespace_delimited_functions(file_contents, function_names, temp_file) @@ -4800,10 +4952,10 @@ file_contents = add_semicolons(file_contents) file_contents = compile_comments(file_contents, comments, temp_file) - file_contents = pretty_print_javascript(file_contents, temp_file) + file_contents = pretty_print_javascript(file_contents, temp_file,list_of_variables+func_names) file_contents = compile_operators(file_contents) output_javascript(file_contents, output_js_file, temp_file)