nilac 0.0.4.1.3 → 0.0.4.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,7 +1,11 @@
1
1
  # Nila
2
2
 
3
3
  Nila is a dialect of Coffeescript with a Rubyspired syntax. Nilac is the official compiler for the Nila language. Nila language is
4
- still in its infancy. The gems released are developmental releases not production releases. So use with caution.
4
+ still in its infancy. The gems released are developmental releases not production releases. So use with caution.
5
+
6
+ ## Current Version
7
+
8
+ [![Gem Version](https://badge.fury.io/rb/nilac.png)](http://badge.fury.io/rb/nilac)
5
9
 
6
10
  ## Requirements
7
11
 
data/bin/nilac CHANGED
@@ -122,7 +122,7 @@ def compile(input_file_path,*output_file_name)
122
122
 
123
123
  multiline_comment = file_contents_as_string[start_of_multiline_comment..end_of_multiline_comment+3]
124
124
 
125
- modified_file_contents = modified_file_contents.gsub(multiline_comment,"--multiline_comment[#{multiline_comment_counter}]")
125
+ modified_file_contents = modified_file_contents.gsub(multiline_comment,"--multiline_comment[#{multiline_comment_counter}]\n\n")
126
126
 
127
127
  multiline_comment_counter += 1
128
128
 
@@ -265,7 +265,7 @@ def compile(input_file_path,*output_file_name)
265
265
 
266
266
  single_line_comments << comment
267
267
 
268
- current_row = current_row.gsub(comment,"--single_line_comment[#{singleline_comment_counter}]")
268
+ current_row = current_row.gsub(comment,"--single_line_comment[#{singleline_comment_counter}]\n\n")
269
269
 
270
270
  singleline_comment_counter += 1
271
271
 
@@ -1321,8 +1321,11 @@ def compile(input_file_path,*output_file_name)
1321
1321
 
1322
1322
  "puts" => "console.log",
1323
1323
 
1324
+ "p" => "console.log",
1325
+
1324
1326
  "print" => "process.stdout.write"
1325
1327
 
1328
+
1326
1329
  }
1327
1330
 
1328
1331
  function_map = function_map_replacements.keys
@@ -1403,6 +1406,8 @@ def compile(input_file_path,*output_file_name)
1403
1406
 
1404
1407
  modified_string = string.dup
1405
1408
 
1409
+ modified_string = modified_string.rstrip + modified_string.split(modified_string.rstrip)[1].gsub(" ","")
1410
+
1406
1411
  modified_string = modified_string.sub(function+" ",function+"(")
1407
1412
 
1408
1413
  modified_string = modified_string.sub("\n",")\n")
@@ -1435,9 +1440,41 @@ def compile(input_file_path,*output_file_name)
1435
1440
 
1436
1441
  def compile_conditional_structures(input_file_contents,temporary_nila_file)
1437
1442
 
1438
- #Currently the following conditional structures have been implemented
1443
+ def replace_unless_until(input_file_contents)
1444
+
1445
+ modified_file_contents = input_file_contents.clone
1446
+
1447
+ possible_unless_commands = input_file_contents.reject {|element| !element.include?("unless")}
1448
+
1449
+ unless_commands = possible_unless_commands.reject {|element| !element.lstrip.split("unless")[0].empty?}
1450
+
1451
+ unless_commands.each do |command|
1452
+
1453
+ junk,condition = command.split("unless ")
1454
+
1455
+ replacement_string = "if !(#{condition.lstrip.rstrip})\n"
1456
+
1457
+ modified_file_contents[modified_file_contents.index(command)] = replacement_string
1458
+
1459
+ end
1439
1460
 
1440
- #1. If and While Inline Statements
1461
+ possible_until_commands = input_file_contents.reject {|element| !element.include?("until")}
1462
+
1463
+ until_commands = possible_until_commands.reject {|element| !element.lstrip.split("until")[0].empty?}
1464
+
1465
+ until_commands.each do |command|
1466
+
1467
+ junk,condition = command.split("until ")
1468
+
1469
+ replacement_string = "while !(#{condition.lstrip.rstrip})\n"
1470
+
1471
+ modified_file_contents[modified_file_contents.index(command)] = replacement_string
1472
+
1473
+ end
1474
+
1475
+ return modified_file_contents
1476
+
1477
+ end
1441
1478
 
1442
1479
  def compile_inline_conditionals(input_file_contents,temporary_nila_file)
1443
1480
 
@@ -1697,7 +1734,7 @@ def compile(input_file_path,*output_file_name)
1697
1734
 
1698
1735
  end
1699
1736
 
1700
- if_statement_indexes = if_statement_indexes.flatten + [-1]
1737
+ if_statement_indexes = [0] + if_statement_indexes.flatten + [-1]
1701
1738
 
1702
1739
  controlregexp = /(while |def )/
1703
1740
 
@@ -1787,65 +1824,285 @@ def compile(input_file_path,*output_file_name)
1787
1824
 
1788
1825
  end
1789
1826
 
1790
- file_contents = compile_regular_if(input_file_contents,temporary_nila_file)
1827
+ def compile_regular_while(input_file_contents,temporary_nila_file)
1791
1828
 
1792
- file_contents = compile_inline_conditionals(file_contents,temporary_nila_file)
1829
+ def convert_string_to_array(input_string,temporary_nila_file)
1793
1830
 
1794
- return file_contents
1831
+ file_id = open(temporary_nila_file, 'w')
1795
1832
 
1796
- end
1833
+ file_id.write(input_string)
1797
1834
 
1798
- def compile_comments(input_file_contents,comments,temporary_nila_file)
1835
+ file_id.close()
1799
1836
 
1800
- #This method converts Nila comments into pure Javascript comments. This method
1801
- #handles both single line and multiline comments.
1837
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
1802
1838
 
1803
- file_contents_as_string = input_file_contents.join
1839
+ return line_by_line_contents
1804
1840
 
1805
- single_line_comments = comments[0]
1841
+ end
1806
1842
 
1807
- multiline_comments = comments[1]
1843
+ def extract_while_blocks(while_statement_indexes,input_file_contents)
1808
1844
 
1809
- single_line_comment_counter = 1
1845
+ possible_while_blocks = []
1810
1846
 
1811
- multi_line_comment_counter = 1
1847
+ while_block_counter = 0
1812
1848
 
1813
- for x in 0...single_line_comments.length
1849
+ extracted_blocks = []
1814
1850
 
1815
- current_singleline_comment = "--single_line_comment[#{single_line_comment_counter}]"
1851
+ controlregexp = /(if |while |def )/
1816
1852
 
1817
- replacement_singleline_string = single_line_comments[x].sub("#","//")
1853
+ rejectionregexp = /( if | while )/
1818
1854
 
1819
- file_contents_as_string = file_contents_as_string.sub(current_singleline_comment,replacement_singleline_string)
1855
+ for x in 0...while_statement_indexes.length-1
1820
1856
 
1821
- single_line_comment_counter += 1
1857
+ possible_while_blocks << input_file_contents[while_statement_indexes[x]..while_statement_indexes[x+1]]
1822
1858
 
1859
+ end
1823
1860
 
1824
- end
1861
+ end_counter = 0
1825
1862
 
1826
- for y in 0...multiline_comments.length
1863
+ end_index = []
1827
1864
 
1828
- current_multiline_comment = "--multiline_comment[#{multi_line_comment_counter}]"
1865
+ current_block = []
1829
1866
 
1830
- replacement_multiline_string = multiline_comments[y].sub("=begin","/*\n")
1867
+ possible_while_blocks.each_with_index do |block|
1831
1868
 
1832
- replacement_multiline_string = replacement_multiline_string.sub("=end","\n*/")
1869
+ current_block += block
1833
1870
 
1834
- file_contents_as_string = file_contents_as_string.sub(current_multiline_comment,replacement_multiline_string)
1871
+ current_block.each_with_index do |line,index|
1835
1872
 
1836
- multi_line_comment_counter += 1
1873
+ if line.strip.eql? "end"
1874
+
1875
+ end_counter += 1
1876
+
1877
+ end_index << index
1878
+
1879
+ end
1880
+
1881
+ end
1882
+
1883
+ if end_counter > 0
1884
+
1885
+ until end_index.empty?
1886
+
1887
+ array_extract = current_block[0..end_index[0]].reverse
1888
+
1889
+ index_counter = 0
1890
+
1891
+ array_extract.each_with_index do |line|
1892
+
1893
+ break if (line.lstrip.index(controlregexp) != nil and line.lstrip.index(rejectionregexp).nil?)
1894
+
1895
+ index_counter += 1
1896
+
1897
+ end
1898
+
1899
+ block_extract = array_extract[0..index_counter].reverse
1900
+
1901
+ extracted_blocks << block_extract
1902
+
1903
+ block_start = current_block.index(block_extract[0])
1904
+
1905
+ block_end = current_block.index(block_extract[-1])
1906
+
1907
+ current_block[block_start..block_end] = "--whileblock#{while_block_counter}"
1908
+
1909
+ while_block_counter += 1
1910
+
1911
+ end_counter = 0
1912
+
1913
+ end_index = []
1914
+
1915
+ current_block.each_with_index do |line,index|
1916
+
1917
+ if line.strip.eql? "end"
1918
+
1919
+ end_counter += 1
1920
+
1921
+ end_index << index
1922
+
1923
+ end
1924
+
1925
+ end
1926
+
1927
+ end
1928
+
1929
+ end
1930
+
1931
+ end
1932
+
1933
+ return current_block,extracted_blocks
1934
+
1935
+ end
1936
+
1937
+ def compile_while_syntax(input_block)
1938
+
1939
+ strings = []
1940
+
1941
+ string_counter = 0
1942
+
1943
+ modified_input_block = input_block.dup
1944
+
1945
+ input_block.each_with_index do |line,index|
1946
+
1947
+ if line.include?("\"")
1948
+
1949
+ opening_quotes = line.index("\"")
1950
+
1951
+ string_extract = line[opening_quotes..line.index("\"",opening_quotes+1)]
1952
+
1953
+ strings << string_extract
1954
+
1955
+ modified_input_block[index] = modified_input_block[index].sub(string_extract,"--string{#{string_counter}}")
1956
+
1957
+ string_counter += 1
1958
+
1959
+ end
1960
+
1961
+ end
1962
+
1963
+ input_block = modified_input_block
1964
+
1965
+ starting_line = input_block[0]
1966
+
1967
+ starting_line = starting_line + "\n" if starting_line.lstrip == starting_line
1968
+
1969
+ junk,condition = starting_line.split("while")
1970
+
1971
+ input_block[0] = "whaaleskey (#{condition.lstrip.rstrip.gsub("?","")}) {\n"
1972
+
1973
+ input_block[-1] = input_block[-1].lstrip.sub("end","}")
1974
+
1975
+ modified_input_block = input_block.dup
1976
+
1977
+ input_block.each_with_index do |line,index|
1978
+
1979
+ if line.include?("--string{")
1980
+
1981
+ junk,remains = line.split("--string{")
1982
+
1983
+ string_index,junk = remains.split("}")
1984
+
1985
+ modified_input_block[index] = modified_input_block[index].sub("--string{#{string_index.strip}}",strings[string_index.strip.to_i])
1986
+
1987
+ end
1988
+
1989
+ end
1990
+
1991
+ return modified_input_block
1992
+
1993
+ end
1994
+
1995
+ possible_while_statements = input_file_contents.reject {|element| !element.include?("while")}
1996
+
1997
+ if !possible_while_statements.empty?
1998
+
1999
+ while_statement_indexes = []
2000
+
2001
+ possible_while_statements.each do |statement|
2002
+
2003
+ while_statement_indexes << input_file_contents.dup.each_index.select {|index| input_file_contents[index] == statement}
2004
+
2005
+ end
2006
+
2007
+ while_statement_indexes = [0] + while_statement_indexes.flatten + [-1]
2008
+
2009
+ controlregexp = /(if |def )/
2010
+
2011
+ modified_input_contents,extracted_statements = extract_while_blocks(while_statement_indexes,input_file_contents.clone)
2012
+
2013
+ joined_blocks = extracted_statements.collect {|element| element.join}
2014
+
2015
+ while_statements = joined_blocks.reject {|element| element.index(controlregexp) != nil}
2016
+
2017
+ rejected_elements = joined_blocks - while_statements
2018
+
2019
+ rejected_elements_index = []
2020
+
2021
+ rejected_elements.each do |element|
2022
+
2023
+ rejected_elements_index << joined_blocks.each_index.select {|index| joined_blocks[index] == element}
2024
+
2025
+ end
2026
+
2027
+ while_blocks_index = (0...extracted_statements.length).to_a
2028
+
2029
+ rejected_elements_index = rejected_elements_index.flatten
2030
+
2031
+ while_blocks_index -= rejected_elements_index
2032
+
2033
+ modified_while_statements = while_statements.collect {|string| convert_string_to_array(string,temporary_nila_file)}
2034
+
2035
+ modified_while_statements = modified_while_statements.collect {|block| compile_while_syntax(block)}.reverse
2036
+
2037
+ while_blocks_index = while_blocks_index.collect {|element| "--whileblock#{element}"}.reverse
2038
+
2039
+ rejected_elements_index = rejected_elements_index.collect {|element| "--whileblock#{element}"}.reverse
2040
+
2041
+ rejected_elements = rejected_elements.reverse
2042
+
2043
+ joined_file_contents = modified_input_contents.join
2044
+
2045
+ until while_blocks_index.empty? and rejected_elements_index.empty?
2046
+
2047
+ if !while_blocks_index.empty?
2048
+
2049
+ if joined_file_contents.include?(while_blocks_index[0])
2050
+
2051
+ joined_file_contents = joined_file_contents.sub(while_blocks_index[0],modified_while_statements[0].join)
2052
+
2053
+ while_blocks_index.delete_at(0)
2054
+
2055
+ modified_while_statements.delete_at(0)
2056
+
2057
+ else
2058
+
2059
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0],rejected_elements[0])
2060
+
2061
+ rejected_elements_index.delete_at(0)
2062
+
2063
+ rejected_elements.delete_at(0)
2064
+
2065
+ end
2066
+
2067
+ else
2068
+
2069
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0],rejected_elements[0])
2070
+
2071
+ rejected_elements_index.delete_at(0)
2072
+
2073
+ rejected_elements.delete_at(0)
2074
+
2075
+ end
2076
+
2077
+ end
2078
+
2079
+ else
2080
+
2081
+ joined_file_contents = input_file_contents.join
2082
+
2083
+ end
2084
+
2085
+ file_id = open(temporary_nila_file, 'w')
2086
+
2087
+ file_id.write(joined_file_contents)
2088
+
2089
+ file_id.close()
2090
+
2091
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
2092
+
2093
+ return line_by_line_contents
1837
2094
 
