nilac 0.0.4.3.0 → 0.0.4.3.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c3f566c76264dbd8ac2ebae19881d66ef275f331
4
+ data.tar.gz: a06a5560e01677ced9c242d89c92c7cbf3840d05
5
+ SHA512:
6
+ metadata.gz: 0c4a4b86198e3513a73a477c726810e2fd1f579311677e6c5af00d4442a2b2ca878a2f4cf9fd51da9e1921f81b05d82e6dca2269f0e4cc2fabd64dd9b37c19df
7
+ data.tar.gz: 3382cb6fa6a92a6a905e73c36c1f358ddeb4b9d67ae06152876eb755f53624bc0fd0457453db9378bba33c95d70f7bdf810784068c07d7c9aaee300f4c549942
data/bin/nilac CHANGED
@@ -333,6 +333,8 @@ def compile(input_file_path, *output_file_name)
333
333
 
334
334
  test_string = string_extract[0..closed_curly_brace_index[0]]
335
335
 
336
+ puts test_string
337
+
336
338
  original_string = test_string.dup
337
339
 
338
340
  if test_string.include?("{")
@@ -606,6 +608,50 @@ def compile(input_file_path, *output_file_name)
606
608
 
607
609
  def compile_parallel_assignment(input_file_contents, temporary_nila_file)
608
610
 
611
+ def arrayify_right_side(input_string)
612
+
613
+ javascript_regexp = /(if |while |for |function |function\()/
614
+
615
+ if input_string.include?("=") and input_string.index(javascript_regexp) == nil and input_string.strip[0..3] != "_ref" and !input_string.split("=")[1].include?("[")
616
+
617
+ modified_input_string = input_string.dup
618
+
619
+ right_side = input_string.split("=")[1]
620
+
621
+ if right_side.include?(",")
622
+
623
+ splits = right_side.split(",")
624
+
625
+ replacement_string = []
626
+
627
+ splits.each do |str|
628
+
629
+ unless str.include?(")") and !str.include?("(")
630
+
631
+ replacement_string << str
632
+
633
+ else
634
+
635
+ replacement_string[-1] = replacement_string[-1]+ "," +str
636
+
637
+ end
638
+
639
+ end
640
+
641
+ replacement_string = " [#{replacement_string.join(",").strip}]\n"
642
+
643
+ input_string = input_string.sub(right_side,replacement_string)
644
+
645
+ end
646
+
647
+ end
648
+
649
+ return input_string
650
+
651
+ end
652
+
653
+ input_file_contents = input_file_contents.collect {|element| arrayify_right_side(element)}
654
+
609
655
  possible_variable_lines = input_file_contents.reject { |element| !element.include? "=" }
610
656
 
611
657
  possible_parallel_assignment = possible_variable_lines.reject { |element| !element.split("=")[0].include? "," }
@@ -798,8 +844,24 @@ def compile(input_file_path, *output_file_name)
798
844
 
799
845
  line_by_line_contents = read_file_line_by_line(temporary_nila_file)
800
846
 
847
+ for_loop_variables = []
848
+
849
+ for_loop_statements = line_by_line_contents.reject {|line| !line.include?("for")}
850
+
851
+ for_loop_statements.each do |statement|
852
+
853
+ varis = statement.split("for (")[1].split(";",2)[0].split(",")
854
+
855
+ for_loop_variables << varis.collect {|vari| vari.strip.split("=")[0].strip}
856
+
857
+ for_loop_variables = for_loop_variables.flatten
858
+
859
+ end
860
+
801
861
  variables += loop_variables
802
862
 
863
+ variables += for_loop_variables
864
+
803
865
  variables = variables.flatten
804
866
 
805
867
  if variables.length > 0
@@ -1900,7 +1962,8 @@ def compile(input_file_path, *output_file_name)
1900
1962
 
1901
1963
  "p" => "console.log",
1902
1964
 
1903
- "print" => "process.stdout.write"
1965
+ "print" => "process.stdout.write",
1966
+
1904
1967
  }
1905
1968
 
1906
1969
  function_map = function_map_replacements.keys
@@ -1925,6 +1988,46 @@ def compile(input_file_path, *output_file_name)
1925
1988
 
1926
1989
  end
1927
1990
 
1991
+ def compile_ruby_methods(input_file_contents)
1992
+
1993
+ # These are some interesting methods that we really miss in Javascript.
1994
+ # So we have made these methods available
1995
+
1996
+ method_map_replacement = {
1997
+
1998
+ ".split" => ".split(\" \")",
1999
+
2000
+ ".strip" => ".replace(/^\s+|\s+$/g,'')",
2001
+
2002
+ ".lstrip" => ".replace(/^\s+/g,\"\")",
2003
+
2004
+ ".rstrip" => ".replace(/\s+$/g,\"\")"
2005
+ }
2006
+
2007
+ method_map = method_map_replacement.keys
2008
+
2009
+ method_map_regex = method_map.collect {|name| name.gsub(".","\\.")}
2010
+
2011
+ method_map_regex = Regexp.new(method_map_regex.join("|"))
2012
+
2013
+ modified_file_contents = input_file_contents.clone
2014
+
2015
+ input_file_contents.each_with_index do |line, index|
2016
+
2017
+ if line.match(method_map_regex)
2018
+
2019
+ unless method_match.include?(line+"(")
2020
+
2021
+ puts line
2022
+
2023
+ end
2024
+
2025
+ end
2026
+
2027
+ end
2028
+
2029
+ end
2030
+
1928
2031
  def compile_whitespace_delimited_functions(input_file_contents, function_names, temporary_nila_file)
1929
2032
 
1930
2033
  def extract(input_string, pattern_start, pattern_end)
