bin/nilac in nilac-0.0.4.2.6 vs bin/nilac in nilac-0.0.4.2.8
- old
+ new
@@ -385,23 +385,23 @@
string_split = line.split(interpol)
if string_split[1].eql?("\"\n")
- replacement_string = "\" + " + interpol[2...-1]
+ replacement_string = "\" + " + "(#{interpol[2...-1]})"
modified_file_contents[index] = modified_file_contents[index].sub(interpol+"\"", replacement_string)
elsif string_split[1].eql?("\")\n")
- replacement_string = "\" + " + interpol[2...-1]
+ replacement_string = "\" + " + "(#{interpol[2...-1]})"
- modified_file_contents[index] = modified_file_contents[index].sub(interpol+"\"", replacement_string)
+ modified_file_contents[index] = modified_file_contents[index].sub(interpol + "\"", replacement_string)
else
- replacement_string = "\" + " + interpol[2...-1] + " + \""
+ replacement_string = "\"" + " + " + "(#{interpol[2...-1]})" + " + \""
modified_file_contents[index] = modified_file_contents[index].sub(interpol, replacement_string)
end
@@ -684,11 +684,11 @@
param, value = paramvalue.split("=")
replacement_parameters << param.lstrip.rstrip
- replacement_string = replacement_string + "\n" + "if (#{param.lstrip.rstrip} == null) {\n #{paramvalue.lstrip.rstrip}\n}\n" +"\n"
+ replacement_string = replacement_string + "\n" + "if (#{param.lstrip.rstrip} equequ null) {\n #{paramvalue.lstrip.rstrip}\n}\n" +"\n"
end
return replacement_string, default_value_parameters, replacement_parameters
@@ -732,11 +732,11 @@
return line_by_line_contents
end
- def get_variables(input_file_contents, temporary_nila_file)
+ def get_variables(input_file_contents, temporary_nila_file, *loop_variables)
variables = []
input_file_contents = input_file_contents.collect { |element| element.gsub("==", "equalequal") }
@@ -752,15 +752,17 @@
input_file_contents = input_file_contents.collect { |element| element.gsub("%=", "modequal") }
input_file_contents = input_file_contents.collect { |element| element.gsub("=~", "matchequal") }
+ javascript_regexp = /(if |while |for )/
+
for x in 0...input_file_contents.length
current_row = input_file_contents[x]
- if current_row.include?("=") and !current_row.include?("def")
+ if current_row.include?("=") and current_row.index(javascript_regexp) == nil
current_row = current_row.rstrip + "\n"
current_row_split = current_row.split("=")
@@ -794,10 +796,14 @@
file_id.close()
line_by_line_contents = read_file_line_by_line(temporary_nila_file)
+ variables += 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
@@ -1144,23 +1150,27 @@
def compile_hashes(input_file_contents,temporary_nila_file)
def compile_multiline_hashes(input_file_contents,temporary_nila_file)
+ javascript_regexp = /(if |while |for |function |function\()/
+
possible_hashes = input_file_contents.reject { |element| !element.include?("{") }
possible_multiline_hashes = possible_hashes.reject { |element| element.include?("}") }
+ possible_multiline_hashes = possible_multiline_hashes.reject {|element| element.index(javascript_regexp) != nil}
+
multiline_hashes = []
possible_multiline_hashes.each do |starting_line|
index = input_file_contents.index(starting_line)
line = starting_line
- until line.include?("}")
+ until line.include?("}\n")
index += 1
line = input_file_contents[index]
@@ -1198,16 +1208,52 @@
end
def compile_inline_hashes(input_file_contents)
- modified_file_contents = input_file_contents.clone.collect {|element| element.gsub(/(#|%)\w?\{/,"innerrii0opol115")}
+ def replace_strings(input_string)
- possible_inline_hashes = modified_file_contents.reject {|element| !element.include?("{")}
+ string_counter = 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
+
+ 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
+
+ return input_string
+
+ end
+
+ javascript_regexp = /(if |while |for |function |function\(|%[qQ]*\{)/
+
+ modified_file_contents = input_file_contents.clone.collect {|element| replace_strings(element)}
+
+ possible_inline_hashes = modified_file_contents.reject {|element| element.count("{") != 1}
+
+ possible_inline_hashes = possible_inline_hashes.reject {|element| element.count("}") != 1}
+
+ possible_inline_hashes = possible_inline_hashes.reject {|element| element.index(javascript_regexp) != nil}
+
possible_inline_hashes.each do |hash|
+ hash = input_file_contents[modified_file_contents.index(hash)]
+
hash_extract = hash[hash.index("{")..hash.index("}")]
contents = hash_extract[1...-1].split(",")
hash_contents = []
@@ -1388,10 +1434,34 @@
return file_contents
end
+ def compile_integers(input_file_contents)
+
+ modified_file_contents = input_file_contents.clone
+
+ input_file_contents.each_with_index do |line,index|
+
+ matches = line.scan(/(([0-9]+_)+([0-9]+|$))/)
+
+ unless matches.empty?
+
+ matches.each do |match_arr|
+
+ modified_file_contents[index] = modified_file_contents[index].sub(match_arr[0],match_arr[0].gsub("_",""))
+
+ end
+
+ end
+
+ end
+
+ return modified_file_contents
+
+ end
+
def compile_named_functions(input_file_contents, named_code_blocks, nested_functions, temporary_nila_file)
#This method compiles all the named Nila functions. Below is an example of what is meant
#by named/explicit function
@@ -1401,15 +1471,15 @@
#
#end
#The above function will compile to
- #function square(input_number) {
+ #square = function(input_number) {
#
# return input_number*input_number;
#
- #}
+ #};
def is_parameterless?(input_function_block)
if input_function_block[0].include?("(")
@@ -1656,10 +1726,20 @@
return input_array
end
+ def coffee_type_function(input_array)
+
+ function_name = input_array[0].split("function ")[1].split("(")[0].lstrip
+
+ input_array[0] = "#{function_name} = function(" + input_array[0].split("function ")[1].split("(")[1].lstrip
+
+ return input_array
+
+ end
+
def compile_function(input_array, temporary_nila_file)
modified_input_array = input_array.dup
if is_parameterless?(modified_input_array)
@@ -1716,11 +1796,11 @@
end
end
- modified_input_array[-1] = input_array[-1].sub "end", "}\n"
+ modified_input_array[-1] = input_array[-1].sub "end", "};\n"
modified_input_array = compile_parallel_assignment(modified_input_array, temporary_nila_file)
variables = lexical_scoped_variables(modified_input_array)
@@ -1736,10 +1816,12 @@
modified_input_array = add_auto_return_statement(modified_input_array)
modified_input_array = compile_multiple_return(modified_input_array)
+ modified_input_array = coffee_type_function(modified_input_array)
+
return modified_input_array
end
def extract_function_name(input_code_block)
@@ -1873,12 +1955,18 @@
all_start_locations.each do |location|
extracted_string = input_string[location..-1]
- pattern << extracted_string[0..extracted_string.index(pattern_end)]
+ string_extract = extracted_string[0..extracted_string.index(pattern_end)]
+ if !string_extract.include?(" = function(")
+
+ pattern << string_extract
+
+ end
+
end
return pattern
end
@@ -2979,10 +3067,114 @@
return file_contents
end
+ def compile_loops(input_file_contents,temporary_nila_file)
+
+ def compile_times_loop(input_file_contents,temporary_nila_file)
+
+ def compile_one_line_blocks(input_block)
+
+ block_parameters, block_contents = input_block[1...-1].split("|",2)[1].split("|",2)
+
+ compiled_block = "(function(#{block_parameters.lstrip.rstrip}) {\n\n #{block_contents} \n\n}(_i))_!;\n"
+
+ return compiled_block
+
+ end
+
+ modified_file_contents = input_file_contents.clone
+
+ possible_times_loop = input_file_contents.reject{ |element| !element.include?(".times")}
+
+ oneliner_times_loop = possible_times_loop.reject {|element| !element.include?("{") and !element.include?("}")}
+
+ #multiline_times_loop = possible_times_loop-oneliner_times_loop
+
+ #multiline_times_loop = multiline_times_loop.reject {|element| !element.include?(" do ")}
+
+ loop_variables = []
+
+ unless oneliner_times_loop.empty?
+
+ oneliner_times_loop.each do |loop|
+
+ original_loop = loop.clone
+
+ string_counter = 1
+
+ extracted_string = []
+
+ while loop.include?("\"")
+
+ string_extract = loop[loop.index("\"")..loop.index("\"",loop.index("\"")+1)]
+
+ extracted_string << string_extract
+
+ loop = loop.sub(string_extract,"--repstring#{string_counter}")
+
+ string_counter += 1
+
+ end
+
+ block_extract = loop[loop.index("{")..loop.index("}")]
+
+ compiled_block = ""
+
+ if block_extract.count("|") == 2
+
+ compiled_block = compile_one_line_blocks(block_extract)
+
+ extracted_string.each_with_index do |string,index|
+
+ compiled_block = compiled_block.sub("--repstring#{index+1}",string)
+
+ end
+
+ else
+
+ compiled_block = block_extract[1...-1].lstrip.rstrip
+
+ extracted_string.each_with_index do |string,index|
+
+ compiled_block = compiled_block.sub("--repstring#{index+1}",string)
+
+ end
+
+ end
+
+ times_counter = loop.split(".times")[0].lstrip
+
+ 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
+
+ loop_variables = ["_i","_j"]
+
+ end
+
+ file_id = open(temporary_nila_file, 'w')
+
+ file_id.write(modified_file_contents.join)
+
+ file_id.close()
+
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
+
+ return line_by_line_contents,loop_variables
+
+ end
+
+ file_contents,loop_variables = compile_times_loop(input_file_contents,temporary_nila_file)
+
+ return file_contents,loop_variables
+
+ end
+
def add_semicolons(input_file_contents)
def comment(input_string)
if input_string.include?("--single_line_comment")
@@ -3280,11 +3472,11 @@
def roll_blocks(input_file_contents, code_block_starting_locations)
if !code_block_starting_locations.empty?
- controlregexp = /(if |while |function |function\(|((=|:)\s+\{))/
+ controlregexp = /(if |for |while |\(function\(|= function\(|((=|:)\s+\{))/
code_block_starting_locations = [0, code_block_starting_locations, -1].flatten
possible_blocks = []
@@ -3294,25 +3486,39 @@
for x in 0...code_block_starting_locations.length-1
possible_blocks << input_file_contents[code_block_starting_locations[x]..code_block_starting_locations[x+1]]
+ if possible_blocks.length > 1
+
+ possible_blocks[-1] = possible_blocks[-1][1..-1]
+
+ end
+
end
end_counter = 0
end_index = []
current_block = []
possible_blocks.each_with_index do |block|
- current_block += block
+ if !block[0].eql?(current_block[-1])
+ current_block += block
+
+ else
+
+ current_block += block[1..-1]
+
+ end
+
current_block.each_with_index do |line, index|
- if line.lstrip.eql? "}\n" or line.lstrip.eql?("};\n")
+ if line.lstrip.eql? "}\n" or line.lstrip.eql?("};\n") or line.lstrip.include?("_!;\n")
end_counter += 1
end_index << index
@@ -3352,11 +3558,11 @@
end_index = []
current_block.each_with_index do |line, index|
- if line.lstrip.eql? "}\n"
+ if line.lstrip.eql? "}\n" or line.lstrip.eql?("};\n") or line.lstrip.include?("_!;\n")
end_counter += 1
end_index << index
@@ -3410,11 +3616,11 @@
return input_string
end
- javascript_regexp = /(if |while |function |function\(|((=|:)\s+\{))/
+ javascript_regexp = /(if |for |while |\(function\(|= function\(|((=|:)\s+\{))/
javascript_file_contents = javascript_file_contents.collect { |element| element.sub("Euuf", "if") }
javascript_file_contents = javascript_file_contents.collect { |element| element.sub("whaaleskey", "while") }
@@ -3624,16 +3830,20 @@
input_file_contents = input_file_contents.collect { |element| element.sub("==", "===") }
input_file_contents = input_file_contents.collect { |element| element.sub("!=", "!==") }
+ input_file_contents = input_file_contents.collect { |element| element.sub("equequ", "==") }
+
input_file_contents = input_file_contents.collect { |element| element.sub("elsuf", "else if") }
input_file_contents = input_file_contents.collect { |element| compile_power_operator(element) }
input_file_contents = input_file_contents.collect {|element| compile_match_operator(element)}
+ input_file_contents = input_file_contents.collect {|element| element.gsub("_!;",";")}
+
return input_file_contents
end
def pretty_print_nila(input_file_contents, temporary_nila_file)
@@ -3667,36 +3877,42 @@
file_contents = split_semicolon_seperated_expressions(file_contents)
file_contents = compile_heredocs(file_contents, temp_file)
+ file_contents,loop_vars = compile_loops(file_contents,temp_file)
+
file_contents = compile_interpolated_strings(file_contents)
file_contents = compile_hashes(file_contents,temp_file)
file_contents = compile_conditional_structures(file_contents, temp_file)
file_contents = compile_arrays(file_contents, temp_file)
file_contents = compile_strings(file_contents)
+ file_contents = compile_integers(file_contents)
+
file_contents = compile_default_values(file_contents, temp_file)
file_contents, named_functions, nested_functions = replace_named_functions(file_contents, temp_file)
comments = [singleline_comments, multiline_comments]
file_contents = compile_parallel_assignment(file_contents, temp_file)
- list_of_variables, file_contents = get_variables(file_contents, temp_file)
-
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)
+
file_contents, ruby_functions = compile_custom_function_map(file_contents)
function_names << ruby_functions
+ list_of_variables += loop_vars
+
file_contents = compile_whitespace_delimited_functions(file_contents, function_names, temp_file)
file_contents = remove_question_marks(file_contents, list_of_variables, temp_file)
file_contents = add_semicolons(file_contents)
@@ -3771,10 +3987,10 @@
return remaining_string[0...remaining_string.length-path_finder]
end
-nilac_version = "0.0.4.2.6"
+nilac_version = "0.0.4.2.8"
opts = Slop.parse do
on :c, :compile=, 'Compile Nila File', as:Array, delimiter:":"
on :h, :help, 'Help With Nilac' do