nilac 0.0.4.3.9.4 → 0.0.4.3.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/examples/marky.js +22 -3
  3. data/examples/marky.nila +29 -2
  4. data/lib/nilac/compile_classes.rb +6 -6
  5. data/lib/nilac/compile_custom_function_map.rb +8 -2
  6. data/lib/nilac/compile_existential_operator.rb +169 -0
  7. data/lib/nilac/compile_named_functions.rb +1 -1
  8. data/lib/nilac/compile_operators.rb +46 -40
  9. data/lib/nilac/compile_require_statements.rb +43 -0
  10. data/lib/nilac/compile_ruby_methods.rb +43 -1
  11. data/lib/nilac/compile_ternary_operator.rb +6 -0
  12. data/lib/nilac/compile_whitespace_delimited_functions.rb +15 -8
  13. data/lib/nilac/get_variables.rb +5 -1
  14. data/lib/nilac/pretty_print_javascript.rb +2 -2
  15. data/lib/nilac/rollblocks.rb +100 -0
  16. data/lib/nilac/version.rb +1 -1
  17. data/lib/nilac.rb +12 -2
  18. data/shark/features/add_auto_return_statement.feature +1 -1
  19. data/shark/features/array_and_string_indexing.feature +1 -1
  20. data/shark/features/automatic_require_statements.feature +11 -0
  21. data/shark/features/barebones_compilation.feature +1 -1
  22. data/shark/features/case_when.feature +1 -1
  23. data/shark/features/default_method_parameters.feature +1 -1
  24. data/shark/features/fix_newlines.feature +1 -1
  25. data/shark/features/hashes.feature +1 -1
  26. data/shark/features/heredoc.feature +1 -1
  27. data/shark/features/if_then_else.feature +1 -1
  28. data/shark/features/loop.feature +1 -1
  29. data/shark/features/method_multiple_return.feature +1 -1
  30. data/shark/features/multiline_array.feature +1 -1
  31. data/shark/features/multiple_variable_initialization.feature +1 -1
  32. data/shark/features/numbers.feature +1 -1
  33. data/shark/features/regular_for.feature +1 -1
  34. data/shark/features/regular_if.feature +1 -1
  35. data/shark/features/regular_while.feature +1 -1
  36. data/shark/features/ruby_methods.feature +1 -1
  37. data/shark/features/ruby_operators.feature +1 -1
  38. data/shark/features/splats.feature +1 -1
  39. data/shark/features/string_interpolation.feature +1 -1
  40. data/shark/features/strings.feature +1 -1
  41. data/shark/features/times.feature +1 -1
  42. data/shark/features/unless_until.feature +1 -1
  43. data/shark/features/whitespace_delimitation.feature +1 -1
  44. data/shark/test_files/correct_required_module.js +14 -0
  45. data/shark/test_files/required_module.nila +11 -0
  46. data/shark/test_files/sample_class.nila +10 -0
  47. metadata +10 -4
  48. data/shark/features/array_sugar.feature +0 -11
  49. data/shark/test_files/array_sugar.js +0 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: be01c68cfa1e5e7f6280330bf0b50bd2730edde8
4
- data.tar.gz: ac88db8f9ed0a1138526a7b856eb20b2e3152b5e
3
+ metadata.gz: 193275e07ff9a381b98f8cba61b4178b4aa67746
4
+ data.tar.gz: fdb154e462dccec62c8b6c92221303d86c47c50f
5
5
  SHA512:
6
- metadata.gz: 993a625a2b7e1345315e1c6dff8c1773f4d7f8306b85d7e951e74b41a2dc43f19170e86e59050e828047f0619b7bd97fc7cdce6ad5c9fa74374b967c1cc4e12a
7
- data.tar.gz: cc64aca1ed1feddeee5f5abc18a452c11a142ed2c1dcae66538ba74fff190a406759a9d3e11db63cf832c00dbb93e282a5c2cce44584b9dd1f9c5941589d493f
6
+ metadata.gz: e46d6cf55cd21f52feed937192522cc287a34e79979b2f8cbb199154d8cddb38b279b1ccab3bb6a69533df0bb93e557d21c408051ed903f47e7a1b78e8e81513
7
+ data.tar.gz: 46eabf7a2ad1f5188e1398120971fcc0ecbcacf7befb88b0f067da15dbc02f79d4d0a99e88c57178612783c81c3e1ea93073c7e02f6a887d4710c5ecf551b971
data/examples/marky.js CHANGED
@@ -1,6 +1,6 @@
1
1
  //Written using Nila. Visit http://adhithyan15.github.io/nila