@@ -2713,6 +2816,322 @@ def compile(input_file_path, *output_file_name)
2713
2816
 
2714
2817
  end
2715
2818
 
2819
+ def compile_regular_for(input_file_contents, temporary_nila_file)
2820
+
2821
+ def convert_string_to_array(input_string, temporary_nila_file)
2822
+
2823
+ file_id = open(temporary_nila_file, 'w')
2824
+
2825
+ file_id.write(input_string)
2826
+
2827
+ file_id.close()
2828
+
2829
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
2830
+
2831
+ return line_by_line_contents
2832
+
2833
+ end
2834
+
2835
+ def extract_for_blocks(for_statement_indexes, input_file_contents)
2836
+
2837
+ possible_for_blocks = []
2838
+
2839
+ for_block_counter = 0
2840
+
2841
+ extracted_blocks = []
2842
+
2843
+ controlregexp = /(if |while |def |for )/
2844
+
2845
+ rejectionregexp = /( if | while )/
2846
+
2847
+ for x in 0...for_statement_indexes.length-1
2848
+
2849
+ possible_for_blocks << input_file_contents[for_statement_indexes[x]..for_statement_indexes[x+1]]
2850
+
2851
+ end
2852
+
2853
+ end_counter = 0
2854
+
2855
+ end_index = []
2856
+
2857
+ current_block = []
2858
+
2859
+ possible_for_blocks.each_with_index do |block|
2860
+
2861
+ current_block += block
2862
+
2863
+ current_block.each_with_index do |line, index|
2864
+
2865
+ if line.strip.eql? "end"
2866
+
2867
+ end_counter += 1
2868
+
2869
+ end_index << index
2870
+
2871
+ end
2872
+
2873
+ end
2874
+
2875
+ if end_counter > 0
2876
+
2877
+ until end_index.empty?
2878
+
2879
+ array_extract = current_block[0..end_index[0]].reverse
2880
+
2881
+ index_counter = 0
2882
+
2883
+ array_extract.each_with_index do |line|
2884
+
2885
+ break if (line.lstrip.index(controlregexp) != nil and line.lstrip.index(rejectionregexp).nil?)
2886
+
2887
+ index_counter += 1
2888
+
2889
+ end
2890
+
2891
+ block_extract = array_extract[0..index_counter].reverse
2892
+
2893
+ extracted_blocks << block_extract
2894
+
2895
+ block_start = current_block.index(block_extract[0])
2896
+
2897
+ block_end = current_block.index(block_extract[-1])
2898
+
2899
+ current_block[block_start..block_end] = "--forblock#{for_block_counter}"
2900
+
2901
+ for_block_counter += 1
2902
+
2903
+ end_counter = 0
2904
+
2905
+ end_index = []
2906
+
2907
+ current_block.each_with_index do |line, index|
2908
+
2909
+ if line.strip.eql? "end"
2910
+
2911
+ end_counter += 1
2912
+
2913
+ end_index << index
2914
+
2915
+ end
2916
+
2917
+ end
2918
+
2919
+ end
2920
+
2921
+ end
2922
+
2923
+ end
2924
+
2925
+ return current_block, extracted_blocks
2926
+
2927
+ end
2928
+
2929
+ def compile_for_syntax(input_block)
2930
+
2931
+ def compile_condition(input_condition, input_block)
2932
+
2933
+ variable,array_name = input_condition.split("in")
2934
+
2935
+ if array_name.strip.include?("[") and array_name.strip.include?("]")
2936
+
2937
+ replacement_array = "_ref1 = #{array_name.strip}\n\n"
2938
+
2939
+ replacement_string = "#{variable.strip} = _ref1[_i];\n\n"
2940
+
2941
+ input_block = [replacement_array] + input_block.insert(1,replacement_string)
2942
+
2943
+ input_block[1] = "for (_i = 0, _j = _ref1.length; _i < _j; _i += 1) {\n\n"
2944
+
2945
+ elsif array_name.strip.include?("..")
2946
+
2947
+ array_type = if array_name.strip.include?("...") then 0 else 1 end
2948
+
2949
+ if array_type == 0
2950
+
2951
+ num1,num2 = array_name.strip.split("...")
2952
+
2953
+ input_block[0] = "for (#{variable.strip} = #{num1}, _j = #{num2}; #{variable.strip} <= _j; #{variable.strip} += 1) {\n\n"
2954
+
2955
+ else
2956
+
2957
+ num1,num2 = array_name.strip.split("..")
2958
+
2959
+ input_block[0] = "for (#{variable.strip} = #{num1}, _j = #{num2}; #{variable.strip} < _j; #{variable.strip} += 1) {\n\n"
2960
+
2961
+ end
2962
+
2963
+ else
2964
+
2965
+ input_block[0] = "for (_i = 0, _j = #{array_name.strip}.length; _i < _j; _i += 1) {\n\n"
2966
+
2967
+ input_block = input_block.insert(1,"#{variable.strip} = #{array_name.strip}[_i];\n\n")
2968
+
2969
+ end
2970
+
2971
+ return input_block
2972
+
2973
+ end
2974
+
2975
+ modified_input_block = input_block.dup
2976
+
2977
+ strings = []
2978
+
2979
+ string_counter = 0
2980
+
2981
+ input_block.each_with_index do |line, index|
2982
+
2983
+ if line.include?("\"")
2984
+
2985
+ opening_quotes = line.index("\"")
2986
+
2987
+ string_extract = line[opening_quotes..line.index("\"", opening_quotes+1)]
2988
+
2989
+ strings << string_extract
2990
+
2991
+ modified_input_block[index] = modified_input_block[index].sub(string_extract, "--string{#{string_counter}}")
2992
+
2993
+ string_counter += 1
2994
+
2995
+ end
2996
+
2997
+ end
2998
+
2999
+ input_block = modified_input_block
3000
+
3001
+ starting_line = input_block[0]
3002
+
3003
+ starting_line = starting_line + "\n" if starting_line.lstrip == starting_line
3004
+
3005
+ junk, condition = starting_line.split("for")
3006
+
3007
+ input_block[-1] = input_block[-1].lstrip.sub("end", "}")
3008
+
3009
+ input_block = compile_condition(condition,input_block)
3010
+
3011
+ modified_input_block = input_block.dup
3012
+
3013
+ input_block.each_with_index do |line, index|
3014
+
3015
+ if line.include?("--string{")
3016
+
3017
+ junk, remains = line.split("--string{")
3018
+
3019
+ string_index, junk = remains.split("}")
3020
+
3021
+ modified_input_block[index] = modified_input_block[index].sub("--string{#{string_index.strip}}", strings[string_index.strip.to_i])
3022
+
3023
+ end
3024
+
3025
+ end
3026
+
3027
+ return modified_input_block
3028
+
3029
+ end
3030
+
3031
+ possible_for_statements = input_file_contents.reject { |element| !element.include?("for") }
3032
+
3033
+ possible_for_statements = possible_for_statements.reject {|element| element.include?("for (")}
3034
+
3035
+ if !possible_for_statements.empty?
3036
+
3037
+ for_statement_indexes = []
3038
+
3039
+ possible_for_statements.each do |statement|
3040
+
3041
+ for_statement_indexes << input_file_contents.dup.each_index.select { |index| input_file_contents[index] == statement }
3042
+
3043
+ end
3044
+
3045
+ for_statement_indexes = [0] + for_statement_indexes.flatten + [-1]
3046
+
3047
+ controlregexp = /(if |def |while )/
3048
+
3049
+ modified_input_contents, extracted_statements = extract_for_blocks(for_statement_indexes, input_file_contents.clone)
3050
+
3051
+ joined_blocks = extracted_statements.collect { |element| element.join }
3052
+
3053
+ for_statements = joined_blocks.reject { |element| element.index(controlregexp) != nil }
3054
+
3055
+ rejected_elements = joined_blocks - for_statements
3056
+
3057
+ rejected_elements_index = []
3058
+
3059
+ rejected_elements.each do |element|
3060
+
3061
+ rejected_elements_index << joined_blocks.each_index.select { |index| joined_blocks[index] == element }
3062
+
3063
+ end
3064
+
3065
+ for_blocks_index = (0...extracted_statements.length).to_a
3066
+
3067
+ rejected_elements_index = rejected_elements_index.flatten
3068
+
3069
+ for_blocks_index -= rejected_elements_index
3070
+
3071
+ modified_for_statements = for_statements.collect { |string| convert_string_to_array(string, temporary_nila_file) }
3072
+
3073
+ modified_for_statements = modified_for_statements.collect { |block| compile_for_syntax(block) }.reverse
3074
+
3075
+ for_blocks_index = for_blocks_index.collect { |element| "--forblock#{element}" }.reverse
3076
+
3077
+ rejected_elements_index = rejected_elements_index.collect { |element| "--forblock#{element}" }.reverse
3078
+
3079
+ rejected_elements = rejected_elements.reverse
3080
+
3081
+ joined_file_contents = modified_input_contents.join
3082
+
3083
+ until for_blocks_index.empty? and rejected_elements_index.empty?
3084
+
3085
+ if !for_blocks_index.empty?
3086
+
3087
+ if joined_file_contents.include?(for_blocks_index[0])
3088
+
3089
+ joined_file_contents = joined_file_contents.sub(for_blocks_index[0], modified_for_statements[0].join)
3090
+
3091
+ for_blocks_index.delete_at(0)
3092
+
3093
+ modified_for_statements.delete_at(0)
3094
+
3095
+ else
3096
+
3097
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0], rejected_elements[0].join)
3098
+
3099
+ rejected_elements_index.delete_at(0)
3100
+
3101
+ rejected_elements.delete_at(0)
3102
+
3103
+ end
3104
+
3105
+ else
3106
+
3107
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0], rejected_elements[0].join)
3108
+
3109
+ rejected_elements_index.delete_at(0)
3110
+
3111
+ rejected_elements.delete_at(0)
3112
+
3113
+ end
3114
+
3115
+ end
3116
+
3117
+ else
3118
+
3119
+ joined_file_contents = input_file_contents.join
3120
+
3121
+ end
3122
+
3123
+ file_id = open(temporary_nila_file, 'w')
3124
+
3125
+ file_id.write(joined_file_contents)
3126
+
3127
+ file_id.close()
3128
+
3129
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
3130
+
3131
+ return line_by_line_contents
3132
+
3133
+ end
3134
+
2716
3135
  def compile_loop_keyword(input_file_contents,temporary_nila_file)
