bin/nilac in nilac-0.0.4.0 vs bin/nilac in nilac-0.0.4.0.1

- old
+ new

@@ -466,10 +466,88 @@ return line_by_line_contents end + def compile_default_values(input_file_contents,temporary_nila_file) + + #This method compiles default values present in functions. An example is provided below + + # def fill(container = "cup",liquid = "coffee") + # puts "Filling the #{container} with #{liquid}" + # end + + def parse_default_values(input_function_definition) + + puts input_function_definition + + split1,split2 = input_function_definition.split("(") + + split2,split3 = split2.split(")") + + function_parameters = split2.split(",") + + default_value_parameters = function_parameters.reject {|element| !element.include?"="} + + replacement_parameters = [] + + replacement_string = "" + + default_value_parameters.each do |paramvalue| + + 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" + + end + + return replacement_string,default_value_parameters,replacement_parameters + + end + + possible_default_values = input_file_contents.dup.reject {|element| !element.include?("def")} + + possible_default_values = possible_default_values.reject {|element| !element.include?("=")} + + if !possible_default_values.empty? + + possible_default_values.each do |line| + + current_line_index = input_file_contents.each_index.select {|index| input_file_contents[index] == line}.flatten[0] + + replacement_string,value_parameters,replacement_parameters = parse_default_values(line) + + modified_line = line.dup + + value_parameters.each_with_index do |val,index| + + modified_line = modified_line.sub(val,replacement_parameters[index]) + + end + + input_file_contents[current_line_index] = modified_line + + input_file_contents[current_line_index + 1] = replacement_string + + end + + end + + file_id = open(temporary_nila_file, 'w') + + file_id.write(input_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. @@ -723,10 +801,14 @@ range_index,split3 = split2.split("]") index_start,index_end = range_index.split ".." + index_start = "" if index_start.nil? + + index_end = "" if index_end.nil? + replacement_string = nil if index_end.strip == "end" replacement_string = split1 + ".slice(#{index_start})\n" @@ -816,26 +898,52 @@ def lexical_scoped_variables(input_function_block) #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 + controlregexp = /(if |while |def |function |function\()/ + variables = [] + function_name,parameters = input_function_block[0].split("(") + + parameters = parameters.split(")")[0].split(",") + + parameters = parameters.collect {|element| element.strip} + input_function_block.each do |line| - if line.include? "=" and !line.include?("def") + if line.include? "=" and line.index(controlregexp).nil? current_line_split = line.strip.split("=") variables << current_line_split[0].rstrip end end - variables.uniq + parameters.each do |param| + if variables.include?(param) + + variables.delete(param) + + end + + end + + if variables.empty? + + return [] + + else + + return variables.uniq + + end + end def remove_question_marks(input_file_contents,input_list,temporary_nila_file) joined_file_contents = input_file_contents.join @@ -1074,10 +1182,12 @@ end modified_input_array[-1] = input_array[-1].sub "end","}\n" + modified_input_array = compile_multiple_variable_initialization(modified_input_array,temporary_nila_file) + variables = lexical_scoped_variables(modified_input_array) if !variables.empty? variable_string = "\nvar " + variables.join(", ") + "\n" @@ -1820,10 +1930,12 @@ file_contents = compile_interpolated_strings(file_contents) file_contents = compile_arrays(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_multiple_variable_initialization(file_contents,temp_file) @@ -1914,10 +2026,10 @@ return remaining_string[0...remaining_string.length-path_finder] end -nilac_version = "0.0.3.9" +nilac_version = "0.0.4.0" opts = Slop.parse do on :c, :compile=, 'Compile Nila File', as:Array, delimiter:":" on :h, :help, 'Help With Nilac' do