2
2
  (function() {
3
- var commandline_args, parse_markdown;
3
+ var commandline_args, file_name, fs, parse_markdown, parsed_args;
4
4
 
5
5
  // Marky is a simple markdown parser written in Nila and runs on Nodejs.
6
6
 
@@ -16,7 +16,15 @@
16
16
 
17
17
  // If you want to learn more about Nila, please visit http://adhithyan15.github.io/nila
18
18
 
19
- parse_markdown = function() {
19
+ fs = require('fs');
20
+
21
+ parse_markdown = function(input_file) {
22
+ fs.readFile(input_file, 'utf8',function(err,data) {
23
+ if (err) {
24
+ console.log(err);
25
+ }
26
+ console.log(data);
27
+ });
20
28
  };
21
29
 
22
30
  commandline_args = [];
@@ -25,6 +33,17 @@
25
33
  commandline_args.push(val);
26
34
  });
27
35
 
28
- console.log(commandline_args.slice(2));
36
+ parsed_args = commandline_args.slice(2);
37
+
38
+ if (!(parsed_args.length === 0)) {
39
+ if (parsed_args[0].indexOf("-c") !== -1){
40
+ file_name = parsed_args[1];
41
+ if (!((typeof file_name === "undefined"))) {
42
+ parse_markdown(file_name);
43
+ } else {
44
+ console.log("No file has been specified!");
45
+ }
46
+ }
47
+ }
29
48
 
30
49
  }).call(this);
data/examples/marky.nila CHANGED
@@ -8,7 +8,14 @@
8
8
 
9
9
  # If you want to learn more about Nila, please visit http://adhithyan15.github.io/nila
10
10
 
11
- def parse_markdown
11
+ require 'fs'
12
+
13
+ def parse_markdown(input_file)
14
+
15
+ fs.readFile input_file, 'utf8', do |err,data|
16
+ puts err if err
17
+ puts data
18
+ end
12
19
 
13
20
  end
14
21
 
@@ -20,4 +27,24 @@ process.argv.forEach do |val,index,array|
20
27
 
21
28
  end
22
29
 
23
- puts commandline_args[2..last]
30
+ parsed_args = commandline_args[2..last]
31
+
32
+ unless parsed_args.empty?
33
+
34
+ if parsed_args[0].include?("-c")
35
+
36
+ file_name = parsed_args[1]
37
+
38
+ unless file_name.undefined?
39
+
40
+ parse_markdown file_name
41
+
42
+ else
43
+
44
+ puts "No file has been specified!"
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -3,17 +3,17 @@
3
3
  # Nila will have support for classes. But the current implementation is not ready for prime time
4
4
  # yet. So it won't be documented.
5
5
 
6
- def extract_classes
6
+ def extract_classes(file_contents)
7
7
 
8
- builtin_classes = [
8
+ # This method will extract code blocks between class .. end blocks and try to convert to
9
+ # Javascript equivalent code
9
10
 
10
- "String",
11
+ possible_classes = file_contents.reject {|element| !element.index(/class\s*\w{1,}\s*<?\s?\w{1,}?/)}
11
12
 
12
- "Array",
13
- ]
14
13
 
15
- end
16
14
 
15
+ end
17
16
 
17
+ extract_classes(input_file_contents)
18
18
 
19
19
  end
@@ -1,4 +1,6 @@
1
- def compile_custom_function_map(input_file_contents)
1
+ require_relative 'replace_strings'
2
+
3
+ def compile_custom_function_map(input_file_contents)
2
4
 
3
5
  function_map_replacements = {
4
6
 
@@ -8,6 +10,8 @@
8
10
 
9
11
  "print" => "process.stdout.write",
10
12
 
13
+ "require" => "require",
14
+
11
15
  }
12
16
 
13
17
  function_map = function_map_replacements.keys
@@ -20,7 +24,9 @@
20
24
 
21
25
  function_map.each do |function|
22
26
 
23
- if line.include?(function+"(") or line.include?(function+" ") and line.index(javascript_regexp) == nil
27
+ test_line = replace_strings(line)
28
+
29
+ if test_line.include?(function+"(") or test_line.include?(function+" ") and test_line.index(javascript_regexp) == nil
24
30
 
25
31
  testsplit = line.split(function)
26
32
 