2717
3136
 
2718
3137
  def convert_string_to_array(input_string, temporary_nila_file)
@@ -3051,12 +3470,16 @@ def compile(input_file_path, *output_file_name)
3051
3470
 
3052
3471
  file_contents = compile_ternary_if(input_file_contents)
3053
3472
 
3473
+ puts file_contents
3474
+
3054
3475
  file_contents, rejected_lines = ignore_statement_modifiers(file_contents)
3055
3476
 
3056
3477
  file_contents = replace_unless_until(file_contents)
3057
3478
 
3058
3479
  file_contents = compile_regular_if(file_contents, temporary_nila_file)
3059
3480
 
3481
+ file_contents = compile_regular_for(file_contents, temporary_nila_file)
3482
+
3060
3483
  file_contents = compile_regular_while(file_contents, temporary_nila_file)
3061
3484
 
3062
3485
  file_contents = compile_loop_keyword(file_contents,temporary_nila_file)
@@ -3083,7 +3506,7 @@ def compile(input_file_path, *output_file_name)
3083
3506
 
3084
3507
  end
3085
3508
 
3086
- def extract_variable_names(input_file_contents, temporary_nila_file)
3509
+ def extract_variable_names(input_file_contents)
3087
3510
 
3088
3511
  variables = []
3089
3512
 
