nilac 0.0.4.3.9.2 → 0.0.4.3.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/bin/nilac +9 -5748
- data/examples/StringMagic.nila +23 -0
- data/examples/countdown.nila +31 -0
- data/examples/decBin.nila +3 -3
- data/examples/marky.js +30 -0
- data/examples/marky.nila +23 -0
- data/lib/nilac/ErrorDeclarations.rb +11 -0
- data/lib/nilac/add_semicolons.rb +57 -0
- data/lib/nilac/compile_arrays.rb +266 -0
- data/lib/nilac/compile_blocks.rb +137 -0
- data/lib/nilac/compile_case_statement.rb +168 -0
- data/lib/nilac/compile_classes.rb +18 -0
- data/lib/nilac/compile_comments.rb +79 -0
- data/lib/nilac/compile_conditional_structures.rb +1378 -0
- data/lib/nilac/compile_custom_function_map.rb +45 -0
- data/lib/nilac/compile_default_values.rb +160 -0
- data/lib/nilac/compile_hashes.rb +123 -0
- data/lib/nilac/compile_heredocs.rb +62 -0
- data/lib/nilac/compile_integers.rb +23 -0
- data/lib/nilac/compile_interpolated_strings.rb +133 -0
- data/lib/nilac/compile_loops.rb +211 -0
- data/lib/nilac/compile_named_functions.rb +690 -0
- data/lib/nilac/compile_operators.rb +83 -0
- data/lib/nilac/compile_parallel_assignment.rb +103 -0
- data/lib/nilac/compile_ruby_methods.rb +58 -0
- data/lib/nilac/compile_special_keywords.rb +44 -0
- data/lib/nilac/compile_strings.rb +145 -0
- data/lib/nilac/compile_whitespace_delimited_functions.rb +97 -0
- data/lib/nilac/create_mac_executable.rb +25 -0
- data/lib/nilac/extract_parsable_file.rb +23 -0
- data/lib/nilac/find_all_matching_indices.rb +18 -0
- data/lib/nilac/find_file_name.rb +13 -0
- data/lib/nilac/find_file_path.rb +13 -0
- data/lib/nilac/get_variables.rb +123 -0
- data/lib/nilac/output_javascript.rb +13 -0
- data/lib/nilac/parse_arguments.rb +41 -0
- data/lib/nilac/pretty_print_javascript.rb +457 -0
- data/lib/nilac/read_file_line_by_line.rb +11 -0
- data/lib/nilac/remove_question_marks.rb +46 -0
- data/lib/nilac/replace_multiline_comments.rb +92 -0
- data/lib/nilac/replace_named_functions.rb +154 -0
- data/lib/nilac/replace_singleline_comments.rb +45 -0
- data/lib/nilac/replace_strings.rb +35 -0
- data/lib/nilac/split_semicolon_seperated_expressions.rb +39 -0
- data/lib/nilac/strToArray.rb +15 -0
- data/lib/nilac/version.rb +1 -1
- data/lib/nilac.rb +324 -1
- data/nilac.gemspec +0 -1
- data/shark/features/add_auto_return_statement.feature +1 -1
- data/shark/features/array_and_string_indexing.feature +1 -1
- data/shark/features/barebones_compilation.feature +1 -1
- data/shark/features/case_when.feature +1 -1
- data/shark/features/default_method_parameters.feature +1 -1
- data/shark/features/fix_newlines.feature +1 -1
- data/shark/features/hashes.feature +1 -1
- data/shark/features/heredoc.feature +1 -1
- data/shark/features/if_then_else.feature +1 -1
- data/shark/features/loop.feature +1 -1
- data/shark/features/method_multiple_return.feature +1 -1
- data/shark/features/multiline_array.feature +1 -1
- data/shark/features/multiple_variable_initialization.feature +1 -1
- data/shark/features/numbers.feature +1 -1
- data/shark/features/regular_for.feature +1 -1
- data/shark/features/regular_if.feature +1 -1
- data/shark/features/regular_while.feature +1 -1
- data/shark/features/ruby_methods.feature +1 -1
- data/shark/features/ruby_operators.feature +1 -1
- data/shark/features/splats.feature +1 -1
- data/shark/features/string_interpolation.feature +1 -1
- data/shark/features/strings.feature +1 -1
- data/shark/features/times.feature +1 -1
- data/shark/features/unless_until.feature +1 -1
- data/shark/features/whitespace_delimitation.feature +1 -1
- metadata +46 -18
- data/src/nilac.rb +0 -5753
@@ -0,0 +1,123 @@
|
|
1
|
+
require_relative 'replace_strings'
|
2
|
+
|
3
|
+
require_relative 'read_file_line_by_line'
|
4
|
+
|
5
|
+
def get_variables(input_file_contents, temporary_nila_file, *loop_variables)
|
6
|
+
|
7
|
+
variables = []
|
8
|
+
|
9
|
+
input_file_contents = input_file_contents.collect { |element| element.gsub("==", "equalequal") }
|
10
|
+
|
11
|
+
input_file_contents = input_file_contents.collect { |element| element.gsub("!=", "notequal") }
|
12
|
+
|
13
|
+
input_file_contents = input_file_contents.collect { |element| element.gsub("+=", "plusequal") }
|
14
|
+
|
15
|
+
input_file_contents = input_file_contents.collect { |element| element.gsub("-=", "minusequal") }
|
16
|
+
|
17
|
+
input_file_contents = input_file_contents.collect { |element| element.gsub("*=", "multiequal") }
|
18
|
+
|
19
|
+
input_file_contents = input_file_contents.collect { |element| element.gsub("/=", "divequal") }
|
20
|
+
|
21
|
+
input_file_contents = input_file_contents.collect { |element| element.gsub("%=", "modequal") }
|
22
|
+
|
23
|
+
input_file_contents = input_file_contents.collect { |element| element.gsub("=~", "matchequal") }
|
24
|
+
|
25
|
+
input_file_contents = input_file_contents.collect { |element| element.gsub(">=", "greatequal") }
|
26
|
+
|
27
|
+
input_file_contents = input_file_contents.collect { |element| element.gsub("<=", "lessyequal") }
|
28
|
+
|
29
|
+
modified_file_contents = input_file_contents.clone
|
30
|
+
|
31
|
+
input_file_contents = input_file_contents.collect {|element| replace_strings(element)}
|
32
|
+
|
33
|
+
javascript_regexp = /(if |while |for )/
|
34
|
+
|
35
|
+
for x in 0...input_file_contents.length
|
36
|
+
|
37
|
+
current_row = input_file_contents[x]
|
38
|
+
|
39
|
+
if current_row.include?("=") and current_row.index(javascript_regexp) == nil
|
40
|
+
|
41
|
+
current_row = current_row.rstrip + "\n"
|
42
|
+
|
43
|
+
current_row_split = current_row.split("=")
|
44
|
+
|
45
|
+
for y in 0...current_row_split.length
|
46
|
+
|
47
|
+
current_row_split[y] = current_row_split[y].strip
|
48
|
+
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
if current_row_split[0].include?("[") or current_row_split[0].include?("(")
|
53
|
+
|
54
|
+
current_row_split[0] = current_row_split[0][0...current_row_split[0].index("[")]
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
current_row_split[0] = current_row_split[0].split(".",2)[0].strip if current_row_split[0].include?(".")
|
59
|
+
|
60
|
+
variables << current_row_split[0]
|
61
|
+
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
input_file_contents[x] = current_row
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
file_contents_as_string = modified_file_contents.join
|
70
|
+
|
71
|
+
file_id = open(temporary_nila_file, 'w')
|
72
|
+
|
73
|
+
file_id.write(file_contents_as_string)
|
74
|
+
|
75
|
+
file_id.close()
|
76
|
+
|
77
|
+
line_by_line_contents = read_file_line_by_line(temporary_nila_file)
|
78
|
+
|
79
|
+
for_loop_variables = []
|
80
|
+
|
81
|
+
for_loop_statements = line_by_line_contents.reject {|line| !line.include?("for")}
|
82
|
+
|
83
|
+
for_loop_statements = for_loop_statements.reject {|line| line.include?("forEach")}
|
84
|
+
|
85
|
+
for_loop_statements.each do |statement|
|
86
|
+
|
87
|
+
varis = statement.split("for (")[1].split(";",2)[0].split(",")
|
88
|
+
|
89
|
+
for_loop_variables << varis.collect {|vari| vari.strip.split("=")[0].strip}
|
90
|
+
|
91
|
+
for_loop_variables = for_loop_variables.flatten
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
variables += loop_variables
|
96
|
+
|
97
|
+
variables += for_loop_variables
|
98
|
+
|
99
|
+
variables = variables.flatten
|
100
|
+
|
101
|
+
line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("plusequal", "+=") }
|
102
|
+
|
103
|
+
line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("minusequal", "-=") }
|
104
|
+
|
105
|
+
line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("multiequal", "*=") }
|
106
|
+
|
107
|
+
line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("divequal", "/=") }
|
108
|
+
|
109
|
+
line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("modequal", "%=") }
|
110
|
+
|
111
|
+
line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("equalequal", "==") }
|
112
|
+
|
113
|
+
line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("notequal", "!=") }
|
114
|
+
|
115
|
+
line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("matchequal", "=~") }
|
116
|
+
|
117
|
+
line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("greatequal", ">=") }
|
118
|
+
|
119
|
+
line_by_line_contents = line_by_line_contents.collect { |element| element.gsub("lessyequal", "<=") }
|
120
|
+
|
121
|
+
return variables.uniq, line_by_line_contents
|
122
|
+
|
123
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
def output_javascript(file_contents, output_file, temporary_nila_file)
|
2
|
+
|
3
|
+
file_id = open(output_file, 'w')
|
4
|
+
|
5
|
+
File.delete(temporary_nila_file)
|
6
|
+
|
7
|
+
file_id.write("//Written using Nila. Visit http://adhithyan15.github.io/nila\n")
|
8
|
+
|
9
|
+
file_id.write(file_contents.join)
|
10
|
+
|
11
|
+
file_id.close()
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
def parse_arguments(input_argv)
|
2
|
+
|
3
|
+
argument_map = {
|
4
|
+
|
5
|
+
%w{c compile} => "compile",
|
6
|
+
|
7
|
+
%w{r run} => "run",
|
8
|
+
|
9
|
+
%w{h help} => "help",
|
10
|
+
|
11
|
+
%w{v version} => "version",
|
12
|
+
|
13
|
+
%w{b build} => "build",
|
14
|
+
|
15
|
+
%w{u update} => "update",
|
16
|
+
|
17
|
+
%w{re release} => "release",
|
18
|
+
|
19
|
+
}
|
20
|
+
|
21
|
+
output_hash = {}
|
22
|
+
|
23
|
+
argument_map.each do |key,val|
|
24
|
+
|
25
|
+
if input_argv.include?("-#{key[0]}") or input_argv.include?("--#{key[1]}")
|
26
|
+
|
27
|
+
output_hash[val.to_sym] = input_argv.reject {|element| element.include?("-#{key[0]}")} if input_argv.include?("-#{key[0]}")
|
28
|
+
|
29
|
+
output_hash[val.to_sym] = input_argv.reject {|element| element.include?("--#{key[1]}")} if input_argv.include?("--#{key[1]}")
|
30
|
+
|
31
|
+
else
|
32
|
+
|
33
|
+
output_hash[val.to_sym] = nil
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
return output_hash
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,457 @@
|
|
1
|
+
require_relative 'find_all_matching_indices'
|
2
|
+
|
3
|
+
require_relative 'read_file_line_by_line'
|
4
|
+
|
5
|
+
def pretty_print_javascript(javascript_file_contents, temporary_nila_file,declarable_variables)
|
6
|
+
|
7
|
+
def reset_tabs(input_file_contents)
|
8
|
+
|
9
|
+
#This method removes all the predefined tabs to avoid problems in
|
10
|
+
#later parts of the beautifying process.
|
11
|
+
|
12
|
+
for x in 0...input_file_contents.length
|
13
|
+
|
14
|
+
current_row = input_file_contents[x]
|
15
|
+
|
16
|
+
if !current_row.eql?("\n")
|
17
|
+
|
18
|
+
current_row = current_row.lstrip
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
input_file_contents[x] = current_row
|
23
|
+
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
return input_file_contents
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
def convert_string_to_array(input_string, temporary_nila_file)
|
32
|
+
|
33
|
+
file_id = open(temporary_nila_file, 'w')
|
34
|
+
|
35
|
+
file_id.write(input_string)
|
36
|
+
|
37
|
+
file_id.close()
|
38
|
+
|
39
|
+
line_by_line_contents = read_file_line_by_line(temporary_nila_file)
|
40
|
+
|
41
|
+
return line_by_line_contents
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
def fix_newlines(file_contents)
|
46
|
+
|
47
|
+
def extract_blocks(file_contents)
|
48
|
+
|
49
|
+
javascript_regexp = /(if |while |for |case |default:|switch\(|function\(|((=|:)\s+\{))/
|
50
|
+
|
51
|
+
block_starting_lines = file_contents.dup.reject { |element| element.index(javascript_regexp).nil? }[1..-1]
|
52
|
+
|
53
|
+
block_starting_lines = block_starting_lines.reject { |element| element.include?(" ") }
|
54
|
+
|
55
|
+
initial_starting_lines = block_starting_lines.dup
|
56
|
+
|
57
|
+
starting_line_indices = []
|
58
|
+
|
59
|
+
block_starting_lines.each do |line|
|
60
|
+
|
61
|
+
starting_line_indices << file_contents.index(line)
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
block_ending_lines = file_contents.dup.each_index.select { |index| (file_contents[index].eql? " }\n" or file_contents[index].eql? " };\n" or file_contents[index].lstrip.eql?("});\n"))}
|
66
|
+
|
67
|
+
modified_file_contents = file_contents.dup
|
68
|
+
|
69
|
+
code_blocks = []
|
70
|
+
|
71
|
+
starting_index = starting_line_indices[0]
|
72
|
+
|
73
|
+
begin
|
74
|
+
|
75
|
+
for x in 0...initial_starting_lines.length
|
76
|
+
|
77
|
+
code_blocks << modified_file_contents[starting_index..block_ending_lines[0]]
|
78
|
+
|
79
|
+
modified_file_contents[starting_index..block_ending_lines[0]] = []
|
80
|
+
|
81
|
+
modified_file_contents.insert(starting_index, " *****")
|
82
|
+
|
83
|
+
block_starting_lines = modified_file_contents.dup.reject { |element| element.index(javascript_regexp).nil? }[1..-1]
|
84
|
+
|
85
|
+
block_starting_lines = block_starting_lines.reject { |element| element.include?(" ") }
|
86
|
+
|
87
|
+
starting_line_indices = []
|
88
|
+
|
89
|
+
block_starting_lines.each do |line|
|
90
|
+
|
91
|
+
starting_line_indices << modified_file_contents.index(line)
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
block_ending_lines = modified_file_contents.dup.each_index.select { |index| (modified_file_contents[index].eql? " }\n" or modified_file_contents[index].eql? " };\n" or modified_file_contents[index].lstrip.eql?("});\n")) }
|
96
|
+
|
97
|
+
starting_index = starting_line_indices[0]
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
rescue TypeError
|
102
|
+
#
|
103
|
+
puts "Whitespace was left unfixed!"
|
104
|
+
#
|
105
|
+
rescue ArgumentError
|
106
|
+
#
|
107
|
+
puts "Whitespace was left unfixed!"
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
return modified_file_contents, code_blocks
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
compact_contents = file_contents.reject { |element| element.lstrip.eql? "" }
|
116
|
+
|
117
|
+
compact_contents, code_blocks = extract_blocks(compact_contents)
|
118
|
+
|
119
|
+
processed_contents = compact_contents[1...-1].collect { |line| line+"\n" }
|
120
|
+
|
121
|
+
compact_contents = [compact_contents[0]] + processed_contents + [compact_contents[-1]]
|
122
|
+
|
123
|
+
code_block_locations = compact_contents.each_index.select { |index| compact_contents[index].eql? " *****\n" }
|
124
|
+
|
125
|
+
initial_locations = code_block_locations.dup
|
126
|
+
|
127
|
+
starting_index = code_block_locations[0]
|
128
|
+
|
129
|
+
for x in 0...initial_locations.length
|
130
|
+
|
131
|
+
code_blocks[x][-1] = code_blocks[x][-1] + "\n"
|
132
|
+
|
133
|
+
compact_contents = compact_contents[0...starting_index] + code_blocks[x] + compact_contents[starting_index+1..-1]
|
134
|
+
|
135
|
+
code_block_locations = compact_contents.each_index.select { |index| compact_contents[index].eql? " *****\n" }
|
136
|
+
|
137
|
+
starting_index = code_block_locations[0]
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
return compact_contents
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
def roll_blocks(input_file_contents, code_block_starting_locations)
|
146
|
+
|
147
|
+
if !code_block_starting_locations.empty?
|
148
|
+
|
149
|
+
controlregexp = /(if |for |while |case |default:|switch\(|,function\(|\(function\(|= function\(|((=|:)\s+\{))/
|
150
|
+
|
151
|
+
code_block_starting_locations = [0, code_block_starting_locations, -1].flatten
|
152
|
+
|
153
|
+
possible_blocks = []
|
154
|
+
|
155
|
+
block_counter = 0
|
156
|
+
|
157
|
+
extracted_blocks = []
|
158
|
+
|
159
|
+
for x in 0...code_block_starting_locations.length-1
|
160
|
+
|
161
|
+
possible_blocks << input_file_contents[code_block_starting_locations[x]..code_block_starting_locations[x+1]]
|
162
|
+
|
163
|
+
if possible_blocks.length > 1
|
164
|
+
|
165
|
+
possible_blocks[-1] = possible_blocks[-1][1..-1]
|
166
|
+
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
end_counter = 0
|
172
|
+
|
173
|
+
end_index = []
|
174
|
+
|
175
|
+
current_block = []
|
176
|
+
|
177
|
+
possible_blocks.each_with_index do |block|
|
178
|
+
|
179
|
+
if !block[0].eql?(current_block[-1])
|
180
|
+
|
181
|
+
current_block += block
|
182
|
+
|
183
|
+
else
|
184
|
+
|
185
|
+
current_block += block[1..-1]
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
current_block.each_with_index do |line, index|
|
190
|
+
|
191
|
+
if line.lstrip.eql? "}\n" or line.lstrip.eql?("};\n") or line.lstrip.include?("_!;\n") or line.lstrip.include?("});\n")
|
192
|
+
|
193
|
+
end_counter += 1
|
194
|
+
|
195
|
+
end_index << index
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
if end_counter > 0
|
202
|
+
|
203
|
+
until end_index.empty?
|
204
|
+
|
205
|
+
array_extract = current_block[0..end_index[0]].reverse
|
206
|
+
|
207
|
+
index_counter = 0
|
208
|
+
|
209
|
+
array_extract.each_with_index do |line|
|
210
|
+
|
211
|
+
break if line.index(controlregexp) != nil
|
212
|
+
|
213
|
+
index_counter += 1
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
block_extract = array_extract[0..index_counter].reverse
|
218
|
+
|
219
|
+
extracted_blocks << block_extract
|
220
|
+
|
221
|
+
block_start = current_block.index(block_extract[0])
|
222
|
+
|
223
|
+
block_end = current_block.index(block_extract[-1])
|
224
|
+
|
225
|
+
current_block[block_start..block_end] = "--block#{block_counter}\n"
|
226
|
+
|
227
|
+
block_counter += 1
|
228
|
+
|
229
|
+
end_counter = 0
|
230
|
+
|
231
|
+
end_index = []
|
232
|
+
|
233
|
+
current_block.each_with_index do |line, index|
|
234
|
+
|
235
|
+
if line.lstrip.eql? "}\n" or line.lstrip.eql?("};\n") or line.lstrip.include?("_!;\n") or line.lstrip.include?("});\n")
|
236
|
+
|
237
|
+
end_counter += 1
|
238
|
+
|
239
|
+
end_index << index
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
end
|
244
|
+
|
245
|
+
end
|
246
|
+
|
247
|
+
end
|
248
|
+
|
249
|
+
end
|
250
|
+
|
251
|
+
return current_block, extracted_blocks
|
252
|
+
|
253
|
+
else
|
254
|
+
|
255
|
+
return input_file_contents, []
|
256
|
+
|
257
|
+
end
|
258
|
+
|
259
|
+
end
|
260
|
+
|
261
|
+
def fix_syntax_indentation(input_file_contents)
|
262
|
+
|
263
|
+
fixableregexp = /(else |elsuf )/
|
264
|
+
|
265
|
+
need_fixes = input_file_contents.reject { |line| line.index(fixableregexp).nil? }
|
266
|
+
|
267
|
+
need_fixes.each do |fix|
|
268
|
+
|
269
|
+
input_file_contents[input_file_contents.index(fix)] = input_file_contents[input_file_contents.index(fix)].sub(" ", "")
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
return input_file_contents
|
274
|
+
|
275
|
+
end
|
276
|
+
|
277
|
+
def replace_ignored_words(input_string)
|
278
|
+
|
279
|
+
ignorable_keywords = [/if/, /while/, /function/,/function/]
|
280
|
+
|
281
|
+
dummy_replacement_words = ["eeuuff", "whaalesskkey", "conffoolotion"]
|
282
|
+
|
283
|
+
dummy_replacement_words.each_with_index do |word, index|
|
284
|
+
|
285
|
+
input_string = input_string.sub(word, ignorable_keywords[index].inspect[1...-1])
|
286
|
+
|
287
|
+
end
|
288
|
+
|
289
|
+
return input_string
|
290
|
+
|
291
|
+
end
|
292
|
+
|
293
|
+
javascript_regexp = /(if |for |while |case |default:|switch\(|\(function\(|= function\(|((=|:)\s+\{))/
|
294
|
+
|
295
|
+
if declarable_variables.length > 0
|
296
|
+
|
297
|
+
declaration_string = "var " + declarable_variables.flatten.uniq.sort.join(", ") + ";\n\n"
|
298
|
+
|
299
|
+
javascript_file_contents = [declaration_string,javascript_file_contents].flatten
|
300
|
+
|
301
|
+
end
|
302
|
+
|
303
|
+
javascript_file_contents = javascript_file_contents.collect { |element| element.sub("Euuf", "if") }
|
304
|
+
|
305
|
+
javascript_file_contents = javascript_file_contents.collect { |element| element.sub("whaaleskey", "while") }
|
306
|
+
|
307
|
+
javascript_file_contents = reset_tabs(javascript_file_contents)
|
308
|
+
|
309
|
+
starting_locations = []
|
310
|
+
|
311
|
+
javascript_file_contents.each_with_index do |line, index|
|
312
|
+
|
313
|
+
if line.index(javascript_regexp) != nil
|
314
|
+
|
315
|
+
starting_locations << index
|
316
|
+
|
317
|
+
end
|
318
|
+
|
319
|
+
end
|
320
|
+
|
321
|
+
remaining_file_contents, blocks = roll_blocks(javascript_file_contents, starting_locations)
|
322
|
+
|
323
|
+
joined_file_contents = ""
|
324
|
+
|
325
|
+
if !blocks.empty?
|
326
|
+
|
327
|
+
remaining_file_contents = remaining_file_contents.collect { |element| " " + element }
|
328
|
+
|
329
|
+
main_blocks = remaining_file_contents.reject { |element| !element.include?("--block") }
|
330
|
+
|
331
|
+
main_block_numbers = main_blocks.collect { |element| element.split("--block")[1] }
|
332
|
+
|
333
|
+
modified_blocks = main_blocks.dup
|
334
|
+
|
335
|
+
soft_tabs = " "
|
336
|
+
|
337
|
+
for x in (0...main_blocks.length)
|
338
|
+
|
339
|
+
soft_tabs_counter = 1
|
340
|
+
|
341
|
+
current_block = blocks[main_block_numbers[x].to_i]
|
342
|
+
|
343
|
+
current_block = [soft_tabs + current_block[0]] + current_block[1...-1] + [soft_tabs*(soft_tabs_counter)+current_block[-1]]
|
344
|
+
|
345
|
+
soft_tabs_counter += 1
|
346
|
+
|
347
|
+
current_block = [current_block[0]] + current_block[1...-1].collect { |element| soft_tabs*(soft_tabs_counter)+element } + [current_block[-1]]
|
348
|
+
|
349
|
+
nested_block = current_block.clone.reject { |row| !row.include?("--block") }
|
350
|
+
|
351
|
+
nested_block = nested_block.collect { |element| element.split("--block")[1] }
|
352
|
+
|
353
|
+
nested_block = nested_block.collect { |element| element.rstrip.to_i }
|
354
|
+
|
355
|
+
modified_nested_block = nested_block.clone
|
356
|
+
|
357
|
+
current_block = current_block.join("\n")
|
358
|
+
|
359
|
+
until modified_nested_block.empty?
|
360
|
+
|
361
|
+
nested_block.each do |block_index|
|
362
|
+
|
363
|
+
nested_block_contents = blocks[block_index]
|
364
|
+
|
365
|
+
nested_block_contents = nested_block_contents[0...-1] + [soft_tabs*(soft_tabs_counter)+nested_block_contents[-1]]
|
366
|
+
|
367
|
+
soft_tabs_counter += 1
|
368
|
+
|
369
|
+
nested_block_contents = [nested_block_contents[0]] + nested_block_contents[1...-1].collect { |element| soft_tabs*(soft_tabs_counter)+element } + [nested_block_contents[-1]]
|
370
|
+
|
371
|
+
nested_block_contents = nested_block_contents.reject { |element| element.gsub(" ", "").eql?("") }
|
372
|
+
|
373
|
+
current_block = current_block.sub("--block#{block_index}", nested_block_contents.join)
|
374
|
+
|
375
|
+
blocks[block_index] = nested_block_contents
|
376
|
+
|
377
|
+
modified_nested_block.delete_at(0)
|
378
|
+
|
379
|
+
soft_tabs_counter -= 1
|
380
|
+
|
381
|
+
end
|
382
|
+
|
383
|
+
current_block = convert_string_to_array(current_block, temporary_nila_file)
|
384
|
+
|
385
|
+
nested_block = current_block.reject { |element| !element.include?("--block") }
|
386
|
+
|
387
|
+
nested_block = nested_block.collect { |element| element.split("--block")[1] }
|
388
|
+
|
389
|
+
nested_block = nested_block.collect { |element| element.rstrip.to_i }
|
390
|
+
|
391
|
+
modified_nested_block = nested_block.clone
|
392
|
+
|
393
|
+
current_block = current_block.join
|
394
|
+
|
395
|
+
if !nested_block.empty?
|
396
|
+
|
397
|
+
soft_tabs_counter += 1
|
398
|
+
|
399
|
+
end
|
400
|
+
|
401
|
+
end
|
402
|
+
|
403
|
+
modified_blocks[x] = current_block
|
404
|
+
|
405
|
+
end
|
406
|
+
|
407
|
+
remaining_file_contents = ["(function() {\n", remaining_file_contents, "\n}).call(this);"].flatten
|
408
|
+
|
409
|
+
joined_file_contents = remaining_file_contents.join
|
410
|
+
|
411
|
+
main_blocks.each_with_index do |block_id, index|
|
412
|
+
|
413
|
+
joined_file_contents = joined_file_contents.sub(block_id, modified_blocks[index])
|
414
|
+
|
415
|
+
end
|
416
|
+
|
417
|
+
else
|
418
|
+
|
419
|
+
remaining_file_contents = remaining_file_contents.collect { |element| " " + element }
|
420
|
+
|
421
|
+
remaining_file_contents = ["(function() {\n", remaining_file_contents, "\n}).call(this);"].flatten
|
422
|
+
|
423
|
+
joined_file_contents = remaining_file_contents.join
|
424
|
+
|
425
|
+
end
|
426
|
+
|
427
|
+
file_id = open(temporary_nila_file, 'w')
|
428
|
+
|
429
|
+
file_id.write(joined_file_contents)
|
430
|
+
|
431
|
+
file_id.close()
|
432
|
+
|
433
|
+
line_by_line_contents = read_file_line_by_line(temporary_nila_file)
|
434
|
+
|
435
|
+
line_by_line_contents = line_by_line_contents.collect {|element| element.gsub("%$%$ {","")}
|
436
|
+
|
437
|
+
line_by_line_contents = fix_newlines(line_by_line_contents)
|
438
|
+
|
439
|
+
removable_indices = line_by_line_contents.each_index.select {|index| line_by_line_contents[index].strip == "%$%$;" }
|
440
|
+
|
441
|
+
while line_by_line_contents.join.include?("%$%$;")
|
442
|
+
|
443
|
+
line_by_line_contents.delete_at(removable_indices[0])
|
444
|
+
|
445
|
+
line_by_line_contents.delete_at(removable_indices[0])
|
446
|
+
|
447
|
+
removable_indices = line_by_line_contents.each_index.select {|index| line_by_line_contents[index].strip == "%$%$;" }
|
448
|
+
|
449
|
+
end
|
450
|
+
|
451
|
+
line_by_line_contents = fix_syntax_indentation(line_by_line_contents)
|
452
|
+
|
453
|
+
line_by_line_contents = line_by_line_contents.collect { |element| replace_ignored_words(element) }
|
454
|
+
|
455
|
+
return line_by_line_contents
|
456
|
+
|
457
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative 'read_file_line_by_line'
|
2
|
+
|
3
|
+
def remove_question_marks(input_file_contents, variable_list, temporary_nila_file)
|
4
|
+
|
5
|
+
#A method to remove question marks from global variable names. Local variables are dealt
|
6
|
+
#with in their appropriate scope.
|
7
|
+
|
8
|
+
#Params:
|
9
|
+
#input_file_contents => An array containing the contents of the input nila file
|
10
|
+
#variable_list => An array containing all the global variables declared in the file
|
11
|
+
#temporary_nila_file => A file object used to write temporary contents
|
12
|
+
|
13
|
+
#Example:
|
14
|
+
|
15
|
+
#Nila
|
16
|
+
#isprime? = false
|
17
|
+
|
18
|
+
#Javascript Output
|
19
|
+
#var isprime;
|
20
|
+
#isprime = false;
|
21
|
+
|
22
|
+
#Returns a modified input_file_contents with all the question marks removed
|
23
|
+
|
24
|
+
joined_file_contents = input_file_contents.join
|
25
|
+
|
26
|
+
variable_list.each do |var|
|
27
|
+
|
28
|
+
if var.include? "?"
|
29
|
+
|
30
|
+
joined_file_contents = joined_file_contents.gsub(var, var[0...-1])
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
file_id = open(temporary_nila_file, 'w')
|
37
|
+
|
38
|
+
file_id.write(joined_file_contents)
|
39
|
+
|
40
|
+
file_id.close()
|
41
|
+
|
42
|
+
line_by_line_contents = read_file_line_by_line(temporary_nila_file)
|
43
|
+
|
44
|
+
return line_by_line_contents
|
45
|
+
|
46
|
+
end
|