nilac 0.0.4.3.9.7.1 → 0.0.4.3.9.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -2
  3. data/Rakefile +1 -1
  4. data/cookbook/case_insensitive_compare.js +13 -0
  5. data/cookbook/case_insensitive_compare.nila +7 -0
  6. data/cookbook/compare_strings.js +13 -0
  7. data/cookbook/compare_strings.nila +7 -0
  8. data/cookbook/concat11.js +13 -0
  9. data/cookbook/concat11.nila +4 -0
  10. data/cookbook/concat12.js +23 -0
  11. data/cookbook/concat12.nila +15 -0
  12. data/cookbook/find_substring.js +15 -0
  13. data/cookbook/find_substring.nila +5 -0
  14. data/cookbook/string_interpolation.js +7 -0
  15. data/cookbook/string_interpolation.nila +1 -0
  16. data/cookbook/substring_extract.js +21 -0
  17. data/cookbook/substring_extract.nila +8 -0
  18. data/examples/my_file.js +5 -0
  19. data/examples/repl.js +0 -33
  20. data/examples/repl.nila +36 -8
  21. data/examples/temp_nila.nila +63 -0
  22. data/lib/nilac.rb +83 -68
  23. data/lib/nilac/add_line_numbers.rb +17 -0
  24. data/lib/nilac/add_semicolons.rb +13 -3
  25. data/lib/nilac/compile_arrays.rb +38 -5
  26. data/lib/nilac/compile_blocks.rb +87 -25
  27. data/lib/nilac/compile_case_statement.rb +4 -0
  28. data/lib/nilac/compile_chained_comparison.rb +35 -0
  29. data/lib/nilac/compile_comments.rb +9 -0
  30. data/lib/nilac/compile_conditional_structures.rb +26 -3
  31. data/lib/nilac/compile_custom_function_map.rb +24 -2
  32. data/lib/nilac/compile_hashes.rb +3 -1
  33. data/lib/nilac/compile_heredocs.rb +2 -0
  34. data/lib/nilac/compile_inline_for.rb +13 -0
  35. data/lib/nilac/compile_interpolated_strings.rb +184 -5
  36. data/lib/nilac/compile_lambdas.rb +24 -8
  37. data/lib/nilac/compile_new_keyword.rb +37 -0
  38. data/lib/nilac/compile_nilac_options.rb +79 -0
  39. data/lib/nilac/compile_parallel_assignment.rb +3 -1
  40. data/lib/nilac/compile_ranges.rb +60 -0
  41. data/lib/nilac/compile_require_statements.rb +4 -2
  42. data/lib/nilac/compile_ruby_math.rb +20 -0
  43. data/lib/nilac/compile_ruby_methods.rb +49 -0
  44. data/lib/nilac/compile_whitespace_delimited_functions.rb +15 -1
  45. data/lib/nilac/extract_paranthesis_contents.rb +37 -0
  46. data/lib/nilac/extract_strings.rb +63 -0
  47. data/lib/nilac/fix_javascript_traps.rb +89 -0
  48. data/lib/nilac/friendly_errors.rb +128 -0
  49. data/lib/nilac/get_variables.rb +48 -14
  50. data/lib/nilac/lexical_scoped_function_variables.rb +1 -3
  51. data/lib/nilac/optimizer.rb +13 -0
  52. data/lib/nilac/paranthesis_compactor.rb +3 -1
  53. data/lib/nilac/parse_arguments.rb +21 -7
  54. data/lib/nilac/pretty_print_javascript.rb +152 -18
  55. data/lib/nilac/pretty_print_nila.rb +17 -0
  56. data/lib/nilac/replace_comparison_operators.rb +25 -0
  57. data/lib/nilac/replace_multiline_comments.rb +4 -0
  58. data/lib/nilac/replace_singleline_comments.rb +23 -0
  59. data/lib/nilac/replace_strings.rb +3 -3
  60. data/lib/nilac/rollblocks.rb +7 -3
  61. data/lib/nilac/split_semicolon_seperated_expressions.rb +3 -17
  62. data/lib/nilac/square_brackets_compactor.rb +37 -0
  63. data/lib/nilac/version.rb +1 -1
  64. data/shark/features/arrays.feature +11 -0
  65. data/shark/features/builtin_new.feature +11 -0
  66. data/shark/features/comments.feature +11 -0
  67. data/shark/features/fix_newlines.feature +1 -1
  68. data/shark/features/variables.feature +11 -0
  69. data/shark/test_files/array_string_indexing.nila +5 -1
  70. data/shark/test_files/arrays.nila +3 -0
  71. data/shark/test_files/blocks.nila +10 -0
  72. data/shark/test_files/builtin_new.nila +3 -0
  73. data/shark/test_files/case.nila +8 -0
  74. data/shark/test_files/chained_comparison.nila +19 -0
  75. data/shark/test_files/comments.nila +15 -0
  76. data/shark/test_files/correct.js +1 -1
  77. data/shark/test_files/correct_arrays.js +7 -0
  78. data/shark/test_files/correct_blocks.js +14 -1
  79. data/shark/test_files/correct_builtin_new.js +9 -0
  80. data/shark/test_files/correct_case.js +10 -2
  81. data/shark/test_files/correct_comments.js +19 -0
  82. data/shark/test_files/correct_conditional_assignment.js +1 -1
  83. data/shark/test_files/correct_default_parameters.js +1 -1
  84. data/shark/test_files/correct_for.js +1 -1
  85. data/shark/test_files/correct_hashes.js +1 -1
  86. data/shark/test_files/correct_heredoc.js +6 -2
  87. data/shark/test_files/correct_if_then_else.js +5 -1
  88. data/shark/test_files/correct_indexing.js +7 -3
  89. data/shark/test_files/correct_initialization.js +1 -1
  90. data/shark/test_files/correct_javascript_methods.js +1 -1
  91. data/shark/test_files/correct_loop.js +1 -1
  92. data/shark/test_files/correct_monkey_patch.js +1 -1
  93. data/shark/test_files/correct_multiline_array.js +1 -1
  94. data/shark/test_files/correct_multiple_return.js +1 -1
  95. data/shark/test_files/correct_numbers.js +1 -1
  96. data/shark/test_files/correct_operators.js +1 -1
  97. data/shark/test_files/correct_regular_if.js +1 -1
  98. data/shark/test_files/correct_regular_while.js +1 -1
  99. data/shark/test_files/correct_required_module.js +1 -1
  100. data/shark/test_files/correct_return.js +1 -1
  101. data/shark/test_files/correct_ruby_methods.js +1 -1
  102. data/shark/test_files/correct_single_return.js +1 -1
  103. data/shark/test_files/{perfect.js → correct_spacing.js} +1 -1
  104. data/shark/test_files/correct_splats.js +1 -2
  105. data/shark/test_files/correct_string_interpolation.js +8 -2
  106. data/shark/test_files/correct_string_operators.js +1 -1
  107. data/shark/test_files/correct_times.js +1 -1
  108. data/shark/test_files/correct_unless_until.js +2 -4
  109. data/shark/test_files/correct_variables.js +13 -0
  110. data/shark/test_files/correct_whitespace_delimiter.js +1 -1
  111. data/shark/test_files/existential_operators.nila +7 -0
  112. data/shark/test_files/expression.nila +6 -0
  113. data/shark/test_files/global_variables_client.nila +5 -0
  114. data/shark/test_files/heredoc.nila +11 -1
  115. data/shark/test_files/if_then_else.nila +3 -1
  116. data/shark/test_files/inline_for.nila +5 -0
  117. data/shark/test_files/no_return.js +11 -0
  118. data/shark/test_files/parseInt_trap.nila +3 -0
  119. data/shark/test_files/single_return.js +11 -0
  120. data/shark/test_files/string_interpolation.nila +7 -1
  121. data/shark/test_files/variables.nila +7 -0
  122. metadata +54 -3