@@ -3169,9 +3592,9 @@ def compile(input_file_path, *output_file_name)
3169
3592
 
3170
3593
  file_extract = input_file_contents[0..index_counter]
3171
3594
 
3172
- file_variables = extract_variable_names(file_extract,temporary_nila_file)
3595
+ file_variables = extract_variable_names(file_extract)
3173
3596
 
3174
- block_variables = extract_variable_names(loop_extract,temporary_nila_file)
3597
+ block_variables = extract_variable_names(loop_extract)
3175
3598
 
3176
3599
  var_need_of_declaration = file_variables-block_variables-["_i","_j"]
3177
3600
 
@@ -3987,10 +4410,6 @@ def compile(input_file_path, *output_file_name)
3987
4410
 
3988
4411
  file_contents = compile_conditional_structures(file_contents, temp_file)
3989
4412
 
3990
- file_contents = compile_arrays(file_contents, temp_file)
3991
-
3992
- file_contents = compile_strings(file_contents)
3993
-
3994
4413
  file_contents = compile_integers(file_contents)
3995
4414
 
3996
4415
  file_contents = compile_default_values(file_contents, temp_file)
@@ -4001,12 +4420,18 @@ def compile(input_file_path, *output_file_name)
4001
4420
 
4002
4421
  file_contents = compile_parallel_assignment(file_contents, temp_file)
4003
4422
 
4423
+ file_contents = compile_arrays(file_contents, temp_file)
4424
+
4425
+ file_contents = compile_strings(file_contents)
4426
+
4004
4427
  file_contents, function_names = compile_named_functions(file_contents, named_functions, nested_functions, temp_file)
4005
4428
 
4006
4429
  list_of_variables, file_contents = get_variables(file_contents, temp_file,loop_vars)
4007
4430
 
4008
4431
  file_contents, ruby_functions = compile_custom_function_map(file_contents)
4009
4432
 
4433
+ #compile_ruby_methods(file_contents)
4434
+
4010
4435
  function_names << ruby_functions
4011
4436
 
4012
4437
  list_of_variables += loop_vars
@@ -4089,7 +4514,7 @@ def find_file_path(input_path, file_extension)
4089
4514
 
4090
4515
  end
4091
4516
 
4092
- nilac_version = "0.0.4.3.0"
4517
+ nilac_version = "0.0.4.3.3"
4093
4518
 
4094
4519
  opts = Slop.parse do
4095
4520
  on :c, :compile=, 'Compile Nila File', as:Array, delimiter:":"
data/lib/nilac/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Nilac
2
- VERSION = "0.0.4.3.0"
2
+ VERSION = "0.0.4.3.3"
3
3
  end
@@ -0,0 +1,11 @@
1
+ Feature: Compiling for loops in a nila program
2
+ Scenario: Input with several kinds of for loops
3
+ Given the input file "regular_for.nila"
4
+ When the ~compiler is run
5
+ The output file must be "regular_for.js"
6
+ The output file must equal "correct_for.js"
7
+
8
+ Configurations:
9
+
10
+ ~compiler => src/nilac.rb
11
+ :v $cliusage => ruby :v --compile $file
@@ -0,0 +1,27 @@
1
+ //Written using Nila. Visit http://adhithyan15.github.io/nila
2
+ (function() {
3
+ var _i, _j, _ref1, fruit, fruits, x;
4
+
5
+ _ref1 = [1,2,3,4];
6
+
7
+ for (_i = 0, _j = _ref1.length; _i < _j; _i += 1) {
8
+ x = _ref1[_i];
9
+ console.log(x);
10
+ }
11
+
12
+ for (x = 0, _j = 5; x <= _j; x += 1) {
13
+ console.log(x);
14
+ }
15
+
16
+ for (x = 0, _j = 5; x < _j; x += 1) {
17
+ console.log(x);
18
+ }
19
+
20
+ fruits = ['Apple','Banana', 'Mango'];
21
+
22
+ for (_i = 0, _j = fruits.length; _i < _j; _i += 1) {
23
+ fruit = fruits[_i];
24
+ console.log(fruit);
25
+ }
26
+
27
+ }).call(this);
@@ -1,6 +1,6 @@
1
1
  //Written using Nila. Visit http://adhithyan15.github.io/nila
2
2
  (function() {
3
- var _ref1, first_name, last_name, name_split, parse_name;
3
+ var _ref1, count, current_val, first_name, last_name, name_split, name_split1, name_split2, next_val, parse_name;
4
4
 
5
5
  // This file demonstrates multiple variable initialization
6
6
 
@@ -18,4 +18,18 @@
18
18
 
19
19
  console.log(first_name + " " + last_name);
20
20
 
21
+ _ref1 = [0, 1, 1];
22
+
23
+ current_val = _ref1[0];
24
+
25
+ next_val = _ref1[1];
26
+
27
+ count = _ref1[2];
28
+
29
+ _ref1 = ["Adhithya Rajasekaran".split((" "),1),"Adhithya Rajasekaran".split(" ",2)];
30
+
31
+ name_split1 = _ref1[0];
32
+
33
+ name_split2 = _ref1[1];
34
+
21
35
  }).call(this);
@@ -11,3 +11,7 @@ end
11
11
  first_name,last_name = parse_name("Adhithya Rajasekaran")
12
12
 