1838
2095
  end
1839
2096
 
1840
- file_id = open(temporary_nila_file, 'w')
2097
+ file_contents = replace_unless_until(input_file_contents)
1841
2098
 
1842
- file_id.write(file_contents_as_string)
2099
+ file_contents = compile_regular_if(file_contents,temporary_nila_file)
1843
2100
 
1844
- file_id.close()
2101
+ file_contents = compile_regular_while(file_contents,temporary_nila_file)
1845
2102
 
1846
- line_by_line_contents = read_file_line_by_line(temporary_nila_file)
2103
+ file_contents = compile_inline_conditionals(file_contents,temporary_nila_file)
1847
2104
 
1848
- line_by_line_contents
2105
+ return file_contents
1849
2106
 
1850
2107
  end
1851
2108
 
@@ -1869,7 +2126,7 @@ def compile(input_file_path,*output_file_name)
1869
2126
 
1870
2127
  end
1871
2128
 
1872
- reject_regexp = /(function |Euuf |if |else|elsuf|switch |case|while |for )/
2129
+ reject_regexp = /(function |Euuf |if |else|elsuf|switch |case|while |whaaleskey |for )/
1873
2130
 
1874
2131
  modified_file_contents = []
1875
2132
 
@@ -1923,7 +2180,85 @@ def compile(input_file_path,*output_file_name)
1923
2180
 
