lib/nilac/compile_existential_operator.rb in nilac-0.0.4.3.9.5 vs lib/nilac/compile_existential_operator.rb in nilac-0.0.4.3.9.6
- old
+ new
@@ -4,15 +4,101 @@
# This method compiles Nila's existential operator.
#
# Example:
#
+ # input_args[0].exist?
+ #
+ # The above line should compile to
+ #
+ # typeof input_args[0] !== undefined && input_args[0] !== nil
+
+ modified_contents = input_file_contents.collect {|element| replace_strings(element)}
+
+ possible_existential_calls = modified_contents.reject {|element| !element.include?(".exist")}
+
+ split_regexp = /((if|while) \((!\()?)/
+
+ second_split_regexp = /(&&|\|\|)/
+
+ possible_existential_calls.each do |statement|
+
+ reconstructed_statement = input_file_contents[modified_contents.index(statement)]
+
+ counter = 0
+
+ while reconstructed_statement.include?(".exist")
+
+ before,after = reconstructed_statement.split(".exist",2)
+
+ if counter == 0
+
+ split_data = before.split(split_regexp)
+
+ before2 = split_data[1]
+
+ if before2.eql?("if (") or before2.eql?("while (")
+
+ usage = split_data[3]
+
+ elsif before2.eql?("if (!(") or before2.eql?("while (!(")
+
+ usage = split_data[4]
+
+ end
+
+ replacement_string = "(typeof #{usage} !== \"undefined\" &|&| #{usage} !== null)"
+
+ reconstructed_statement = before.split(before2)[0] + before2 + replacement_string + after
+
+ elsif counter > 0
+
+ split_data = before.split(second_split_regexp)
+
+ before2 = split_data[0]
+
+ operator = split_data[1]
+
+ operator = " &|&| " if operator.strip.eql?("&&")
+
+ operator = " |$|$ " if operator.strip.eql?("||")
+
+ usage = split_data[2]
+
+ replacement_string = "(typeof #{usage} !== \"undefined\" &|&| #{usage} !== null)"
+
+ reconstructed_statement = before2 + operator + replacement_string + after
+
+ end
+
+ counter += 1
+
+ end
+
+ reconstructed_statement = reconstructed_statement.gsub("&|&|","&&").gsub("|$|$","&&")
+
+ input_file_contents[modified_contents.index(statement)] = reconstructed_statement
+
+ modified_contents = input_file_contents
+
+ end
+
+ return input_file_contents
+
+end
+
+def compile_nil_operator(input_file_contents)
+
+ # This method compiles Nila's existential operator.
+ #
+ # Example:
+ #
# input_args[0].nil?
#
# The above line should compile to
#
- # typeof input_args[0] === undefined && input_args[0] === nil
+ # input_args[0] === nil
modified_contents = input_file_contents.collect {|element| replace_strings(element)}
possible_existential_calls = modified_contents.reject {|element| !element.include?(".nil")}
@@ -44,11 +130,11 @@
usage = split_data[4]
end
- replacement_string = "(typeof #{usage} === \"undefined\" &|&| #{usage} === null)"
+ replacement_string = "(#{usage} === null)"
reconstructed_statement = before.split(before2)[0] + before2 + replacement_string + after
elsif counter > 0
@@ -62,11 +148,11 @@
operator = " |$|$ " if operator.strip.eql?("||")
usage = split_data[2]
- replacement_string = "(typeof #{usage} === \"undefined\" &|&| #{usage} === null)"
+ replacement_string = "(#{usage} === null)"
reconstructed_statement = before2 + operator + replacement_string + after
end
@@ -76,10 +162,12 @@
reconstructed_statement = reconstructed_statement.gsub("&|&|","&&").gsub("|$|$","&&")
input_file_contents[modified_contents.index(statement)] = reconstructed_statement
+ modified_contents = input_file_contents
+
end
return input_file_contents
end
@@ -159,9 +247,11 @@
end
reconstructed_statement = reconstructed_statement.gsub("&|&|","&&").gsub("|$|$","&&")
input_file_contents[modified_contents.index(statement)] = reconstructed_statement
+
+ modified_contents = input_file_contents
end
return input_file_contents
\ No newline at end of file