@@ -0,0 +1,169 @@
1
+ require_relative 'replace_strings'
2
+
3
+ def compile_existential_operators(input_file_contents)
4
+
5
+ # This method compiles Nila's existential operator.
6
+ #
7
+ # Example:
8
+ #
9
+ # input_args[0].nil?
10
+ #
11
+ # The above line should compile to
12
+ #
13
+ # typeof input_args[0] === undefined && input_args[0] === nil
14
+
15
+ modified_contents = input_file_contents.collect {|element| replace_strings(element)}
16
+
17
+ possible_existential_calls = modified_contents.reject {|element| !element.include?(".nil")}
18
+
19
+ split_regexp = /((if|while) \((!\()?)/
20
+
21
+ second_split_regexp = /(&&|\|\|)/
22
+
23
+ possible_existential_calls.each do |statement|
24
+
25
+ reconstructed_statement = input_file_contents[modified_contents.index(statement)]
26
+
27
+ counter = 0
28
+
29
+ while reconstructed_statement.include?(".nil")
30
+
31
+ before,after = reconstructed_statement.split(".nil",2)
32
+
33
+ if counter == 0
34
+
35
+ split_data = before.split(split_regexp)
36
+
37
+ before2 = split_data[1]
38
+
39
+ if before2.eql?("if (") or before2.eql?("while (")
40
+
41
+ usage = split_data[3]
42
+
43
+ elsif before2.eql?("if (!(") or before2.eql?("while (!(")
44
+
45
+ usage = split_data[4]
46
+
47
+ end
48
+
49
+ replacement_string = "(typeof #{usage} === \"undefined\" &|&| #{usage} === null)"
50
+
51
+ reconstructed_statement = before.split(before2)[0] + before2 + replacement_string + after
52
+
53
+ elsif counter > 0
54
+
55
+ split_data = before.split(second_split_regexp)
56
+
57
+ before2 = split_data[0]
58
+
59
+ operator = split_data[1]
60
+
61
+ operator = " &|&| " if operator.strip.eql?("&&")
62
+
63
+ operator = " |$|$ " if operator.strip.eql?("||")
64
+
65
+ usage = split_data[2]
66
+
67
+ replacement_string = "(typeof #{usage} === \"undefined\" &|&| #{usage} === null)"
68
+
69
+ reconstructed_statement = before2 + operator + replacement_string + after
70
+
71
+ end
72
+
73
+ counter += 1
74
+
75
+ end
76
+
77
+ reconstructed_statement = reconstructed_statement.gsub("&|&|","&&").gsub("|$|$","&&")
78
+
79
+ input_file_contents[modified_contents.index(statement)] = reconstructed_statement
80
+
81
+ end
82
+
83
+ return input_file_contents
84
+
85
+ end
86
+
87
+ def compile_undefined_operator(input_file_contents)
88
+
89
+ # This method compiles Nila's .undefined? method to native Javascript
90
+
91
+ # Example:
92
+ #
93
+ # input_args[0].undefined?
94
+ #
95
+ # The above line should compile to
96
+ #
97
+ # typeof input_args[0] === "undefined"
98
+
99
+ modified_contents = input_file_contents.collect {|element| replace_strings(element)}
100
+
101
+ possible_existential_calls = modified_contents.reject {|element| !element.include?(".undefined")}
102
+
103
+ split_regexp = /((if|while) \((!\()?)/
104
+
105
+ second_split_regexp = /(&&|\|\|)/
106
+
107
+ possible_existential_calls.each do |statement|
108
+
109
+ reconstructed_statement = input_file_contents[modified_contents.index(statement)]
110
+
111
+ counter = 0
112
+
113
+ while reconstructed_statement.include?(".undefined")
114
+
115
+ before,after = reconstructed_statement.split(".undefined",2)
116
+
117
+ if counter == 0
118
+
119
+ split_data = before.split(split_regexp)
120
+
121
+ before2 = split_data[1]
122
+
123
+ if before2.eql?("if (") or before2.eql?("while (")
124
+
125
+ usage = split_data[3]
126
+
127
+ elsif before2.eql?("if (!(") or before2.eql?("while (!(")
128
+
129
+ usage = split_data[4]
130
+
131
+ end
132
+
133
+ replacement_string = "(typeof #{usage} === \"undefined\")"
134
+
135
+ reconstructed_statement = before.split(before2)[0] + before2 + replacement_string + after
136
+
137
+ elsif counter > 0
138
+
139
+ split_data = before.split(second_split_regexp)
140
+
141
+ before2 = split_data[0]
142
+
143
+ operator = split_data[1]
144
+
145
+ operator = " &|&| " if operator.strip.eql?("&&")
146
+
147
+ operator = " |$|$ " if operator.strip.eql?("||")
148
+
149
+ usage = split_data[2]
150
+
151
+ replacement_string = "(typeof #{usage} === \"undefined\")"
152
+
153
+ reconstructed_statement = before2 + operator + replacement_string + after
154
+
155
+ end
156
+
157
+ counter += 1
158
+
159
+ end
160
+
161
+ reconstructed_statement = reconstructed_statement.gsub("&|&|","&&").gsub("|$|$","&&")
162
+
163
+ input_file_contents[modified_contents.index(statement)] = reconstructed_statement
164
+
165
+ end
166
+
167
+ return input_file_contents
168
+
169
+ end
@@ -174,7 +174,7 @@ def compile_named_functions(input_file_contents, named_code_blocks, nested_funct
174
174
 
175
175
  rejected_array = rejected_array[1..-1]
176
176
 
177
- if !rejected_array[0].strip.eql?("}")
177
+ unless rejected_array[0].strip.eql?("}") or rejected_array[0].strip.eql?("})")
178
178
 
179
179
  if !rejected_array[0].strip.eql?("end") and !rejected_array[0].strip.include?("--single_line_comment")
180
180
 
@@ -1,83 +1,89 @@
1
- def compile_operators(input_file_contents)
1
+ require_relative 'compile_existential_operator'
2
2
 
3
- def compile_power_operator(input_string)
3
+ def compile_operators(input_file_contents)
4
4
 
5
- matches = input_string.scan(/(\w{1,}\*\*\w{1,})/).to_a.flatten
5
+ def compile_power_operator(input_string)
6
6
 
7
- unless matches.empty?
7
+ matches = input_string.scan(/(\w{1,}\*\*\w{1,})/).to_a.flatten
8
8
 
9
- matches.each do |match|
9
+ unless matches.empty?
10
10
 
11
- left, right = match.split("**")
11
+ matches.each do |match|
12
12
 
13
- input_string = input_string.sub(match, "Math.pow(#{left},#{right})")
13
+ left, right = match.split("**")
14
14
 
15
- end
15
+ input_string = input_string.sub(match, "Math.pow(#{left},#{right})")
16
16
 
17
17
  end
18
18
 
19
- return input_string
20
-
21
19
  end
22
20
 
23
- def compile_match_operator(input_string)
21
+ return input_string
24
22
 
25
- rejection_exp = /( aannddy | orriioo |nnoottyy )/
23
+ end
26
24
 
27
- if input_string.include?("=~")
25
+ def compile_match_operator(input_string)
28
26
 
29
- input_string = input_string.gsub(" && "," aannddy ").gsub(" || "," orriioo ").gsub("!","nnoottyy")
27
+ rejection_exp = /( aannddy | orriioo |nnoottyy )/
30
28
 
31
- left, right = input_string.split("=~")
29
+ if input_string.include?("=~")
32
30
 
33
- if left.index(rejection_exp) != nil
31
+ input_string = input_string.gsub(" && ", " aannddy ").gsub(" || ", " orriioo ").gsub("!", "nnoottyy")
34
32
 
35
- left = left[left.index(rejection_exp)..-1]
33
+ left, right = input_string.split("=~")
36
34
 
37
- elsif left.index(/\(/)
35
+ if left.index(rejection_exp) != nil
38
36
 
39
- left = left[left.index(/\(/)+1..-1]
37
+ left = left[left.index(rejection_exp)..-1]
40
38
 
41
- end
39
+ elsif left.index(/\(/)
42
40
 
43
- if right.index(rejection_exp) != nil
41
+ left = left[left.index(/\(/)+1..-1]
44
42
 
45
- right = right[0...right.index(rejection_exp)]
43
+ end
46
44
 
47
- elsif right.index(/\)/)
45
+ if right.index(rejection_exp) != nil
48
46
 
49
- right = right[0...right.index(/\)/)]
47
+ right = right[0...right.index(rejection_exp)]
50
48
 
51
- end
49
+ elsif right.index(/\)/)
52
50
 
53
- original_string = "#{left}=~#{right}"
51
+ right = right[0...right.index(/\)/)]
54
52
 
55
- replacement_string = "#{left.rstrip} = #{left.rstrip}.match(#{right.lstrip.rstrip})"
53
+ end
56
54
 
57
- input_string = input_string.sub(original_string,replacement_string)
55
+ original_string = "#{left}=~#{right}"
58
56
 
59
- input_string = input_string.gsub(" aannddy "," && ").gsub(" orriioo "," || ").gsub("nnoottyy","!")
57
+ replacement_string = "#{left.rstrip} = #{left.rstrip}.match(#{right.lstrip.rstrip})"
60
58
 
61
- end
59
+ input_string = input_string.sub(original_string, replacement_string)
62
60
 
63
- return input_string
61
+ input_string = input_string.gsub(" aannddy ", " && ").gsub(" orriioo ", " || ").gsub("nnoottyy", "!")
64
62
 
65
63
  end
66
64
 
67
- input_file_contents = input_file_contents.collect { |element| element.sub("==", "===") }
65
+ return input_string
66
+
67
+ end
68
+
69
+ input_file_contents = input_file_contents.collect { |element| element.sub("==", "===") }
70
+
71
+ input_file_contents = input_file_contents.collect { |element| element.sub("!=", "!==") }
72
+
73
+ input_file_contents = input_file_contents.collect { |element| element.sub("equequ", "==") }
68
74
 
69
- input_file_contents = input_file_contents.collect { |element| element.sub("!=", "!==") }
75
+ input_file_contents = input_file_contents.collect { |element| element.sub("elsuf", "else if") }
70
76
 
71
- input_file_contents = input_file_contents.collect { |element| element.sub("equequ", "==") }
77
+ input_file_contents = compile_existential_operators(input_file_contents)
72
78
 
73
- input_file_contents = input_file_contents.collect { |element| element.sub("elsuf", "else if") }
79
+ input_file_contents = compile_undefined_operator(input_file_contents)
74
80
 
75
- input_file_contents = input_file_contents.collect { |element| compile_power_operator(element) }
81
+ input_file_contents = input_file_contents.collect { |element| compile_power_operator(element) }
76
82
 
77
- input_file_contents = input_file_contents.collect {|element| compile_match_operator(element)}
83
+ input_file_contents = input_file_contents.collect { |element| compile_match_operator(element) }
78
84
 
79
- input_file_contents = input_file_contents.collect {|element| element.gsub("_!;",";")}
85
+ input_file_contents = input_file_contents.collect { |element| element.gsub("_!;", ";") }
80
86
 
81
- return input_file_contents
87
+ return input_file_contents
82
88
 
83
- end
89
+ end
@@ -0,0 +1,43 @@
1
+ require_relative 'replace_strings'
2
+
3
+ def compile_require_statements(input_file_contents)
4
+
5
+ # This method automatically assigns variables to Node.js require statements
6
+ #
7
+ # Example:
8
+ #
9
+ # require 'fs'
10
+ #
11
+ # will compile to
12
+ #
13
+ # var fs = require('fs')
14
+
15
+ modified_file_contents = input_file_contents.collect {|element| replace_strings(element)}
16
+
17
+ possible_require_calls = modified_file_contents.reject {|element| !element.include?("require")}
18
+
19
+ possible_require_calls.each do |statement|
20
+
21
+ original_statement = input_file_contents[modified_file_contents.index(statement)]
22
+
23
+ before,required_module = original_statement.split("require")
24
+
25
+ unless before.include?("=")
26
+
27
+ required_module = required_module.strip
28
+
29
+ required_module = required_module[2...-2] if required_module.include?("(") and required_module.include?(")")
30
+
31
+ required_module = required_module[1...-1] if required_module.include?("'")
32
+
33
+ replacement_string = "#{required_module} = #{original_statement}"
34
+
35
+ input_file_contents[modified_file_contents.index(statement)] = replacement_string
36
+
37
+ end
38
+
39
+ end
40
+
41
+ return input_file_contents
42
+
43
+ end
@@ -3,6 +3,46 @@
3
3
  # These are some interesting methods that we really miss in Javascript.
4
4
  # So we have made these methods available
5
5
 
6
+ def compile_complex_methods(input_file_contents)
7
+
8
+ method_map_replacement = {
9
+
10
+ ".include" => [".indexOf"," != -1"],
11
+
12
+ }
13
+
14
+ method_map_replacement.each do |val,key|
15
+
16
+ possible_complex_method_calls = input_file_contents.reject {|element| !element.include?(val)}
17
+
18
+ possible_complex_method_calls.each do |statement|
19
+
20
+ before,after = statement.split(val)
21
+
22
+ after,remains = after.split
23
+
24
+ if after[-1].eql?(")")
25
+
26
+ after = after[0...-1] + key[1] + after[-1]
27
+
28
+ elsif after.strip.eql?(after)
29
+
30
+ after = after + key[1]
31
+
32
+ end
33
+
34
+ reconstructed_statement = before + key[0] + after + remains
35
+
36
+ input_file_contents[input_file_contents.index(statement)] = reconstructed_statement
37
+
38
+ end
39
+
40
+ end
41
+
42
+ return input_file_contents
43
+
44
+ end
45
+
6
46
  method_map_replacement = {
7
47
 
8
48
  ".split" => ".split(\" \")",
@@ -19,7 +59,7 @@
19
59
 
20
60
  ".reverse" => ".reverse()",
21
61
 
22
- ".empty?" => ".length == 0",
62
+ ".empty" => ".length == 0",
23
63
 
24
64
  ".upcase" => ".toUpperCase()",
25
65
 
@@ -53,6 +93,8 @@
53
93
 
54
94
  end
55
95
 
96
+ modified_file_contents = compile_complex_methods(modified_file_contents)
97
+
56
98
  return modified_file_contents
57
99
 
58
100
  end
@@ -0,0 +1,6 @@
1
+ # This method compiles plain ternary operators into equivalent Javascript code
2
+
3
+ def compile_ternary_operator(input_file_contents)
4
+
5
+
6
+ end
@@ -1,4 +1,5 @@
1
1
  require_relative 'read_file_line_by_line'
2
+ require_relative 'replace_strings'
2
3
 
3
4
  def compile_whitespace_delimited_functions(input_file_contents, function_names, temporary_nila_file)
4
5
 
@@ -52,25 +53,31 @@ require_relative 'read_file_line_by_line'
52
53
 
53
54
  joined_file_contents = input_file_contents.join
54
55
 
56
+ test_contents = replace_strings(joined_file_contents)
57
+
55
58
  function_names.each do |list_of_functions|
56
59
 
57
60
  list_of_functions.each do |function|
58
61
 
59
- matching_strings = extract(joined_file_contents, function+" ", "\n")
62
+ if test_contents.include?(function)
63
+
64
+ matching_strings = extract(joined_file_contents, function+" ", "\n")
65
+
66
+ matching_strings.each do |string|
60
67
 
61
- matching_strings.each do |string|
68
+ modified_string = string.dup
62
69
 
63
- modified_string = string.dup
70
+ modified_string = modified_string.rstrip + modified_string.split(modified_string.rstrip)[1].gsub(" ", "")
64
71
 
65
- modified_string = modified_string.rstrip + modified_string.split(modified_string.rstrip)[1].gsub(" ", "")
72
+ modified_string = modified_string.sub(function+" ", function+"(")
66
73
 
67
- modified_string = modified_string.sub(function+" ", function+"(")
74
+ modified_string = modified_string.split("#{function}(")[0] + "#{function}(" + modified_string.split("#{function}(")[1].lstrip
68
75
 
69
- modified_string = modified_string.split("#{function}(")[0] + "#{function}(" + modified_string.split("#{function}(")[1].lstrip
76
+ modified_string = modified_string.sub("\n", ")\n")
70
77
 
71
- modified_string = modified_string.sub("\n", ")\n")
78
+ joined_file_contents = joined_file_contents.sub(string, modified_string)
72
79
 
73
- joined_file_contents = joined_file_contents.sub(string, modified_string)
80
+ end
74
81
 
75
82
  end
76
83
 
@@ -76,12 +76,16 @@ require_relative 'read_file_line_by_line'
76
76
 
77
77
  line_by_line_contents = read_file_line_by_line(temporary_nila_file)
78
78
 
79
+ test_contents = line_by_line_contents.collect {|element| replace_strings(element)}
80
+
79
81
  for_loop_variables = []
80
82
 
81
- for_loop_statements = line_by_line_contents.reject {|line| !line.include?("for")}
83
+ for_loop_statements = test_contents.reject {|line| !line.include?("for")}
82
84
 
83
85
  for_loop_statements = for_loop_statements.reject {|line| line.include?("forEach")}
84
86
 
87
+ puts for_loop_statements
88
+
85
89
  for_loop_statements.each do |statement|
86
90
 
87
91
  varis = statement.split("for (")[1].split(";",2)[0].split(",")
@@ -46,7 +46,7 @@ require_relative 'read_file_line_by_line'
46
46
 
47
47
  def extract_blocks(file_contents)
48
48
 
49
- javascript_regexp = /(if |while |for |case |default:|switch\(|function\(|((=|:)\s+\{))/
49
+ javascript_regexp = /(if |for |while |case |default:|switch\(|\(function\(|= function\(|function\(|((=|:)\s+\{))/
50
50
 
51
51
  block_starting_lines = file_contents.dup.reject { |element| element.index(javascript_regexp).nil? }[1..-1]
52
52
 
@@ -62,7 +62,7 @@ require_relative 'read_file_line_by_line'
62
62
 
63
63
  end
64
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"))}
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].strip.eql?("});\n"))}
66
66
 
67
67
  modified_file_contents = file_contents.dup
68
68
 
@@ -0,0 +1,100 @@
1
+ def extract_blocks(statement_indexes, input_file_contents)
2
+
3
+ possible_if_blocks = []
4
+
5
+ if_block_counter = 0
6
+
7
+ extracted_blocks = []
8
+
9
+ controlregexp = /(if |while |def | do )/
10
+
11
+ for x in 0...if_statement_indexes.length-1
12
+
13
+ possible_if_blocks << input_file_contents[if_statement_indexes[x]..if_statement_indexes[x+1]]
14
+
15
+ end
16
+
17
+ end_counter = 0
18
+
19
+ end_index = []
20
+
21
+ current_block = []
22
+
23
+ possible_if_blocks.each_with_index do |block|
24
+
25
+ unless current_block[-1] == block[0]
26
+
27
+ current_block += block
28
+
29
+ else
30
+
31
+ current_block += block[1..-1]
32
+
33
+ end
34
+
35
+
36
+ current_block.each_with_index do |line, index|
37
+
38
+ if line.strip.eql? "end"
39
+
40
+ end_counter += 1
41
+
42
+ end_index << index
43
+
44
+ end
45
+
46
+ end
47
+
48
+ if end_counter > 0
49
+
50
+ until end_index.empty?
51
+
52
+ array_extract = current_block[0..end_index[0]].reverse
53
+
54
+ index_counter = 0
55
+
56
+ array_extract.each_with_index do |line|
57
+
58
+ break if (line.lstrip.index(controlregexp) != nil and line.lstrip.index(rejectionregexp).nil?)
59
+
60
+ index_counter += 1
61
+
62
+ end
63
+
64
+ block_extract = array_extract[0..index_counter].reverse
65
+
66
+ extracted_blocks << block_extract
67
+
68
+ block_start = current_block.index(block_extract[0])
69
+
70
+ block_end = current_block.index(block_extract[-1])
71
+
72
+ current_block[block_start..block_end] = "--ifblock#{if_block_counter}"
73
+
74
+ if_block_counter += 1
75
+
76
+ end_counter = 0
77
+
78
+ end_index = []
79
+
80
+ current_block.each_with_index do |line, index|
81
+
82
+ if line.strip.eql? "end"
83
+
84
+ end_counter += 1
85
+
86
+ end_index << index
87
+
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+
98
+ return current_block, extracted_blocks
99
+
100
+ end
data/lib/nilac/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Nilac
2
- VERSION = "0.0.4.3.9.4"
2
+ VERSION = "0.0.4.3.9.5"
3
3
  end
data/lib/nilac.rb CHANGED
@@ -19,6 +19,8 @@ module Nilac
19
19
 
20
20
  require 'nilac/extract_parsable_file'
21
21
 
22
+ require 'nilac/compile_require_statements'
23
+
22
24
  require 'nilac/replace_multiline_comments'
23
25
 
24
26
  require 'nilac/split_semicolon_seperated_expressions'
@@ -80,6 +82,8 @@ module Nilac
80
82
  require 'nilac/create_mac_executable'
81
83
 
82
84
  require 'nilac/parse_arguments'
85
+
86
+ require 'nilac/compile_classes'
83
87
 
84
88
  class NilaCompiler
85
89
 
@@ -97,12 +101,16 @@ module Nilac
97
101
 
98
102
  file_contents = extract_parsable_file(file_contents)
99
103
 
104
+ file_contents = compile_require_statements(file_contents)
105
+
100
106
  file_contents, multiline_comments, temp_file, output_js_file = replace_multiline_comments(file_contents, input_file_path, *output_file_name)
101
107
 
102
108
  file_contents, singleline_comments = replace_singleline_comments(file_contents)
103
109
 
104
110
  file_contents = split_semicolon_seperated_expressions(file_contents)
105
111
 
112
+ compile_classes(file_contents)
113
+
106
114
  file_contents = compile_heredocs(file_contents, temp_file)
107
115
 
108
116
  file_contents,loop_vars = compile_loops(file_contents,temp_file)
@@ -315,8 +323,10 @@ end
315
323
 
316
324
  if ARGV.include?("--test")
317
325
 
318
- compiler = Nilac::NilaCompiler.new(ARGV)
326
+ ARGV.delete("--test")
327
+
328
+ compiler = Nilac::NilaCompiler.new(ARGV)
319
329
 
320
- compiler.start_compile()
330
+ compiler.start_compile()
321
331
 
322
332
  end
@@ -15,4 +15,4 @@ in a function. It is a rubyesque feature imported into Nila.
15
15
  Configurations:
16
16
 
17
17
  ~compiler => lib/nilac.rb
18
- :v $cliusage => ruby :v --compile $file
18
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature bring Ruby's indexing capabililty to Javascript.
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -0,0 +1,11 @@
1
+ Feature: Automatically add variable declaration for required modules. Very useful for Node.js developers.
2
+ Scenario: Input file with required modules
3
+ Given the input file "required_module.nila"
4
+ When the ~compiler is run
5
+ The output file must be "required_module.js"
6
+ The output file must equal "correct_required_module.js"
7
+
8
+ Configurations:
9
+
10
+ ~compiler => lib/nilac.rb
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: Compiling a single line nila program
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature bring Ruby's case when structure to Nila
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -9,4 +9,4 @@ feature addresses that issue.
9
9
  Configurations:
10
10
 
11
11
  ~compiler => lib/nilac.rb
12
- :v $cliusage => ruby :v --compile $file
12
+ :v $cliusage => ruby :v --test --compile $file
@@ -10,4 +10,4 @@ which will make it fail in a JSLint test.
10
10
  Configurations:
11
11
 
12
12
  ~compiler => lib/nilac.rb
13
- :v $cliusage => ruby :v --compile $file
13
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature brings Ruby style Hashes to Nila
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature brings heredocs to Nila
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature brings single line if-then-else statement to Nila
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature brings loop keyword to Nila
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -14,4 +14,4 @@ Feature: Javascript, by default, doesn't allow for the return of multiple values
14
14
  Configurations:
15
15
 
16
16
  ~compiler => lib/nilac.rb
17
- :v $cliusage => ruby :v --compile $file
17
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature brings multiline arrays to Nila
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -9,4 +9,4 @@ Feature: Javascript doesn't allow multiple variable initializations from a metho
9
9
  Configurations:
10
10
 
11
11
  ~compiler => lib/nilac.rb
12
- :v $cliusage => ruby :v --compile $file
12
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature brings Ruby's number features to Nila
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: Compiling for loops in a nila program
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature bring Ruby's if statement to Nila.
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature bring Ruby's while statement to Nila.
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature brings Ruby's native String and Array methods to Nila.
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature bring Ruby style operators to Nila
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature brings powerful Splat arguments for Methods/Functions
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature bring Ruby style string interpolation to Nila
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature bring Ruby's string features to Nila
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature brings Ruby's .times method to Nila
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature bring Ruby's unless and until statements to Nila.
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -8,4 +8,4 @@ Feature: This feature brings whitespace delimitation features to Nila
8
8
  Configurations:
9
9
 
10
10
  ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
11
+ :v $cliusage => ruby :v --test --compile $file
@@ -0,0 +1,14 @@
1
+ //Written using Nila. Visit http://adhithyan15.github.io/nila
2
+ (function() {
3
+ var fs;
4
+
5
+ fs = require('fs');
6
+
7
+ fs.readFile('/etc/hosts','utf8',function(err,data) {
8
+ if (err) {
9
+ console.log(err);
10
+ }
11
+ console.log(data);
12
+ });
13
+
14
+ }).call(this);
@@ -0,0 +1,11 @@
1
+ require 'fs'
2
+
3
+ http = require 'http'
4
+
5
+ fs.readFile '/etc/hosts','utf8', do |err,data|
6
+
7
+ puts err if err
8
+
9
+ puts data
10
+
11
+ end
@@ -0,0 +1,10 @@
1
+ class Language
2
+ def initialize(name, creator)
3
+ @name = name
4
+ @creator = creator
5
+ end
6
+
7
+ def description
8
+ puts "I'm #{@name} and I was created by #{@creator}!"
9
+ end
10
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nilac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4.3.9.4
4
+ version: 0.0.4.3.9.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adhithya Rajasekaran
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-01 00:00:00.000000000 Z
11
+ date: 2013-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shark
@@ -55,6 +55,7 @@ files:
55
55
  - lib/nilac/compile_conditional_structures.rb
56
56
  - lib/nilac/compile_custom_function_map.rb
57
57
  - lib/nilac/compile_default_values.rb
58
+ - lib/nilac/compile_existential_operator.rb
58
59
  - lib/nilac/compile_hashes.rb
59
60
  - lib/nilac/compile_heredocs.rb
60
61
  - lib/nilac/compile_integers.rb
@@ -63,9 +64,11 @@ files:
63
64
  - lib/nilac/compile_named_functions.rb
64
65
  - lib/nilac/compile_operators.rb
65
66
  - lib/nilac/compile_parallel_assignment.rb
67
+ - lib/nilac/compile_require_statements.rb
66
68
  - lib/nilac/compile_ruby_methods.rb
67
69
  - lib/nilac/compile_special_keywords.rb
68
70
  - lib/nilac/compile_strings.rb
71
+ - lib/nilac/compile_ternary_operator.rb
69
72
  - lib/nilac/compile_whitespace_delimited_functions.rb
70
73
  - lib/nilac/create_mac_executable.rb
71
74
  - lib/nilac/extract_parsable_file.rb
@@ -82,13 +85,14 @@ files:
82
85
  - lib/nilac/replace_named_functions.rb
83
86
  - lib/nilac/replace_singleline_comments.rb
84
87
  - lib/nilac/replace_strings.rb
88
+ - lib/nilac/rollblocks.rb
85
89
  - lib/nilac/split_semicolon_seperated_expressions.rb
86
90
  - lib/nilac/strToArray.rb
87
91
  - lib/nilac/version.rb
88
92
  - nilac.gemspec
89
93
  - shark/features/add_auto_return_statement.feature
90
94
  - shark/features/array_and_string_indexing.feature
91
- - shark/features/array_sugar.feature
95
+ - shark/features/automatic_require_statements.feature
92
96
  - shark/features/barebones_compilation.feature
93
97
  - shark/features/case_when.feature
94
98
  - shark/features/default_method_parameters.feature
@@ -113,7 +117,6 @@ files:
113
117
  - shark/features/unless_until.feature
114
118
  - shark/features/whitespace_delimitation.feature
115
119
  - shark/test_files/array_string_indexing.nila
116
- - shark/test_files/array_sugar.js
117
120
  - shark/test_files/array_sugar.nila
118
121
  - shark/test_files/case.nila
119
122
  - shark/test_files/class.nila
@@ -133,6 +136,7 @@ files:
133
136
  - shark/test_files/correct_operators.js
134
137
  - shark/test_files/correct_regular_if.js
135
138
  - shark/test_files/correct_regular_while.js
139
+ - shark/test_files/correct_required_module.js
136
140
  - shark/test_files/correct_return.js
137
141
  - shark/test_files/correct_ruby_methods.js
138
142
  - shark/test_files/correct_single_return.js
@@ -158,7 +162,9 @@ files:
158
162
  - shark/test_files/regular_for.nila
159
163
  - shark/test_files/regular_if.nila
160
164
  - shark/test_files/regular_while.nila
165
+ - shark/test_files/required_module.nila
161
166
  - shark/test_files/ruby_methods.nila
167
+ - shark/test_files/sample_class.nila
162
168
  - shark/test_files/simple.nila
163
169
  - shark/test_files/single_return.nila
164
170
  - shark/test_files/splats.nila
@@ -1,11 +0,0 @@
1
- Feature: This feature brings Ruby's Syntactic Sugar for Arrays to Nila
2
- Scenario: Input file with Ruby style arrays
3
- Given the input file "array_sugar.nila"
4
- When the ~compiler is run
5
- The output file must be "array_sugar.js"
6
- The output file must equal "correct_array_sugar.js"
7
-
8
- Configurations:
9
-
10
- ~compiler => lib/nilac.rb
11
- :v $cliusage => ruby :v --compile $file
@@ -1,9 +0,0 @@
1
- //Written using Nila. Visit http://adhithyan15.github.io/nila
2
- (function() {
3
- var date_time, names;
4
-
5
- names = ["joe", "kelly", "bill", "james", "smith"];
6
-
7
- date_time = ["\"The", "current", "date", "and", "time", "is", "\\" + (new Date().toString()) + \"];
8
-
9
- }).call(this);