1924
2181
  end
1925
2182
 
1926
- def pretty_print_javascript(javascript_file_contents,temporary_nila_file)
2183
+ def compile_comments(input_file_contents,comments,temporary_nila_file)
2184
+
2185
+ #This method converts Nila comments into pure Javascript comments. This method
2186
+ #handles both single line and multiline comments.
2187
+
2188
+ file_contents_as_string = input_file_contents.join
2189
+
2190
+ single_line_comments = comments[0]
2191
+
2192
+ multiline_comments = comments[1]
2193
+
2194
+ single_line_comment_counter = 1
2195
+
2196
+ multi_line_comment_counter = 1
2197
+
2198
+ ignorable_keywords = [/if/,/while/,/function/]
2199
+
2200
+ dummy_replacement_words = ["eeuuff","whaalesskkey","conffoolotion"]
2201
+
2202
+ for x in 0...single_line_comments.length
2203
+
2204
+ current_singleline_comment = "--single_line_comment[#{single_line_comment_counter}]"
2205
+
2206
+ replacement_singleline_string = single_line_comments[x].sub("#","//")
2207
+
2208
+ ignorable_keywords.each_with_index do |keyword,index|
2209
+
2210
+ if replacement_singleline_string.index(keyword) != nil
2211
+
2212
+ replacement_singleline_string = replacement_singleline_string.sub(keyword.inspect[1...-1],dummy_replacement_words[index])
2213
+
2214
+ end
2215
+
2216
+ end
2217
+
2218
+ file_contents_as_string = file_contents_as_string.sub(current_singleline_comment,replacement_singleline_string)
2219
+
2220
+ single_line_comment_counter += 1
2221
+
2222
+
2223
+ end
2224
+
2225
+ for y in 0...multiline_comments.length
2226
+
2227
+ current_multiline_comment = "--multiline_comment[#{multi_line_comment_counter}]"
2228
+
2229
+ replacement_multiline_string = multiline_comments[y].sub("=begin","/*\n")
2230
+
2231
+ replacement_multiline_string = replacement_multiline_string.sub("=end","\n*/")
2232
+
2233
+ ignorable_keywords.each_with_index do |keyword,index|
2234
+
2235
+ if replacement_multiline_string.index(keyword) != nil
2236
+
2237
+ replacement_multiline_string = replacement_multiline_string.sub(keyword.inspect[1...-1],dummy_replacement_words[index])
2238
+
2239
+ end
2240
+
2241
+ end
2242
+
2243
+ file_contents_as_string = file_contents_as_string.sub(current_multiline_comment,replacement_multiline_string)
2244
+
2245
+ multi_line_comment_counter += 1
2246
+
2247
+ end
2248
+
2249
+ file_id = open(temporary_nila_file, 'w')
2250
+
2251
+ file_id.write(file_contents_as_string)
2252
+
2253
+ file_id.close()
2254
+
2255
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
2256
+
2257
+ line_by_line_contents
2258
+
2259
+ end
2260
+
2261
+ def pretty_print_javascript(javascript_file_contents,temporary_nila_file,comments)
1927
2262
 