13
13
  puts first_name + " " + last_name
14
+
15
+ current_val, next_val, count = 0, 1, 1
16
+
17
+ name_split1,name_split2 = "Adhithya Rajasekaran".split((" "),1),"Adhithya Rajasekaran".split(" ",2)
@@ -0,0 +1,17 @@
1
+ for x in [1,2,3,4]
2
+ puts x
3
+ end
4
+
5
+ for x in 0...5
6
+ puts x
7
+ end
8
+
9
+ for x in 0..5
10
+ puts x
11
+ end
12
+
13
+ fruits = ['Apple','Banana', 'Mango']
14
+
15
+ for fruit in fruits
16
+ puts fruit
17
+ end
@@ -10,4 +10,4 @@ a = 5
10
10
 
11
11
  puts "Twice the number is #{2*n}"
12
12
 
13
- end
13
+ end
data/src/nilac.rb CHANGED
@@ -331,6 +331,8 @@ def compile(input_file_path, *output_file_name)
331
331
 
332
332
  test_string = string_extract[0..closed_curly_brace_index[0]]
333
333
 
334
+ puts test_string
335
+
334
336
  original_string = test_string.dup
335
337
 
336
338
  if test_string.include?("{")
@@ -604,6 +606,50 @@ def compile(input_file_path, *output_file_name)
604
606
 
605
607
  def compile_parallel_assignment(input_file_contents, temporary_nila_file)
606
608
 
609
+ def arrayify_right_side(input_string)
610
+
611
+ javascript_regexp = /(if |while |for |function |function\()/
612
+
613
+ if input_string.include?("=") and input_string.index(javascript_regexp) == nil and input_string.strip[0..3] != "_ref" and !input_string.split("=")[1].include?("[")
614
+
615
+ modified_input_string = input_string.dup
616
+
617
+ right_side = input_string.split("=")[1]
618
+
619
+ if right_side.include?(",")
620
+
621
+ splits = right_side.split(",")
622
+
623
+ replacement_string = []
624
+
625
+ splits.each do |str|
626
+
627
+ unless str.include?(")") and !str.include?("(")
628
+
629
+ replacement_string << str
630
+
631
+ else
632
+
633
+ replacement_string[-1] = replacement_string[-1]+ "," +str
634
+
635
+ end
636
+
637
+ end
638
+
639
+ replacement_string = " [#{replacement_string.join(",").strip}]\n"
640
+
641
+ input_string = input_string.sub(right_side,replacement_string)
642
+
643
+ end
644
+
645
+ end
646
+
647
+ return input_string
648
+
649
+ end
650
+
651
+ input_file_contents = input_file_contents.collect {|element| arrayify_right_side(element)}
652
+
607
653
  possible_variable_lines = input_file_contents.reject { |element| !element.include? "=" }
608
654
 
609
655
  possible_parallel_assignment = possible_variable_lines.reject { |element| !element.split("=")[0].include? "," }
@@ -796,8 +842,24 @@ def compile(input_file_path, *output_file_name)
796
842
 
797
843
  line_by_line_contents = read_file_line_by_line(temporary_nila_file)
798
844
 
845
+ for_loop_variables = []
846
+
847
+ for_loop_statements = line_by_line_contents.reject {|line| !line.include?("for")}
848
+
849
+ for_loop_statements.each do |statement|
850
+
851
+ varis = statement.split("for (")[1].split(";",2)[0].split(",")
852
+
853
+ for_loop_variables << varis.collect {|vari| vari.strip.split("=")[0].strip}
854
+
855
+ for_loop_variables = for_loop_variables.flatten
856
+
857
+ end
858
+
799
859
  variables += loop_variables
800
860
 
861
+ variables += for_loop_variables
862
+
801
863
  variables = variables.flatten
802
864
 
803
865
  if variables.length > 0
@@ -1898,7 +1960,8 @@ def compile(input_file_path, *output_file_name)
1898
1960
 
1899
1961
  "p" => "console.log",
1900
1962
 
1901
- "print" => "process.stdout.write"
1963
+ "print" => "process.stdout.write",
1964
+
1902
1965
  }
1903
1966
 
1904
1967
  function_map = function_map_replacements.keys
@@ -1923,6 +1986,46 @@ def compile(input_file_path, *output_file_name)
1923
1986
 
1924
1987
  end
1925
1988
 
1989
+ def compile_ruby_methods(input_file_contents)
1990
+
1991
+ # These are some interesting methods that we really miss in Javascript.
1992
+ # So we have made these methods available
1993
+
1994
+ method_map_replacement = {
1995
+
1996
+ ".split" => ".split(\" \")",
1997
+
1998
+ ".strip" => ".replace(/^\s+|\s+$/g,'')",
1999
+
2000
+ ".lstrip" => ".replace(/^\s+/g,\"\")",
2001
+
2002
+ ".rstrip" => ".replace(/\s+$/g,\"\")"
2003
+ }
2004
+
2005
+ method_map = method_map_replacement.keys
2006
+
2007
+ method_map_regex = method_map.collect {|name| name.gsub(".","\\.")}
2008
+
2009
+ method_map_regex = Regexp.new(method_map_regex.join("|"))
2010
+
2011
+ modified_file_contents = input_file_contents.clone
2012
+
2013
+ input_file_contents.each_with_index do |line, index|
2014
+
2015
+ if line.match(method_map_regex)
2016
+
2017
+ unless method_match.include?(line+"(")
2018
+
2019
+ puts line
2020
+
2021
+ end
2022
+
2023
+ end
2024
+
2025
+ end
2026
+
2027
+ end
2028
+
1926
2029
  def compile_whitespace_delimited_functions(input_file_contents, function_names, temporary_nila_file)
1927
2030
 
