nilac 0.0.3.7 → 0.0.3.8
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/nilac.rb +211 -3
- data/lib/nilac/version.rb +1 -1
- data/shark/features/method_multiple_return.feature +17 -0
- data/shark/features/multiple_variable_initialization.feature +12 -0
- data/shark/test_files/correct.js +1 -1
- data/shark/test_files/correct_initialization.js +21 -0
- data/shark/test_files/correct_multiple_return.js +19 -0
- data/shark/test_files/correct_return.js +1 -1
- data/shark/test_files/correct_single_return.js +1 -1
- data/shark/test_files/multiple_initialization.js +21 -0
- data/shark/test_files/multiple_initialization.nila +13 -0
- data/shark/test_files/multiple_return.js +19 -0
- data/shark/test_files/multiple_return.nila +17 -0
- data/shark/test_files/perfect.js +1 -1
- metadata +9 -1
data/bin/nilac.rb
CHANGED
@@ -412,6 +412,60 @@ def compile(input_file_path,*output_file_name)
|
|
412
412
|
|
413
413
|
end
|
414
414
|
|
415
|
+
def compile_multiple_variable_initialization(input_file_contents,temporary_nila_file)
|
416
|
+
|
417
|
+
possible_variable_lines = input_file_contents.reject {|element| !element.include?"="}
|
418
|
+
|
419
|
+
possible_multiple_initialization = possible_variable_lines.reject {|element| !element.split("=")[0].include?","}
|
420
|
+
|
421
|
+
multiple_initialization_index = []
|
422
|
+
|
423
|
+
possible_multiple_initialization.each do |statement|
|
424
|
+
|
425
|
+
location_array = input_file_contents.each_index.select { |index| input_file_contents[index] == statement}
|
426
|
+
|
427
|
+
multiple_initialization_index << location_array[0]
|
428
|
+
|
429
|
+
end
|
430
|
+
|
431
|
+
modified_file_contents = input_file_contents.dup
|
432
|
+
|
433
|
+
multiple_init_counter = 1
|
434
|
+
|
435
|
+
possible_multiple_initialization.each_with_index do |line,index|
|
436
|
+
|
437
|
+
line_split = line.split(" = ")
|
438
|
+
|
439
|
+
right_side_variables = line_split[0].split(",")
|
440
|
+
|
441
|
+
replacement_string = "multipleinit#{multiple_init_counter} = #{line_split[1]}\n\n"
|
442
|
+
|
443
|
+
variable_string = ""
|
444
|
+
|
445
|
+
right_side_variables.each_with_index do |variable,var_index|
|
446
|
+
|
447
|
+
variable_string = variable_string + variable.rstrip + " = multipleinit#{multiple_init_counter}[#{var_index}]\n\n"
|
448
|
+
|
449
|
+
end
|
450
|
+
|
451
|
+
replacement_string = replacement_string + variable_string
|
452
|
+
|
453
|
+
modified_file_contents[multiple_initialization_index[index]] = replacement_string
|
454
|
+
|
455
|
+
end
|
456
|
+
|
457
|
+
file_id = open(temporary_nila_file, 'w')
|
458
|
+
|
459
|
+
file_id.write(modified_file_contents.join)
|
460
|
+
|
461
|
+
file_id.close()
|
462
|
+
|
463
|
+
line_by_line_contents = read_file_line_by_line(temporary_nila_file)
|
464
|
+
|
465
|
+
return line_by_line_contents
|
466
|
+
|
467
|
+
end
|
468
|
+
|
415
469
|
def get_variables(input_file_contents,temporary_nila_file)
|
416
470
|
|
417
471
|
#This method is solely focused on getting a list of variables to be declared.
|
@@ -700,6 +754,134 @@ def compile(input_file_path,*output_file_name)
|
|
700
754
|
|
701
755
|
end
|
702
756
|
|
757
|
+
def compile_multiple_return(input_array)
|
758
|
+
|
759
|
+
def find_all_matching_indices(input_string,pattern)
|
760
|
+
|
761
|
+
locations = []
|
762
|
+
|
763
|
+
index = input_string.index(pattern)
|
764
|
+
|
765
|
+
while index != nil
|
766
|
+
|
767
|
+
locations << index
|
768
|
+
|
769
|
+
index = input_string.index(pattern,index+1)
|
770
|
+
|
771
|
+
|
772
|
+
end
|
773
|
+
|
774
|
+
return locations
|
775
|
+
|
776
|
+
|
777
|
+
end
|
778
|
+
|
779
|
+
modified_input_array = input_array.dup
|
780
|
+
|
781
|
+
return_statements = input_array.dup.reject {|element| !element.include?"return"}
|
782
|
+
|
783
|
+
multiple_return_statements = return_statements.dup.reject {|element| !element.include?","}
|
784
|
+
|
785
|
+
modified_multiple_return_statements = multiple_return_statements.dup
|
786
|
+
|
787
|
+
return_statement_index = []
|
788
|
+
|
789
|
+
multiple_return_statements.each do |statement|
|
790
|
+
|
791
|
+
location_array = modified_input_array.each_index.select { |index| modified_input_array[index] == statement}
|
792
|
+
|
793
|
+
return_statement_index << location_array[0]
|
794
|
+
|
795
|
+
end
|
796
|
+
|
797
|
+
multiple_return_statements.each_with_index do |return_statement,index|
|
798
|
+
|
799
|
+
replacement_counter = 0
|
800
|
+
|
801
|
+
if return_statement.include? "\""
|
802
|
+
|
803
|
+
starting_quotes = find_all_matching_indices(return_statement,"\"")
|
804
|
+
|
805
|
+
for x in 0...(starting_quotes.length)/2
|
806
|
+
|
807
|
+
quotes = return_statement[starting_quotes[x]..starting_quotes[x+1]]
|
808
|
+
|
809
|
+
replacement_counter += 1
|
810
|
+
|
811
|
+
modified_multiple_return_statements[index] = modified_multiple_return_statements[index].sub(quotes,"repstring#{1}")
|
812
|
+
|
813
|
+
modified_input_array[return_statement_index[index]] = modified_multiple_return_statements[index].sub(quotes,"repstring#{1}")
|
814
|
+
|
815
|
+
end
|
816
|
+
|
817
|
+
end
|
818
|
+
|
819
|
+
end
|
820
|
+
|
821
|
+
modified_multiple_return_statements = modified_multiple_return_statements.reject {|element| !element.include?","}
|
822
|
+
|
823
|
+
return_statement_index = []
|
824
|
+
|
825
|
+
modified_multiple_return_statements.each do |statement|
|
826
|
+
|
827
|
+
location_array = modified_input_array.each_index.select { |index| modified_input_array[index] == statement}
|
828
|
+
|
829
|
+
return_statement_index << location_array[0]
|
830
|
+
|
831
|
+
end
|
832
|
+
|
833
|
+
modified_multiple_return_statements.each_with_index do |return_statement,index|
|
834
|
+
|
835
|
+
method_call_counter = 0
|
836
|
+
|
837
|
+
if return_statement.include?"("
|
838
|
+
|
839
|
+
open_paran_location = find_all_matching_indices(return_statement,"(")
|
840
|
+
|
841
|
+
open_paran_location.each do |paran_index|
|
842
|
+
|
843
|
+
method_call = return_statement[paran_index..return_statement.index(")",paran_index+1)]
|
844
|
+
|
845
|
+
method_call_counter += 1
|
846
|
+
|
847
|
+
modified_multiple_return_statements[index] = modified_multiple_return_statements[index].sub(method_call,"methodcall#{method_call_counter}")
|
848
|
+
|
849
|
+
modified_input_array[return_statement_index[index]] = modified_multiple_return_statements[index].sub(method_call,"methodcall#{method_call_counter}")
|
850
|
+
|
851
|
+
end
|
852
|
+
|
853
|
+
end
|
854
|
+
|
855
|
+
end
|
856
|
+
|
857
|
+
modified_multiple_return_statements = modified_multiple_return_statements.reject {|element| !element.include?(",")}
|
858
|
+
|
859
|
+
return_statement_index = []
|
860
|
+
|
861
|
+
modified_multiple_return_statements.each do |statement|
|
862
|
+
|
863
|
+
location_array = modified_input_array.each_index.select { |index| modified_input_array[index] == statement}
|
864
|
+
|
865
|
+
return_statement_index << location_array[0]
|
866
|
+
|
867
|
+
end
|
868
|
+
|
869
|
+
return_statement_index.each do |index|
|
870
|
+
|
871
|
+
original_statement = input_array[index]
|
872
|
+
|
873
|
+
statement_split = original_statement.split("return ")
|
874
|
+
|
875
|
+
replacement_split = "return [" + statement_split[1].rstrip + "]\n\n"
|
876
|
+
|
877
|
+
input_array[index] = replacement_split
|
878
|
+
|
879
|
+
end
|
880
|
+
|
881
|
+
return input_array
|
882
|
+
|
883
|
+
end
|
884
|
+
|
703
885
|
def compile_function(input_array,temporary_nila_file)
|
704
886
|
|
705
887
|
modified_input_array = input_array.dup
|
@@ -776,6 +958,8 @@ def compile(input_file_path,*output_file_name)
|
|
776
958
|
|
777
959
|
modified_input_array = add_auto_return_statement(modified_input_array)
|
778
960
|
|
961
|
+
modified_input_array = compile_multiple_return(modified_input_array)
|
962
|
+
|
779
963
|
return modified_input_array
|
780
964
|
|
781
965
|
end
|
@@ -1238,6 +1422,8 @@ def compile(input_file_path,*output_file_name)
|
|
1238
1422
|
|
1239
1423
|
starting_index = starting_line_indices[0]
|
1240
1424
|
|
1425
|
+
begin
|
1426
|
+
|
1241
1427
|
for x in 0...initial_starting_lines.length
|
1242
1428
|
|
1243
1429
|
code_blocks << modified_file_contents[starting_index..block_ending_lines[0]]
|
@@ -1264,6 +1450,12 @@ def compile(input_file_path,*output_file_name)
|
|
1264
1450
|
|
1265
1451
|
end
|
1266
1452
|
|
1453
|
+
rescue TypeError
|
1454
|
+
|
1455
|
+
puts "Whitespace was left unfixed!"
|
1456
|
+
|
1457
|
+
end
|
1458
|
+
|
1267
1459
|
return modified_file_contents,code_blocks
|
1268
1460
|
|
1269
1461
|
end
|
@@ -1474,7 +1666,7 @@ def compile(input_file_path,*output_file_name)
|
|
1474
1666
|
|
1475
1667
|
File.delete(temporary_nila_file)
|
1476
1668
|
|
1477
|
-
file_id.write("//Written
|
1669
|
+
file_id.write("//Written using Nila. Visit http://adhithyan15.github.io/nila\n")
|
1478
1670
|
|
1479
1671
|
file_id.write(file_contents.join)
|
1480
1672
|
|
@@ -1500,6 +1692,8 @@ def compile(input_file_path,*output_file_name)
|
|
1500
1692
|
|
1501
1693
|
comments = [singleline_comments,multiline_comments]
|
1502
1694
|
|
1695
|
+
file_contents = compile_multiple_variable_initialization(file_contents,temp_file)
|
1696
|
+
|
1503
1697
|
list_of_variables,file_contents = get_variables(file_contents,temp_file)
|
1504
1698
|
|
1505
1699
|
file_contents = compile_arrays(file_contents)
|
@@ -1576,7 +1770,21 @@ def find_file_name(input_path,file_extension)
|
|
1576
1770
|
|
1577
1771
|
end
|
1578
1772
|
|
1579
|
-
|
1773
|
+
def find_file_path(input_path,file_extension)
|
1774
|
+
|
1775
|
+
extension_remover = input_path.split(file_extension)
|
1776
|
+
|
1777
|
+
remaining_string = extension_remover[0].reverse
|
1778
|
+
|
1779
|
+
path_finder = remaining_string.index("/")
|
1780
|
+
|
1781
|
+
remaining_string = remaining_string.reverse
|
1782
|
+
|
1783
|
+
return remaining_string[0...remaining_string.length-path_finder]
|
1784
|
+
|
1785
|
+
end
|
1786
|
+
|
1787
|
+
nilac_version = "0.0.3.8"
|
1580
1788
|
|
1581
1789
|
opts = Slop.parse do
|
1582
1790
|
on :c, :compile=, 'Compile Nila File', as:Array, delimiter:":"
|
@@ -1712,7 +1920,7 @@ elsif opts[:run] != nil
|
|
1712
1920
|
|
1713
1921
|
compile(file_path)
|
1714
1922
|
|
1715
|
-
js_file_name = find_file_name(file_path,".nila") + ".js"
|
1923
|
+
js_file_name = find_file_path(file_path,".nila") + find_file_name(file_path,".nila") + ".js"
|
1716
1924
|
|
1717
1925
|
node_output = `node #{js_file_name}`
|
1718
1926
|
|
data/lib/nilac/version.rb
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
Feature: Javascript, by default, doesn't allow for the return of multiple values. So this is Nila's attempt to fix this problem.
|
2
|
+
Scenario: Input function with multiple return statement
|
3
|
+
Given the input file "multiple_return.nila"
|
4
|
+
When the ~compiler is run
|
5
|
+
The output file must be "multiple_return.js"
|
6
|
+
The output file must equal "correct_multiple_return.js"
|
7
|
+
|
8
|
+
Scenario: Input function with a single return statement
|
9
|
+
Given the input file "single_return.nila"
|
10
|
+
When the ~compiler is run
|
11
|
+
The output file must be "single_return.js"
|
12
|
+
The output file must equal "correct_single_return.js"
|
13
|
+
|
14
|
+
Configurations:
|
15
|
+
|
16
|
+
~compiler => bin/nilac.rb
|
17
|
+
:v $cliusage => ruby :v --compile $file
|
@@ -0,0 +1,12 @@
|
|
1
|
+
Feature: Javascript doesn't allow multiple variable initializations from a method call. So this feature attempts to fix this problem. This feature is complementary to the multiple return feature.
|
2
|
+
|
3
|
+
Scenario: Multiple variables initialized from a method call.
|
4
|
+
Given the input file "multiple_initialization.nila"
|
5
|
+
When the ~compiler is run
|
6
|
+
The output file must be "multiple_initialization.js"
|
7
|
+
The output file must equal "correct_initialization.js"
|
8
|
+
|
9
|
+
Configurations:
|
10
|
+
|
11
|
+
~compiler => bin/nilac.rb
|
12
|
+
:v $cliusage => ruby :v --compile $file
|
data/shark/test_files/correct.js
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
//Written using Nila. Visit http://adhithyan15.github.io/nila
|
2
|
+
(function() {
|
3
|
+
var multipleinit1, first_name, last_name;
|
4
|
+
|
5
|
+
// This file demonstrates multiple variable initialization
|
6
|
+
|
7
|
+
function parse_name(input_name) {
|
8
|
+
var name_split;
|
9
|
+
name_split = input_name.split(" ");
|
10
|
+
return [name_split[0],name_split[1]];
|
11
|
+
}
|
12
|
+
|
13
|
+
multipleinit1 = parse_name("Adhithya Rajasekaran");
|
14
|
+
|
15
|
+
first_name = multipleinit1[0];
|
16
|
+
|
17
|
+
last_name = multipleinit1[1];
|
18
|
+
|
19
|
+
console.log(first_name + " " + last_name);
|
20
|
+
|
21
|
+
}).call(this);
|
@@ -0,0 +1,19 @@
|
|
1
|
+
//Written using Nila. Visit http://adhithyan15.github.io/nila
|
2
|
+
(function() {
|
3
|
+
var parsed_name;
|
4
|
+
|
5
|
+
// This method demonstrates multiple return values
|
6
|
+
|
7
|
+
function parse_name(input_name) {
|
8
|
+
var name_split;
|
9
|
+
name_split = input_name.split(" ");
|
10
|
+
return [name_split[0],name_split[1]];
|
11
|
+
}
|
12
|
+
|
13
|
+
function test_method() {
|
14
|
+
return console.log("Hello, Adhithya");
|
15
|
+
}
|
16
|
+
|
17
|
+
parsed_name = parse_name("Adhithya Rajasekaran");
|
18
|
+
|
19
|
+
}).call(this);
|
@@ -0,0 +1,21 @@
|
|
1
|
+
//Written using Nila. Visit http://adhithyan15.github.io/nila
|
2
|
+
(function() {
|
3
|
+
var multipleinit1, first_name, last_name;
|
4
|
+
|
5
|
+
// This file demonstrates multiple variable initialization
|
6
|
+
|
7
|
+
function parse_name(input_name) {
|
8
|
+
var name_split;
|
9
|
+
name_split = input_name.split(" ");
|
10
|
+
return [name_split[0],name_split[1]];
|
11
|
+
}
|
12
|
+
|
13
|
+
multipleinit1 = parse_name("Adhithya Rajasekaran");
|
14
|
+
|
15
|
+
first_name = multipleinit1[0];
|
16
|
+
|
17
|
+
last_name = multipleinit1[1];
|
18
|
+
|
19
|
+
console.log(first_name + " " + last_name);
|
20
|
+
|
21
|
+
}).call(this);
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# This file demonstrates multiple variable initialization
|
2
|
+
|
3
|
+
def parse_name(input_name)
|
4
|
+
|
5
|
+
name_split = input_name.split(" ")
|
6
|
+
|
7
|
+
return name_split[0],name_split[1]
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
first_name,last_name = parse_name("Adhithya Rajasekaran")
|
12
|
+
|
13
|
+
puts first_name + " " + last_name
|
@@ -0,0 +1,19 @@
|
|
1
|
+
//Written using Nila. Visit http://adhithyan15.github.io/nila
|
2
|
+
(function() {
|
3
|
+
var parsed_name;
|
4
|
+
|
5
|
+
// This method demonstrates multiple return values
|
6
|
+
|
7
|
+
function parse_name(input_name) {
|
8
|
+
var name_split;
|
9
|
+
name_split = input_name.split(" ");
|
10
|
+
return [name_split[0],name_split[1]];
|
11
|
+
}
|
12
|
+
|
13
|
+
function test_method() {
|
14
|
+
return console.log("Hello, Adhithya");
|
15
|
+
}
|
16
|
+
|
17
|
+
parsed_name = parse_name("Adhithya Rajasekaran");
|
18
|
+
|
19
|
+
}).call(this);
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# This method demonstrates multiple return values
|
2
|
+
|
3
|
+
def parse_name(input_name)
|
4
|
+
|
5
|
+
name_split = input_name.split(" ")
|
6
|
+
|
7
|
+
return name_split[0],name_split[1]
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_method()
|
12
|
+
|
13
|
+
puts "Hello, Adhithya"
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
parsed_name = parse_name("Adhithya Rajasekaran")
|
data/shark/test_files/perfect.js
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nilac
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.3.
|
4
|
+
version: 0.0.3.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -69,10 +69,18 @@ files:
|
|
69
69
|
- shark/features/add_auto_return_statement.feature
|
70
70
|
- shark/features/barebones_compilation.feature
|
71
71
|
- shark/features/fix_newlines.feature
|
72
|
+
- shark/features/method_multiple_return.feature
|
73
|
+
- shark/features/multiple_variable_initialization.feature
|
72
74
|
- shark/test_files/correct.js
|
75
|
+
- shark/test_files/correct_initialization.js
|
76
|
+
- shark/test_files/correct_multiple_return.js
|
73
77
|
- shark/test_files/correct_return.js
|
74
78
|
- shark/test_files/correct_single_return.js
|
75
79
|
- shark/test_files/erratic.nila
|
80
|
+
- shark/test_files/multiple_initialization.js
|
81
|
+
- shark/test_files/multiple_initialization.nila
|
82
|
+
- shark/test_files/multiple_return.js
|
83
|
+
- shark/test_files/multiple_return.nila
|
76
84
|
- shark/test_files/no_return.nila
|
77
85
|
- shark/test_files/perfect.js
|
78
86
|
- shark/test_files/simple.nila
|