nilac 0.0.4.3.9.1 → 0.0.4.3.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/bin/nilac +308 -18
- data/lib/nilac/version.rb +1 -1
- data/shark/features/splats.feature +11 -0
- data/shark/test_files/case.nila +1 -1
- data/shark/test_files/correct_case.js +1 -1
- data/shark/test_files/correct_splats.js +89 -0
- data/shark/test_files/splats.nila +36 -0
- data/src/nilac.rb +308 -18
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8cec9f92a950d69f17f9a9e5d2fc2e240bffa540
|
4
|
+
data.tar.gz: 5c4ab209155e48ad3c44c934e7abfbe9d6d5e82b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0508eff8f8d817ece1ea7c4359cd87f9135db0dad59cd399d6b1e5d664673bc2981bed328721108ecac990289d930ffc7b8eb280f6e5db7e4c1c40cd4a5b74c
|
7
|
+
data.tar.gz: fa3284945a8f4564e1e4611b3d6678d28808650d009fbda5472bfa356cf81d2a3d866e980f83ff933a8d054282301ac8de8c4f07b4733f0d7e8cb4ca771b4223
|
data/.gitignore
CHANGED
data/bin/nilac
CHANGED
@@ -8,6 +8,20 @@
|
|
8
8
|
require 'slop'
|
9
9
|
require 'fileutils'
|
10
10
|
|
11
|
+
# The following are error classes used by Nilac to give you detailed error information
|
12
|
+
|
13
|
+
class ParseError < RuntimeError
|
14
|
+
|
15
|
+
def initialize(message)
|
16
|
+
|
17
|
+
puts "ParseError: " + message
|
18
|
+
|
19
|
+
abort
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
11
25
|
|
12
26
|
def compile(input_file_path, *output_file_name)
|
13
27
|
|
@@ -784,6 +798,39 @@ def compile(input_file_path, *output_file_name)
|
|
784
798
|
# puts "Filling the #{container} with #{liquid}"
|
785
799
|
# end
|
786
800
|
|
801
|
+
def errorFree(function_params)
|
802
|
+
|
803
|
+
# This method checks for use cases in complex arguments where a default argument is used
|
804
|
+
# after an optional argument. This will result in erroneous output. So this method will
|
805
|
+
# stop it from happening.
|
806
|
+
|
807
|
+
# Example:
|
808
|
+
# def method_name(a,b,*c,d = 1,c,e)
|
809
|
+
|
810
|
+
optional_param = function_params.reject {|element| !replace_strings(element).include?("*")}[0]
|
811
|
+
|
812
|
+
unless optional_param.nil?
|
813
|
+
|
814
|
+
after_splat = function_params[function_params.index(optional_param)+1..-1]
|
815
|
+
|
816
|
+
if after_splat.reject {|element| !element.include?("=")}.empty?
|
817
|
+
|
818
|
+
true
|
819
|
+
|
820
|
+
else
|
821
|
+
|
822
|
+
ParseError.new("You cannot have default argument after an optional argument! Change the following usage!\n#{function_params.join(",")}")
|
823
|
+
|
824
|
+
end
|
825
|
+
|
826
|
+
else
|
827
|
+
|
828
|
+
true
|
829
|
+
|
830
|
+
end
|
831
|
+
|
832
|
+
end
|
833
|
+
|
787
834
|
def parse_default_values(input_function_definition)
|
788
835
|
|
789
836
|
split1, split2 = input_function_definition.split("(")
|
@@ -792,23 +839,27 @@ def compile(input_file_path, *output_file_name)
|
|
792
839
|
|
793
840
|
function_parameters = split2.split(",")
|
794
841
|
|
795
|
-
|
842
|
+
if errorFree(function_parameters)
|
796
843
|
|
797
|
-
|
844
|
+
default_value_parameters = function_parameters.reject { |element| !element.include? "=" }
|
798
845
|
|
799
|
-
|
846
|
+
replacement_parameters = []
|
800
847
|
|
801
|
-
|
848
|
+
replacement_string = ""
|
802
849
|
|
803
|
-
|
850
|
+
default_value_parameters.each do |paramvalue|
|
804
851
|
|
805
|
-
|
852
|
+
param, value = paramvalue.split("=")
|
806
853
|
|
807
|
-
|
854
|
+
replacement_parameters << param.lstrip.rstrip
|
808
855
|
|
809
|
-
|
856
|
+
replacement_string = replacement_string + "\n" + "if (#{param.lstrip.rstrip} equequ null) {\n #{paramvalue.lstrip.rstrip}\n}\n" +"\n"
|
810
857
|
|
811
|
-
|
858
|
+
end
|
859
|
+
|
860
|
+
return replacement_string, default_value_parameters, replacement_parameters
|
861
|
+
|
862
|
+
end
|
812
863
|
|
813
864
|
end
|
814
865
|
|
@@ -858,7 +909,7 @@ def compile(input_file_path, *output_file_name)
|
|
858
909
|
|
859
910
|
input_file_contents[current_line_index] = modified_line
|
860
911
|
|
861
|
-
input_file_contents
|
912
|
+
input_file_contents.insert(current_line_index+1,replacement_string)
|
862
913
|
|
863
914
|
end
|
864
915
|
|
@@ -1840,7 +1891,7 @@ def compile(input_file_path, *output_file_name)
|
|
1840
1891
|
|
1841
1892
|
else
|
1842
1893
|
|
1843
|
-
return variables.uniq
|
1894
|
+
return variables.uniq.sort
|
1844
1895
|
|
1845
1896
|
end
|
1846
1897
|
|
@@ -1882,6 +1933,8 @@ def compile(input_file_path, *output_file_name)
|
|
1882
1933
|
|
1883
1934
|
rejected_array = reversed_input_array.reject { |content| content.lstrip.eql?("") }
|
1884
1935
|
|
1936
|
+
rejected_array = rejected_array.reject {|content| content.strip.eql?("")}
|
1937
|
+
|
1885
1938
|
rejected_array = rejected_array[1..-1]
|
1886
1939
|
|
1887
1940
|
if !rejected_array[0].strip.eql?("}")
|
@@ -2106,6 +2159,16 @@ def compile(input_file_path, *output_file_name)
|
|
2106
2159
|
|
2107
2160
|
modified_input_array = compile_parallel_assignment(modified_input_array, temporary_nila_file)
|
2108
2161
|
|
2162
|
+
modified_input_array = compile_multiple_ruby_func_calls(modified_input_array)
|
2163
|
+
|
2164
|
+
modified_input_array = add_auto_return_statement(modified_input_array)
|
2165
|
+
|
2166
|
+
modified_input_array = compile_multiple_return(modified_input_array)
|
2167
|
+
|
2168
|
+
modified_input_array = coffee_type_function(modified_input_array)
|
2169
|
+
|
2170
|
+
modified_input_array = compile_splats(modified_input_array)
|
2171
|
+
|
2109
2172
|
variables = lexical_scoped_variables(modified_input_array)
|
2110
2173
|
|
2111
2174
|
if !variables.empty?
|
@@ -2118,12 +2181,6 @@ def compile(input_file_path, *output_file_name)
|
|
2118
2181
|
|
2119
2182
|
modified_input_array = remove_question_marks(modified_input_array, variables, temporary_nila_file)
|
2120
2183
|
|
2121
|
-
modified_input_array = add_auto_return_statement(modified_input_array)
|
2122
|
-
|
2123
|
-
modified_input_array = compile_multiple_return(modified_input_array)
|
2124
|
-
|
2125
|
-
modified_input_array = coffee_type_function(modified_input_array)
|
2126
|
-
|
2127
2184
|
return modified_input_array
|
2128
2185
|
|
2129
2186
|
end
|
@@ -2148,6 +2205,237 @@ def compile(input_file_path, *output_file_name)
|
|
2148
2205
|
|
2149
2206
|
end
|
2150
2207
|
|
2208
|
+
def compile_splats(input_function_block)
|
2209
|
+
|
2210
|
+
def strToArray(input_string)
|
2211
|
+
|
2212
|
+
file_id = File.new('hello.nila','w')
|
2213
|
+
|
2214
|
+
file_id.write(input_string)
|
2215
|
+
|
2216
|
+
file_id.close()
|
2217
|
+
|
2218
|
+
line_by_line_contents = read_file_line_by_line('hello.nila')
|
2219
|
+
|
2220
|
+
File.delete(file_id)
|
2221
|
+
|
2222
|
+
return line_by_line_contents
|
2223
|
+
|
2224
|
+
end
|
2225
|
+
|
2226
|
+
def errorFree(function_params,optional_param)
|
2227
|
+
|
2228
|
+
# This method checks for use cases in complex arguments where a default argument is used
|
2229
|
+
# after an optional argument. This will result in erroneous output. So this method will
|
2230
|
+
# stop it from happening.
|
2231
|
+
|
2232
|
+
# Example:
|
2233
|
+
# def method_name(a,b,*c,d = 1,c,e)
|
2234
|
+
|
2235
|
+
after_splat = function_params[function_params.index(optional_param)+1..-1]
|
2236
|
+
|
2237
|
+
if after_splat.reject {|element| !element.include?("=")}.empty?
|
2238
|
+
|
2239
|
+
true
|
2240
|
+
|
2241
|
+
else
|
2242
|
+
|
2243
|
+
raise "You cannot have a default argument after an optional argument!"
|
2244
|
+
|
2245
|
+
false
|
2246
|
+
|
2247
|
+
end
|
2248
|
+
|
2249
|
+
end
|
2250
|
+
|
2251
|
+
function_params = input_function_block[0].split("function(")[1].split(")")[0].split(",")
|
2252
|
+
|
2253
|
+
unless function_params.reject{|element| !replace_strings(element).include?("*")}.empty?
|
2254
|
+
|
2255
|
+
mod_function_params = function_params.reject {|element| replace_strings(element).include?("*")}
|
2256
|
+
|
2257
|
+
opt_index = 0
|
2258
|
+
|
2259
|
+
# If there are multiple optional params declared by mistake, only the first optional param is used.
|
2260
|
+
|
2261
|
+
optional_param = function_params.reject {|element| !replace_strings(element).include?("*")}[0]
|
2262
|
+
|
2263
|
+
if function_params.index(optional_param).eql?(function_params.length-1)
|
2264
|
+
|
2265
|
+
mod_function_params.each_with_index do |param,index|
|
2266
|
+
|
2267
|
+
input_function_block.insert(index+1,"#{param} = arguments[#{index}]\n\n")
|
2268
|
+
|
2269
|
+
opt_index = index + 1
|
2270
|
+
|
2271
|
+
end
|
2272
|
+
|
2273
|
+
replacement_string = "#{optional_param.gsub("*","")} = []\n\n"
|
2274
|
+
|
2275
|
+
replacement_string += "for (var i=#{opt_index};i<arguments.length;i++) {\n #{optional_param.gsub("*","")}.push(arguments[i]); \n}\n\n"
|
2276
|
+
|
2277
|
+
input_function_block.insert(opt_index+1,replacement_string)
|
2278
|
+
|
2279
|
+
input_function_block[0] = input_function_block[0].sub(function_params.join(","),"")
|
2280
|
+
|
2281
|
+
else
|
2282
|
+
|
2283
|
+
before_splat = function_params[0...function_params.index(optional_param)]
|
2284
|
+
|
2285
|
+
after_splat = function_params[function_params.index(optional_param)+1..-1]
|
2286
|
+
|
2287
|
+
cont_index = 0
|
2288
|
+
|
2289
|
+
if errorFree(function_params,optional_param)
|
2290
|
+
|
2291
|
+
before_splat.each_with_index do |param,index|
|
2292
|
+
|
2293
|
+
input_function_block.insert(index+1,"#{param} = arguments[#{index}]\n\n")
|
2294
|
+
|
2295
|
+
cont_index = index + 1
|
2296
|
+
|
2297
|
+
end
|
2298
|
+
|
2299
|
+
after_splat.each_with_index do |param,index|
|
2300
|
+
|
2301
|
+
input_function_block.insert(cont_index+1,"#{param} = arguments[arguments.length-#{after_splat.length - index}]\n\n")
|
2302
|
+
|
2303
|
+
cont_index = cont_index + 1
|
2304
|
+
|
2305
|
+
end
|
2306
|
+
|
2307
|
+
replacement_string = "#{optional_param.gsub("*","")} = []\n\n"
|
2308
|
+
|
2309
|
+
replacement_string += "for (var i=#{function_params.index(optional_param)};i < arguments.length-#{after_splat.length};i++) {\n #{optional_param.gsub("*","")}.push(arguments[i]); \n}\n\n"
|
2310
|
+
|
2311
|
+
input_function_block.insert(cont_index+1,replacement_string)
|
2312
|
+
|
2313
|
+
input_function_block[0] = input_function_block[0].sub(function_params.join(","),"")
|
2314
|
+
|
2315
|
+
end
|
2316
|
+
|
2317
|
+
end
|
2318
|
+
|
2319
|
+
end
|
2320
|
+
|
2321
|
+
return strToArray(input_function_block.join)
|
2322
|
+
|
2323
|
+
end
|
2324
|
+
|
2325
|
+
def compile_multiple_ruby_func_calls(input_file_contents)
|
2326
|
+
|
2327
|
+
def strToArray(input_string)
|
2328
|
+
|
2329
|
+
file_id = File.new('hello.nila','w')
|
2330
|
+
|
2331
|
+
file_id.write(input_string)
|
2332
|
+
|
2333
|
+
file_id.close()
|
2334
|
+
|
2335
|
+
line_by_line_contents = read_file_line_by_line('hello.nila')
|
2336
|
+
|
2337
|
+
File.delete(file_id)
|
2338
|
+
|
2339
|
+
return line_by_line_contents
|
2340
|
+
|
2341
|
+
end
|
2342
|
+
|
2343
|
+
def replace_strings(input_string)
|
2344
|
+
|
2345
|
+
string_counter = 0
|
2346
|
+
|
2347
|
+
if input_string.count("\"") % 2 == 0
|
2348
|
+
|
2349
|
+
while input_string.include?("\"")
|
2350
|
+
|
2351
|
+
string_extract = input_string[input_string.index("\"")..input_string.index("\"",input_string.index("\"")+1)]
|
2352
|
+
|
2353
|
+
input_string = input_string.sub(string_extract,"--repstring#{string_counter}")
|
2354
|
+
|
2355
|
+
string_counter += 1
|
2356
|
+
|
2357
|
+
end
|
2358
|
+
|
2359
|
+
end
|
2360
|
+
|
2361
|
+
if input_string.count("'") % 2 == 0
|
2362
|
+
|
2363
|
+
while input_string.include?("'")
|
2364
|
+
|
2365
|
+
string_extract = input_string[input_string.index("'")..input_string.index("'",input_string.index("'")+1)]
|
2366
|
+
|
2367
|
+
input_string = input_string.sub(string_extract,"--repstring#{string_counter}")
|
2368
|
+
|
2369
|
+
string_counter += 1
|
2370
|
+
|
2371
|
+
end
|
2372
|
+
|
2373
|
+
end
|
2374
|
+
|
2375
|
+
input_string = input_string.gsub(/\((\w{0,},)*\w{0,}\)/,"--$k$")
|
2376
|
+
|
2377
|
+
return input_string
|
2378
|
+
|
2379
|
+
end
|
2380
|
+
|
2381
|
+
function_calls = []
|
2382
|
+
|
2383
|
+
replacement_calls = []
|
2384
|
+
|
2385
|
+
function_map = %w{puts p print}
|
2386
|
+
|
2387
|
+
javascript_regexp = /(if |for |while |\(function\(|= function\(|((=|:)\s+\{))/
|
2388
|
+
|
2389
|
+
stringified_input = input_file_contents.collect {|element| replace_strings(element)}
|
2390
|
+
|
2391
|
+
function_map.each do |func|
|
2392
|
+
|
2393
|
+
func_calls = input_file_contents.reject {|line| !(line.include?(func+"(") or line.include?(func+" ") and line.index(javascript_regexp) == nil)}
|
2394
|
+
|
2395
|
+
modified_func_calls = func_calls.collect {|element| replace_strings(element)}
|
2396
|
+
|
2397
|
+
modified_func_calls = modified_func_calls.reject {|element| !element.include?(",")}
|
2398
|
+
|
2399
|
+
call_collector = []
|
2400
|
+
|
2401
|
+
modified_func_calls.each_with_index do |ele|
|
2402
|
+
|
2403
|
+
call_collector << input_file_contents[stringified_input.index(ele)]
|
2404
|
+
|
2405
|
+
end
|
2406
|
+
|
2407
|
+
function_calls << modified_func_calls
|
2408
|
+
|
2409
|
+
rep_calls = []
|
2410
|
+
|
2411
|
+
call_collector.each do |fcall|
|
2412
|
+
|
2413
|
+
multiple_call = fcall.split(func)[1].split(",")
|
2414
|
+
|
2415
|
+
multiple_call = multiple_call.collect {|element| "\n#{func} " + element.strip + "\n\n"}
|
2416
|
+
|
2417
|
+
rep_calls << multiple_call.join
|
2418
|
+
|
2419
|
+
end
|
2420
|
+
|
2421
|
+
replacement_calls << rep_calls
|
2422
|
+
|
2423
|
+
end
|
2424
|
+
|
2425
|
+
replacement_calls = replacement_calls.flatten
|
2426
|
+
|
2427
|
+
function_calls = function_calls.flatten
|
2428
|
+
|
2429
|
+
function_calls.each_with_index do |fcall,index|
|
2430
|
+
|
2431
|
+
input_file_contents[stringified_input.index(fcall)] = replacement_calls[index]
|
2432
|
+
|
2433
|
+
end
|
2434
|
+
|
2435
|
+
return strToArray(input_file_contents.join)
|
2436
|
+
|
2437
|
+
end
|
2438
|
+
|
2151
2439
|
joined_file_contents = input_file_contents.join
|
2152
2440
|
|
2153
2441
|
unless named_code_blocks.empty?
|
@@ -2190,7 +2478,7 @@ def compile(input_file_path, *output_file_name)
|
|
2190
2478
|
|
2191
2479
|
file_id.close()
|
2192
2480
|
|
2193
|
-
line_by_line_contents = read_file_line_by_line(temporary_nila_file)
|
2481
|
+
line_by_line_contents = compile_multiple_ruby_func_calls(read_file_line_by_line(temporary_nila_file))
|
2194
2482
|
|
2195
2483
|
return line_by_line_contents, function_names
|
2196
2484
|
|
@@ -2446,6 +2734,8 @@ def compile(input_file_path, *output_file_name)
|
|
2446
2734
|
|
2447
2735
|
modified_string = modified_string.sub(function+" ", function+"(")
|
2448
2736
|
|
2737
|
+
modified_string = modified_string.split("#{function}(")[0] + "#{function}(" + modified_string.split("#{function}(")[1].lstrip
|
2738
|
+
|
2449
2739
|
modified_string = modified_string.sub("\n", ")\n")
|
2450
2740
|
|
2451
2741
|
joined_file_contents = joined_file_contents.sub(string, modified_string)
|
data/lib/nilac/version.rb
CHANGED
@@ -0,0 +1,11 @@
|
|
1
|
+
Feature: This feature brings powerful Splat arguments for Methods/Functions
|
2
|
+
Scenario: Input file with multiple splats parameters for methods/functions
|
3
|
+
Given the input file "splats.nila"
|
4
|
+
When the ~compiler is run
|
5
|
+
The output file must be "splats.js"
|
6
|
+
The output file must equal "correct_splats.js"
|
7
|
+
|
8
|
+
Configurations:
|
9
|
+
|
10
|
+
~compiler => src/nilac.rb
|
11
|
+
:v $cliusage => ruby :v --compile $file
|
data/shark/test_files/case.nila
CHANGED
@@ -0,0 +1,89 @@
|
|
1
|
+
//Written using Nila. Visit http://adhithyan15.github.io/nila
|
2
|
+
(function() {
|
3
|
+
var args_unleashed, default_args, mixed_args, two_or_more;
|
4
|
+
|
5
|
+
// These example were taken from The Well Grounded Rubyist by Manning
|
6
|
+
|
7
|
+
// Copyright: Manning Publications
|
8
|
+
|
9
|
+
two_or_more = function() {
|
10
|
+
var a, b, c;
|
11
|
+
a = arguments[0];
|
12
|
+
b = arguments[1];
|
13
|
+
c = [];
|
14
|
+
for (var i=2;i<arguments.length;i++) {
|
15
|
+
c.push(arguments[i]);
|
16
|
+
}
|
17
|
+
console.log("I require two or more arguments!");
|
18
|
+
console.log("And sure enough, I got: ");
|
19
|
+
console.log(a);
|
20
|
+
console.log(b);
|
21
|
+
return console.log(c);
|
22
|
+
};
|
23
|
+
|
24
|
+
two_or_more(1,2,3,4,5);
|
25
|
+
|
26
|
+
default_args = function(a,b,c) {
|
27
|
+
if (c == null) {
|
28
|
+
c = 1;
|
29
|
+
}
|
30
|
+
console.log("Values of variables: ");
|
31
|
+
console.log(a);
|
32
|
+
console.log(b);
|
33
|
+
return console.log(c);
|
34
|
+
};
|
35
|
+
|
36
|
+
default_args(3,2);
|
37
|
+
|
38
|
+
default_args(4,5,6);
|
39
|
+
|
40
|
+
mixed_args = function() {
|
41
|
+
var a, b, c, d, e, f;
|
42
|
+
a = arguments[0];
|
43
|
+
b = arguments[1];
|
44
|
+
c = arguments[2];
|
45
|
+
e = arguments[arguments.length-2];
|
46
|
+
f = arguments[arguments.length-1];
|
47
|
+
d = [];
|
48
|
+
for (var i=3;i < arguments.length-2;i++) {
|
49
|
+
d.push(arguments[i]);
|
50
|
+
}
|
51
|
+
console.log("Arguments:");
|
52
|
+
console.log(a);
|
53
|
+
console.log(b);
|
54
|
+
console.log(c);
|
55
|
+
console.log(d);
|
56
|
+
console.log(e);
|
57
|
+
return console.log(f);
|
58
|
+
};
|
59
|
+
|
60
|
+
mixed_args(0,1,2,3,4,5,6,7,8);
|
61
|
+
|
62
|
+
args_unleashed = function() {
|
63
|
+
var a, b, c, d, e;
|
64
|
+
a = arguments[0];
|
65
|
+
b = arguments[1];
|
66
|
+
d = arguments[arguments.length-2];
|
67
|
+
e = arguments[arguments.length-1];
|
68
|
+
c = [];
|
69
|
+
for (var i=2;i < arguments.length-2;i++) {
|
70
|
+
c.push(arguments[i]);
|
71
|
+
}
|
72
|
+
if (b == null) {
|
73
|
+
b=1;
|
74
|
+
}
|
75
|
+
console.log("Arguments:");
|
76
|
+
console.log(a);
|
77
|
+
console.log(b);
|
78
|
+
console.log(c);
|
79
|
+
console.log(d);
|
80
|
+
return console.log(e);
|
81
|
+
};
|
82
|
+
|
83
|
+
args_unleashed(1,2,3,4,5);
|
84
|
+
|
85
|
+
args_unleashed(1,2,3,4);
|
86
|
+
|
87
|
+
args_unleashed(1,2,3,4,5,6,7,8);
|
88
|
+
|
89
|
+
}).call(this);
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# These example were taken from The Well Grounded Rubyist by Manning
|
2
|
+
# Copyright: Manning Publications
|
3
|
+
|
4
|
+
def two_or_more(a,b,*c)
|
5
|
+
puts "I require two or more arguments!"
|
6
|
+
puts "And sure enough, I got: "
|
7
|
+
puts a, b, c
|
8
|
+
end
|
9
|
+
|
10
|
+
two_or_more 1,2,3,4,5
|
11
|
+
|
12
|
+
def default_args(a,b,c = 1)
|
13
|
+
puts "Values of variables: ",a,b,c
|
14
|
+
end
|
15
|
+
|
16
|
+
default_args 3,2
|
17
|
+
|
18
|
+
default_args 4,5,6
|
19
|
+
|
20
|
+
def mixed_args(a,b,c,*d,e,f)
|
21
|
+
puts "Arguments:"
|
22
|
+
puts a,b,c,d,e,f
|
23
|
+
end
|
24
|
+
|
25
|
+
mixed_args(0,1,2,3,4,5,6,7,8)
|
26
|
+
|
27
|
+
def args_unleashed(a,b=1,*c,d,e)
|
28
|
+
puts "Arguments:"
|
29
|
+
p a,b,c,d,e
|
30
|
+
end
|
31
|
+
|
32
|
+
args_unleashed(1,2,3,4,5)
|
33
|
+
|
34
|
+
args_unleashed(1,2,3,4)
|
35
|
+
|
36
|
+
args_unleashed(1,2,3,4,5,6,7,8)
|
data/src/nilac.rb
CHANGED
@@ -6,6 +6,20 @@
|
|
6
6
|
require 'slop'
|
7
7
|
require 'fileutils'
|
8
8
|
|
9
|
+
# The following are error classes used by Nilac to give you detailed error information
|
10
|
+
|
11
|
+
class ParseError < RuntimeError
|
12
|
+
|
13
|
+
def initialize(message)
|
14
|
+
|
15
|
+
puts "ParseError: " + message
|
16
|
+
|
17
|
+
abort
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
9
23
|
|
10
24
|
def compile(input_file_path, *output_file_name)
|
11
25
|
|
@@ -782,6 +796,39 @@ def compile(input_file_path, *output_file_name)
|
|
782
796
|
# puts "Filling the #{container} with #{liquid}"
|
783
797
|
# end
|
784
798
|
|
799
|
+
def errorFree(function_params)
|
800
|
+
|
801
|
+
# This method checks for use cases in complex arguments where a default argument is used
|
802
|
+
# after an optional argument. This will result in erroneous output. So this method will
|
803
|
+
# stop it from happening.
|
804
|
+
|
805
|
+
# Example:
|
806
|
+
# def method_name(a,b,*c,d = 1,c,e)
|
807
|
+
|
808
|
+
optional_param = function_params.reject {|element| !replace_strings(element).include?("*")}[0]
|
809
|
+
|
810
|
+
unless optional_param.nil?
|
811
|
+
|
812
|
+
after_splat = function_params[function_params.index(optional_param)+1..-1]
|
813
|
+
|
814
|
+
if after_splat.reject {|element| !element.include?("=")}.empty?
|
815
|
+
|
816
|
+
true
|
817
|
+
|
818
|
+
else
|
819
|
+
|
820
|
+
ParseError.new("You cannot have default argument after an optional argument! Change the following usage!\n#{function_params.join(",")}")
|
821
|
+
|
822
|
+
end
|
823
|
+
|
824
|
+
else
|
825
|
+
|
826
|
+
true
|
827
|
+
|
828
|
+
end
|
829
|
+
|
830
|
+
end
|
831
|
+
|
785
832
|
def parse_default_values(input_function_definition)
|
786
833
|
|
787
834
|
split1, split2 = input_function_definition.split("(")
|
@@ -790,23 +837,27 @@ def compile(input_file_path, *output_file_name)
|
|
790
837
|
|
791
838
|
function_parameters = split2.split(",")
|
792
839
|
|
793
|
-
|
840
|
+
if errorFree(function_parameters)
|
794
841
|
|
795
|
-
|
842
|
+
default_value_parameters = function_parameters.reject { |element| !element.include? "=" }
|
796
843
|
|
797
|
-
|
844
|
+
replacement_parameters = []
|
798
845
|
|
799
|
-
|
846
|
+
replacement_string = ""
|
800
847
|
|
801
|
-
|
848
|
+
default_value_parameters.each do |paramvalue|
|
802
849
|
|
803
|
-
|
850
|
+
param, value = paramvalue.split("=")
|
804
851
|
|
805
|
-
|
852
|
+
replacement_parameters << param.lstrip.rstrip
|
806
853
|
|
807
|
-
|
854
|
+
replacement_string = replacement_string + "\n" + "if (#{param.lstrip.rstrip} equequ null) {\n #{paramvalue.lstrip.rstrip}\n}\n" +"\n"
|
808
855
|
|
809
|
-
|
856
|
+
end
|
857
|
+
|
858
|
+
return replacement_string, default_value_parameters, replacement_parameters
|
859
|
+
|
860
|
+
end
|
810
861
|
|
811
862
|
end
|
812
863
|
|
@@ -856,7 +907,7 @@ def compile(input_file_path, *output_file_name)
|
|
856
907
|
|
857
908
|
input_file_contents[current_line_index] = modified_line
|
858
909
|
|
859
|
-
input_file_contents
|
910
|
+
input_file_contents.insert(current_line_index+1,replacement_string)
|
860
911
|
|
861
912
|
end
|
862
913
|
|
@@ -1838,7 +1889,7 @@ def compile(input_file_path, *output_file_name)
|
|
1838
1889
|
|
1839
1890
|
else
|
1840
1891
|
|
1841
|
-
return variables.uniq
|
1892
|
+
return variables.uniq.sort
|
1842
1893
|
|
1843
1894
|
end
|
1844
1895
|
|
@@ -1880,6 +1931,8 @@ def compile(input_file_path, *output_file_name)
|
|
1880
1931
|
|
1881
1932
|
rejected_array = reversed_input_array.reject { |content| content.lstrip.eql?("") }
|
1882
1933
|
|
1934
|
+
rejected_array = rejected_array.reject {|content| content.strip.eql?("")}
|
1935
|
+
|
1883
1936
|
rejected_array = rejected_array[1..-1]
|
1884
1937
|
|
1885
1938
|
if !rejected_array[0].strip.eql?("}")
|
@@ -2104,6 +2157,16 @@ def compile(input_file_path, *output_file_name)
|
|
2104
2157
|
|
2105
2158
|
modified_input_array = compile_parallel_assignment(modified_input_array, temporary_nila_file)
|
2106
2159
|
|
2160
|
+
modified_input_array = compile_multiple_ruby_func_calls(modified_input_array)
|
2161
|
+
|
2162
|
+
modified_input_array = add_auto_return_statement(modified_input_array)
|
2163
|
+
|
2164
|
+
modified_input_array = compile_multiple_return(modified_input_array)
|
2165
|
+
|
2166
|
+
modified_input_array = coffee_type_function(modified_input_array)
|
2167
|
+
|
2168
|
+
modified_input_array = compile_splats(modified_input_array)
|
2169
|
+
|
2107
2170
|
variables = lexical_scoped_variables(modified_input_array)
|
2108
2171
|
|
2109
2172
|
if !variables.empty?
|
@@ -2116,12 +2179,6 @@ def compile(input_file_path, *output_file_name)
|
|
2116
2179
|
|
2117
2180
|
modified_input_array = remove_question_marks(modified_input_array, variables, temporary_nila_file)
|
2118
2181
|
|
2119
|
-
modified_input_array = add_auto_return_statement(modified_input_array)
|
2120
|
-
|
2121
|
-
modified_input_array = compile_multiple_return(modified_input_array)
|
2122
|
-
|
2123
|
-
modified_input_array = coffee_type_function(modified_input_array)
|
2124
|
-
|
2125
2182
|
return modified_input_array
|
2126
2183
|
|
2127
2184
|
end
|
@@ -2146,6 +2203,237 @@ def compile(input_file_path, *output_file_name)
|
|
2146
2203
|
|
2147
2204
|
end
|
2148
2205
|
|
2206
|
+
def compile_splats(input_function_block)
|
2207
|
+
|
2208
|
+
def strToArray(input_string)
|
2209
|
+
|
2210
|
+
file_id = File.new('hello.nila','w')
|
2211
|
+
|
2212
|
+
file_id.write(input_string)
|
2213
|
+
|
2214
|
+
file_id.close()
|
2215
|
+
|
2216
|
+
line_by_line_contents = read_file_line_by_line('hello.nila')
|
2217
|
+
|
2218
|
+
File.delete(file_id)
|
2219
|
+
|
2220
|
+
return line_by_line_contents
|
2221
|
+
|
2222
|
+
end
|
2223
|
+
|
2224
|
+
def errorFree(function_params,optional_param)
|
2225
|
+
|
2226
|
+
# This method checks for use cases in complex arguments where a default argument is used
|
2227
|
+
# after an optional argument. This will result in erroneous output. So this method will
|
2228
|
+
# stop it from happening.
|
2229
|
+
|
2230
|
+
# Example:
|
2231
|
+
# def method_name(a,b,*c,d = 1,c,e)
|
2232
|
+
|
2233
|
+
after_splat = function_params[function_params.index(optional_param)+1..-1]
|
2234
|
+
|
2235
|
+
if after_splat.reject {|element| !element.include?("=")}.empty?
|
2236
|
+
|
2237
|
+
true
|
2238
|
+
|
2239
|
+
else
|
2240
|
+
|
2241
|
+
raise "You cannot have a default argument after an optional argument!"
|
2242
|
+
|
2243
|
+
false
|
2244
|
+
|
2245
|
+
end
|
2246
|
+
|
2247
|
+
end
|
2248
|
+
|
2249
|
+
function_params = input_function_block[0].split("function(")[1].split(")")[0].split(",")
|
2250
|
+
|
2251
|
+
unless function_params.reject{|element| !replace_strings(element).include?("*")}.empty?
|
2252
|
+
|
2253
|
+
mod_function_params = function_params.reject {|element| replace_strings(element).include?("*")}
|
2254
|
+
|
2255
|
+
opt_index = 0
|
2256
|
+
|
2257
|
+
# If there are multiple optional params declared by mistake, only the first optional param is used.
|
2258
|
+
|
2259
|
+
optional_param = function_params.reject {|element| !replace_strings(element).include?("*")}[0]
|
2260
|
+
|
2261
|
+
if function_params.index(optional_param).eql?(function_params.length-1)
|
2262
|
+
|
2263
|
+
mod_function_params.each_with_index do |param,index|
|
2264
|
+
|
2265
|
+
input_function_block.insert(index+1,"#{param} = arguments[#{index}]\n\n")
|
2266
|
+
|
2267
|
+
opt_index = index + 1
|
2268
|
+
|
2269
|
+
end
|
2270
|
+
|
2271
|
+
replacement_string = "#{optional_param.gsub("*","")} = []\n\n"
|
2272
|
+
|
2273
|
+
replacement_string += "for (var i=#{opt_index};i<arguments.length;i++) {\n #{optional_param.gsub("*","")}.push(arguments[i]); \n}\n\n"
|
2274
|
+
|
2275
|
+
input_function_block.insert(opt_index+1,replacement_string)
|
2276
|
+
|
2277
|
+
input_function_block[0] = input_function_block[0].sub(function_params.join(","),"")
|
2278
|
+
|
2279
|
+
else
|
2280
|
+
|
2281
|
+
before_splat = function_params[0...function_params.index(optional_param)]
|
2282
|
+
|
2283
|
+
after_splat = function_params[function_params.index(optional_param)+1..-1]
|
2284
|
+
|
2285
|
+
cont_index = 0
|
2286
|
+
|
2287
|
+
if errorFree(function_params,optional_param)
|
2288
|
+
|
2289
|
+
before_splat.each_with_index do |param,index|
|
2290
|
+
|
2291
|
+
input_function_block.insert(index+1,"#{param} = arguments[#{index}]\n\n")
|
2292
|
+
|
2293
|
+
cont_index = index + 1
|
2294
|
+
|
2295
|
+
end
|
2296
|
+
|
2297
|
+
after_splat.each_with_index do |param,index|
|
2298
|
+
|
2299
|
+
input_function_block.insert(cont_index+1,"#{param} = arguments[arguments.length-#{after_splat.length - index}]\n\n")
|
2300
|
+
|
2301
|
+
cont_index = cont_index + 1
|
2302
|
+
|
2303
|
+
end
|
2304
|
+
|
2305
|
+
replacement_string = "#{optional_param.gsub("*","")} = []\n\n"
|
2306
|
+
|
2307
|
+
replacement_string += "for (var i=#{function_params.index(optional_param)};i < arguments.length-#{after_splat.length};i++) {\n #{optional_param.gsub("*","")}.push(arguments[i]); \n}\n\n"
|
2308
|
+
|
2309
|
+
input_function_block.insert(cont_index+1,replacement_string)
|
2310
|
+
|
2311
|
+
input_function_block[0] = input_function_block[0].sub(function_params.join(","),"")
|
2312
|
+
|
2313
|
+
end
|
2314
|
+
|
2315
|
+
end
|
2316
|
+
|
2317
|
+
end
|
2318
|
+
|
2319
|
+
return strToArray(input_function_block.join)
|
2320
|
+
|
2321
|
+
end
|
2322
|
+
|
2323
|
+
def compile_multiple_ruby_func_calls(input_file_contents)
|
2324
|
+
|
2325
|
+
def strToArray(input_string)
|
2326
|
+
|
2327
|
+
file_id = File.new('hello.nila','w')
|
2328
|
+
|
2329
|
+
file_id.write(input_string)
|
2330
|
+
|
2331
|
+
file_id.close()
|
2332
|
+
|
2333
|
+
line_by_line_contents = read_file_line_by_line('hello.nila')
|
2334
|
+
|
2335
|
+
File.delete(file_id)
|
2336
|
+
|
2337
|
+
return line_by_line_contents
|
2338
|
+
|
2339
|
+
end
|
2340
|
+
|
2341
|
+
def replace_strings(input_string)
|
2342
|
+
|
2343
|
+
string_counter = 0
|
2344
|
+
|
2345
|
+
if input_string.count("\"") % 2 == 0
|
2346
|
+
|
2347
|
+
while input_string.include?("\"")
|
2348
|
+
|
2349
|
+
string_extract = input_string[input_string.index("\"")..input_string.index("\"",input_string.index("\"")+1)]
|
2350
|
+
|
2351
|
+
input_string = input_string.sub(string_extract,"--repstring#{string_counter}")
|
2352
|
+
|
2353
|
+
string_counter += 1
|
2354
|
+
|
2355
|
+
end
|
2356
|
+
|
2357
|
+
end
|
2358
|
+
|
2359
|
+
if input_string.count("'") % 2 == 0
|
2360
|
+
|
2361
|
+
while input_string.include?("'")
|
2362
|
+
|
2363
|
+
string_extract = input_string[input_string.index("'")..input_string.index("'",input_string.index("'")+1)]
|
2364
|
+
|
2365
|
+
input_string = input_string.sub(string_extract,"--repstring#{string_counter}")
|
2366
|
+
|
2367
|
+
string_counter += 1
|
2368
|
+
|
2369
|
+
end
|
2370
|
+
|
2371
|
+
end
|
2372
|
+
|
2373
|
+
input_string = input_string.gsub(/\((\w{0,},)*\w{0,}\)/,"--$k$")
|
2374
|
+
|
2375
|
+
return input_string
|
2376
|
+
|
2377
|
+
end
|
2378
|
+
|
2379
|
+
function_calls = []
|
2380
|
+
|
2381
|
+
replacement_calls = []
|
2382
|
+
|
2383
|
+
function_map = %w{puts p print}
|
2384
|
+
|
2385
|
+
javascript_regexp = /(if |for |while |\(function\(|= function\(|((=|:)\s+\{))/
|
2386
|
+
|
2387
|
+
stringified_input = input_file_contents.collect {|element| replace_strings(element)}
|
2388
|
+
|
2389
|
+
function_map.each do |func|
|
2390
|
+
|
2391
|
+
func_calls = input_file_contents.reject {|line| !(line.include?(func+"(") or line.include?(func+" ") and line.index(javascript_regexp) == nil)}
|
2392
|
+
|
2393
|
+
modified_func_calls = func_calls.collect {|element| replace_strings(element)}
|
2394
|
+
|
2395
|
+
modified_func_calls = modified_func_calls.reject {|element| !element.include?(",")}
|
2396
|
+
|
2397
|
+
call_collector = []
|
2398
|
+
|
2399
|
+
modified_func_calls.each_with_index do |ele|
|
2400
|
+
|
2401
|
+
call_collector << input_file_contents[stringified_input.index(ele)]
|
2402
|
+
|
2403
|
+
end
|
2404
|
+
|
2405
|
+
function_calls << modified_func_calls
|
2406
|
+
|
2407
|
+
rep_calls = []
|
2408
|
+
|
2409
|
+
call_collector.each do |fcall|
|
2410
|
+
|
2411
|
+
multiple_call = fcall.split(func)[1].split(",")
|
2412
|
+
|
2413
|
+
multiple_call = multiple_call.collect {|element| "\n#{func} " + element.strip + "\n\n"}
|
2414
|
+
|
2415
|
+
rep_calls << multiple_call.join
|
2416
|
+
|
2417
|
+
end
|
2418
|
+
|
2419
|
+
replacement_calls << rep_calls
|
2420
|
+
|
2421
|
+
end
|
2422
|
+
|
2423
|
+
replacement_calls = replacement_calls.flatten
|
2424
|
+
|
2425
|
+
function_calls = function_calls.flatten
|
2426
|
+
|
2427
|
+
function_calls.each_with_index do |fcall,index|
|
2428
|
+
|
2429
|
+
input_file_contents[stringified_input.index(fcall)] = replacement_calls[index]
|
2430
|
+
|
2431
|
+
end
|
2432
|
+
|
2433
|
+
return strToArray(input_file_contents.join)
|
2434
|
+
|
2435
|
+
end
|
2436
|
+
|
2149
2437
|
joined_file_contents = input_file_contents.join
|
2150
2438
|
|
2151
2439
|
unless named_code_blocks.empty?
|
@@ -2188,7 +2476,7 @@ def compile(input_file_path, *output_file_name)
|
|
2188
2476
|
|
2189
2477
|
file_id.close()
|
2190
2478
|
|
2191
|
-
line_by_line_contents = read_file_line_by_line(temporary_nila_file)
|
2479
|
+
line_by_line_contents = compile_multiple_ruby_func_calls(read_file_line_by_line(temporary_nila_file))
|
2192
2480
|
|
2193
2481
|
return line_by_line_contents, function_names
|
2194
2482
|
|
@@ -2444,6 +2732,8 @@ def compile(input_file_path, *output_file_name)
|
|
2444
2732
|
|
2445
2733
|
modified_string = modified_string.sub(function+" ", function+"(")
|
2446
2734
|
|
2735
|
+
modified_string = modified_string.split("#{function}(")[0] + "#{function}(" + modified_string.split("#{function}(")[1].lstrip
|
2736
|
+
|
2447
2737
|
modified_string = modified_string.sub("\n", ")\n")
|
2448
2738
|
|
2449
2739
|
joined_file_contents = joined_file_contents.sub(string, modified_string)
|
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
|
+
version: 0.0.4.3.9.2
|
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-10-
|
11
|
+
date: 2013-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: shark
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- shark/features/regular_while.feature
|
77
77
|
- shark/features/ruby_methods.feature
|
78
78
|
- shark/features/ruby_operators.feature
|
79
|
+
- shark/features/splats.feature
|
79
80
|
- shark/features/string_interpolation.feature
|
80
81
|
- shark/features/strings.feature
|
81
82
|
- shark/features/times.feature
|
@@ -102,6 +103,7 @@ files:
|
|
102
103
|
- shark/test_files/correct_return.js
|
103
104
|
- shark/test_files/correct_ruby_methods.js
|
104
105
|
- shark/test_files/correct_single_return.js
|
106
|
+
- shark/test_files/correct_splats.js
|
105
107
|
- shark/test_files/correct_string_interpolation.js
|
106
108
|
- shark/test_files/correct_string_operators.js
|
107
109
|
- shark/test_files/correct_times.js
|
@@ -126,6 +128,7 @@ files:
|
|
126
128
|
- shark/test_files/ruby_methods.nila
|
127
129
|
- shark/test_files/simple.nila
|
128
130
|
- shark/test_files/single_return.nila
|
131
|
+
- shark/test_files/splats.nila
|
129
132
|
- shark/test_files/string_interpolation.nila
|
130
133
|
- shark/test_files/string_operators.nila
|
131
134
|
- shark/test_files/times.nila
|