1928
2031
  def extract(input_string, pattern_start, pattern_end)
@@ -2711,6 +2814,322 @@ def compile(input_file_path, *output_file_name)
2711
2814
 
2712
2815
  end
2713
2816
 
2817
+ def compile_regular_for(input_file_contents, temporary_nila_file)
2818
+
2819
+ def convert_string_to_array(input_string, temporary_nila_file)
2820
+
2821
+ file_id = open(temporary_nila_file, 'w')
2822
+
2823
+ file_id.write(input_string)
2824
+
2825
+ file_id.close()
2826
+
2827
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
2828
+
2829
+ return line_by_line_contents
2830
+
2831
+ end
2832
+
2833
+ def extract_for_blocks(for_statement_indexes, input_file_contents)
2834
+
2835
+ possible_for_blocks = []
2836
+
2837
+ for_block_counter = 0
2838
+
2839
+ extracted_blocks = []
2840
+
2841
+ controlregexp = /(if |while |def |for )/
2842
+
2843
+ rejectionregexp = /( if | while )/
2844
+
2845
+ for x in 0...for_statement_indexes.length-1
2846
+
2847
+ possible_for_blocks << input_file_contents[for_statement_indexes[x]..for_statement_indexes[x+1]]
2848
+
2849
+ end
2850
+
2851
+ end_counter = 0
2852
+
2853
+ end_index = []
2854
+
2855
+ current_block = []
2856
+
2857
+ possible_for_blocks.each_with_index do |block|
2858
+
2859
+ current_block += block
2860
+
2861
+ current_block.each_with_index do |line, index|
2862
+
2863
+ if line.strip.eql? "end"
2864
+
2865
+ end_counter += 1
2866
+
2867
+ end_index << index
2868
+
2869
+ end
2870
+
2871
+ end
2872
+
2873
+ if end_counter > 0
2874
+
2875
+ until end_index.empty?
2876
+
2877
+ array_extract = current_block[0..end_index[0]].reverse
2878
+
2879
+ index_counter = 0
2880
+
2881
+ array_extract.each_with_index do |line|
2882
+
2883
+ break if (line.lstrip.index(controlregexp) != nil and line.lstrip.index(rejectionregexp).nil?)
2884
+
2885
+ index_counter += 1
2886
+
2887
+ end
2888
+
2889
+ block_extract = array_extract[0..index_counter].reverse
2890
+
2891
+ extracted_blocks << block_extract
2892
+
2893
+ block_start = current_block.index(block_extract[0])
2894
+
2895
+ block_end = current_block.index(block_extract[-1])
2896
+
2897
+ current_block[block_start..block_end] = "--forblock#{for_block_counter}"
2898
+
2899
+ for_block_counter += 1
2900
+
2901
+ end_counter = 0
2902
+
2903
+ end_index = []
2904
+
2905
+ current_block.each_with_index do |line, index|
2906
+
2907
+ if line.strip.eql? "end"
2908
+
2909
+ end_counter += 1
2910
+
2911
+ end_index << index
2912
+
2913
+ end
2914
+
2915
+ end
2916
+
2917
+ end
2918
+
2919
+ end
2920
+
2921
+ end
2922
+
2923
+ return current_block, extracted_blocks
2924
+
2925
+ end
2926
+
2927
+ def compile_for_syntax(input_block)
2928
+
2929
+ def compile_condition(input_condition, input_block)
2930
+
2931
+ variable,array_name = input_condition.split("in")
2932
+
2933
+ if array_name.strip.include?("[") and array_name.strip.include?("]")
2934
+
2935
+ replacement_array = "_ref1 = #{array_name.strip}\n\n"
2936
+
2937
+ replacement_string = "#{variable.strip} = _ref1[_i];\n\n"
2938
+
2939
+ input_block = [replacement_array] + input_block.insert(1,replacement_string)
2940
+
2941
+ input_block[1] = "for (_i = 0, _j = _ref1.length; _i < _j; _i += 1) {\n\n"
2942
+
2943
+ elsif array_name.strip.include?("..")
2944
+
2945
+ array_type = if array_name.strip.include?("...") then 0 else 1 end
2946
+
2947
+ if array_type == 0
2948
+
2949
+ num1,num2 = array_name.strip.split("...")
2950
+
2951
+ input_block[0] = "for (#{variable.strip} = #{num1}, _j = #{num2}; #{variable.strip} <= _j; #{variable.strip} += 1) {\n\n"
2952
+
2953
+ else
2954
+
2955
+ num1,num2 = array_name.strip.split("..")
2956
+
2957
+ input_block[0] = "for (#{variable.strip} = #{num1}, _j = #{num2}; #{variable.strip} < _j; #{variable.strip} += 1) {\n\n"
2958
+
2959
+ end
2960
+
2961
+ else
2962
+
2963
+ input_block[0] = "for (_i = 0, _j = #{array_name.strip}.length; _i < _j; _i += 1) {\n\n"
2964
+
2965
+ input_block = input_block.insert(1,"#{variable.strip} = #{array_name.strip}[_i];\n\n")
2966
+
2967
+ end
2968
+
2969
+ return input_block
2970
+
2971
+ end
2972
+
2973
+ modified_input_block = input_block.dup
2974
+
2975
+ strings = []
2976
+
2977
+ string_counter = 0
2978
+
2979
+ input_block.each_with_index do |line, index|
2980
+
2981
+ if line.include?("\"")
2982
+
2983
+ opening_quotes = line.index("\"")
2984
+
2985
+ string_extract = line[opening_quotes..line.index("\"", opening_quotes+1)]
2986
+
2987
+ strings << string_extract
2988
+
2989
+ modified_input_block[index] = modified_input_block[index].sub(string_extract, "--string{#{string_counter}}")
2990
+
2991
+ string_counter += 1
2992
+
2993
+ end
2994
+
2995
+ end
2996
+
2997
+ input_block = modified_input_block
2998
+
2999
+ starting_line = input_block[0]
3000
+
3001
+ starting_line = starting_line + "\n" if starting_line.lstrip == starting_line
3002
+
3003
+ junk, condition = starting_line.split("for")
3004
+
3005
+ input_block[-1] = input_block[-1].lstrip.sub("end", "}")
3006
+
3007
+ input_block = compile_condition(condition,input_block)
3008
+
3009
+ modified_input_block = input_block.dup
3010
+
3011
+ input_block.each_with_index do |line, index|
3012
+
3013
+ if line.include?("--string{")
3014
+
3015
+ junk, remains = line.split("--string{")
3016
+
3017
+ string_index, junk = remains.split("}")
3018
+
3019
+ modified_input_block[index] = modified_input_block[index].sub("--string{#{string_index.strip}}", strings[string_index.strip.to_i])
3020
+
3021
+ end
3022
+
3023
+ end
3024
+
3025
+ return modified_input_block
3026
+
3027
+ end
3028
+
3029
+ possible_for_statements = input_file_contents.reject { |element| !element.include?("for") }
3030
+
3031
+ possible_for_statements = possible_for_statements.reject {|element| element.include?("for (")}
3032
+
3033
+ if !possible_for_statements.empty?
3034
+
3035
+ for_statement_indexes = []
3036
+
3037
+ possible_for_statements.each do |statement|
3038
+
3039
+ for_statement_indexes << input_file_contents.dup.each_index.select { |index| input_file_contents[index] == statement }
3040
+
3041
+ end
3042
+
3043
+ for_statement_indexes = [0] + for_statement_indexes.flatten + [-1]
3044
+
3045
+ controlregexp = /(if |def |while )/
3046
+
3047
+ modified_input_contents, extracted_statements = extract_for_blocks(for_statement_indexes, input_file_contents.clone)
3048
+
3049
+ joined_blocks = extracted_statements.collect { |element| element.join }
3050
+
3051
+ for_statements = joined_blocks.reject { |element| element.index(controlregexp) != nil }
3052
+
3053
+ rejected_elements = joined_blocks - for_statements
3054
+
3055
+ rejected_elements_index = []
3056
+
3057
+ rejected_elements.each do |element|
3058
+
3059
+ rejected_elements_index << joined_blocks.each_index.select { |index| joined_blocks[index] == element }
3060
+
3061
+ end
3062
+
3063
+ for_blocks_index = (0...extracted_statements.length).to_a
3064
+
3065
+ rejected_elements_index = rejected_elements_index.flatten
3066
+
3067
+ for_blocks_index -= rejected_elements_index
3068
+
3069
+ modified_for_statements = for_statements.collect { |string| convert_string_to_array(string, temporary_nila_file) }
3070
+
3071
+ modified_for_statements = modified_for_statements.collect { |block| compile_for_syntax(block) }.reverse
3072
+
3073
+ for_blocks_index = for_blocks_index.collect { |element| "--forblock#{element}" }.reverse
3074
+
3075
+ rejected_elements_index = rejected_elements_index.collect { |element| "--forblock#{element}" }.reverse
3076
+
3077
+ rejected_elements = rejected_elements.reverse
3078
+
3079
+ joined_file_contents = modified_input_contents.join
3080
+
3081
+ until for_blocks_index.empty? and rejected_elements_index.empty?
3082
+
3083
+ if !for_blocks_index.empty?
3084
+
3085
+ if joined_file_contents.include?(for_blocks_index[0])
3086
+
3087
+ joined_file_contents = joined_file_contents.sub(for_blocks_index[0], modified_for_statements[0].join)
3088
+
3089
+ for_blocks_index.delete_at(0)
3090
+
3091
+ modified_for_statements.delete_at(0)
3092
+
3093
+ else
3094
+
3095
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0], rejected_elements[0].join)
3096
+
3097
+ rejected_elements_index.delete_at(0)
3098
+
3099
+ rejected_elements.delete_at(0)
3100
+
3101
+ end
3102
+
3103
+ else
3104
+
3105
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0], rejected_elements[0].join)
3106
+
3107
+ rejected_elements_index.delete_at(0)
3108
+
3109
+ rejected_elements.delete_at(0)
3110
+
3111
+ end
3112
+
3113
+ end
3114
+
3115
+ else
3116
+
3117
+ joined_file_contents = input_file_contents.join
3118
+
3119
+ end
3120
+
3121
+ file_id = open(temporary_nila_file, 'w')
3122
+
3123
+ file_id.write(joined_file_contents)
3124
+
3125
+ file_id.close()
3126
+
3127
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
3128
+
3129
+ return line_by_line_contents
3130
+
3131
+ end
3132
+
2714
3133
  def compile_loop_keyword(input_file_contents,temporary_nila_file)