1928
2263
  def reset_tabs(input_file_contents)
1929
2264
 
@@ -1983,22 +2318,6 @@ def compile(input_file_path,*output_file_name)
1983
2318
 
1984
2319
  end
1985
2320
 
1986
- def previous_formatting(input_string,tab_counter,temporary_nila_file)
1987
-
1988
- string_as_array = convert_string_to_array(input_string,temporary_nila_file)
1989
-
1990
- modified_array = []
1991
-
1992
- string_as_array.each do |line|
1993
-
1994
- modified_array << " "*tab_counter + line
1995
-
1996
- end
1997
-
1998
- return modified_array.join
1999
-
2000
- end
2001
-
2002
2321
  def fix_newlines(file_contents)
2003
2322
 
2004
2323
  def extract_blocks(file_contents)
@@ -2218,10 +2537,28 @@ def compile(input_file_path,*output_file_name)
2218
2537
 
2219
2538
  end
2220
2539
 
2540
+ def replace_ignored_words(input_string)
2541
+
2542
+ ignorable_keywords = [/if/,/while/,/function/]
2543
+
2544
+ dummy_replacement_words = ["eeuuff","whaalesskkey","conffoolotion"]
2545
+
2546
+ dummy_replacement_words.each_with_index do |word,index|
2547
+
2548
+ input_string = input_string.sub(word,ignorable_keywords[index].inspect[1...-1])
2549
+
2550
+ end
2551
+
2552
+ return input_string
2553
+
2554
+ end
2555
+
2221
2556
  javascript_regexp = /(if |while |function |function\()/
2222
2557
 
2223
2558
  javascript_file_contents = javascript_file_contents.collect {|element| element.sub("Euuf","if")}
2224
2559
 
2560
+ javascript_file_contents = javascript_file_contents.collect {|element| element.sub("whaaleskey","while")}
2561
+
2225
2562
  javascript_file_contents = reset_tabs(javascript_file_contents)
2226
2563
 
2227
2564
  starting_locations = []
@@ -2322,8 +2659,6 @@ def compile(input_file_path,*output_file_name)
2322
2659
 
2323
2660
  end
2324
2661
 
2325
-
2326
-
2327
2662
  remaining_file_contents = ["(function() {\n",remaining_file_contents,"\n}).call(this);"].flatten
2328
2663
 
2329
2664
  joined_file_contents = remaining_file_contents.join
@@ -2344,7 +2679,6 @@ def compile(input_file_path,*output_file_name)
2344
2679
 
2345
2680
  end
2346
2681
 
2347
-
2348
2682
  file_id = open(temporary_nila_file, 'w')
2349
2683
 
2350
2684
  file_id.write(joined_file_contents)
@@ -2357,6 +2691,8 @@ def compile(input_file_path,*output_file_name)
2357
2691
 
2358
2692
  line_by_line_contents = fix_syntax_indentation(line_by_line_contents)
2359
2693
 
2694
+ line_by_line_contents = line_by_line_contents.collect {|element| replace_ignored_words(element)}
2695
+
2360
2696
  return line_by_line_contents
2361
2697
 
2362
2698
  end
@@ -2439,7 +2775,7 @@ def compile(input_file_path,*output_file_name)
2439
2775
 
2440
2776
  file_contents = compile_comments(file_contents,comments,temp_file)
2441
2777
 
2442
- file_contents = pretty_print_javascript(file_contents,temp_file)
2778
+ file_contents = pretty_print_javascript(file_contents,temp_file,comments)
2443
2779
 
2444
2780
  file_contents = compile_operators(file_contents)
2445
2781
 
data/lib/nilac/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Nilac
2
- VERSION = "0.0.4.1.3"
2
+ VERSION = "0.0.4.1.4"
3
3
  end
@@ -0,0 +1,11 @@
1
+ Feature: This feature bring Ruby's while statement to Nila.
2
+ Scenario: Input function with while statements
3
+ Given the input file "regular_while.nila"
4
+ When the ~compiler is run
5
+ The output file must be "regular_while.js"
6
+ The output file must equal "correct_regular_while.js"
7
+
8
+ Configurations:
9
+
10
+ ~compiler => src/nilac.rb
11
+ :v $cliusage => ruby :v --compile $file
@@ -1,10 +1,10 @@
1
1
  //Written using Nila. Visit http://adhithyan15.github.io/nila
2
2
  (function() {
3
- if (visitor_present) {;
3
+ if (visitor_present) {
4
4
  //This file is for demonstration purpose. It doesn't really achieve anything
5
- if (active || happy) {;
5
+ if (active || happy) {
6
6
  console.log("Hello Wonderful Visitor!");
7
- } else if (idle && not_engaged) {;
7
+ } else if (idle && not_engaged) {
8
8
  console.log("Hello Visitor! It is time to engage!");
9
9
  } else {
10
10
  console.log("Hello user!");
@@ -0,0 +1,14 @@
1
+ //Written using Nila. Visit http://adhithyan15.github.io/nila
2
+ (function() {
3
+ var counter;
4
+
5
+ // This file will demonstrate Nila's while loops
6
+
7
+ counter = 1;
8
+
9
+ while (counter < 11) {
10
+ console.log(counter);
11
+ counter = counter + 1;
12
+ }
13
+
14
+ }).call(this);
@@ -12,10 +12,16 @@
12
12
  console.log("There's no such person");
13
13
  }
14
14
 
15
- i = 0;
15
+ if (!(person.present)) {
16
+ console.log("There's no such person");
17
+ }
18
+
19
+ i=1;
16
20
 
17
21
  while (!(i > 10)) {
18
- i += 1;
22
+ process.stdout.write("" + i + " ");
23
+ i+=1;
24
+ //redo if i > 10
19
25
  }
20
26
 
21
27
  }).call(this);
@@ -0,0 +1,7 @@
1
+ # This file will demonstrate Nila's while loops
2
+
3
+ counter = 1
4
+ while counter < 11
5
+ puts counter
6
+ counter = counter + 1
7
+ end
@@ -5,6 +5,13 @@
5
5
 
6
6
  puts "There's no such person" unless person.present?
7
7
 
8
- i = 0
8
+ unless person.present?
9
+ puts "There's no such person"
10
+ end
9
11
 
10
- i += 1 until i > 10
12
+ i=1
13
+ until i > 10
14
+ print "#{i} "
15
+ i+=1
16
+ #redo if i > 10
17
+ end
data/src/nilac.rb CHANGED
@@ -120,7 +120,7 @@ def compile(input_file_path,*output_file_name)
120
120
 
121
121
  multiline_comment = file_contents_as_string[start_of_multiline_comment..end_of_multiline_comment+3]
122
122
 
123
- modified_file_contents = modified_file_contents.gsub(multiline_comment,"--multiline_comment[#{multiline_comment_counter}]")
123
+ modified_file_contents = modified_file_contents.gsub(multiline_comment,"--multiline_comment[#{multiline_comment_counter}]\n\n")
124
124
 
125
125
  multiline_comment_counter += 1
126
126
 
@@ -263,7 +263,7 @@ def compile(input_file_path,*output_file_name)
263
263
 
264
264
  single_line_comments << comment
265
265
 
266
- current_row = current_row.gsub(comment,"--single_line_comment[#{singleline_comment_counter}]")
266
+ current_row = current_row.gsub(comment,"--single_line_comment[#{singleline_comment_counter}]\n\n")
267
267
 
268
268
  singleline_comment_counter += 1
269
269
 
@@ -1319,8 +1319,11 @@ def compile(input_file_path,*output_file_name)
1319
1319
 
1320
1320
  "puts" => "console.log",
1321
1321
 
1322
+ "p" => "console.log",
1323
+
1322
1324
  "print" => "process.stdout.write"
1323
1325
 
1326
+
1324
1327
  }
1325
1328
 
1326
1329
  function_map = function_map_replacements.keys
@@ -1401,6 +1404,8 @@ def compile(input_file_path,*output_file_name)
1401
1404
 
1402
1405
  modified_string = string.dup
1403
1406
 
1407
+ modified_string = modified_string.rstrip + modified_string.split(modified_string.rstrip)[1].gsub(" ","")
1408
+
1404
1409
  modified_string = modified_string.sub(function+" ",function+"(")
1405
1410
 
1406
1411
  modified_string = modified_string.sub("\n",")\n")
@@ -1433,9 +1438,41 @@ def compile(input_file_path,*output_file_name)
1433
1438
 
1434
1439
  def compile_conditional_structures(input_file_contents,temporary_nila_file)
1435
1440
 
1436
- #Currently the following conditional structures have been implemented
1441
+ def replace_unless_until(input_file_contents)
1442
+
1443
+ modified_file_contents = input_file_contents.clone
1444
+
1445
+ possible_unless_commands = input_file_contents.reject {|element| !element.include?("unless")}
1446
+
1447
+ unless_commands = possible_unless_commands.reject {|element| !element.lstrip.split("unless")[0].empty?}
1448
+
1449
+ unless_commands.each do |command|
1450
+
1451
+ junk,condition = command.split("unless ")
1452
+
1453
+ replacement_string = "if !(#{condition.lstrip.rstrip})\n"
1454
+
1455
+ modified_file_contents[modified_file_contents.index(command)] = replacement_string
1456
+
1457
+ end
1437
1458
 
1438
- #1. If and While Inline Statements
1459
+ possible_until_commands = input_file_contents.reject {|element| !element.include?("until")}
1460
+
1461
+ until_commands = possible_until_commands.reject {|element| !element.lstrip.split("until")[0].empty?}
1462
+
1463
+ until_commands.each do |command|
1464
+
1465
+ junk,condition = command.split("until ")
1466
+
1467
+ replacement_string = "while !(#{condition.lstrip.rstrip})\n"
1468
+
1469
+ modified_file_contents[modified_file_contents.index(command)] = replacement_string
1470
+
1471
+ end
1472
+
1473
+ return modified_file_contents
1474
+
1475
+ end
1439
1476
 
1440
1477
  def compile_inline_conditionals(input_file_contents,temporary_nila_file)
1441
1478
 
@@ -1695,7 +1732,7 @@ def compile(input_file_path,*output_file_name)
1695
1732
 
1696
1733
  end
1697
1734
 
1698
- if_statement_indexes = if_statement_indexes.flatten + [-1]
1735
+ if_statement_indexes = [0] + if_statement_indexes.flatten + [-1]
1699
1736
 
1700
1737
  controlregexp = /(while |def )/
1701
1738
 
@@ -1785,65 +1822,285 @@ def compile(input_file_path,*output_file_name)
1785
1822
 
1786
1823
  end
1787
1824
 
1788
- file_contents = compile_regular_if(input_file_contents,temporary_nila_file)
1825
+ def compile_regular_while(input_file_contents,temporary_nila_file)
1789
1826
 
1790
- file_contents = compile_inline_conditionals(file_contents,temporary_nila_file)
1827
+ def convert_string_to_array(input_string,temporary_nila_file)
1791
1828
 
1792
- return file_contents
1829
+ file_id = open(temporary_nila_file, 'w')
1793
1830
 
1794
- end
1831
+ file_id.write(input_string)
1795
1832
 
1796
- def compile_comments(input_file_contents,comments,temporary_nila_file)
1833
+ file_id.close()
1797
1834
 
1798
- #This method converts Nila comments into pure Javascript comments. This method
1799
- #handles both single line and multiline comments.
1835
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
1800
1836
 
1801
- file_contents_as_string = input_file_contents.join
1837
+ return line_by_line_contents
1802
1838
 
1803
- single_line_comments = comments[0]
1839
+ end
1804
1840
 
1805
- multiline_comments = comments[1]
1841
+ def extract_while_blocks(while_statement_indexes,input_file_contents)
1806
1842
 
1807
- single_line_comment_counter = 1
1843
+ possible_while_blocks = []
1808
1844
 
1809
- multi_line_comment_counter = 1
1845
+ while_block_counter = 0
1810
1846
 
1811
- for x in 0...single_line_comments.length
1847
+ extracted_blocks = []
1812
1848
 
1813
- current_singleline_comment = "--single_line_comment[#{single_line_comment_counter}]"
1849
+ controlregexp = /(if |while |def )/
1814
1850
 
1815
- replacement_singleline_string = single_line_comments[x].sub("#","//")
1851
+ rejectionregexp = /( if | while )/
1816
1852
 
1817
- file_contents_as_string = file_contents_as_string.sub(current_singleline_comment,replacement_singleline_string)
1853
+ for x in 0...while_statement_indexes.length-1
1818
1854
 
1819
- single_line_comment_counter += 1
1855
+ possible_while_blocks << input_file_contents[while_statement_indexes[x]..while_statement_indexes[x+1]]
1820
1856
 
1857
+ end
1821
1858
 
1822
- end
1859
+ end_counter = 0
1823
1860
 
1824
- for y in 0...multiline_comments.length
1861
+ end_index = []
1825
1862
 
1826
- current_multiline_comment = "--multiline_comment[#{multi_line_comment_counter}]"
1863
+ current_block = []
1827
1864
 
1828
- replacement_multiline_string = multiline_comments[y].sub("=begin","/*\n")
1865
+ possible_while_blocks.each_with_index do |block|
1829
1866
 
1830
- replacement_multiline_string = replacement_multiline_string.sub("=end","\n*/")
1867
+ current_block += block
1831
1868
 
1832
- file_contents_as_string = file_contents_as_string.sub(current_multiline_comment,replacement_multiline_string)
1869
+ current_block.each_with_index do |line,index|
1833
1870
 
1834
- multi_line_comment_counter += 1
1871
+ if line.strip.eql? "end"
1872
+
1873
+ end_counter += 1
1874
+
1875
+ end_index << index
1876
+
1877
+ end
1878
+
1879
+ end
1880
+
1881
+ if end_counter > 0
1882
+
1883
+ until end_index.empty?
1884
+
1885
+ array_extract = current_block[0..end_index[0]].reverse
1886
+
1887
+ index_counter = 0
1888
+
1889
+ array_extract.each_with_index do |line|
1890
+
1891
+ break if (line.lstrip.index(controlregexp) != nil and line.lstrip.index(rejectionregexp).nil?)
1892
+
1893
+ index_counter += 1
1894
+
1895
+ end
1896
+
1897
+ block_extract = array_extract[0..index_counter].reverse
1898
+
1899
+ extracted_blocks << block_extract
1900
+
1901
+ block_start = current_block.index(block_extract[0])
1902
+
1903
+ block_end = current_block.index(block_extract[-1])
1904
+
1905
+ current_block[block_start..block_end] = "--whileblock#{while_block_counter}"
1906
+
1907
+ while_block_counter += 1
1908
+
1909
+ end_counter = 0
1910
+
1911
+ end_index = []
1912
+
1913
+ current_block.each_with_index do |line,index|
1914
+
1915
+ if line.strip.eql? "end"
1916
+
1917
+ end_counter += 1
1918
+
1919
+ end_index << index
1920
+
1921
+ end
1922
+
1923
+ end
1924
+
1925
+ end
1926
+
1927
+ end
1928
+
1929
+ end
1930
+
1931
+ return current_block,extracted_blocks
1932
+
1933
+ end
1934
+
1935
+ def compile_while_syntax(input_block)
1936
+
1937
+ strings = []
1938
+
1939
+ string_counter = 0
1940
+
1941
+ modified_input_block = input_block.dup
1942
+
1943
+ input_block.each_with_index do |line,index|
1944
+
1945
+ if line.include?("\"")
1946
+
1947
+ opening_quotes = line.index("\"")
1948
+
1949
+ string_extract = line[opening_quotes..line.index("\"",opening_quotes+1)]
1950
+
1951
+ strings << string_extract
1952
+
1953
+ modified_input_block[index] = modified_input_block[index].sub(string_extract,"--string{#{string_counter}}")
1954
+
1955
+ string_counter += 1
1956
+
1957
+ end
1958
+
1959
+ end
1960
+
1961
+ input_block = modified_input_block
1962
+
1963
+ starting_line = input_block[0]
1964
+
1965
+ starting_line = starting_line + "\n" if starting_line.lstrip == starting_line
1966
+
1967
+ junk,condition = starting_line.split("while")
1968
+
1969
+ input_block[0] = "whaaleskey (#{condition.lstrip.rstrip.gsub("?","")}) {\n"
1970
+
1971
+ input_block[-1] = input_block[-1].lstrip.sub("end","}")
1972
+
1973
+ modified_input_block = input_block.dup
1974
+
1975
+ input_block.each_with_index do |line,index|
1976
+
1977
+ if line.include?("--string{")
1978
+
1979
+ junk,remains = line.split("--string{")
1980
+
1981
+ string_index,junk = remains.split("}")
1982
+
1983
+ modified_input_block[index] = modified_input_block[index].sub("--string{#{string_index.strip}}",strings[string_index.strip.to_i])
1984
+
1985
+ end
1986
+
1987
+ end
1988
+
1989
+ return modified_input_block
1990
+
1991
+ end
1992
+
1993
+ possible_while_statements = input_file_contents.reject {|element| !element.include?("while")}
1994
+
1995
+ if !possible_while_statements.empty?
1996
+
1997
+ while_statement_indexes = []
1998
+
1999
+ possible_while_statements.each do |statement|
2000
+
2001
+ while_statement_indexes << input_file_contents.dup.each_index.select {|index| input_file_contents[index] == statement}
2002
+
2003
+ end
2004
+
2005
+ while_statement_indexes = [0] + while_statement_indexes.flatten + [-1]
2006
+
2007
+ controlregexp = /(if |def )/
2008
+
2009
+ modified_input_contents,extracted_statements = extract_while_blocks(while_statement_indexes,input_file_contents.clone)
2010
+
2011
+ joined_blocks = extracted_statements.collect {|element| element.join}
2012
+
2013
+ while_statements = joined_blocks.reject {|element| element.index(controlregexp) != nil}
2014
+
2015
+ rejected_elements = joined_blocks - while_statements
2016
+
2017
+ rejected_elements_index = []
2018
+
2019
+ rejected_elements.each do |element|
2020
+
2021
+ rejected_elements_index << joined_blocks.each_index.select {|index| joined_blocks[index] == element}
2022
+
2023
+ end
2024
+
2025
+ while_blocks_index = (0...extracted_statements.length).to_a
2026
+
2027
+ rejected_elements_index = rejected_elements_index.flatten
2028
+
2029
+ while_blocks_index -= rejected_elements_index
2030
+
2031
+ modified_while_statements = while_statements.collect {|string| convert_string_to_array(string,temporary_nila_file)}
2032
+
2033
+ modified_while_statements = modified_while_statements.collect {|block| compile_while_syntax(block)}.reverse
2034
+
2035
+ while_blocks_index = while_blocks_index.collect {|element| "--whileblock#{element}"}.reverse
2036
+
2037
+ rejected_elements_index = rejected_elements_index.collect {|element| "--whileblock#{element}"}.reverse
2038
+
2039
+ rejected_elements = rejected_elements.reverse
2040
+
2041
+ joined_file_contents = modified_input_contents.join
2042
+
2043
+ until while_blocks_index.empty? and rejected_elements_index.empty?
2044
+
2045
+ if !while_blocks_index.empty?
2046
+
2047
+ if joined_file_contents.include?(while_blocks_index[0])
2048
+
2049
+ joined_file_contents = joined_file_contents.sub(while_blocks_index[0],modified_while_statements[0].join)
2050
+
2051
+ while_blocks_index.delete_at(0)
2052
+
2053
+ modified_while_statements.delete_at(0)
2054
+
2055
+ else
2056
+
2057
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0],rejected_elements[0])
2058
+
2059
+ rejected_elements_index.delete_at(0)
2060
+
2061
+ rejected_elements.delete_at(0)
2062
+
2063
+ end
2064
+
2065
+ else
2066
+
2067
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0],rejected_elements[0])
2068
+
2069
+ rejected_elements_index.delete_at(0)
2070
+
2071
+ rejected_elements.delete_at(0)
2072
+
2073
+ end
2074
+
2075
+ end
2076
+
2077
+ else
2078
+
2079
+ joined_file_contents = input_file_contents.join
2080
+
2081
+ end
2082
+
2083
+ file_id = open(temporary_nila_file, 'w')
2084
+
2085
+ file_id.write(joined_file_contents)
2086
+
2087
+ file_id.close()
2088
+
2089
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
2090
+
2091
+ return line_by_line_contents
1835
2092
 
