nilac 0.0.4.3.9.2 → 0.0.4.3.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/bin/nilac +9 -5748
  4. data/examples/StringMagic.nila +23 -0
  5. data/examples/countdown.nila +31 -0
  6. data/examples/decBin.nila +3 -3
  7. data/examples/marky.js +30 -0
  8. data/examples/marky.nila +23 -0
  9. data/lib/nilac/ErrorDeclarations.rb +11 -0
  10. data/lib/nilac/add_semicolons.rb +57 -0
  11. data/lib/nilac/compile_arrays.rb +266 -0
  12. data/lib/nilac/compile_blocks.rb +137 -0
  13. data/lib/nilac/compile_case_statement.rb +168 -0
  14. data/lib/nilac/compile_classes.rb +18 -0
  15. data/lib/nilac/compile_comments.rb +79 -0
  16. data/lib/nilac/compile_conditional_structures.rb +1378 -0
  17. data/lib/nilac/compile_custom_function_map.rb +45 -0
  18. data/lib/nilac/compile_default_values.rb +160 -0
  19. data/lib/nilac/compile_hashes.rb +123 -0
  20. data/lib/nilac/compile_heredocs.rb +62 -0
  21. data/lib/nilac/compile_integers.rb +23 -0
  22. data/lib/nilac/compile_interpolated_strings.rb +133 -0
  23. data/lib/nilac/compile_loops.rb +211 -0
  24. data/lib/nilac/compile_named_functions.rb +690 -0
  25. data/lib/nilac/compile_operators.rb +83 -0
  26. data/lib/nilac/compile_parallel_assignment.rb +103 -0
  27. data/lib/nilac/compile_ruby_methods.rb +58 -0
  28. data/lib/nilac/compile_special_keywords.rb +44 -0
  29. data/lib/nilac/compile_strings.rb +145 -0
  30. data/lib/nilac/compile_whitespace_delimited_functions.rb +97 -0
  31. data/lib/nilac/create_mac_executable.rb +25 -0
  32. data/lib/nilac/extract_parsable_file.rb +23 -0
  33. data/lib/nilac/find_all_matching_indices.rb +18 -0
  34. data/lib/nilac/find_file_name.rb +13 -0
  35. data/lib/nilac/find_file_path.rb +13 -0
  36. data/lib/nilac/get_variables.rb +123 -0
  37. data/lib/nilac/output_javascript.rb +13 -0
  38. data/lib/nilac/parse_arguments.rb +41 -0
  39. data/lib/nilac/pretty_print_javascript.rb +457 -0
  40. data/lib/nilac/read_file_line_by_line.rb +11 -0
  41. data/lib/nilac/remove_question_marks.rb +46 -0
  42. data/lib/nilac/replace_multiline_comments.rb +92 -0
  43. data/lib/nilac/replace_named_functions.rb +154 -0
  44. data/lib/nilac/replace_singleline_comments.rb +45 -0
  45. data/lib/nilac/replace_strings.rb +35 -0
  46. data/lib/nilac/split_semicolon_seperated_expressions.rb +39 -0
  47. data/lib/nilac/strToArray.rb +15 -0
  48. data/lib/nilac/version.rb +1 -1
  49. data/lib/nilac.rb +324 -1
  50. data/nilac.gemspec +0 -1
  51. data/shark/features/add_auto_return_statement.feature +1 -1
  52. data/shark/features/array_and_string_indexing.feature +1 -1
  53. data/shark/features/barebones_compilation.feature +1 -1
  54. data/shark/features/case_when.feature +1 -1
  55. data/shark/features/default_method_parameters.feature +1 -1
  56. data/shark/features/fix_newlines.feature +1 -1
  57. data/shark/features/hashes.feature +1 -1
  58. data/shark/features/heredoc.feature +1 -1
  59. data/shark/features/if_then_else.feature +1 -1
  60. data/shark/features/loop.feature +1 -1
  61. data/shark/features/method_multiple_return.feature +1 -1
  62. data/shark/features/multiline_array.feature +1 -1
  63. data/shark/features/multiple_variable_initialization.feature +1 -1
  64. data/shark/features/numbers.feature +1 -1
  65. data/shark/features/regular_for.feature +1 -1
  66. data/shark/features/regular_if.feature +1 -1
  67. data/shark/features/regular_while.feature +1 -1
  68. data/shark/features/ruby_methods.feature +1 -1
  69. data/shark/features/ruby_operators.feature +1 -1
  70. data/shark/features/splats.feature +1 -1
  71. data/shark/features/string_interpolation.feature +1 -1
  72. data/shark/features/strings.feature +1 -1
  73. data/shark/features/times.feature +1 -1
  74. data/shark/features/unless_until.feature +1 -1
  75. data/shark/features/whitespace_delimitation.feature +1 -1
  76. metadata +46 -18
  77. data/src/nilac.rb +0 -5753