2715
3134
 
2716
3135
  def convert_string_to_array(input_string, temporary_nila_file)
@@ -3049,12 +3468,16 @@ def compile(input_file_path, *output_file_name)
3049
3468
 
3050
3469
  file_contents = compile_ternary_if(input_file_contents)
3051
3470
 
3471
+ puts file_contents
3472
+
3052
3473
  file_contents, rejected_lines = ignore_statement_modifiers(file_contents)
3053
3474
 
3054
3475
  file_contents = replace_unless_until(file_contents)
3055
3476
 
3056
3477
  file_contents = compile_regular_if(file_contents, temporary_nila_file)
3057
3478
 
3479
+ file_contents = compile_regular_for(file_contents, temporary_nila_file)
3480
+
3058
3481
  file_contents = compile_regular_while(file_contents, temporary_nila_file)
3059
3482
 
3060
3483
  file_contents = compile_loop_keyword(file_contents,temporary_nila_file)
@@ -3081,7 +3504,7 @@ def compile(input_file_path, *output_file_name)
3081
3504
 
3082
3505
  end
3083
3506
 
3084
- def extract_variable_names(input_file_contents, temporary_nila_file)
3507
+ def extract_variable_names(input_file_contents)
3085
3508
 
3086
3509
  variables = []
3087
3510
 