1836
2093
  end
1837
2094
 
1838
- file_id = open(temporary_nila_file, 'w')
2095
+ file_contents = replace_unless_until(input_file_contents)
1839
2096
 
1840
- file_id.write(file_contents_as_string)
2097
+ file_contents = compile_regular_if(file_contents,temporary_nila_file)
1841
2098
 
1842
- file_id.close()
2099
+ file_contents = compile_regular_while(file_contents,temporary_nila_file)
1843
2100
 
1844
- line_by_line_contents = read_file_line_by_line(temporary_nila_file)
2101
+ file_contents = compile_inline_conditionals(file_contents,temporary_nila_file)
1845
2102
 
1846
- line_by_line_contents
2103
+ return file_contents
1847
2104
 
1848
2105
  end
1849
2106
 
@@ -1867,7 +2124,7 @@ def compile(input_file_path,*output_file_name)
1867
2124
 
1868
2125
  end
1869
2126
 
1870
- reject_regexp = /(function |Euuf |if |else|elsuf|switch |case|while |for )/
2127
+ reject_regexp = /(function |Euuf |if |else|elsuf|switch |case|while |whaaleskey |for )/
1871
2128
 
1872
2129
  modified_file_contents = []
1873
2130
 
@@ -1921,7 +2178,85 @@ def compile(input_file_path,*output_file_name)
1921
2178
 
