bin/nilac in nilac-0.0.2 vs bin/nilac in nilac-0.0.3
- old
+ new
@@ -1,7 +1,6 @@
#!/usr/bin/env ruby
-# encoding: utf-8
#Nilac is the official Nila compiler. It compiles Nila into pure Javascript. Nilac is currently
#written in Ruby but will be self hosted in the upcoming years. Nila targets mostly the
#Node.js developers rather than client side developers.
@@ -208,87 +207,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
-
- #Heredocs preserve all the formatting
-
- 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
-
- 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|
@@ -1359,42 +1281,10 @@
file_id.close()
line_by_line_contents = read_file_line_by_line(temporary_nila_file)
- var_declaration_index = 0
-
- for x in 0...line_by_line_contents.length
-
- current_row = line_by_line_contents[x]
-
- if current_row.lstrip.include?("var ")
-
- var_declaration_index = x
-
- break
-
- end
-
- end
-
- empty_check = line_by_line_contents[x+1..x+4]
-
- empty_check.each_with_index do |content,index|
-
- empty_check[index] = content.lstrip
-
- end
-
- empty_check = empty_check.delete_if {|content| content.empty?}
-
- if empty_check.empty?
-
- line_by_line_contents.delete_at(x+1);line_by_line_contents.delete_at(x+2)
-
- end
-
return line_by_line_contents
end
def pretty_print_nila(input_file_contents)
@@ -1409,184 +1299,29 @@
end
def create_self_invoking_function(input_file_contents)
- # A feature imported from Coffeescript. This makes all the function private by default
+ # A feature imported from Coffeescript 1.6.1. 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
end
- def compile_license_functions(input_file_contents,temporary_nila_file)
-
- #This method will make it easier for developers to add open source licensing to their projects
-
- #Currently the following licenses are supported
-
- #1. MIT License
-
- #Example of MIT License => mit_license(year,authors)
-
- #mit_license("2013","Adhithya Rajasekaran, Sri Madhavi Rajasekaran")
-
- 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
-
- license_functions = ["mit_license(","apache_license"]
-
- mit_license = <<-licensetext
-
- /*
-
- Copyright yer, <auth>
-
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”),
- to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
- and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- IN THE SOFTWARE.
-
- */
-
-
- licensetext
-
- apache_license = <<-licensetext
-
- /*
-
- Copyright [yer] [auth]
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-
- */
-
-
- licensetext
-
- license_texts = [mit_license,apache_license]
-
- license_params = [2,2]
-
- license_replaceables = ["yer","auth","orgg"]
-
- joined_file_contents = input_file_contents.join
-
- license_functions.each_with_index do |function,index|
-
- function_locations = find_all_matching_indices(joined_file_contents,function)
-
- function_locations.each do |location|
-
- string_extract = joined_file_contents[location..-1]
-
- function_end = string_extract.index(")")
-
- function_call = string_extract[0..function_end]
-
- parameters = function_call.split(function)[1][0...-1]
-
- parameters = parameters.split(":")
-
- parameters.each_with_index do |param,index|
-
- parameters[index] = param[1...-1]
-
- end
-
- replacement_string = license_texts[index]
-
- for x in 0...parameters.length
-
- replacement_string = replacement_string.sub(license_replaceables[x],parameters[x])
-
- end
-
- replacement_array = replacement_string.split("\n")
-
- modified_replacement_array = replacement_array.dup
-
- replacement_array.each_with_index do |line,index|
-
- modified_replacement_array[index] = line.lstrip
-
- end
-
- replacement_string = replacement_array.join("\n")
-
- if joined_file_contents.include?(function_call+";\n")
-
- joined_file_contents = joined_file_contents.sub(function_call+";\n","")
-
- else
-
- joined_file_contents = joined_file_contents.sub(function_call+";","")
-
- end
-
- joined_file_contents = replacement_string.lstrip + "\n\n" + joined_file_contents
-
- 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
-
def output_javascript(file_contents,output_file,temporary_nila_file)
file_id = open(output_file, 'w')
File.delete(temporary_nila_file)
- file_id.write("// Generated by Nilac 0.0.1 Visit http://adhithyan15.github.com/nila to know more\n\n")
+ file_id.write("//Written in Nila and compiled into Javascript.Have fun!\n\n")
+ file_id.write("//Visit http://adhithyan15.github.com/nila to know more!\n\n")
+
file_id.write(file_contents.join)
file_id.close()
end
@@ -1597,11 +1332,11 @@
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_heredoc_strings(file_contents,temp_file)
file_contents = compile_interpolated_strings(file_contents)
file_contents,singleline_comments = replace_singleline_comments(file_contents)
@@ -1627,21 +1362,31 @@
file_contents = compile_comments(file_contents,comments,temp_file)
file_contents = create_self_invoking_function(file_contents)
- file_contents = compile_license_functions(file_contents,temp_file)
-
file_contents = pretty_print_javascript(file_contents,temp_file)
output_javascript(file_contents,output_js_file,temp_file)
end
def create_executable(input_file)
+ def read_file_line_by_line(input_path)
+
+ file_id = open(input_path)
+
+ file_line_by_line = file_id.readlines()
+
+ file_id.close
+
+ return file_line_by_line
+
+ end
+
windows_output = `ocra --add-all-core #{input_file}`
end
def create_mac_executable(input_file)
@@ -1712,10 +1457,12 @@
end
opts.on("-b", "--build", "Builds Itself") do
- create_executable("nilac.rb")
+ file_path = Dir.pwd + "/nilac.rb"
+
+ create_executable(file_path)
puts "Build Successful!"
end