@@ -14,7 +14,9 @@ def compile_custom_function_map(input_file_contents)
14
14
 
15
15
  "alert" => "alert",
16
16
 
17
- "prompt" => "prompt"
17
+ "prompt" => "prompt",
18
+
19
+ "parseInt" => "parseInt"
18
20
 
19
21
  }
20
22
 
@@ -28,6 +30,8 @@ def compile_custom_function_map(input_file_contents)
28
30
 
29
31
  function_map.each do |function|
30
32
 
33
+ include_comment = false
34
+
31
35
  test_line = replace_strings(line)
32
36
 
33
37
  if test_line.include?(function+"(") or test_line.include?(function+" ") and test_line.index(javascript_regexp) == nil
@@ -38,9 +42,27 @@ def compile_custom_function_map(input_file_contents)
38
42
 
39
43
  testsplit[0] = " " if testsplit[0].eql?("")
40
44
 
45
+ if testsplit[1].include?("--single_line_comment")
46
+
47
+ include_comment = true
48
+
49
+ end
50
+
41
51
  if testsplit[0][-1].eql?(" ") or testsplit[0].eql?("return")
42
52
 
43
- modified_file_contents[index] = line.gsub(function, function_map_replacements[function])
53
+ if include_comment
54
+
55
+ modified_line, comment = line.split("--single_line_comment")
56
+
57
+ modified_file_contents[index] = modified_line.gsub(function, function_map_replacements[function])
58
+
59
+ modified_file_contents[index] = modified_file_contents[index] + "--single_line_comment#{comment}"
60
+
61
+ else
62
+
63
+ modified_file_contents[index] = line.gsub(function, function_map_replacements[function])
64
+
65
+ end
44
66
 
45
67
  end
46
68
 
@@ -14,6 +14,8 @@ require_relative 'read_file_line_by_line'
14
14
 
15
15
  possible_hashes = input_file_contents.reject { |element| !element.include?("{") }
16
16
 
17
+ possible_hashes = possible_hashes.reject {|element| element.include?("loop")}
18
+
17
19
  possible_multiline_hashes = possible_hashes.reject { |element| element.include?("}") }
18
20
 
19
21
  possible_multiline_hashes = possible_multiline_hashes.reject {|element| element.index(javascript_regexp) != nil}
@@ -68,7 +70,7 @@ require_relative 'read_file_line_by_line'
68
70
 
69
71
  def compile_inline_hashes(input_file_contents)
70
72
 
71
- javascript_regexp = /(if |while |for |function |function\(|%[qQw]*\{|lambda\s*\{|\s*->\s*\{)/
73
+ javascript_regexp = /(if |while |for |function |function\(|%[qQw]*\{|lambda\s*\{|\s*->\s*\{|loop\s*)/
72
74
 
73
75
  modified_file_contents = input_file_contents.clone.collect {|element| replace_strings(element)}
74
76
 
@@ -44,6 +44,8 @@ require_relative 'read_file_line_by_line'
44
44
 
45
45
  end
46
46
 
47
+ replacement_string = replacement_string.gsub("\\#","#")
48
+
47
49
  joined_file_contents = joined_file_contents.sub(heredoc + heredoc_extract + delimiter, replacement_string)
48
50
 
49
51
  end
@@ -0,0 +1,13 @@
1
+ require_relative 'replace_strings'
2
+
3
+ def compile_inline_for(input_file_contents)
4
+
5
+ test_regex = /( for )/
6
+
7
+ possible_inline_for = input_file_contents.reject {|element| element.index(test_regex).nil?}
8
+
9
+ possible_inline_for = possible_inline_for.reject {|element| element.rstrip.lstrip.index(test_regex).nil?}
10
+
11
+
12
+
13
+ end
@@ -1,7 +1,74 @@
1
1
  require_relative 'find_all_matching_indices'
2
+ require_relative 'replace_strings'
3
+ require_relative 'extract_strings'
4
+ require_relative 'replace_comparison_operators'
2
5
 
3
6
  def compile_interpolated_strings(input_file_contents)
4
7
 
8
+ def interpolation_variables(input_string)
9
+
10
+ interpolation_vars = []
11
+
12
+ modified_expressions = []
13
+
14
+ input_string = input_string[2..-1]
15
+
16
+ if replace_strings(input_string).include?(",")
17
+
18
+ replaced_string = replace_strings(input_string)
19
+
20
+ extracted_strings = extract_strings(input_string)
21
+
22
+ semicolon_seperated_expressions = replaced_string.split(",")
23
+
24
+ semicolon_seperated_expressions.each do |expr|
25
+
26
+ matched_strings = expr.scan(/--repstring\d+/).to_a
27
+
28
+ if matched_strings.empty?
29
+
30
+ modified_expressions << expr
31
+
32
+ else
33
+
34
+ matched_strings.each do |str|
35
+
36
+ junk,string_id = str.split("--repstring")
37
+
38
+ modified_expressions << expr.sub("--repstring#{string_id}",extracted_strings[string_id.to_i])
39
+
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ if modified_expressions.empty?
49
+
50
+ modified_expressions << input_string
51
+
52
+ end
53
+
54
+ modified_expressions.each do |expr|
55
+
56
+ if replace_strings(expr).include?("=")
57
+
58
+ replaced_string = replace_strings(expr)
59
+
60
+ string_split = replaced_string.split("=")
61
+
62
+ interpolation_vars << string_split[0].strip
63
+
64
+ end
65
+
66
+ end
67
+
68
+ return interpolation_vars
69
+
70
+ end
71
+
5
72
  def replace_string_arrays(input_string)
6
73
 
7
74
  def extract(input_string, pattern_start, pattern_end)
@@ -32,10 +99,16 @@ require_relative 'find_all_matching_indices'
32
99
 
33
100
  end
34
101
 
102
+ interpolation_vars = []
103
+
35
104
  modified_file_contents = input_file_contents.dup
36
105
 
37
106
  single_quoted_strings = input_file_contents.reject { |element| !(element.count("'") >= 2) }
38
107
 
108
+ single_quoted_string_array = []
109
+
110
+ single_quote_counter = 0
111
+
39
112
  single_quoted_strings.each do |str|
40
113
 
41
114
  modified_string = str.dup
@@ -46,7 +119,11 @@ require_relative 'find_all_matching_indices'
46
119
 
47
120
  string_extract = modified_string[first_index..modified_string.index("'", first_index+1)]
48
121
 
49
- modified_string = modified_string.sub(string_extract, "--single_quoted")
122
+ single_quoted_string_array << string_extract
123
+
124
+ modified_string = modified_string.sub(string_extract, "--single_quoted#{single_quote_counter}")
125
+
126
+ single_quote_counter += 1
50
127
 
51
128
  end
52
129
 
@@ -60,6 +137,20 @@ require_relative 'find_all_matching_indices'
60
137
 
61
138
  modified_line = line.dup
62
139
 
140
+ possible_single_quoted_strings = modified_line.scan(/--single_quoted\d+/).to_a
141
+
142
+ unless possible_single_quoted_strings.empty?
143
+
144
+ possible_single_quoted_strings.each do |quoted_string|
145
+
146
+ junk,string_id = quoted_string.split("--single_quoted")
147
+
148
+ modified_line = modified_line.sub("--single_quoted#{string_id}",single_quoted_string_array[string_id.to_i])
149
+
150
+ end
151
+
152
+ end
153
+
63
154
  interpol_starting_loc = find_all_matching_indices(modified_line, "\#{") + [-1]
64
155
 
65
156
  interpolated_strings = []
@@ -130,25 +221,111 @@ require_relative 'find_all_matching_indices'
130
221
 
131
222
  interpolated_strings.each do |interpol|
132
223
 
224
+ original_interpol = interpol.clone
225
+
133
226
  string_split = line.split(interpol)
134
227
 
135
228
  if string_split[1].eql?("\"\n")
136
229
 
230
+ if replace_strings(interpol).include?(";")
231
+
232
+ interpol = replace_strings(interpol)
233
+
234
+ string_extracts = extract_strings(interpol)
235
+
236
+ interpol = interpol.gsub(";",",")
237
+
238
+ string_extracts.each do |str|
239
+
240
+ matched_strings = str.scan(/--repstring/).to_a
241
+
242
+ matched_strings.each do |matched_string|
243
+
244
+ junk,string_id = matched_string.split("--repstring")
245
+
246
+ interpol = interpol.sub("--repstring#{string_id}",string_extracts[string_id.to_i])
247
+
248
+ end
249
+
250
+ end
251
+
252
+ interpol = replace_comparison_operators(interpol)
253
+
254
+ end
255
+
256
+ interpolation_variables(interpol)
257
+
137
258
  replacement_string = "\" + " + "(#{interpol[2...-1]})"
138
259
 
139
- modified_file_contents[index] = modified_file_contents[index].sub(interpol+"\"", replacement_string)
260
+ modified_file_contents[index] = modified_file_contents[index].sub(original_interpol+"\"", replacement_string)
140
261
 
141
262
  elsif string_split[1].eql?("\")\n")
142
263
 
264
+ if replace_strings(interpol).include?(";")
265
+
266
+ interpol = replace_strings(interpol)
267
+
268
+ string_extracts = extract_strings(interpol)
269
+
270
+ interpol = interpol.gsub(";",",")
271
+
272
+ string_extracts.each do |str|
273
+
274
+ matched_strings = str.scan(/--repstring/).to_a
275
+
276
+ matched_strings.each do |matched_string|
277
+
278
+ junk,string_id = matched_string.split("--repstring")
279
+
280
+ interpol = interpol.sub("--repstring#{string_id}",string_extracts[string_id.to_i])
281
+
282
+ end
283
+
284
+ end
285
+
286
+ interpol = replace_comparison_operators(interpol)
287
+
288
+ end
289
+
290
+ interpolation_variables(interpol)
291
+
143
292
  replacement_string = "\" + " + "(#{interpol[2...-1]})"
144
293
 
145
- modified_file_contents[index] = modified_file_contents[index].sub(interpol + "\"", replacement_string)
294
+ modified_file_contents[index] = modified_file_contents[index].sub(original_interpol + "\"", replacement_string)
146
295
 
147
296
  else
148
297
 
298
+ if replace_strings(interpol).include?(";")
299
+
300
+ interpol = replace_strings(interpol)
301
+
302
+ string_extracts = extract_strings(interpol)
303
+
304
+ interpol = interpol.gsub(";",",")
305
+
306
+ string_extracts.each do |str|
307
+
308
+ matched_strings = str.scan(/--repstring/).to_a
309
+
310
+ matched_strings.each do |matched_string|
311
+
312
+ junk,string_id = matched_string.split("--repstring")
313
+
314
+ interpol = interpol.sub("--repstring#{string_id}",string_extracts[string_id.to_i])
315
+
316
+ end
317
+
318
+ end
319
+
320
+ interpol = replace_comparison_operators(interpol)
321
+
322
+ end
323
+
324
+ interpolation_vars << interpolation_variables(interpol)
325
+
149
326
  replacement_string = "\"" + " + " + "(#{interpol[2...-1]})" + " + \""
150
327
 
151
- modified_file_contents[index] = modified_file_contents[index].sub(interpol, replacement_string)
328
+ modified_file_contents[index] = modified_file_contents[index].sub(original_interpol, replacement_string)
152
329
 
153
330
  end
154
331
 
@@ -158,6 +335,8 @@ require_relative 'find_all_matching_indices'
158
335
 
159
336
  end
160
337
 
161
- return modified_file_contents
338
+ interpolation_vars = interpolation_vars.flatten.uniq
339
+
340
+ return modified_file_contents, interpolation_vars
162
341
 
163
342
  end
@@ -12,7 +12,15 @@ def compile_lambdas(input_file_contents,temporary_nila_file)
12
12
 
13
13
  if parameterless
14
14
 
15
- block_contents = input_block[3...-1]
15
+ if input_block[0] = "{" and input_block[-1] = "}"
16
+
17
+ block_contents = input_block[1...-1]
18
+
19
+ else
20
+
21
+ block_contents = input_block[3...-1]
22
+
23
+ end
16
24
 
17
25
  compiled_lambda = "function() {\n\n #{block_contents.strip} \n\n}"
18
26
 
@@ -48,7 +56,9 @@ def compile_lambdas(input_file_contents,temporary_nila_file)
48
56
 
49
57
  input_file_contents = input_file_contents.collect {|element| (replace_strings(element).include?(" -> ") ? element.gsub(" -> "," lambda ") : element)}
50
58
 
51
- input_file_contents = input_file_contents.collect {|element| element.gsub("append","appand")}
59
+ input_file_contents = input_file_contents.collect {|element| element.gsub("document","decccccumennt")}
60
+
61
+ input_file_contents = input_file_contents.collect {|element| element.gsub("append","appand").gsub(" do"," do ").gsub("do "," do ")}
52
62
 
53
63
  possible_lambdas = input_file_contents.reject {|line| !replace_strings(line).include?("lambda")}
54
64
 
@@ -74,7 +84,7 @@ def compile_lambdas(input_file_contents,temporary_nila_file)
74
84
 
75
85
  loop_extract = input_file_contents[starting_counter..index_counter]
76
86
 
77
- var_name,block = loop_extract.join.split(/\s*do/)
87
+ var_name,block = loop_extract.join.split(/\s*do\s+/)
78
88
 
79
89
  var_name = var_name.split(/\s*=\s*lambda/)[0].strip
80
90
 
@@ -92,15 +102,17 @@ def compile_lambdas(input_file_contents,temporary_nila_file)
92
102
 
93
103
  possible_lambdas = possible_lambdas.reject {|element| !element.include?("{") and !element.include?("}")}
94
104
 
95
- possible_lambdas = possible_lambdas.collect {|element| element.gsub(/lambda\s*\{/,"lambda !_{")}
96
-
97
105
  modified_file_contents = input_file_contents.clone
98
106
 
99
107
  unless possible_lambdas.empty?
100
108
 
101
- possible_lambdas.each do |loop|
109
+ original_loops = possible_lambdas.clone
110
+
111
+ possible_lambdas = possible_lambdas.collect {|element| element.gsub(/lambda\s*\{/,"lambda !_{")}
102
112
 
103
- original_loop = loop.clone
113
+ possible_lambdas.each_with_index do |loop,index|
114
+
115
+ original_loop = original_loops[index]
104
116
 
105
117
  string_counter = 1
106
118
 
@@ -182,7 +194,9 @@ def compile_lambdas(input_file_contents,temporary_nila_file)
182
194
 
183
195
  replacement_string = replacement_array.join
184
196
 
185
- modified_file_contents[input_file_contents.index(original_loop)] = replacement_string[-1].eql?(";") ? replacement_string : replacement_string + ";"
197
+ replacement_string = replacement_string[-1].eql?(";") ? replacement_string : (replacement_string + ";")
198
+
199
+ modified_file_contents[input_file_contents.index(original_loop)] = (replacement_string + "\n\n")
186
200
 
187
201
  end
188
202
 
@@ -190,6 +204,8 @@ def compile_lambdas(input_file_contents,temporary_nila_file)
190
204
 
191
205
  modified_file_contents = modified_file_contents.collect {|element| element.gsub("appand","append")}
192
206
 
207
+ modified_file_contents = modified_file_contents.collect {|element| element.gsub("decccccumennt","document")}
208
+
193
209
  file_id = open(temporary_nila_file, 'w')
194
210
 
195
211
  file_id.write(modified_file_contents.join)
@@ -0,0 +1,37 @@
1
+ require_relative 'replace_strings'
2
+
3
+ def compile_new_keyword(input_file_contents)
4
+
5
+ # This construct compiles builtin Ruby type initializations to Javascript equivalents
6
+
7
+ # Eg: String.new(somestring) => new String(somestring)
8
+
9
+ match_hash = {
10
+
11
+ "String.new" => "new String",
12
+
13
+ }
14
+
15
+ replaced_contents = input_file_contents.collect {|element| replace_strings(element)}
16
+
17
+ matchables = match_hash.keys
18
+
19
+ matched_values = match_hash.values
20
+
21
+ matchables.each_with_index do |key,key_index|
22
+
23
+ replaced_contents.each_with_index do |element,index|
24
+
25
+ if element.include?(key)
26
+
27
+ input_file_contents[index] = input_file_contents[index].gsub(key,matched_values[key_index])
28
+
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ return input_file_contents
36
+
37
+ end