@@ -0,0 +1,1378 @@
1
+ require_relative 'read_file_line_by_line'
2
+
3
+ def compile_conditional_structures(input_file_contents, temporary_nila_file)
4
+
5
+ def replace_unless_until(input_file_contents)
6
+
7
+ modified_file_contents = input_file_contents.clone
8
+
9
+ possible_unless_commands = input_file_contents.reject { |element| !element.include?("unless") }
10
+
11
+ unless_commands = possible_unless_commands.reject { |element| !element.lstrip.split("unless")[0].empty? }
12
+
13
+ unless_commands.each do |command|
14
+
15
+ junk, condition = command.split("unless ")
16
+
17
+ condition = condition.gsub(" and "," && ").gsub(" or "," || ").gsub(" not "," !")
18
+
19
+ replacement_string = "if !(#{condition.lstrip.rstrip})\n"
20
+
21
+ modified_file_contents[modified_file_contents.index(command)] = replacement_string
22
+
23
+ end
24
+
25
+ possible_until_commands = input_file_contents.reject { |element| !element.include?("until") }
26
+
27
+ until_commands = possible_until_commands.reject { |element| !element.lstrip.split("until")[0].empty? }
28
+
29
+ until_commands.each do |command|
30
+
31
+ junk, condition = command.split("until ")
32
+
33
+ condition = condition.gsub(" and "," && ").gsub(" or "," || ").gsub(" not "," !")
34
+
35
+ replacement_string = "while !(#{condition.lstrip.rstrip})\n"
36
+
37
+ modified_file_contents[modified_file_contents.index(command)] = replacement_string
38
+
39
+ end
40
+
41
+ return modified_file_contents
42
+
43
+ end
44
+
45
+ def compile_ternary_if(input_file_contents)
46
+
47
+ possible_ternary_if = input_file_contents.reject{|element| !element.include?("if")}
48
+
49
+ possible_ternary_if = possible_ternary_if.reject {|element| !element.include?("then")}
50
+
51
+ possible_ternary_if.each do |statement|
52
+
53
+ statement_extract = statement[statement.index("if")..statement.index("end")+2]
54
+
55
+ condition = statement_extract.split("then")[0].split("if")[1].lstrip.rstrip
56
+
57
+ true_condition = statement_extract.split("then")[1].split("else")[0].lstrip.rstrip
58
+
59
+ false_condition = statement_extract.split("else")[1].split("end")[0].lstrip.rstrip
60
+
61
+ replacement_string = "#{condition} ? #{true_condition} : #{false_condition}"
62
+
63
+ input_file_contents[input_file_contents.index(statement)] = input_file_contents[input_file_contents.index(statement)].sub(statement_extract,replacement_string)
64
+
65
+ end
66
+
67
+ return input_file_contents
68
+
69
+ end
70
+
71
+ def compile_inline_conditionals(input_file_contents, temporary_nila_file)
72
+
73
+ conditionals = [/( if )/, /( while )/, /( unless )/, /( until )/]
74
+
75
+ plain_conditionals = [" if ", " while ", " unless ", " until "]
76
+
77
+ joined_file_contents = input_file_contents.join
78
+
79
+ output_statement = ""
80
+
81
+ conditionals.each_with_index do |regex, index|
82
+
83
+ matching_lines = input_file_contents.reject { |content| content.index(regex).nil? }
84
+
85
+ matching_lines.each do |line|
86
+
87
+ line_split = line.split(plain_conditionals[index])
88
+
89
+ condition = line_split[1]
90
+
91
+ condition = condition.gsub(" and "," && ").gsub(" or "," || ").gsub(" not "," !")
92
+
93
+ if index == 0
94
+
95
+ output_statement = "if (#{condition.lstrip.rstrip.gsub("?", "")}) {\n\n#{line_split[0]}\n}\n"
96
+
97
+ elsif index == 1
98
+
99
+ output_statement = "while (#{condition.lstrip.rstrip.gsub("?", "")}) {\n\n#{line_split[0]}\n}\n"
100
+
101
+ elsif index == 2
102
+
103
+ output_statement = "if (!(#{condition.lstrip.rstrip.gsub("?", "")})) {\n\n#{line_split[0]}\n}\n"
104
+
105
+ elsif index == 3
106
+
107
+ output_statement = "while (!(#{condition.lstrip.rstrip.gsub("?", "")})) {\n\n#{line_split[0]}\n}\n"
108
+
109
+ end
110
+
111
+ joined_file_contents = joined_file_contents.sub(line, output_statement)
112
+
113
+ end
114
+
115
+ end
116
+
117
+ file_id = open(temporary_nila_file, 'w')
118
+
119
+ file_id.write(joined_file_contents)
120
+
121
+ file_id.close()
122
+
123
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
124
+
125
+ return line_by_line_contents
126
+
127
+ end
128
+
129
+ def compile_regular_if(input_file_contents, temporary_nila_file)
130
+
131
+ def convert_string_to_array(input_string, temporary_nila_file)
132
+
133
+ file_id = open(temporary_nila_file, 'w')
134
+
135
+ file_id.write(input_string)
136
+
137
+ file_id.close()
138
+
139
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
140
+
141
+ return line_by_line_contents
142
+
143
+ end
144
+
145
+ def extract_if_blocks(if_statement_indexes, input_file_contents)
146
+
147
+ possible_if_blocks = []
148
+
149
+ if_block_counter = 0
150
+
151
+ extracted_blocks = []
152
+
153
+ controlregexp = /(if |while |def | do )/
154
+
155
+ rejectionregexp = /( if | while )/
156
+
157
+ for x in 0...if_statement_indexes.length-1
158
+
159
+ possible_if_blocks << input_file_contents[if_statement_indexes[x]..if_statement_indexes[x+1]]
160
+
161
+ end
162
+
163
+ end_counter = 0
164
+
165
+ end_index = []
166
+
167
+ current_block = []
168
+
169
+ possible_if_blocks.each_with_index do |block|
170
+
171
+ unless current_block[-1] == block[0]
172
+
173
+ current_block += block
174
+
175
+ else
176
+
177
+ current_block += block[1..-1]
178
+
179
+ end
180
+
181
+
182
+ current_block.each_with_index do |line, index|
183
+
184
+ if line.strip.eql? "end"
185
+
186
+ end_counter += 1
187
+
188
+ end_index << index
189
+
190
+ end
191
+
192
+ end
193
+
194
+ if end_counter > 0
195
+
196
+ until end_index.empty?
197
+
198
+ array_extract = current_block[0..end_index[0]].reverse
199
+
200
+ index_counter = 0
201
+
202
+ array_extract.each_with_index do |line|
203
+
204
+ break if (line.lstrip.index(controlregexp) != nil and line.lstrip.index(rejectionregexp).nil?)
205
+
206
+ index_counter += 1
207
+
208
+ end
209
+
210
+ block_extract = array_extract[0..index_counter].reverse
211
+
212
+ extracted_blocks << block_extract
213
+
214
+ block_start = current_block.index(block_extract[0])
215
+
216
+ block_end = current_block.index(block_extract[-1])
217
+
218
+ current_block[block_start..block_end] = "--ifblock#{if_block_counter}"
219
+
220
+ if_block_counter += 1
221
+
222
+ end_counter = 0
223
+
224
+ end_index = []
225
+
226
+ current_block.each_with_index do |line, index|
227
+
228
+ if line.strip.eql? "end"
229
+
230
+ end_counter += 1
231
+
232
+ end_index << index
233
+
234
+ end
235
+
236
+ end
237
+
238
+ end
239
+
240
+ end
241
+
242
+ end
243
+
244
+ return current_block, extracted_blocks
245
+
246
+ end
247
+
248
+ def compile_if_syntax(input_block)
249
+
250
+ strings = []
251
+
252
+ string_counter = 0
253
+
254
+ modified_input_block = input_block.dup
255
+
256
+ input_block.each_with_index do |line, index|
257
+
258
+ if line.include?("\"")
259
+
260
+ opening_quotes = line.index("\"")
261
+
262
+ string_extract = line[opening_quotes..line.index("\"", opening_quotes+1)]
263
+
264
+ strings << string_extract
265
+
266
+ modified_input_block[index] = modified_input_block[index].sub(string_extract, "--string{#{string_counter}}")
267
+
268
+ string_counter += 1
269
+
270
+ end
271
+
272
+ end
273
+
274
+ input_block = modified_input_block
275
+
276
+ starting_line = input_block[0]
277
+
278
+ starting_line = starting_line + "\n" if starting_line.lstrip == starting_line
279
+
280
+ junk, condition = starting_line.split("if")
281
+
282
+ condition = condition.gsub(" and "," && ").gsub(" or "," || ").gsub(" not "," !")
283
+
284
+ input_block[0] = "Euuf (#{condition.lstrip.rstrip.gsub("?", "")}) {\n"
285
+
286
+ input_block[-1] = input_block[-1].lstrip.sub("end", "}")
287
+
288
+ elsif_statements = input_block.reject { |element| !element.include?("elsuf") }
289
+
290
+ elsif_statements.each do |statement|
291
+
292
+ junk, condition = statement.split("elsuf")
293
+
294
+ condition = condition.gsub(" and "," && ").gsub(" or "," || ").gsub(" not "," !")
295
+
296
+ input_block[input_block.index(statement)] = "} elsuf (#{condition.lstrip.rstrip.gsub("?", "")}) {\n"
297
+
298
+ end
299
+
300
+ else_statements = input_block.reject { |element| !element.include?("else") }
301
+
302
+ else_statements.each do |statement|
303
+
304
+ input_block[input_block.index(statement)] = "} else {\n"
305
+
306
+ end
307
+
308
+ modified_input_block = input_block.dup
309
+
310
+ input_block.each_with_index do |line, index|
311
+
312
+ if line.include?("--string{")
313
+
314
+ junk, remains = line.split("--string{")
315
+
316
+ string_index, junk = remains.split("}")
317
+
318
+ modified_input_block[index] = modified_input_block[index].sub("--string{#{string_index.strip}}", strings[string_index.strip.to_i])
319
+
320
+ end
321
+
322
+ end
323
+
324
+ return modified_input_block
325
+
326
+ end
327
+
328
+ input_file_contents = input_file_contents.collect { |element| element.sub("elsif", "elsuf") }
329
+
330
+ possible_if_statements = input_file_contents.reject { |element| !element.include?("if") }
331
+
332
+ possible_if_statements = possible_if_statements.reject { |element| element.include?("else") }
333
+
334
+ possible_if_statements = possible_if_statements.reject { |element| element.lstrip.include?(" if ") }
335
+
336
+ if !possible_if_statements.empty?
337
+
338
+ if_statement_indexes = []
339
+
340
+ possible_if_statements.each do |statement|
341
+
342
+ if_statement_indexes << input_file_contents.dup.each_index.select { |index| input_file_contents[index] == statement }
343
+
344
+ end
345
+
346
+ if_statement_indexes = [0] + if_statement_indexes.flatten + [-1]
347
+
348
+ controlregexp = /(while |def | do )/
349
+
350
+ modified_input_contents, extracted_statements = extract_if_blocks(if_statement_indexes, input_file_contents.clone)
351
+
352
+ joined_blocks = extracted_statements.collect { |element| element.join }
353
+
354
+ if_statements = joined_blocks.reject { |element| element.index(controlregexp) != nil }
355
+
356
+ rejected_elements = joined_blocks - if_statements
357
+
358
+ rejected_elements_index = []
359
+
360
+ rejected_elements.each do |element|
361
+
362
+ rejected_elements_index << joined_blocks.each_index.select { |index| joined_blocks[index] == element }
363
+
364
+ end
365
+
366
+ if_blocks_index = (0...extracted_statements.length).to_a
367
+
368
+ rejected_elements_index = rejected_elements_index.flatten
369
+
370
+ if_blocks_index -= rejected_elements_index
371
+
372
+ modified_if_statements = if_statements.collect { |string| convert_string_to_array(string, temporary_nila_file) }
373
+
374
+ modified_if_statements = modified_if_statements.collect { |block| compile_if_syntax(block) }.reverse
375
+
376
+ if_blocks_index = if_blocks_index.collect { |element| "--ifblock#{element}" }.reverse
377
+
378
+ rejected_elements_index = rejected_elements_index.collect { |element| "--ifblock#{element}" }.reverse
379
+
380
+ rejected_elements = rejected_elements.reverse
381
+
382
+ joined_file_contents = modified_input_contents.join
383
+
384
+ until if_blocks_index.empty? and rejected_elements_index.empty?
385
+
386
+ if !if_blocks_index.empty?
387
+
388
+ if joined_file_contents.include?(if_blocks_index[0])
389
+
390
+ joined_file_contents = joined_file_contents.sub(if_blocks_index[0], modified_if_statements[0].join)
391
+
392
+ if_blocks_index.delete_at(0)
393
+
394
+ modified_if_statements.delete_at(0)
395
+
396
+ else
397
+
398
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0], rejected_elements[0])
399
+
400
+ rejected_elements_index.delete_at(0)
401
+
402
+ rejected_elements.delete_at(0)
403
+
404
+ end
405
+
406
+ else
407
+
408
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0], rejected_elements[0])
409
+
410
+ rejected_elements_index.delete_at(0)
411
+
412
+ rejected_elements.delete_at(0)
413
+
414
+ end
415
+
416
+ end
417
+
418
+ else
419
+
420
+ joined_file_contents = input_file_contents.join
421
+
422
+ end
423
+
424
+ file_id = open(temporary_nila_file, 'w')
425
+
426
+ file_id.write(joined_file_contents)
427
+
428
+ file_id.close()
429
+
430
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
431
+
432
+ return line_by_line_contents
433
+
434
+ end
435
+
436
+ def compile_regular_while(input_file_contents, temporary_nila_file)
437
+
438
+ def convert_string_to_array(input_string, temporary_nila_file)
439
+
440
+ file_id = open(temporary_nila_file, 'w')
441
+
442
+ file_id.write(input_string)
443
+
444
+ file_id.close()
445
+
446
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
447
+
448
+ return line_by_line_contents
449
+
450
+ end
451
+
452
+ def extract_while_blocks(while_statement_indexes, input_file_contents)
453
+
454
+ possible_while_blocks = []
455
+
456
+ while_block_counter = 0
457
+
458
+ extracted_blocks = []
459
+
460
+ controlregexp = /(if |while |def | do )/
461
+
462
+ rejectionregexp = /( if | while )/
463
+
464
+ for x in 0...while_statement_indexes.length-1
465
+
466
+ possible_while_blocks << input_file_contents[while_statement_indexes[x]..while_statement_indexes[x+1]]
467
+
468
+ end
469
+
470
+ end_counter = 0
471
+
472
+ end_index = []
473
+
474
+ current_block = []
475
+
476
+ possible_while_blocks.each_with_index do |block|
477
+
478
+ current_block += block
479
+
480
+ current_block.each_with_index do |line, index|
481
+
482
+ if line.strip.eql? "end"
483
+
484
+ end_counter += 1
485
+
486
+ end_index << index
487
+
488
+ end
489
+
490
+ end
491
+
492
+ if end_counter > 0
493
+
494
+ until end_index.empty?
495
+
496
+ array_extract = current_block[0..end_index[0]].reverse
497
+
498
+ index_counter = 0
499
+
500
+ array_extract.each_with_index do |line|
501
+
502
+ break if (line.lstrip.index(controlregexp) != nil and line.lstrip.index(rejectionregexp).nil?)
503
+
504
+ index_counter += 1
505
+
506
+ end
507
+
508
+ block_extract = array_extract[0..index_counter].reverse
509
+
510
+ extracted_blocks << block_extract
511
+
512
+ block_start = current_block.index(block_extract[0])
513
+
514
+ block_end = current_block.index(block_extract[-1])
515
+
516
+ current_block[block_start..block_end] = "--whileblock#{while_block_counter}"
517
+
518
+ while_block_counter += 1
519
+
520
+ end_counter = 0
521
+
522
+ end_index = []
523
+
524
+ current_block.each_with_index do |line, index|
525
+
526
+ if line.strip.eql? "end"
527
+
528
+ end_counter += 1
529
+
530
+ end_index << index
531
+
532
+ end
533
+
534
+ end
535
+
536
+ end
537
+
538
+ end
539
+
540
+ end
541
+
542
+ return current_block, extracted_blocks
543
+
544
+ end
545
+
546
+ def compile_while_syntax(input_block)
547
+
548
+ modified_input_block = input_block.dup
549
+
550
+ strings = []
551
+
552
+ string_counter = 0
553
+
554
+ input_block.each_with_index do |line, index|
555
+
556
+ if line.include?("\"")
557
+
558
+ opening_quotes = line.index("\"")
559
+
560
+ string_extract = line[opening_quotes..line.index("\"", opening_quotes+1)]
561
+
562
+ strings << string_extract
563
+
564
+ modified_input_block[index] = modified_input_block[index].sub(string_extract, "--string{#{string_counter}}")
565
+
566
+ string_counter += 1
567
+
568
+ end
569
+
570
+ end
571
+
572
+ input_block = modified_input_block
573
+
574
+ starting_line = input_block[0]
575
+
576
+ starting_line = starting_line + "\n" if starting_line.lstrip == starting_line
577
+
578
+ junk, condition = starting_line.split("while")
579
+
580
+ input_block[0] = "whaaleskey (#{condition.lstrip.rstrip.gsub("?", "")}) {\n"
581
+
582
+ input_block[-1] = input_block[-1].lstrip.sub("end", "}")
583
+
584
+ modified_input_block = input_block.dup
585
+
586
+ input_block.each_with_index do |line, index|
587
+
588
+ if line.include?("--string{")
589
+
590
+ junk, remains = line.split("--string{")
591
+
592
+ string_index, junk = remains.split("}")
593
+
594
+ modified_input_block[index] = modified_input_block[index].sub("--string{#{string_index.strip}}", strings[string_index.strip.to_i])
595
+
596
+ end
597
+
598
+ end
599
+
600
+ return modified_input_block
601
+
602
+ end
603
+
604
+ possible_while_statements = input_file_contents.reject { |element| !element.include?("while") }
605
+
606
+ if !possible_while_statements.empty?
607
+
608
+ while_statement_indexes = []
609
+
610
+ possible_while_statements.each do |statement|
611
+
612
+ while_statement_indexes << input_file_contents.dup.each_index.select { |index| input_file_contents[index] == statement }
613
+
614
+ end
615
+
616
+ while_statement_indexes = [0] + while_statement_indexes.flatten + [-1]
617
+
618
+ controlregexp = /(if |def | do )/
619
+
620
+ modified_input_contents, extracted_statements = extract_while_blocks(while_statement_indexes, input_file_contents.clone)
621
+
622
+ joined_blocks = extracted_statements.collect { |element| element.join }
623
+
624
+ while_statements = joined_blocks.reject { |element| element.index(controlregexp) != nil }
625
+
626
+ rejected_elements = joined_blocks - while_statements
627
+
628
+ rejected_elements_index = []
629
+
630
+ rejected_elements.each do |element|
631
+
632
+ rejected_elements_index << joined_blocks.each_index.select { |index| joined_blocks[index] == element }
633
+
634
+ end
635
+
636
+ while_blocks_index = (0...extracted_statements.length).to_a
637
+
638
+ rejected_elements_index = rejected_elements_index.flatten
639
+
640
+ while_blocks_index -= rejected_elements_index
641
+
642
+ modified_while_statements = while_statements.collect { |string| convert_string_to_array(string, temporary_nila_file) }
643
+
644
+ modified_while_statements = modified_while_statements.collect { |block| compile_while_syntax(block) }.reverse
645
+
646
+ while_blocks_index = while_blocks_index.collect { |element| "--whileblock#{element}" }.reverse
647
+
648
+ rejected_elements_index = rejected_elements_index.collect { |element| "--whileblock#{element}" }.reverse
649
+
650
+ rejected_elements = rejected_elements.reverse
651
+
652
+ joined_file_contents = modified_input_contents.join
653
+
654
+ until while_blocks_index.empty? and rejected_elements_index.empty?
655
+
656
+ if !while_blocks_index.empty?
657
+
658
+ if joined_file_contents.include?(while_blocks_index[0])
659
+
660
+ joined_file_contents = joined_file_contents.sub(while_blocks_index[0], modified_while_statements[0].join)
661
+
662
+ while_blocks_index.delete_at(0)
663
+
664
+ modified_while_statements.delete_at(0)
665
+
666
+ else
667
+
668
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0], rejected_elements[0])
669
+
670
+ rejected_elements_index.delete_at(0)
671
+
672
+ rejected_elements.delete_at(0)
673
+
674
+ end
675
+
676
+ else
677
+
678
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0], rejected_elements[0])
679
+
680
+ rejected_elements_index.delete_at(0)
681
+
682
+ rejected_elements.delete_at(0)
683
+
684
+ end
685
+
686
+ end
687
+
688
+ else
689
+
690
+ joined_file_contents = input_file_contents.join
691
+
692
+ end
693
+
694
+ file_id = open(temporary_nila_file, 'w')
695
+
696
+ file_id.write(joined_file_contents)
697
+
698
+ file_id.close()
699
+
700
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
701
+
702
+ return line_by_line_contents
703
+
704
+ end
705
+
706
+ def compile_regular_for(input_file_contents, temporary_nila_file)
707
+
708
+ def convert_string_to_array(input_string, temporary_nila_file)
709
+
710
+ file_id = open(temporary_nila_file, 'w')
711
+
712
+ file_id.write(input_string)
713
+
714
+ file_id.close()
715
+
716
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
717
+
718
+ return line_by_line_contents
719
+
720
+ end
721
+
722
+ def extract_for_blocks(for_statement_indexes, input_file_contents)
723
+
724
+ possible_for_blocks = []
725
+
726
+ for_block_counter = 0
727
+
728
+ extracted_blocks = []
729
+
730
+ controlregexp = /(if |while |def |for | do )/
731
+
732
+ rejectionregexp = /( if | while )/
733
+
734
+ for x in 0...for_statement_indexes.length-1
735
+
736
+ possible_for_blocks << input_file_contents[for_statement_indexes[x]..for_statement_indexes[x+1]]
737
+
738
+ end
739
+
740
+ end_counter = 0
741
+
742
+ end_index = []
743
+
744
+ current_block = []
745
+
746
+ possible_for_blocks.each_with_index do |block|
747
+
748
+ current_block += block
749
+
750
+ current_block.each_with_index do |line, index|
751
+
752
+ if line.strip.eql? "end"
753
+
754
+ end_counter += 1
755
+
756
+ end_index << index
757
+
758
+ end
759
+
760
+ end
761
+
762
+ if end_counter > 0
763
+
764
+ until end_index.empty?
765
+
766
+ array_extract = current_block[0..end_index[0]].reverse
767
+
768
+ index_counter = 0
769
+
770
+ array_extract.each_with_index do |line|
771
+
772
+ break if (line.lstrip.index(controlregexp) != nil and line.lstrip.index(rejectionregexp).nil?)
773
+
774
+ index_counter += 1
775
+
776
+ end
777
+
778
+ block_extract = array_extract[0..index_counter].reverse
779
+
780
+ extracted_blocks << block_extract
781
+
782
+ block_start = current_block.index(block_extract[0])
783
+
784
+ block_end = current_block.index(block_extract[-1])
785
+
786
+ current_block[block_start..block_end] = "--forblock#{for_block_counter}"
787
+
788
+ for_block_counter += 1
789
+
790
+ end_counter = 0
791
+
792
+ end_index = []
793
+
794
+ current_block.each_with_index do |line, index|
795
+
796
+ if line.strip.eql? "end"
797
+
798
+ end_counter += 1
799
+
800
+ end_index << index
801
+
802
+ end
803
+
804
+ end
805
+
806
+ end
807
+
808
+ end
809
+
810
+ end
811
+
812
+ return current_block, extracted_blocks
813
+
814
+ end
815
+
816
+ def compile_for_syntax(input_block)
817
+
818
+ def compile_condition(input_condition, input_block)
819
+
820
+ variable,array_name = input_condition.split("in")
821
+
822
+ if array_name.strip.include?("[") and array_name.strip.include?("]")
823
+
824
+ replacement_array = "_ref1 = #{array_name.strip}\n\n"
825
+
826
+ replacement_string = "#{variable.strip} = _ref1[_i];\n\n"
827
+
828
+ input_block = [replacement_array] + input_block.insert(1,replacement_string)
829
+
830
+ input_block[1] = "for (_i = 0, _j = _ref1.length; _i < _j; _i += 1) {\n\n"
831
+
832
+ elsif array_name.strip.include?("..")
833
+
834
+ array_type = if array_name.strip.include?("...") then 0 else 1 end
835
+
836
+ if array_type == 0
837
+
838
+ num1,num2 = array_name.strip.split("...")
839
+
840
+ input_block[0] = "for (#{variable.strip} = #{num1}, _j = #{num2}; #{variable.strip} <= _j; #{variable.strip} += 1) {\n\n"
841
+
842
+ else
843
+
844
+ num1,num2 = array_name.strip.split("..")
845
+
846
+ input_block[0] = "for (#{variable.strip} = #{num1}, _j = #{num2}; #{variable.strip} < _j; #{variable.strip} += 1) {\n\n"
847
+
848
+ end
849
+
850
+ else
851
+
852
+ input_block[0] = "for (_i = 0, _j = #{array_name.strip}.length; _i < _j; _i += 1) {\n\n"
853
+
854
+ input_block = input_block.insert(1,"#{variable.strip} = #{array_name.strip}[_i];\n\n")
855
+
856
+ end
857
+
858
+ return input_block
859
+
860
+ end
861
+
862
+ modified_input_block = input_block.dup
863
+
864
+ strings = []
865
+
866
+ string_counter = 0
867
+
868
+ input_block.each_with_index do |line, index|
869
+
870
+ if line.include?("\"")
871
+
872
+ opening_quotes = line.index("\"")
873
+
874
+ string_extract = line[opening_quotes..line.index("\"", opening_quotes+1)]
875
+
876
+ strings << string_extract
877
+
878
+ modified_input_block[index] = modified_input_block[index].sub(string_extract, "--string{#{string_counter}}")
879
+
880
+ string_counter += 1
881
+
882
+ end
883
+
884
+ end
885
+
886
+ input_block = modified_input_block
887
+
888
+ starting_line = input_block[0]
889
+
890
+ starting_line = starting_line + "\n" if starting_line.lstrip == starting_line
891
+
892
+ junk, condition = starting_line.split("for")
893
+
894
+ input_block[-1] = input_block[-1].lstrip.sub("end", "}")
895
+
896
+ input_block = compile_condition(condition,input_block)
897
+
898
+ modified_input_block = input_block.dup
899
+
900
+ input_block.each_with_index do |line, index|
901
+
902
+ if line.include?("--string{")
903
+
904
+ junk, remains = line.split("--string{")
905
+
906
+ string_index, junk = remains.split("}")
907
+
908
+ modified_input_block[index] = modified_input_block[index].sub("--string{#{string_index.strip}}", strings[string_index.strip.to_i])
909
+
910
+ end
911
+
912
+ end
913
+
914
+ return modified_input_block
915
+
916
+ end
917
+
918
+ possible_for_statements = input_file_contents.reject { |element| !element.include?("for") }
919
+
920
+ possible_for_statements = possible_for_statements.reject {|element| element.include?("for (")}
921
+
922
+ if !possible_for_statements.empty?
923
+
924
+ for_statement_indexes = []
925
+
926
+ possible_for_statements.each do |statement|
927
+
928
+ for_statement_indexes << input_file_contents.dup.each_index.select { |index| input_file_contents[index] == statement }
929
+
930
+ end
931
+
932
+ for_statement_indexes = [0] + for_statement_indexes.flatten + [-1]
933
+
934
+ controlregexp = /(if |def |while | do )/
935
+
936
+ modified_input_contents, extracted_statements = extract_for_blocks(for_statement_indexes, input_file_contents.clone)
937
+
938
+ joined_blocks = extracted_statements.collect { |element| element.join }
939
+
940
+ for_statements = joined_blocks.reject { |element| element.index(controlregexp) != nil }
941
+
942
+ rejected_elements = joined_blocks - for_statements
943
+
944
+ rejected_elements_index = []
945
+
946
+ rejected_elements.each do |element|
947
+
948
+ rejected_elements_index << joined_blocks.each_index.select { |index| joined_blocks[index] == element }
949
+
950
+ end
951
+
952
+ for_blocks_index = (0...extracted_statements.length).to_a
953
+
954
+ rejected_elements_index = rejected_elements_index.flatten
955
+
956
+ for_blocks_index -= rejected_elements_index
957
+
958
+ modified_for_statements = for_statements.collect { |string| convert_string_to_array(string, temporary_nila_file) }
959
+
960
+ modified_for_statements = modified_for_statements.collect { |block| compile_for_syntax(block) }.reverse
961
+
962
+ for_blocks_index = for_blocks_index.collect { |element| "--forblock#{element}" }.reverse
963
+
964
+ rejected_elements_index = rejected_elements_index.collect { |element| "--forblock#{element}" }.reverse
965
+
966
+ rejected_elements = rejected_elements.reverse
967
+
968
+ joined_file_contents = modified_input_contents.join
969
+
970
+ until for_blocks_index.empty? and rejected_elements_index.empty?
971
+
972
+ if !for_blocks_index.empty?
973
+
974
+ if joined_file_contents.include?(for_blocks_index[0])
975
+
976
+ joined_file_contents = joined_file_contents.sub(for_blocks_index[0], modified_for_statements[0].join)
977
+
978
+ for_blocks_index.delete_at(0)
979
+
980
+ modified_for_statements.delete_at(0)
981
+
982
+ else
983
+
984
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0], rejected_elements[0].join)
985
+
986
+ rejected_elements_index.delete_at(0)
987
+
988
+ rejected_elements.delete_at(0)
989
+
990
+ end
991
+
992
+ else
993
+
994
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0], rejected_elements[0])
995
+
996
+ rejected_elements_index.delete_at(0)
997
+
998
+ rejected_elements.delete_at(0)
999
+
1000
+ end
1001
+
1002
+ end
1003
+
1004
+ else
1005
+
1006
+ joined_file_contents = input_file_contents.join
1007
+
1008
+ end
1009
+
1010
+ file_id = open(temporary_nila_file, 'w')
1011
+
1012
+ file_id.write(joined_file_contents)
1013
+
1014
+ file_id.close()
1015
+
1016
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
1017
+
1018
+ return line_by_line_contents
1019
+
1020
+ end
1021
+
1022
+ def compile_loop_keyword(input_file_contents,temporary_nila_file)
1023
+
1024
+ def convert_string_to_array(input_string, temporary_nila_file)
1025
+
1026
+ file_id = open(temporary_nila_file, 'w')
1027
+
1028
+ file_id.write(input_string)
1029
+
1030
+ file_id.close()
1031
+
1032
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
1033
+
1034
+ return line_by_line_contents
1035
+
1036
+ end
1037
+
1038
+ def extract_loop_blocks(loop_statement_indexes, input_file_contents)
1039
+
1040
+ possible_loop_blocks = []
1041
+
1042
+ loop_block_counter = 0
1043
+
1044
+ extracted_blocks = []
1045
+
1046
+ controlregexp = /(if |while |def |loop )/
1047
+
1048
+ rejectionregexp = /( if | while )/
1049
+
1050
+ for x in 0...loop_statement_indexes.length-1
1051
+
1052
+ possible_loop_blocks << input_file_contents[loop_statement_indexes[x]..loop_statement_indexes[x+1]]
1053
+
1054
+ end
1055
+
1056
+ end_counter = 0
1057
+
1058
+ end_index = []
1059
+
1060
+ current_block = []
1061
+
1062
+ possible_loop_blocks.each_with_index do |block|
1063
+
1064
+ current_block += block
1065
+
1066
+ current_block.each_with_index do |line, index|
1067
+
1068
+ if line.strip.eql? "end"
1069
+
1070
+ end_counter += 1
1071
+
1072
+ end_index << index
1073
+
1074
+ end
1075
+
1076
+ end
1077
+
1078
+ if end_counter > 0
1079
+
1080
+ until end_index.empty?
1081
+
1082
+ array_extract = current_block[0..end_index[0]].reverse
1083
+
1084
+ index_counter = 0
1085
+
1086
+ array_extract.each_with_index do |line|
1087
+
1088
+ break if (line.lstrip.index(controlregexp) != nil and line.lstrip.index(rejectionregexp).nil?)
1089
+
1090
+ index_counter += 1
1091
+
1092
+ end
1093
+
1094
+ block_extract = array_extract[0..index_counter].reverse
1095
+
1096
+ extracted_blocks << block_extract
1097
+
1098
+ block_start = current_block.index(block_extract[0])
1099
+
1100
+ block_end = current_block.index(block_extract[-1])
1101
+
1102
+ current_block[block_start..block_end] = "--loopblock#{loop_block_counter}"
1103
+
1104
+ loop_block_counter += 1
1105
+
1106
+ end_counter = 0
1107
+
1108
+ end_index = []
1109
+
1110
+ current_block.each_with_index do |line, index|
1111
+
1112
+ if line.strip.eql? "end"
1113
+
1114
+ end_counter += 1
1115
+
1116
+ end_index << index
1117
+
1118
+ end
1119
+
1120
+ end
1121
+
1122
+ end
1123
+
1124
+ end
1125
+
1126
+ end
1127
+
1128
+ return current_block, extracted_blocks
1129
+
1130
+ end
1131
+
1132
+ def compile_loop_syntax(input_block)
1133
+
1134
+ modified_input_block = input_block.dup
1135
+
1136
+ strings = []
1137
+
1138
+ string_counter = 0
1139
+
1140
+ input_block.each_with_index do |line, index|
1141
+
1142
+ if line.include?("\"")
1143
+
1144
+ opening_quotes = line.index("\"")
1145
+
1146
+ string_extract = line[opening_quotes..line.index("\"", opening_quotes+1)]
1147
+
1148
+ strings << string_extract
1149
+
1150
+ modified_input_block[index] = modified_input_block[index].sub(string_extract, "--string{#{string_counter}}")
1151
+
1152
+ string_counter += 1
1153
+
1154
+ end
1155
+
1156
+ end
1157
+
1158
+ input_block = modified_input_block
1159
+
1160
+ starting_line = input_block[0]
1161
+
1162
+ starting_line = starting_line + "\n" if starting_line.lstrip == starting_line
1163
+
1164
+ input_block[0] = "whaaleskey (true) {\n"
1165
+
1166
+ input_block[-1] = input_block[-1].lstrip.sub("end", "}")
1167
+
1168
+ modified_input_block = input_block.dup
1169
+
1170
+ input_block.each_with_index do |line, index|
1171
+
1172
+ if line.include?("--string{")
1173
+
1174
+ junk, remains = line.split("--string{")
1175
+
1176
+ string_index, junk = remains.split("}")
1177
+
1178
+ modified_input_block[index] = modified_input_block[index].sub("--string{#{string_index.strip}}", strings[string_index.strip.to_i])
1179
+
1180
+ end
1181
+
1182
+ end
1183
+
1184
+ return modified_input_block
1185
+
1186
+ end
1187
+
1188
+ possible_loop_statements = input_file_contents.reject { |element| !element.include?("loop") }
1189
+
1190
+ if !possible_loop_statements.empty?
1191
+
1192
+ loop_statement_indexes = []
1193
+
1194
+ possible_loop_statements.each do |statement|
1195
+
1196
+ loop_statement_indexes << input_file_contents.dup.each_index.select { |index| input_file_contents[index] == statement }
1197
+
1198
+ end
1199
+
1200
+ loop_statement_indexes = [0] + loop_statement_indexes.flatten + [-1]
1201
+
1202
+ controlregexp = /(if |def )/
1203
+
1204
+ modified_input_contents, extracted_statements = extract_loop_blocks(loop_statement_indexes, input_file_contents.clone)
1205
+
1206
+ joined_blocks = extracted_statements.collect { |element| element.join }
1207
+
1208
+ loop_statements = joined_blocks.reject { |element| element.index(controlregexp) != nil }
1209
+
1210
+ rejected_elements = joined_blocks - loop_statements
1211
+
1212
+ rejected_elements_index = []
1213
+
1214
+ rejected_elements.each do |element|
1215
+
1216
+ rejected_elements_index << joined_blocks.each_index.select { |index| joined_blocks[index] == element }
1217
+
1218
+ end
1219
+
1220
+ loop_blocks_index = (0...extracted_statements.length).to_a
1221
+
1222
+ rejected_elements_index = rejected_elements_index.flatten
1223
+
1224
+ loop_blocks_index -= rejected_elements_index
1225
+
1226
+ modified_loop_statements = loop_statements.collect { |string| convert_string_to_array(string, temporary_nila_file) }
1227
+
1228
+ modified_loop_statements = modified_loop_statements.collect { |block| compile_loop_syntax(block) }.reverse
1229
+
1230
+ loop_blocks_index = loop_blocks_index.collect { |element| "--loopblock#{element}" }.reverse
1231
+
1232
+ rejected_elements_index = rejected_elements_index.collect { |element| "--loopblock#{element}" }.reverse
1233
+
1234
+ rejected_elements = rejected_elements.reverse
1235
+
1236
+ joined_file_contents = modified_input_contents.join
1237
+
1238
+ until loop_blocks_index.empty? and rejected_elements_index.empty?
1239
+
1240
+ if !loop_blocks_index.empty?
1241
+
1242
+ if joined_file_contents.include?(loop_blocks_index[0])
1243
+
1244
+ joined_file_contents = joined_file_contents.sub(loop_blocks_index[0], modified_loop_statements[0].join)
1245
+
1246
+ loop_blocks_index.delete_at(0)
1247
+
1248
+ modified_loop_statements.delete_at(0)
1249
+
1250
+ else
1251
+
1252
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0], rejected_elements[0].join)
1253
+
1254
+ rejected_elements_index.delete_at(0)
1255
+
1256
+ rejected_elements.delete_at(0)
1257
+
1258
+ end
1259
+
1260
+ else
1261
+
1262
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0], rejected_elements[0].join)
1263
+
1264
+ rejected_elements_index.delete_at(0)
1265
+
1266
+ rejected_elements.delete_at(0)
1267
+
1268
+ end
1269
+
1270
+ end
1271
+
1272
+ else
1273
+
1274
+ joined_file_contents = input_file_contents.join
1275
+
1276
+ end
1277
+
1278
+ file_id = open(temporary_nila_file, 'w')
1279
+
1280
+ file_id.write(joined_file_contents)
1281
+
1282
+ file_id.close()
1283
+
1284
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
1285
+
1286
+ return line_by_line_contents
1287
+
1288
+ end
1289
+
1290
+ def ignore_statement_modifiers(input_block)
1291
+
1292
+ modified_input_block = input_block.dup
1293
+
1294
+ rejectionregexp = /( if | while )/
1295
+
1296
+ rejected_lines = {}
1297
+
1298
+ rejected_line_counter = 0
1299
+
1300
+ input_block.each_with_index do |line, index|
1301
+
1302
+ if line.lstrip.index(rejectionregexp) != nil
1303
+
1304
+ rejected_lines["--rejected{#{rejected_line_counter}}\n\n"] = line
1305
+
1306
+ modified_input_block[index] = "--rejected{#{rejected_line_counter}}\n\n"
1307
+
1308
+ rejected_line_counter += 1
1309
+
1310
+ end
1311
+
1312
+ end
1313
+
1314
+ return modified_input_block, rejected_lines
1315
+
1316
+ end
1317
+
1318
+ def replace_statement_modifiers(input_block, rejected_lines)
1319
+
1320
+ unless rejected_lines.empty?
1321
+
1322
+ rejected_replacements = rejected_lines.keys
1323
+
1324
+ loc = 0
1325
+
1326
+ indices = []
1327
+
1328
+ index_counter = 0
1329
+
1330
+ rejected_replacements.each do |replacement_string|
1331
+
1332
+ input_block.each_with_index do |line, index|
1333
+
1334
+ break if line.include?(replacement_string.rstrip)
1335
+
1336
+ index_counter += 1
1337
+
1338
+ end
1339
+
1340
+ indices << index_counter
1341
+
1342
+ index_counter = 0
1343
+
1344
+ end
1345
+
1346
+ indices.each_with_index do |location, index|
1347
+
1348
+ input_block[location] = rejected_lines.values[index] + "\n\n"
1349
+
1350
+ end
1351
+
1352
+ end
1353
+
1354
+ return input_block
1355
+
1356
+ end
1357
+
1358
+ file_contents = compile_ternary_if(input_file_contents)
1359
+
1360
+ file_contents, rejected_lines = ignore_statement_modifiers(file_contents)
1361
+
1362
+ file_contents = replace_unless_until(file_contents)
1363
+
1364
+ file_contents = compile_regular_if(file_contents, temporary_nila_file)
1365
+
1366
+ file_contents = compile_regular_for(file_contents, temporary_nila_file)
1367
+
1368
+ file_contents = compile_regular_while(file_contents, temporary_nila_file)
1369
+
1370
+ file_contents = compile_loop_keyword(file_contents,temporary_nila_file)
1371
+
1372
+ file_contents = replace_statement_modifiers(file_contents, rejected_lines)
1373
+
1374
+ file_contents = compile_inline_conditionals(file_contents, temporary_nila_file)
1375
+
1376
+ return file_contents
1377
+
1378
+ end