@@ -3167,9 +3590,9 @@ def compile(input_file_path, *output_file_name)
3167
3590
 
3168
3591
  file_extract = input_file_contents[0..index_counter]
3169
3592
 
3170
- file_variables = extract_variable_names(file_extract,temporary_nila_file)
3593
+ file_variables = extract_variable_names(file_extract)
3171
3594
 
3172
- block_variables = extract_variable_names(loop_extract,temporary_nila_file)
3595
+ block_variables = extract_variable_names(loop_extract)
3173
3596
 
3174
3597
  var_need_of_declaration = file_variables-block_variables-["_i","_j"]
3175
3598
 
@@ -3985,10 +4408,6 @@ def compile(input_file_path, *output_file_name)
3985
4408
 
3986
4409
  file_contents = compile_conditional_structures(file_contents, temp_file)
3987
4410
 
3988
- file_contents = compile_arrays(file_contents, temp_file)
3989
-
3990
- file_contents = compile_strings(file_contents)
3991
-
3992
4411
  file_contents = compile_integers(file_contents)
3993
4412
 
3994
4413
  file_contents = compile_default_values(file_contents, temp_file)
@@ -3999,12 +4418,18 @@ def compile(input_file_path, *output_file_name)
3999
4418
 
4000
4419
  file_contents = compile_parallel_assignment(file_contents, temp_file)
4001
4420
 
4421
+ file_contents = compile_arrays(file_contents, temp_file)
4422
+
4423
+ file_contents = compile_strings(file_contents)
4424
+
4002
4425
  file_contents, function_names = compile_named_functions(file_contents, named_functions, nested_functions, temp_file)
4003
4426
 
4004
4427
  list_of_variables, file_contents = get_variables(file_contents, temp_file,loop_vars)
4005
4428
 
4006
4429
  file_contents, ruby_functions = compile_custom_function_map(file_contents)
4007
4430
 
4431
+ #compile_ruby_methods(file_contents)
4432
+
4008
4433
  function_names << ruby_functions
4009
4434
 
4010
4435
  list_of_variables += loop_vars
@@ -4087,7 +4512,7 @@ def find_file_path(input_path, file_extension)
4087
4512
 
4088
4513
  end
4089
4514
 
4090
- nilac_version = "0.0.4.3.0"
4515
+ nilac_version = "0.0.4.3.3"
4091
4516
 
4092
4517
  opts = Slop.parse do
4093
4518
  on :c, :compile=, 'Compile Nila File', as:Array, delimiter:":"
metadata CHANGED
@@ -1,46 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nilac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4.3.0
5
- prerelease:
4
+ version: 0.0.4.3.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Adhithya Rajasekaran
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-08-15 00:00:00.000000000 Z
11
+ date: 2013-08-22 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: shark
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: slop
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  description: Nilac is the official compiler of Nila language
@@ -73,6 +68,7 @@ files:
73
68
  - shark/features/multiline_array.feature
74
69
  - shark/features/multiple_variable_initialization.feature
75
70
  - shark/features/numbers.feature
71
+ - shark/features/regular_for.feature
76
72
  - shark/features/regular_if.feature
77
73
  - shark/features/regular_while.feature
78
74
  - shark/features/ruby_operators.feature
@@ -84,6 +80,7 @@ files:
84
80
  - shark/test_files/array_string_indexing.nila
85
81
  - shark/test_files/correct.js
86
82
  - shark/test_files/correct_default_parameters.js
83
+ - shark/test_files/correct_for.js
87
84
  - shark/test_files/correct_hashes.js
88
85
  - shark/test_files/correct_heredoc.js
89
86
  - shark/test_files/correct_if_then_else.js
@@ -116,6 +113,7 @@ files:
116
113
  - shark/test_files/numbers.nila
117
114
  - shark/test_files/operators.nila
118
115
  - shark/test_files/perfect.js
116
+ - shark/test_files/regular_for.nila
119
117
  - shark/test_files/regular_if.nila
120
118
  - shark/test_files/regular_while.nila
121
119
  - shark/test_files/simple.nila
@@ -128,26 +126,26 @@ files:
128
126
  - src/nilac.rb
129
127
  homepage: http://adhithyan15.github.com/nila
130
128
  licenses: []
129
+ metadata: {}
131
130
  post_install_message:
132
131
  rdoc_options: []
133
132
  require_paths:
134
133
  - lib
135
134
  required_ruby_version: !ruby/object:Gem::Requirement
136
- none: false
137
135
  requirements:
138
- - - ! '>='
136
+ - - '>='
139
137
  - !ruby/object:Gem::Version
140
138
  version: '0'
141
139
  required_rubygems_version: !ruby/object:Gem::Requirement
142
- none: false
143
140
  requirements:
144
- - - ! '>='
141
+ - - '>='
145
142
  - !ruby/object:Gem::Version
146
143
  version: '0'
147
144
  requirements: []
148
145
  rubyforge_project:
149
- rubygems_version: 1.8.24
146
+ rubygems_version: 2.0.2
150
147
  signing_key:
151
- specification_version: 3
148
+ specification_version: 4
152
149
  summary: Nilac compiles Nila files into line for line Javascript.
153
150
  test_files: []
151
+ has_rdoc: