bin/nilac in nilac-0.0.4.3.7 vs bin/nilac in nilac-0.0.4.3.8
- old
+ new
@@ -1488,11 +1488,11 @@
return input_string
end
- javascript_regexp = /(if |while |for |function |function\(|%[qQ]*\{)/
+ javascript_regexp = /(if |while |for |function |function\(|%[qQw]*\{)/
modified_file_contents = input_file_contents.clone.collect {|element| replace_strings(element)}
possible_inline_hashes = modified_file_contents.reject {|element| element.count("{") != 1}
@@ -2263,10 +2263,14 @@
".reverse" => ".reverse()",
".empty?" => ".length == 0",
+ ".upcase" => ".toUpperCase()",
+
+ ".downcase" => ".toLowerCase()",
+
}
method_map = method_map_replacement.keys
method_map_regex = method_map.collect {|name| name.gsub(".","\\.")}
@@ -2340,11 +2344,11 @@
keyword_replacement_map = {
"nil" => "null",
- "Array.new" => "Array()"
+ "Array.new" => "new Array()"
}
special_keywords = keyword_replacement_map.keys
@@ -3845,10 +3849,211 @@
return file_contents
end
+ def compile_case_statement(input_file_contents,temporary_nila_file)
+
+ # This method compiles simple Ruby style case statements to Javascript
+ # equivalent switch case statements
+
+ # For an example, look at shark/test_files/case.nila
+
+ 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
+
+ def compile_when_statement(input_block)
+
+ condition,body = input_block[0],input_block[1..-1]
+
+ if replace_strings(condition.split("when ")[1]).include?(",")
+
+ condition_cases = condition.split("when ")[1].split(",").collect {|element| element.strip}
+
+ case_replacement = []
+
+ condition_cases.each do |ccase|
+
+ case_replacement << "case #{ccase}:%$%$ {\n\n"
+
+ case_replacement << body.collect {|element| " " + element.strip + "\n\n"}
+
+ case_replacement << " break\n%$%$\n}\n"
+
+ end
+
+ else
+
+ case_replacement = []
+
+ condition_case = condition.split("when ")[1].strip
+
+ case_replacement << "case #{condition_case}:%$%$ {\n\n"
+
+ case_replacement << body.collect {|element| " " + element.strip + "\n\n"}
+
+ case_replacement << " break\n%$%$\n}\n"
+
+ end
+
+ return case_replacement.join
+
+ end
+
+ modified_file_contents = input_file_contents.clone
+
+ possible_case_statements = input_file_contents.reject {|element| !element.include?("case ")}
+
+ case_statements = []
+
+ possible_case_statements.each do |statement|
+
+ starting_index = input_file_contents.index(statement)
+
+ index = starting_index
+
+ until input_file_contents[index].strip.eql?("end")
+
+ index += 1
+
+ end
+
+ case_statements << input_file_contents[starting_index..index].collect {|element| element.clone}.clone
+
+ end
+
+ legacy = case_statements.collect {|element| element.clone}
+
+ replacement_strings = []
+
+ case_statements.each do |statement_block|
+
+ condition = statement_block[0].split("case")[1].strip
+
+ statement_block[0] = "switch(#{condition}) {\n\n"
+
+ when_statements = statement_block.reject {|element| !replace_strings(element).include?("when")}
+
+ when_statements_index = []
+
+ when_statements.each do |statement|
+
+ when_statements_index << statement_block.each_index.select{|index| statement_block[index] == statement}
+
+ end
+
+ when_statements_index = when_statements_index.flatten
+
+ if replace_strings(statement_block.join).include?("else\n")
+
+ else_statement = statement_block.reject {|element| !replace_strings(element).strip.eql?("else")}
+
+ else_block = statement_block[statement_block.index(else_statement[0])+1...-1]
+
+ when_statements_index = when_statements_index + statement_block.each_index.select {|index| statement_block[index] == else_statement[0] }.to_a
+
+ when_statements_index = when_statements_index.flatten
+
+ statement_block[statement_block.index(else_statement[0])..-1] = ["default: %$%$ {\n\n",else_block.collect{|element| " " + element.strip + "\n\n"},"%$%$\n}\n\n}\n\n"].flatten
+
+ when_statement_blocks = []
+
+ when_statements.each_with_index do |statement,ind|
+
+ when_block = statement_block[when_statements_index[ind]...when_statements_index[ind+1]]
+
+ when_statement_blocks << when_block
+
+ end
+
+ replacement_blocks = when_statement_blocks.collect {|element| compile_when_statement(element)}
+
+ else
+
+ statement_block[-1] = "}\n\n" if statement_block[-1].strip.eql?("end")
+
+ when_statement_blocks = []
+
+ when_statements_index << -1
+
+ when_statements.each_with_index do |statement,ind|
+
+ when_block = statement_block[when_statements_index[ind]...when_statements_index[ind+1]]
+
+ when_statement_blocks << when_block
+
+ end
+
+ replacement_blocks = when_statement_blocks.collect {|element| compile_when_statement(element)}
+
+ end
+
+ statement_block = statement_block.join
+
+ when_statement_blocks.each_with_index do |blck,index|
+
+ statement_block = statement_block.sub(blck.join,replacement_blocks[index])
+
+ end
+
+ replacement_strings << statement_block
+
+ end
+
+ joined_file_contents = modified_file_contents.join
+
+ legacy.each_with_index do |statement,index|
+
+ joined_file_contents = joined_file_contents.sub(statement.join,replacement_strings[index])
+
+ end
+
+ file_id = open(temporary_nila_file, 'w')
+
+ file_id.write(joined_file_contents)
+
+ file_id.close()
+
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
+
+ return line_by_line_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)
@@ -4391,11 +4596,11 @@
def fix_newlines(file_contents)
def extract_blocks(file_contents)
- javascript_regexp = /(if |while |for |function\(|((=|:)\s+\{))/
+ javascript_regexp = /(if |while |for |case |default:|switch\(|function\(|((=|:)\s+\{))/
block_starting_lines = file_contents.dup.reject { |element| element.index(javascript_regexp).nil? }[1..-1]
block_starting_lines = block_starting_lines.reject { |element| element.include?(" ") }
@@ -4491,11 +4696,11 @@
def roll_blocks(input_file_contents, code_block_starting_locations)
if !code_block_starting_locations.empty?
- controlregexp = /(if |for |while |,function\(|\(function\(|= function\(|((=|:)\s+\{))/
+ controlregexp = /(if |for |while |case |default:|switch\(|,function\(|\(function\(|= function\(|((=|:)\s+\{))/
code_block_starting_locations = [0, code_block_starting_locations, -1].flatten
possible_blocks = []
@@ -4635,11 +4840,11 @@
return input_string
end
- javascript_regexp = /(if |for |while |\(function\(|= function\(|((=|:)\s+\{))/
+ javascript_regexp = /(if |for |while |case |default:|switch\(|\(function\(|= function\(|((=|:)\s+\{))/
if declarable_variables.length > 0
declaration_string = "var " + declarable_variables.flatten.uniq.sort.join(", ") + ";\n\n"
@@ -4777,12 +4982,26 @@
file_id.close()
line_by_line_contents = read_file_line_by_line(temporary_nila_file)
+ line_by_line_contents = line_by_line_contents.collect {|element| element.gsub("%$%$ {","")}
+
line_by_line_contents = fix_newlines(line_by_line_contents)
+ removable_indices = line_by_line_contents.each_index.select {|index| line_by_line_contents[index].strip == "%$%$;" }
+
+ while line_by_line_contents.join.include?("%$%$;")
+
+ line_by_line_contents.delete_at(removable_indices[0])
+
+ line_by_line_contents.delete_at(removable_indices[0])
+
+ removable_indices = line_by_line_contents.each_index.select {|index| line_by_line_contents[index].strip == "%$%$;" }
+
+ end
+
line_by_line_contents = fix_syntax_indentation(line_by_line_contents)
line_by_line_contents = line_by_line_contents.collect { |element| replace_ignored_words(element) }
return line_by_line_contents
@@ -4910,10 +5129,12 @@
file_contents = compile_interpolated_strings(file_contents)
file_contents = compile_hashes(file_contents,temp_file)
+ file_contents = compile_case_statement(file_contents,temp_file)
+
file_contents = compile_conditional_structures(file_contents, temp_file)
file_contents = compile_blocks(file_contents,temp_file)
file_contents = compile_integers(file_contents)
@@ -5022,10 +5243,10 @@
return remaining_string[0...remaining_string.length-path_finder]
end
-nilac_version = "0.0.4.3.4"
+nilac_version = "0.0.4.3.8"
opts = Slop.parse do
on :c, :compile=, 'Compile Nila File', as:Array, delimiter:":"
on :h, :help, 'Help With Nilac' do