1922
2179
  end
1923
2180
 
1924
- def pretty_print_javascript(javascript_file_contents,temporary_nila_file)
2181
+ def compile_comments(input_file_contents,comments,temporary_nila_file)
2182
+
2183
+ #This method converts Nila comments into pure Javascript comments. This method
2184
+ #handles both single line and multiline comments.
2185
+
2186
+ file_contents_as_string = input_file_contents.join
2187
+
2188
+ single_line_comments = comments[0]
2189
+
2190
+ multiline_comments = comments[1]
2191
+
2192
+ single_line_comment_counter = 1
2193
+
2194
+ multi_line_comment_counter = 1
2195
+
2196
+ ignorable_keywords = [/if/,/while/,/function/]
2197
+
2198
+ dummy_replacement_words = ["eeuuff","whaalesskkey","conffoolotion"]
2199
+
2200
+ for x in 0...single_line_comments.length
2201
+
2202
+ current_singleline_comment = "--single_line_comment[#{single_line_comment_counter}]"
2203
+
2204
+ replacement_singleline_string = single_line_comments[x].sub("#","//")
2205
+
2206
+ ignorable_keywords.each_with_index do |keyword,index|
2207
+
2208
+ if replacement_singleline_string.index(keyword) != nil
2209
+
2210
+ replacement_singleline_string = replacement_singleline_string.sub(keyword.inspect[1...-1],dummy_replacement_words[index])
2211
+
2212
+ end
2213
+
2214
+ end
2215
+
2216
+ file_contents_as_string = file_contents_as_string.sub(current_singleline_comment,replacement_singleline_string)
2217
+
2218
+ single_line_comment_counter += 1
2219
+
2220
+
2221
+ end
2222
+
2223
+ for y in 0...multiline_comments.length
2224
+
2225
+ current_multiline_comment = "--multiline_comment[#{multi_line_comment_counter}]"
2226
+
2227
+ replacement_multiline_string = multiline_comments[y].sub("=begin","/*\n")
2228
+
2229
+ replacement_multiline_string = replacement_multiline_string.sub("=end","\n*/")
2230
+
2231
+ ignorable_keywords.each_with_index do |keyword,index|
2232
+
2233
+ if replacement_multiline_string.index(keyword) != nil
2234
+
2235
+ replacement_multiline_string = replacement_multiline_string.sub(keyword.inspect[1...-1],dummy_replacement_words[index])
2236
+
2237
+ end
2238
+
2239
+ end
2240
+
2241
+ file_contents_as_string = file_contents_as_string.sub(current_multiline_comment,replacement_multiline_string)
2242
+
2243
+ multi_line_comment_counter += 1
2244
+
2245
+ end
2246
+
2247
+ file_id = open(temporary_nila_file, 'w')
2248
+
2249
+ file_id.write(file_contents_as_string)
2250
+
2251
+ file_id.close()
2252
+
2253
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
2254
+
2255
+ line_by_line_contents
2256
+
2257
+ end
2258
+
2259
+ def pretty_print_javascript(javascript_file_contents,temporary_nila_file,comments)
1925
2260
 
