bin/nilac in nilac-0.0.3.8 vs bin/nilac in nilac-0.0.3.9

- old
+ new

@@ -307,11 +307,11 @@ if current_row.index(nila_regexp) != nil key_word_locations << x - elsif current_row.include?("end\n") || current_row.include?("end") + elsif current_row.lstrip.include?("end\n") || current_row.include?("end") end_locations << x end @@ -412,10 +412,64 @@ return line_by_line_contents,named_functions,nested_functions end + def compile_multiple_variable_initialization(input_file_contents,temporary_nila_file) + + possible_variable_lines = input_file_contents.reject {|element| !element.include?"="} + + possible_multiple_initialization = possible_variable_lines.reject {|element| !element.split("=")[0].include?","} + + multiple_initialization_index = [] + + possible_multiple_initialization.each do |statement| + + location_array = input_file_contents.each_index.select { |index| input_file_contents[index] == statement} + + multiple_initialization_index << location_array[0] + + end + + modified_file_contents = input_file_contents.dup + + multiple_init_counter = 1 + + possible_multiple_initialization.each_with_index do |line,index| + + line_split = line.split(" = ") + + right_side_variables = line_split[0].split(",") + + replacement_string = "multipleinit#{multiple_init_counter} = #{line_split[1]}\n\n" + + variable_string = "" + + right_side_variables.each_with_index do |variable,var_index| + + variable_string = variable_string + variable.rstrip + " = multipleinit#{multiple_init_counter}[#{var_index}]\n\n" + + end + + replacement_string = replacement_string + variable_string + + modified_file_contents[multiple_initialization_index[index]] = replacement_string + + 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 + + end + def get_variables(input_file_contents,temporary_nila_file) #This method is solely focused on getting a list of variables to be declared. #Since Javascript is a dynamic language, Nila doesn't have to worry about following up on those variables. @@ -462,11 +516,11 @@ line_by_line_contents = read_file_line_by_line(temporary_nila_file) if variables.length > 0 - variable_declaration_string = "var " + variables.uniq.join(", ") + "\n\n" + variable_declaration_string = "var " + variables.uniq.sort.join(", ") + "\n\n" line_by_line_contents = [variable_declaration_string,line_by_line_contents].flatten end @@ -524,78 +578,208 @@ #Currently the following kinds of array constructs are compilable # 1. %w{} syntax # 2. Range - Coming soon! - def extract(input_string,pattern_start,pattern_end) + def compile_w_arrays(input_file_contents) - def find_all_matching_indices(input_string,pattern) + def extract(input_string,pattern_start,pattern_end) - locations = [] + def find_all_matching_indices(input_string,pattern) - index = input_string.index(pattern) + locations = [] - while index != nil + index = input_string.index(pattern) - locations << index + while index != nil - index = input_string.index(pattern,index+1) + locations << index + index = input_string.index(pattern,index+1) + + end + + return locations + + end - return locations + all_start_locations = find_all_matching_indices(input_string,pattern_start) + all_end_locations = find_all_matching_indices(input_string,pattern_end) + pattern = [] + + all_start_locations.each_with_index do |location,index| + + pattern << input_string[location..all_end_locations[index]] + + end + + return pattern + end - all_start_locations = find_all_matching_indices(input_string,pattern_start) + def compile_w_syntax(input_string) - all_end_locations = find_all_matching_indices(input_string,pattern_end) + modified_input_string = input_string[3...-1] - pattern = [] + string_split = modified_input_string.split(" ") - all_start_locations.each_with_index do |location,index| + return string_split.to_s - pattern << input_string[location..all_end_locations[index]] + end + modified_file_contents = input_file_contents.dup + + input_file_contents.each_with_index do |line,index| + + if line.include?("%w{") + + string_arrays = extract(line,"%w{","}") + + string_arrays.each do |array| + + modified_file_contents[index] = modified_file_contents[index].sub(array,compile_w_syntax(array)) + + end + + end + end - return pattern + return modified_file_contents end - def compile_w_syntax(input_string) + def compile_array_indexing(input_file_contents) - modified_input_string = input_string[3...-1] + #Nila allows two different kinds of indexing operations on arrays and strings. They are - string_split = modified_input_string.split(" ") + #1. Using Ranges => numbers[0...5] + #2. Using Start and End Indexes => numbers[0,5] - return string_split.to_s + #This method implements this Nila feature - end + possible_indexing_operation = input_file_contents.dup.reject {|element| !element.include?"[" and !element.include?"]"} - modified_file_contents = input_file_contents.dup + possible_range_indexing = possible_indexing_operation.reject {|element| !element.include?".."} - input_file_contents.each_with_index do |line,index| + triple_range_indexing = possible_range_indexing.reject {|element| !element.include?"..."} - if line.include?("%w{") + triple_range_indexes = [] - string_arrays = extract(line,"%w{","}") + triple_range_indexing.each do |line| - string_arrays.each do |array| + triple_range_indexes << input_file_contents.dup.each_index.select {|index| input_file_contents[index] == line} - modified_file_contents[index] = modified_file_contents[index].sub(array,compile_w_syntax(array)) + end + triple_range_indexes = triple_range_indexes.flatten + + triple_range_indexing.each_with_index do |line,index| + + split1,split2 = line.split("[") + + range_index,split3 = split2.split("]") + + index_start,index_end = range_index.split "..." + + replacement_string = nil + + if index_end.strip == "end" + + replacement_string = split1 + ".slice(#{index_start},#{split}.length)\n" + + else + + replacement_string = split1 + ".slice(#{index_start},#{index_end})\n" + end + possible_range_indexing.delete(input_file_contents[triple_range_indexes[index]]) + + possible_indexing_operation.delete(input_file_contents[triple_range_indexes[index]]) + + input_file_contents[triple_range_indexes[index]] = replacement_string + end + double_range_indexing = possible_range_indexing.reject {|element| !element.include?("..")} + + double_range_indexes = [] + + double_range_indexing.each do |line| + + double_range_indexes << input_file_contents.dup.each_index.select {|index| input_file_contents[index] == line} + + end + + double_range_indexes = double_range_indexes.flatten + + double_range_indexing.each_with_index do |line,index| + + split1,split2 = line.split("[") + + range_index,split3 = split2.split("]") + + index_start,index_end = range_index.split ".." + + replacement_string = nil + + if index_end.strip == "end" + + replacement_string = split1 + ".slice(#{index_start})\n" + + elsif index_end.strip == "" and index_start.strip == "" + + replacement_string = split1 + ".slice(0)\n" + + else + + replacement_string = split1 + ".slice(#{index_start},#{index_end}+1)\n" + + end + + possible_range_indexing.delete(input_file_contents[double_range_indexes[index]]) + + possible_indexing_operation.delete(input_file_contents[double_range_indexes[index]]) + + input_file_contents[double_range_indexes[index]] = replacement_string + + end + + duplicating_operations = input_file_contents.dup.reject{|element| !element.include?(".dup")} + + duplicating_operation_indexes = [] + + duplicating_operations.each do |line| + + duplicating_operation_indexes << input_file_contents.dup.each_index.select {|index| input_file_contents[index] == line} + + end + + duplicating_operation_indexes = duplicating_operation_indexes.flatten + + duplicating_operation_indexes.each do |index| + + input_file_contents[index] = input_file_contents[index].sub(".dup",".slice(0)") + + end + + return input_file_contents + end - return modified_file_contents + input_file_contents = compile_w_arrays(input_file_contents) + input_file_contents = compile_array_indexing(input_file_contents) + + return input_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 @@ -700,10 +884,138 @@ return input_array end + def compile_multiple_return(input_array) + + 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 + + modified_input_array = input_array.dup + + return_statements = input_array.dup.reject {|element| !element.include?"return"} + + multiple_return_statements = return_statements.dup.reject {|element| !element.include?","} + + modified_multiple_return_statements = multiple_return_statements.dup + + return_statement_index = [] + + multiple_return_statements.each do |statement| + + location_array = modified_input_array.each_index.select { |index| modified_input_array[index] == statement} + + return_statement_index << location_array[0] + + end + + multiple_return_statements.each_with_index do |return_statement,index| + + replacement_counter = 0 + + if return_statement.include? "\"" + + starting_quotes = find_all_matching_indices(return_statement,"\"") + + for x in 0...(starting_quotes.length)/2 + + quotes = return_statement[starting_quotes[x]..starting_quotes[x+1]] + + replacement_counter += 1 + + modified_multiple_return_statements[index] = modified_multiple_return_statements[index].sub(quotes,"repstring#{1}") + + modified_input_array[return_statement_index[index]] = modified_multiple_return_statements[index].sub(quotes,"repstring#{1}") + + end + + end + + end + + modified_multiple_return_statements = modified_multiple_return_statements.reject {|element| !element.include?","} + + return_statement_index = [] + + modified_multiple_return_statements.each do |statement| + + location_array = modified_input_array.each_index.select { |index| modified_input_array[index] == statement} + + return_statement_index << location_array[0] + + end + + modified_multiple_return_statements.each_with_index do |return_statement,index| + + method_call_counter = 0 + + if return_statement.include?"(" + + open_paran_location = find_all_matching_indices(return_statement,"(") + + open_paran_location.each do |paran_index| + + method_call = return_statement[paran_index..return_statement.index(")",paran_index+1)] + + method_call_counter += 1 + + modified_multiple_return_statements[index] = modified_multiple_return_statements[index].sub(method_call,"methodcall#{method_call_counter}") + + modified_input_array[return_statement_index[index]] = modified_multiple_return_statements[index].sub(method_call,"methodcall#{method_call_counter}") + + end + + end + + end + + modified_multiple_return_statements = modified_multiple_return_statements.reject {|element| !element.include?(",")} + + return_statement_index = [] + + modified_multiple_return_statements.each do |statement| + + location_array = modified_input_array.each_index.select { |index| modified_input_array[index] == statement} + + return_statement_index << location_array[0] + + end + + return_statement_index.each do |index| + + original_statement = input_array[index] + + statement_split = original_statement.split("return ") + + replacement_split = "return [" + statement_split[1].rstrip + "]\n\n" + + input_array[index] = replacement_split + + end + + return input_array + + end + def compile_function(input_array,temporary_nila_file) modified_input_array = input_array.dup if is_parameterless?(modified_input_array) @@ -776,10 +1088,12 @@ modified_input_array = remove_question_marks(modified_input_array,variables,temporary_nila_file) modified_input_array = add_auto_return_statement(modified_input_array) + modified_input_array = compile_multiple_return(modified_input_array) + return modified_input_array end def extract_function_name(input_code_block) @@ -1238,10 +1552,12 @@ code_blocks = [] starting_index = starting_line_indices[0] + begin + for x in 0...initial_starting_lines.length code_blocks << modified_file_contents[starting_index..block_ending_lines[0]] modified_file_contents[starting_index..block_ending_lines[0]] = [] @@ -1264,10 +1580,16 @@ starting_index = starting_line_indices[0] end + rescue TypeError + + puts "Whitespace was left unfixed!" + + end + return modified_file_contents,code_blocks end compact_contents = file_contents.reject {|element| element.lstrip.eql? ""} @@ -1474,11 +1796,11 @@ file_id = open(output_file, 'w') File.delete(temporary_nila_file) - file_id.write("//Written in Nila 0.0.3.3. Visit http://adhithyan15.github.io/nila\n") + file_id.write("//Written using Nila. Visit http://adhithyan15.github.io/nila\n") file_id.write(file_contents.join) file_id.close() @@ -1490,24 +1812,26 @@ file_contents = extract_parsable_file(file_contents) file_contents,multiline_comments,temp_file,output_js_file = replace_multiline_comments(file_contents,input_file_path,*output_file_name) + file_contents,singleline_comments = replace_singleline_comments(file_contents) + file_contents = split_semicolon_seperated_expressions(file_contents) file_contents = compile_interpolated_strings(file_contents) - file_contents,singleline_comments = replace_singleline_comments(file_contents) + file_contents = compile_arrays(file_contents) file_contents,named_functions,nested_functions = replace_named_functions(file_contents,temp_file) comments = [singleline_comments,multiline_comments] + file_contents = compile_multiple_variable_initialization(file_contents,temp_file) + 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) @@ -1576,12 +1900,26 @@ return remaining_string[remaining_string.length-path_finder..-1] end -nilac_version = "0.0.3.7" +def find_file_path(input_path,file_extension) + extension_remover = input_path.split(file_extension) + + remaining_string = extension_remover[0].reverse + + path_finder = remaining_string.index("/") + + remaining_string = remaining_string.reverse + + return remaining_string[0...remaining_string.length-path_finder] + +end + +nilac_version = "0.0.3.9" + opts = Slop.parse do on :c, :compile=, 'Compile Nila File', as:Array, delimiter:":" on :h, :help, 'Help With Nilac' do puts "Nilac is the official compiler for the Nila language.This is a basic help\nmessage with pointers to more information.\n\n" @@ -1615,11 +1953,20 @@ puts nilac_version end on :r, :run=, 'Run Nila File', as:Array - on :m, :buildmac=, 'Build Nilac for Linux/Mac/Rubygems',as:Array + + on :m, :buildmac, 'Build Nilac for Linux/Mac/Rubygems' do + + file_path = Dir.pwd + "/src/nilac.rb" + + create_mac_executable(file_path) + + puts "Build Successful!" + + end end opts = opts.to_hash if opts[:compile] != nil @@ -1712,20 +2059,12 @@ file_path = current_directory + "/" + file compile(file_path) - js_file_name = find_file_name(file_path,".nila") + ".js" + js_file_name = find_file_path(file_path,".nila") + find_file_name(file_path,".nila") + ".js" node_output = `node #{js_file_name}` puts node_output - -elsif opts[:buildmac] != nil - - file_path = Dir.pwd + "/bin/nilac.rb" - - create_mac_executable(file_path) - - puts "Build Successful!" end