bin/nilac in nilac-0.0.3.1 vs bin/nilac in nilac-0.0.3.2

- old
+ new

@@ -20,47 +20,24 @@ return file_line_by_line end + def extract_parsable_file(input_file_contents) - def extract_parsable_array(input_file_contents) + reversed_file_contents = input_file_contents.reverse - #This method finds and removes the __END__ keyword and the following lines in a Nila file. + line_counter = 0 - modified_file_contents = input_file_contents.dup + while !reversed_file_contents[line_counter].strip.include?("__END__") - end_index = 0 + line_counter += 1 - input_file_contents.each_with_index do |line,index| - - if line.strip.eql?("__END__") - - end_index = index - - end - end - if end_index == 0 + return_contents = input_file_contents[0...-1*line_counter-1] - output = modified_file_contents - - else - - output = modified_file_contents[0..end_index-1] - - while output[-1].eql?("\n") - - output.delete_at(-1) - - end - - end - - return output - end def replace_multiline_comments(input_file_contents,nila_file_path) #This method will replace both the single and multiline comments @@ -154,12 +131,16 @@ file_id2 = open(output_js_file, 'w') file_id.write(modified_file_contents) + file_id2.write("//Written in Nila and compiled to Javascript. Have fun!\n\n") + file_id.close() + file_id2.close() + line_by_line_contents = read_file_line_by_line(temporary_nila_file) comments = multiline_comments.dup return line_by_line_contents,comments,temporary_nila_file,output_js_file @@ -192,11 +173,11 @@ if modified_line.include?(";") replacement_line = modified_file_contents[index] - replacement_line = replacement_line.split(";").join("\n\n") + replacement_line = replacement_line.split(";").join("\n\n") + "\n" modified_file_contents[index] = replacement_line end @@ -204,87 +185,10 @@ return modified_file_contents end - def compile_heredoc_strings(input_file_contents,temporary_nila_file) - - #This method will compile all the Heredoc strings in Nila into pure - #Javascript strings. Ruby has two types of Heredocs. Currently, Nila doesn't support both of them. - #Here is an example of what Nila supports - - #long_passage = <<-ipsumtext - # - #Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor - #incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud - #exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute - #irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla - #pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia - #deserunt mollit anim id est laborum. - # - #ipsumtext - - def find_all_matching_indices(input_string,pattern) - - locations = [] - - index = input_string.index(pattern) - - while index != nil - - locations << index - - index = input_string.index(pattern,index+1) - - - end - - return locations - - - end - - preserve_formatting = %w{ \n \t } - - joined_file_contents = input_file_contents.join - - modified_file_contents = joined_file_contents.dup - - heredoc_start_locations = find_all_matching_indices(joined_file_contents,"<<-") - - heredoc_start_locations.each do |location| - - string_extract = joined_file_contents[location..-1] - - end_of_first_line = string_extract.index("\n") - - name_of_heredoc = string_extract[3...end_of_first_line].rstrip - - heredoc_start,heredoc_end = find_all_matching_indices(string_extract,name_of_heredoc) - - heredoc = string_extract[heredoc_start..heredoc_end+name_of_heredoc.length-1] - - heredoc_content = heredoc.split(name_of_heredoc) - - heredoc_content = heredoc_content[1].lstrip.rstrip.gsub("\n","\\n").gsub("\t","\\t") - - modified_file_contents = modified_file_contents.sub(string_extract[heredoc_start-3..heredoc_end+name_of_heredoc.length-1],"\""+heredoc_content+"\"") - - end - - file_id = open(temporary_nila_file, 'w') - - file_id.write(modified_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_interpolated_strings(input_file_contents) modified_file_contents = input_file_contents.dup input_file_contents.each_with_index do |line,index| @@ -389,11 +293,11 @@ if current_row.index(nila_regexp) != nil key_word_locations << x - elsif current_row.include?("end\n") + elsif current_row.include?("end\n") || current_row.include?("end") end_locations << x end @@ -917,21 +821,23 @@ file_id.close() line_by_line_contents = read_file_line_by_line(temporary_nila_file) return line_by_line_contents,function_names - + end def compile_custom_function_map(input_file_contents) - function_map = ["puts"] + function_map = ["puts","print"] function_map_replacements = { - "puts" => "console.log" + "puts" => "console.log", + "print" => "console.log" + } modified_file_contents = input_file_contents.dup input_file_contents.each_with_index do |line,index| @@ -1033,12 +939,58 @@ #Currently the following conditional structures have been implemented #1. If, Elsif, Else Statement + def compile_inline_conditionals(input_file_contents,temporary_nila_file) + conditionals = [/( if )/,/( while )/] + plain_conditionals = [" if "," while "] + + joined_file_contents = input_file_contents.join + + output_statement = "" + + conditionals.each_with_index do |regex,index| + + matching_lines = input_file_contents.reject {|content| !content.index(regex)} + + matching_lines.each do |line| + + line_split = line.split(plain_conditionals[index]) + + if index == 0 + + output_statement = "if (#{line_split[1].lstrip.rstrip}) {\n\n#{line_split[0]}\n}\n" + + elsif index == 1 + + output_statement = "while (#{line_split[1].lstrip.rstrip}) {\n\n#{line_split[0]}\n}\n" + + end + + joined_file_contents = joined_file_contents.sub(line,output_statement) + + end + + 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 + + line_by_line_contents = compile_inline_conditionals(input_file_contents,temporary_nila_file) + end def compile_comments(input_file_contents,comments,temporary_nila_file) #This method converts Nila comments into pure Javascript comments. This method @@ -1241,11 +1193,11 @@ return modified_array.join end - javascript_regexp = /(function |function\()/ + javascript_regexp = /(if |while |function |function\()/ locations = [] javascript_file_contents = reset_tabs(javascript_file_contents) @@ -1255,12 +1207,10 @@ code_block_ending_locations = find_all_matching_indices(joined_file_contents,"}") combined_location = [code_block_starting_locations,code_block_ending_locations.dup].flatten.sort - last_matching_location = 0 - self_invoking_function_extract = joined_file_contents[code_block_starting_locations[0]..code_block_ending_locations[-1]] self_invoking_function_array = convert_string_to_array(self_invoking_function_extract,temporary_nila_file) combined_location.delete_at(0); combined_location.delete_at(-1); code_block_ending_locations.delete_at(-1); code_block_starting_locations.delete_at(0) @@ -1273,36 +1223,50 @@ end modified_self_invoking_array[-1] = "\n\n" + modified_self_invoking_array[-1] + nested_elements = [] + + nested_indices = [] + + modified_starting_locations = code_block_starting_locations.dup + while code_block_ending_locations.length > 0 matching_location = combined_location[combined_location.index(code_block_ending_locations[0])-1] + combined_location.delete(matching_location) + location_among_start_locations = code_block_starting_locations.index(matching_location) - locations << [[matching_location,combined_location[combined_location.index(code_block_ending_locations[0])]]] + if location_among_start_locations == 0 - code_block_ending_locations.delete_at(0) + locations << [[matching_location,combined_location[combined_location.index(code_block_ending_locations[0])]]] - if code_block_ending_locations.length > 0 + else - for x in location_among_start_locations-1..last_matching_location + nested_elements << [matching_location,combined_location[combined_location.index(code_block_ending_locations[0])]] - locations[-1] << [code_block_starting_locations[x],code_block_ending_locations[0]] + nested_indices << modified_starting_locations.index(matching_location) - code_block_ending_locations.delete_at(0) + end - end + combined_location.delete(code_block_ending_locations[0]) - end + code_block_ending_locations.delete_at(0) - last_matching_location = location_among_start_locations + 1 + code_block_starting_locations.delete(matching_location) end + nested_indices.each_with_index do |loc,index| + + locations[loc-1] << nested_elements[index] + + end + modified_locations = [] locations.each do |loc| modified_locations << loc.sort @@ -1377,11 +1341,11 @@ end def create_self_invoking_function(input_file_contents) - # A feature imported from Coffeescript 1.6.1. This makes all the function private by default + # A feature imported from Coffeescript. This makes all the function private by default # and prevents global variables from leaking. modified_file_contents = ["(function() {\n\n",input_file_contents,"\n\n}).call(this);\n"].flatten return modified_file_contents @@ -1404,18 +1368,16 @@ end file_contents = read_file_line_by_line(input_file_path) - file_contents = extract_parsable_array(file_contents) + file_contents = extract_parsable_file(file_contents) file_contents,multiline_comments,temp_file,output_js_file = replace_multiline_comments(file_contents,input_file_path) file_contents = split_semicolon_seperated_expressions(file_contents) - file_contents = compile_heredoc_strings(file_contents,temp_file) - file_contents = compile_interpolated_strings(file_contents) file_contents,singleline_comments = replace_singleline_comments(file_contents) file_contents,named_functions,nested_functions = replace_named_functions(file_contents,temp_file) @@ -1424,10 +1386,12 @@ list_of_variables,file_contents = get_variables(file_contents,temp_file) file_contents = compile_arrays(file_contents) + file_contents = compile_conditional_structures(file_contents,temp_file) + file_contents, function_names = compile_named_functions(file_contents,named_functions,nested_functions,temp_file) file_contents, ruby_functions = compile_custom_function_map(file_contents) function_names << ruby_functions @@ -1479,11 +1443,11 @@ return file_line_by_line end - mac_file_contents = ["#!/usr/bin/env ruby"] + read_file_line_by_line(input_file) + mac_file_contents = ["#!/usr/bin/env ruby\n\n"] + read_file_line_by_line(input_file) mac_file_path = input_file.sub(".rb","") file_id = open(mac_file_path,"w") @@ -1533,27 +1497,26 @@ puts node_output end - opts.on("-b", "--build", "Builds Itself") do + opts.on("-b", "--build FILE", "Builds Itself") do |file| file_path = Dir.pwd + "/nilac.rb" create_executable(file_path) puts "Build Successful!" end - opts.on("-m", "--buildmac", "Builds Mac Executables") do + opts.on("-m", "--buildmac FILE", "Builds Mac Executables") do |macfile| file_path = Dir.pwd + "/nilac.rb" create_mac_executable(file_path) puts "Build Successful!" end - end.parse!