1926
2261
  def reset_tabs(input_file_contents)
1927
2262
 
@@ -1981,22 +2316,6 @@ def compile(input_file_path,*output_file_name)
1981
2316
 
1982
2317
  end
1983
2318
 
1984
- def previous_formatting(input_string,tab_counter,temporary_nila_file)
1985
-
1986
- string_as_array = convert_string_to_array(input_string,temporary_nila_file)
1987
-
1988
- modified_array = []
1989
-
1990
- string_as_array.each do |line|
1991
-
1992
- modified_array << " "*tab_counter + line
1993
-
1994
- end
1995
-
1996
- return modified_array.join
1997
-
1998
- end
1999
-
2000
2319
  def fix_newlines(file_contents)
2001
2320
 
2002
2321
  def extract_blocks(file_contents)
@@ -2216,10 +2535,28 @@ def compile(input_file_path,*output_file_name)
2216
2535
 
2217
2536
  end
2218
2537
 
2538
+ def replace_ignored_words(input_string)
2539
+
2540
+ ignorable_keywords = [/if/,/while/,/function/]
2541
+
2542
+ dummy_replacement_words = ["eeuuff","whaalesskkey","conffoolotion"]
2543
+
2544
+ dummy_replacement_words.each_with_index do |word,index|
2545
+
2546
+ input_string = input_string.sub(word,ignorable_keywords[index].inspect[1...-1])
2547
+
2548
+ end
2549
+
2550
+ return input_string
2551
+
2552
+ end
2553
+
2219
2554
  javascript_regexp = /(if |while |function |function\()/
2220
2555
 
2221
2556
  javascript_file_contents = javascript_file_contents.collect {|element| element.sub("Euuf","if")}
2222
2557
 
2558
+ javascript_file_contents = javascript_file_contents.collect {|element| element.sub("whaaleskey","while")}
2559
+
2223
2560
  javascript_file_contents = reset_tabs(javascript_file_contents)
2224
2561
 
2225
2562
  starting_locations = []
@@ -2320,8 +2657,6 @@ def compile(input_file_path,*output_file_name)
2320
2657
 
2321
2658
  end
2322
2659
 
2323
-
2324
-
2325
2660
  remaining_file_contents = ["(function() {\n",remaining_file_contents,"\n}).call(this);"].flatten
2326
2661
 
2327
2662
  joined_file_contents = remaining_file_contents.join
@@ -2342,7 +2677,6 @@ def compile(input_file_path,*output_file_name)
2342
2677
 
2343
2678
  end
2344
2679
 
2345
-
2346
2680
  file_id = open(temporary_nila_file, 'w')
2347
2681
 
2348
2682
  file_id.write(joined_file_contents)
@@ -2355,6 +2689,8 @@ def compile(input_file_path,*output_file_name)
2355
2689
 
2356
2690
  line_by_line_contents = fix_syntax_indentation(line_by_line_contents)
2357
2691
 
2692
+ line_by_line_contents = line_by_line_contents.collect {|element| replace_ignored_words(element)}
2693
+
2358
2694
  return line_by_line_contents
2359
2695
 
2360
2696
  end
@@ -2437,7 +2773,7 @@ def compile(input_file_path,*output_file_name)
2437
2773
 
2438
2774
  file_contents = compile_comments(file_contents,comments,temp_file)
2439
2775
 
2440
- file_contents = pretty_print_javascript(file_contents,temp_file)
2776
+ file_contents = pretty_print_javascript(file_contents,temp_file,comments)
2441
2777
 
2442
2778
  file_contents = compile_operators(file_contents)
2443
2779
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nilac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4.1.3
4
+ version: 0.0.4.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-19 00:00:00.000000000 Z
12
+ date: 2013-07-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: shark
@@ -68,6 +68,7 @@ files:
68
68
  - shark/features/method_multiple_return.feature
69
69
  - shark/features/multiple_variable_initialization.feature
70
70
  - shark/features/regular_if.feature
71
+ - shark/features/regular_while.feature
71
72
  - shark/features/unless_until.feature
72
73
  - shark/test_files/array_string_indexing.nila
73
74
  - shark/test_files/correct.js
@@ -76,6 +77,7 @@ files:
76
77
  - shark/test_files/correct_initialization.js
77
78
  - shark/test_files/correct_multiple_return.js
78
79
  - shark/test_files/correct_regular_if.js
80
+ - shark/test_files/correct_regular_while.js
79
81
  - shark/test_files/correct_return.js
80
82
  - shark/test_files/correct_single_return.js
81
83
  - shark/test_files/correct_unless_until.js
@@ -86,6 +88,7 @@ files:
86
88
  - shark/test_files/no_return.nila
87
89
  - shark/test_files/perfect.js
88
90
  - shark/test_files/regular_if.nila
91
+ - shark/test_files/regular_while.nila
89
92
  - shark/test_files/simple.nila
90
93
  - shark/test_files/single_return.nila
91
94
  - shark/test_files/unless_until.nila