nilac 0.0.4.0.1 → 0.0.4.1

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.
data/bin/nilac CHANGED
@@ -329,29 +329,37 @@ def compile(input_file_path,*output_file_name)
329
329
 
330
330
  finder_location = current_location
331
331
 
332
- while current_string.index(nila_regexp) == nil
332
+ begin
333
333
 
334
- finder_location -= 1
334
+ while current_string.index(nila_regexp) == nil
335
335
 
336
- current_string = modified_file_contents[finder_location]
336
+ finder_location -= 1
337
337
 
338
- end
338
+ current_string = modified_file_contents[finder_location]
339
+
340
+ end
341
+
342
+ code_block_begin = finder_location
339
343
 
340
- code_block_begin = finder_location
344
+ code_block_end = current_location
341
345
 
342
- code_block_end = current_location
346
+ start_blocks << code_block_begin
343
347
 
344
- start_blocks << code_block_begin
348
+ end_blocks << code_block_end
345
349
 
346
- end_blocks << code_block_end
350
+ code_block_begin_string_split = modified_file_contents[code_block_begin].split(" ")
347
351
 
348
- code_block_begin_string_split = modified_file_contents[code_block_begin].split(" ")
352
+ code_block_begin_string_split[0] = code_block_begin_string_split[0].reverse
349
353
 
350
- code_block_begin_string_split[0] = code_block_begin_string_split[0].reverse
354
+ code_block_begin_string = code_block_begin_string_split.join(" ")
351
355
 
352
- code_block_begin_string = code_block_begin_string_split.join(" ")
356
+ modified_file_contents[code_block_begin] = code_block_begin_string
353
357
 
354
- modified_file_contents[code_block_begin] = code_block_begin_string
358
+ rescue NoMethodError
359
+
360
+ puts "Function compilation failed!"
361
+
362
+ end
355
363
 
356
364
  end
357
365
 
@@ -401,6 +409,7 @@ def compile(input_file_path,*output_file_name)
401
409
 
402
410
  end
403
411
 
412
+
404
413
  file_id = open(temporary_nila_file, 'w')
405
414
 
406
415
  file_id.write(joined_file_contents)
@@ -478,8 +487,6 @@ def compile(input_file_path,*output_file_name)
478
487
 
479
488
  def parse_default_values(input_function_definition)
480
489
 
481
- puts input_function_definition
482
-
483
490
  split1,split2 = input_function_definition.split("(")
484
491
 
485
492
  split2,split3 = split2.split(")")
@@ -548,13 +555,17 @@ def compile(input_file_path,*output_file_name)
548
555
 
549
556
  def get_variables(input_file_contents,temporary_nila_file)
550
557
 
551
- #This method is solely focused on getting a list of variables to be declared.
552
- #Since Javascript is a dynamic language, Nila doesn't have to worry about following up on those variables.
558
+ variables = []
553
559
 
554
- #Semicolons are required in Javascript for successful compilation. So this method adds semicolons at the end of each
555
- #variable usage statements.
560
+ input_file_contents = input_file_contents.collect {|element| element.gsub("+=","plusequal")}
556
561
 
557
- variables = []
562
+ input_file_contents = input_file_contents.collect {|element| element.gsub("-=","minusequal")}
563
+
564
+ input_file_contents = input_file_contents.collect {|element| element.gsub("*=","multiequal")}
565
+
566
+ input_file_contents = input_file_contents.collect {|element| element.gsub("/=","divequal")}
567
+
568
+ input_file_contents = input_file_contents.collect {|element| element.gsub("%=","modequal")}
558
569
 
559
570
  for x in 0...input_file_contents.length
560
571
 
@@ -602,6 +613,16 @@ def compile(input_file_path,*output_file_name)
602
613
 
603
614
  end
604
615
 
616
+ line_by_line_contents = line_by_line_contents.collect {|element| element.gsub("plusequal","+=")}
617
+
618
+ line_by_line_contents = line_by_line_contents.collect {|element| element.gsub("minusequal","-=")}
619
+
620
+ line_by_line_contents = line_by_line_contents.collect {|element| element.gsub("multiequal","*=")}
621
+
622
+ line_by_line_contents = line_by_line_contents.collect {|element| element.gsub("divequal","/=")}
623
+
624
+ line_by_line_contents = line_by_line_contents.collect {|element| element.gsub("modequal","%=")}
625
+
605
626
  return variables.uniq,line_by_line_contents
606
627
 
607
628
  end
@@ -732,13 +753,6 @@ def compile(input_file_path,*output_file_name)
732
753
 
733
754
  def compile_array_indexing(input_file_contents)
734
755
 
735
- #Nila allows two different kinds of indexing operations on arrays and strings. They are
736
-
737
- #1. Using Ranges => numbers[0...5]
738
- #2. Using Start and End Indexes => numbers[0,5]
739
-
740
- #This method implements this Nila feature
741
-
742
756
  possible_indexing_operation = input_file_contents.dup.reject {|element| !element.include?"[" and !element.include?"]"}
743
757
 
744
758
  possible_range_indexing = possible_indexing_operation.reject {|element| !element.include?".."}
@@ -980,13 +994,21 @@ def compile(input_file_path,*output_file_name)
980
994
 
981
995
  rejected_array = reversed_input_array.reject {|content| content.lstrip.eql?("")}
982
996
 
983
- rejected_array = rejected_array.reject {|content| content.strip.eql? "}"}
997
+ rejected_array = rejected_array[1..-1]
998
+
999
+ if !rejected_array[0].strip.eql?("}")
984
1000
 
985
- last_statement = rejected_array[0]
1001
+ if !rejected_array[0].strip.eql?("end")
986
1002
 
987
- replacement_string = "return #{last_statement.lstrip}"
1003
+ last_statement = rejected_array[0]
988
1004
 
989
- input_array[input_array.index(last_statement)] = replacement_string
1005
+ replacement_string = "return #{last_statement.lstrip}"
1006
+
1007
+ input_array[input_array.index(last_statement)] = replacement_string
1008
+
1009
+ end
1010
+
1011
+ end
990
1012
 
991
1013
  end
992
1014
 
@@ -1272,7 +1294,7 @@ def compile(input_file_path,*output_file_name)
1272
1294
 
1273
1295
  "puts" => "console.log",
1274
1296
 
1275
- "print" => "console.log"
1297
+ "print" => "process.stdout.write"
1276
1298
 
1277
1299
  }
1278
1300
 
@@ -1338,30 +1360,38 @@ def compile(input_file_path,*output_file_name)
1338
1360
 
1339
1361
  end
1340
1362
 
1341
- input_file_contents[-1] = input_file_contents[-1] + "\n" if !input_file_contents[-1].include?("\n")
1363
+ begin
1342
1364
 
1343
- joined_file_contents = input_file_contents.join
1365
+ input_file_contents[-1] = input_file_contents[-1] + "\n" if !input_file_contents[-1].include?("\n")
1366
+
1367
+ joined_file_contents = input_file_contents.join
1344
1368
 
1345
- function_names.each do |list_of_functions|
1369
+ function_names.each do |list_of_functions|
1346
1370
 
1347
- list_of_functions.each do |function|
1371
+ list_of_functions.each do |function|
1348
1372
 
1349
- matching_strings = extract(joined_file_contents,function+" ","\n")
1373
+ matching_strings = extract(joined_file_contents,function+" ","\n")
1350
1374
 
1351
- matching_strings.each do |string|
1375
+ matching_strings.each do |string|
1352
1376
 
1353
- modified_string = string.dup
1377
+ modified_string = string.dup
1354
1378
 
1355
- modified_string = modified_string.sub(function+" ",function+"(")
1379
+ modified_string = modified_string.sub(function+" ",function+"(")
1356
1380
 
1357
- modified_string = modified_string.sub("\n",")\n")
1381
+ modified_string = modified_string.sub("\n",")\n")
1358
1382
 
1359
- joined_file_contents = joined_file_contents.sub(string,modified_string)
1383
+ joined_file_contents = joined_file_contents.sub(string,modified_string)
1384
+
1385
+ end
1360
1386
 
1361
1387
  end
1362
1388
 
1363
1389
  end
1364
1390
 
1391
+ rescue NoMethodError
1392
+
1393
+ puts "Whitespace delimitation exited with errors!"
1394
+
1365
1395
  end
1366
1396
 
1367
1397
  file_id = open(temporary_nila_file, 'w')
@@ -1384,9 +1414,9 @@ def compile(input_file_path,*output_file_name)
1384
1414
 
1385
1415
  def compile_inline_conditionals(input_file_contents,temporary_nila_file)
1386
1416
 
1387
- conditionals = [/( if )/,/( while )/]
1417
+ conditionals = [/( if )/,/( while )/,/( unless )/,/( until )/]
1388
1418
 
1389
- plain_conditionals = [" if "," while "]
1419
+ plain_conditionals = [" if "," while "," unless "," until "]
1390
1420
 
1391
1421
  joined_file_contents = input_file_contents.join
1392
1422
 
@@ -1394,7 +1424,7 @@ def compile(input_file_path,*output_file_name)
1394
1424
 
1395
1425
  conditionals.each_with_index do |regex,index|
1396
1426
 
1397
- matching_lines = input_file_contents.reject {|content| !content.index(regex)}
1427
+ matching_lines = input_file_contents.reject {|content| content.index(regex).nil?}
1398
1428
 
1399
1429
  matching_lines.each do |line|
1400
1430
 
@@ -1402,11 +1432,19 @@ def compile(input_file_path,*output_file_name)
1402
1432
 
1403
1433
  if index == 0
1404
1434
 
1405
- output_statement = "if (#{line_split[1].lstrip.rstrip}) {\n\n#{line_split[0]}\n}\n"
1435
+ output_statement = "if (#{line_split[1].lstrip.rstrip.gsub("?","")}) {\n\n#{line_split[0]}\n}\n"
1406
1436
 
1407
1437
  elsif index == 1
1408
1438
 
1409
- output_statement = "while (#{line_split[1].lstrip.rstrip}) {\n\n#{line_split[0]}\n}\n"
1439
+ output_statement = "while (#{line_split[1].lstrip.rstrip.gsub("?","")}) {\n\n#{line_split[0]}\n}\n"
1440
+
1441
+ elsif index == 2
1442
+
1443
+ output_statement = "if (!(#{line_split[1].lstrip.rstrip.gsub("?","")})) {\n\n#{line_split[0]}\n}\n"
1444
+
1445
+ elsif index == 3
1446
+
1447
+ output_statement = "while (!(#{line_split[1].lstrip.rstrip.gsub("?","")})) {\n\n#{line_split[0]}\n}\n"
1410
1448
 
1411
1449
  end
1412
1450
 
@@ -1428,7 +1466,263 @@ def compile(input_file_path,*output_file_name)
1428
1466
 
1429
1467
  end
1430
1468
 
1431
- line_by_line_contents = compile_inline_conditionals(input_file_contents,temporary_nila_file)
1469
+ def compile_regular_if(input_file_contents,temporary_nila_file)
1470
+
1471
+ def convert_string_to_array(input_string,temporary_nila_file)
1472
+
1473
+ file_id = open(temporary_nila_file, 'w')
1474
+
1475
+ file_id.write(input_string)
1476
+
1477
+ file_id.close()
1478
+
1479
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
1480
+
1481
+ return line_by_line_contents
1482
+
1483
+ end
1484
+
1485
+ def extract_if_blocks(if_statement_indexes,input_file_contents)
1486
+
1487
+ possible_if_blocks = []
1488
+
1489
+ if_block_counter = 0
1490
+
1491
+ extracted_blocks = []
1492
+
1493
+ controlregexp = /(if |while |def )/
1494
+
1495
+ rejectionregexp = /( if | while )/
1496
+
1497
+ for x in 0...if_statement_indexes.length-1
1498
+
1499
+ possible_if_blocks << input_file_contents[if_statement_indexes[x]..if_statement_indexes[x+1]]
1500
+
1501
+ end
1502
+
1503
+ end_counter = 0
1504
+
1505
+ end_index = []
1506
+
1507
+ current_block = []
1508
+
1509
+ possible_if_blocks.each_with_index do |block|
1510
+
1511
+ current_block += block
1512
+
1513
+ current_block.each_with_index do |line,index|
1514
+
1515
+ if line.lstrip.eql? "end\n"
1516
+
1517
+ end_counter += 1
1518
+
1519
+ end_index << index
1520
+
1521
+ end
1522
+
1523
+ end
1524
+
1525
+ if end_counter > 0
1526
+
1527
+ until end_index.empty?
1528
+
1529
+ array_extract = current_block[0..end_index[0]].reverse
1530
+
1531
+ index_counter = 0
1532
+
1533
+ array_extract.each_with_index do |line|
1534
+
1535
+ break if (line.lstrip.index(controlregexp) != nil and line.lstrip.index(rejectionregexp).nil?)
1536
+
1537
+ index_counter += 1
1538
+
1539
+ end
1540
+
1541
+ block_extract = array_extract[0..index_counter].reverse
1542
+
1543
+ extracted_blocks << block_extract
1544
+
1545
+ block_start = current_block.index(block_extract[0])
1546
+
1547
+ block_end = current_block.index(block_extract[-1])
1548
+
1549
+ current_block[block_start..block_end] = "--ifblock#{if_block_counter}"
1550
+
1551
+ if_block_counter += 1
1552
+
1553
+ end_counter = 0
1554
+
1555
+ end_index = []
1556
+
1557
+ current_block.each_with_index do |line,index|
1558
+
1559
+ if line.lstrip.eql? "end\n"
1560
+
1561
+ end_counter += 1
1562
+
1563
+ end_index << index
1564
+
1565
+ end
1566
+
1567
+ end
1568
+
1569
+ end
1570
+
1571
+ end
1572
+
1573
+ end
1574
+
1575
+ return current_block,extracted_blocks
1576
+
1577
+ end
1578
+
1579
+ def compile_if_syntax(input_block)
1580
+
1581
+ starting_line = input_block[0]
1582
+
1583
+ starting_line = starting_line + "\n" if starting_line.lstrip == starting_line
1584
+
1585
+ junk,condition = starting_line.split("if")
1586
+
1587
+ input_block[0] = "Euuf (#{condition.lstrip.rstrip.gsub("?","")}) {\n"
1588
+
1589
+ input_block[-1] = input_block[-1].lstrip.sub("end","}")
1590
+
1591
+ elsif_statements = input_block.reject {|element| !element.include?("elsuf")}
1592
+
1593
+ elsif_statements.each do |statement|
1594
+
1595
+ junk,condition = statement.split("elsuf")
1596
+
1597
+ input_block[input_block.index(statement)] = "} elsuf (#{condition.lstrip.rstrip.gsub("?","")}) {\n"
1598
+
1599
+ end
1600
+
1601
+ else_statements = input_block.reject {|element| !element.include?("else")}
1602
+
1603
+ else_statements.each do |statement|
1604
+
1605
+ input_block[input_block.index(statement)] = "} else {\n"
1606
+
1607
+ end
1608
+
1609
+ return input_block
1610
+
1611
+ end
1612
+
1613
+ input_file_contents = input_file_contents.collect {|element| element.sub("elsif","elsuf")}
1614
+
1615
+ possible_if_statements = input_file_contents.reject {|element| !element.include?("if")}
1616
+
1617
+ possible_if_statements = possible_if_statements.reject {|element| element.include?("else")}
1618
+
1619
+ possible_if_statements = possible_if_statements.reject {|element| element.lstrip.include?(" if ")}
1620
+
1621
+ if !possible_if_statements.empty?
1622
+
1623
+ if_statement_indexes = []
1624
+
1625
+ possible_if_statements.each do |statement|
1626
+
1627
+ if_statement_indexes << input_file_contents.dup.each_index.select {|index| input_file_contents[index] == statement}
1628
+
1629
+ end
1630
+
1631
+ if_statement_indexes = if_statement_indexes.flatten + [-1]
1632
+
1633
+ controlregexp = /(while |def )/
1634
+
1635
+ modified_input_contents,extracted_statements = extract_if_blocks(if_statement_indexes,input_file_contents.clone)
1636
+
1637
+ joined_blocks = extracted_statements.collect {|element| element.join}
1638
+
1639
+ if_statements = joined_blocks.reject {|element| element.index(controlregexp) != nil}
1640
+
1641
+ rejected_elements = joined_blocks - if_statements
1642
+
1643
+ rejected_elements_index = []
1644
+
1645
+ rejected_elements.each do |element|
1646
+
1647
+ rejected_elements_index << joined_blocks.each_index.select {|index| joined_blocks[index] == element}
1648
+
1649
+ end
1650
+
1651
+ if_blocks_index = (0...extracted_statements.length).to_a
1652
+
1653
+ rejected_elements_index = rejected_elements_index.flatten
1654
+
1655
+ if_blocks_index -= rejected_elements_index
1656
+
1657
+ modified_if_statements = if_statements.collect {|string| convert_string_to_array(string,temporary_nila_file)}
1658
+
1659
+ modified_if_statements = modified_if_statements.collect {|block| compile_if_syntax(block)}.reverse
1660
+
1661
+ if_blocks_index = if_blocks_index.collect {|element| "--ifblock#{element}"}.reverse
1662
+
1663
+ rejected_elements_index = rejected_elements_index.collect {|element| "--ifblock#{element}"}.reverse
1664
+
1665
+ rejected_elements = rejected_elements.reverse
1666
+
1667
+ joined_file_contents = modified_input_contents.join
1668
+
1669
+ until if_blocks_index.empty? and rejected_elements_index.empty?
1670
+
1671
+ if !if_blocks_index.empty?
1672
+
1673
+ if joined_file_contents.include?(if_blocks_index[0])
1674
+
1675
+ joined_file_contents = joined_file_contents.sub(if_blocks_index[0],modified_if_statements[0].join)
1676
+
1677
+ if_blocks_index.delete_at(0)
1678
+
1679
+ modified_if_statements.delete_at(0)
1680
+
1681
+ else
1682
+
1683
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0],rejected_elements[0])
1684
+
1685
+ rejected_elements_index.delete_at(0)
1686
+
1687
+ rejected_elements.delete_at(0)
1688
+
1689
+ end
1690
+
1691
+ else
1692
+
1693
+ joined_file_contents = joined_file_contents.sub(rejected_elements_index[0],rejected_elements[0])
1694
+
1695
+ rejected_elements_index.delete_at(0)
1696
+
1697
+ rejected_elements.delete_at(0)
1698
+
1699
+ end
1700
+
1701
+ end
1702
+
1703
+ else
1704
+
1705
+ joined_file_contents = input_file_contents.join
1706
+
1707
+ end
1708
+
1709
+ file_id = open(temporary_nila_file, 'w')
1710
+
1711
+ file_id.write(joined_file_contents)
1712
+
1713
+ file_id.close()
1714
+
1715
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
1716
+
1717
+ return line_by_line_contents
1718
+
1719
+ end
1720
+
1721
+ file_contents = compile_regular_if(input_file_contents,temporary_nila_file)
1722
+
1723
+ file_contents = compile_inline_conditionals(file_contents,temporary_nila_file)
1724
+
1725
+ return file_contents
1432
1726
 
1433
1727
  end
1434
1728
 
@@ -1696,6 +1990,10 @@ def compile(input_file_path,*output_file_name)
1696
1990
 
1697
1991
  puts "Whitespace was left unfixed!"
1698
1992
 
1993
+ rescue ArgumentError
1994
+
1995
+ puts "Whitespace was left unfixed!"
1996
+
1699
1997
  end
1700
1998
 
1701
1999
  return modified_file_contents,code_blocks
@@ -1733,141 +2031,254 @@ def compile(input_file_path,*output_file_name)
1733
2031
 
1734
2032
  end
1735
2033
 
1736
- javascript_regexp = /(if |while |function |function\()/
2034
+ def roll_blocks(input_file_contents,code_block_starting_locations)
1737
2035
 
1738
- locations = []
2036
+ if !code_block_starting_locations.empty?
1739
2037
 
1740
- javascript_file_contents = reset_tabs(javascript_file_contents)
2038
+ controlregexp = /(if |while |function |function\()/
1741
2039
 
1742
- joined_file_contents = javascript_file_contents.join
2040
+ code_block_starting_locations = [0,code_block_starting_locations,-1].flatten
1743
2041
 
1744
- code_block_starting_locations = find_all_matching_indices(joined_file_contents,javascript_regexp)
2042
+ possible_blocks = []
1745
2043
 
1746
- code_block_ending_locations = find_all_matching_indices(joined_file_contents,"}")
2044
+ block_counter = 0
1747
2045
 
1748
- combined_location = [code_block_starting_locations,code_block_ending_locations.dup].flatten.sort
2046
+ extracted_blocks = []
1749
2047
 
1750
- self_invoking_function_extract = joined_file_contents[code_block_starting_locations[0]..code_block_ending_locations[-1]]
2048
+ for x in 0...code_block_starting_locations.length-1
1751
2049
 
1752
- self_invoking_function_array = convert_string_to_array(self_invoking_function_extract,temporary_nila_file)
2050
+ possible_blocks << input_file_contents[code_block_starting_locations[x]..code_block_starting_locations[x+1]]
1753
2051
 
1754
- combined_location.delete_at(0); combined_location.delete_at(-1); code_block_ending_locations.delete_at(-1); code_block_starting_locations.delete_at(0)
2052
+ end
1755
2053
 
1756
- modified_self_invoking_array = self_invoking_function_array.dup
2054
+ end_counter = 0
1757
2055
 
1758
- for x in 1...self_invoking_function_array.length-1
2056
+ end_index = []
1759
2057
 
1760
- modified_self_invoking_array[x] = " " + self_invoking_function_array[x]
2058
+ current_block = []
1761
2059
 
1762
- end
2060
+ possible_blocks.each_with_index do |block|
1763
2061
 
1764
- modified_self_invoking_array[-1] = "\n\n" + modified_self_invoking_array[-1]
2062
+ current_block += block
1765
2063
 
1766
- nested_elements = []
2064
+ current_block.each_with_index do |line,index|
1767
2065
 
1768
- nested_indices = []
2066
+ if line.lstrip.eql? "}\n"
1769
2067
 
1770
- modified_starting_locations = code_block_starting_locations.dup
2068
+ end_counter += 1
1771
2069
 
1772
- while code_block_ending_locations.length > 0
2070
+ end_index << index
1773
2071
 
1774
- matching_location = combined_location[combined_location.index(code_block_ending_locations[0])-1]
2072
+ end
1775
2073
 
1776
- combined_location.delete(matching_location)
2074
+ end
1777
2075
 
1778
- location_among_start_locations = code_block_starting_locations.index(matching_location)
2076
+ if end_counter > 0
1779
2077
 
1780
- if location_among_start_locations == 0
2078
+ until end_index.empty?
1781
2079
 
1782
- locations << [[matching_location,combined_location[combined_location.index(code_block_ending_locations[0])]]]
2080
+ array_extract = current_block[0..end_index[0]].reverse
1783
2081
 
1784
- else
2082
+ index_counter = 0
1785
2083
 
1786
- nested_elements << [matching_location,combined_location[combined_location.index(code_block_ending_locations[0])]]
2084
+ array_extract.each_with_index do |line|
1787
2085
 
1788
- nested_indices << modified_starting_locations.index(matching_location)
2086
+ break if line.index(controlregexp) != nil
1789
2087
 
1790
- end
2088
+ index_counter += 1
1791
2089
 
1792
- combined_location.delete(code_block_ending_locations[0])
2090
+ end
1793
2091
 
1794
- code_block_ending_locations.delete_at(0)
2092
+ block_extract = array_extract[0..index_counter].reverse
1795
2093
 
1796
- code_block_starting_locations.delete(matching_location)
2094
+ extracted_blocks << block_extract
1797
2095
 
1798
- end
2096
+ block_start = current_block.index(block_extract[0])
1799
2097
 
1800
- nested_indices.each_with_index do |loc,index|
2098
+ block_end = current_block.index(block_extract[-1])
1801
2099
 
1802
- begin
2100
+ current_block[block_start..block_end] = "--block#{block_counter}"
1803
2101
 
1804
- locations[loc-1] << nested_elements[index]
2102
+ block_counter += 1
1805
2103
 
1806
- rescue NoMethodError
2104
+ end_counter = 0
1807
2105
 
1808
- puts "The pretty printing process exited with errors!"
2106
+ end_index = []
2107
+
2108
+ current_block.each_with_index do |line,index|
2109
+
2110
+ if line.lstrip.eql? "}\n"
2111
+
2112
+ end_counter += 1
2113
+
2114
+ end_index << index
2115
+
2116
+ end
2117
+
2118
+ end
2119
+
2120
+ end
2121
+
2122
+ end
2123
+
2124
+ end
2125
+
2126
+ return current_block,extracted_blocks
2127
+
2128
+ else
2129
+
2130
+ return input_file_contents,[]
1809
2131
 
1810
2132
  end
1811
2133
 
2134
+ end
2135
+
2136
+ def fix_syntax_indentation(input_file_contents)
1812
2137
 
2138
+ fixableregexp = /(else |elsuf )/
2139
+
2140
+ need_fixes = input_file_contents.reject {|line| line.index(fixableregexp).nil?}
2141
+
2142
+ need_fixes.each do |fix|
2143
+
2144
+ input_file_contents[input_file_contents.index(fix)] = input_file_contents[input_file_contents.index(fix)].sub(" ","")
2145
+
2146
+ end
2147
+
2148
+ return input_file_contents
1813
2149
 
1814
2150
  end
1815
2151
 
1816
- modified_locations = []
2152
+ javascript_regexp = /(if |while |function |function\()/
1817
2153
 
1818
- locations.each do |loc|
2154
+ javascript_file_contents = javascript_file_contents.collect {|element| element.sub("Euuf","if")}
2155
+
2156
+ javascript_file_contents = reset_tabs(javascript_file_contents)
1819
2157
 
1820
- modified_locations << loc.sort
2158
+ starting_locations = []
2159
+
2160
+ javascript_file_contents.each_with_index do |line,index|
2161
+
2162
+ if line.index(javascript_regexp) != nil
2163
+
2164
+ starting_locations << index
2165
+
2166
+ end
1821
2167
 
1822
2168
  end
1823
2169
 
1824
- modified_joined_file_contents = joined_file_contents.dup
2170
+ remaining_file_contents,blocks = roll_blocks(javascript_file_contents,starting_locations)
1825
2171
 
1826
- modified_joined_file_contents = modified_joined_file_contents.sub(self_invoking_function_extract,modified_self_invoking_array.join)
2172
+ joined_file_contents = ""
1827
2173
 
1828
- modified_locations.each do |location|
2174
+ if !blocks.empty?
1829
2175
 
1830
- soft_tabs_counter = 2
2176
+ remaining_file_contents = remaining_file_contents.collect {|element| " " + element}
1831
2177
 
1832
- location.each do |sublocation|
2178
+ main_blocks = remaining_file_contents.reject {|element| !element.include?("--block")}
1833
2179
 
1834
- string_extract = joined_file_contents[sublocation[0]..sublocation[1]]
2180
+ main_block_numbers = main_blocks.collect {|element| element.split("--block")[1]}
1835
2181
 
1836
- string_extract_array = convert_string_to_array(string_extract,temporary_nila_file)
2182
+ modified_blocks = main_blocks.dup
1837
2183
 
1838
- if soft_tabs_counter > 1
2184
+ soft_tabs = " "
1839
2185
 
1840
- string_extract_array[0] = " "*(soft_tabs_counter-1) + string_extract_array[0]
2186
+ for x in (0...main_blocks.length)
1841
2187
 
1842
- string_extract_array[-1] = " "*(soft_tabs_counter-1) + string_extract_array[-1]
2188
+ soft_tabs_counter = 1
1843
2189
 
1844
- end
2190
+ current_block = blocks[main_block_numbers[x].to_i]
1845
2191
 
1846
- for x in 1...string_extract_array.length-1
2192
+ current_block = [soft_tabs + current_block[0]] + current_block[1...-1] + [soft_tabs*(soft_tabs_counter)+current_block[-1]]
1847
2193
 
1848
- string_extract_array[x] = " "*soft_tabs_counter + string_extract_array[x]
2194
+ soft_tabs_counter += 1
1849
2195
 
1850
- end
2196
+ current_block = [current_block[0]] + current_block[1...-1].collect {|element| soft_tabs*(soft_tabs_counter)+element} + [current_block[-1]]
1851
2197
 
1852
- if soft_tabs_counter > 1
2198
+ nested_block = current_block.reject {|row| !row.include?("--block")}
1853
2199
 
1854
- modified_joined_file_contents = modified_joined_file_contents.sub(previous_formatting(string_extract,soft_tabs_counter-1,temporary_nila_file),string_extract_array.join)
2200
+ nested_block = nested_block.collect {|element| element.split("--block")[1]}
1855
2201
 
1856
- else
2202
+ nested_block = nested_block.collect {|element| element.rstrip.to_i}
2203
+
2204
+ modified_nested_block = nested_block.clone
2205
+
2206
+ current_block = current_block.join
2207
+
2208
+ until modified_nested_block.empty?
2209
+
2210
+ nested_block.each do |block_index|
2211
+
2212
+ nested_block_contents = blocks[block_index]
2213
+
2214
+ nested_block_contents = nested_block_contents[0...-1] + [soft_tabs*(soft_tabs_counter)+nested_block_contents[-1]]
2215
+
2216
+ soft_tabs_counter += 1
2217
+
2218
+ nested_block_contents = [nested_block_contents[0]] + nested_block_contents[1...-1].collect {|element| soft_tabs*(soft_tabs_counter)+element} + [nested_block_contents[-1]]
2219
+
2220
+ nested_block_contents = nested_block_contents.reject {|element| element.gsub(" ","").eql?("")}
2221
+
2222
+ current_block = current_block.sub("--block#{block_index}",nested_block_contents.join)
2223
+
2224
+ blocks[block_index] = nested_block_contents
2225
+
2226
+ modified_nested_block.delete_at(0)
2227
+
2228
+ soft_tabs_counter -= 1
2229
+
2230
+ end
2231
+
2232
+ current_block = convert_string_to_array(current_block,temporary_nila_file)
2233
+
2234
+ nested_block = current_block.reject {|element| !element.include?("--block")}
2235
+
2236
+ nested_block = nested_block.collect {|element| element.split("--block")[1]}
2237
+
2238
+ nested_block = nested_block.collect {|element| element.rstrip.to_i}
1857
2239
 
1858
- modified_joined_file_contents = modified_joined_file_contents.sub(string_extract,string_extract_array.join)
2240
+ modified_nested_block = nested_block.clone
2241
+
2242
+ current_block = current_block.join
2243
+
2244
+ if !nested_block.empty?
2245
+
2246
+ soft_tabs_counter += 1
2247
+
2248
+ end
1859
2249
 
1860
2250
  end
1861
2251
 
1862
- soft_tabs_counter += 1
2252
+ modified_blocks[x] = current_block
1863
2253
 
1864
2254
  end
1865
2255
 
2256
+
2257
+
2258
+ remaining_file_contents = ["(function() {\n",remaining_file_contents,"\n}).call(this);"].flatten
2259
+
2260
+ joined_file_contents = remaining_file_contents.join
2261
+
2262
+ main_blocks.each_with_index do |block_id,index|
2263
+
2264
+ joined_file_contents = joined_file_contents.sub(block_id,modified_blocks[index])
2265
+
2266
+ end
2267
+
2268
+ else
2269
+
2270
+ remaining_file_contents = remaining_file_contents.collect {|element| " " + element}
2271
+
2272
+ remaining_file_contents = ["(function() {\n",remaining_file_contents,"\n}).call(this);"].flatten
2273
+
2274
+ joined_file_contents = remaining_file_contents.join
2275
+
1866
2276
  end
1867
2277
 
2278
+
1868
2279
  file_id = open(temporary_nila_file, 'w')
1869
2280
 
1870
- file_id.write(modified_joined_file_contents)
2281
+ file_id.write(joined_file_contents)
1871
2282
 
1872
2283
  file_id.close()
1873
2284
 
@@ -1875,30 +2286,31 @@ def compile(input_file_path,*output_file_name)
1875
2286
 
1876
2287
  line_by_line_contents = fix_newlines(line_by_line_contents)
1877
2288
 
2289
+ line_by_line_contents = fix_syntax_indentation(line_by_line_contents)
2290
+
1878
2291
  return line_by_line_contents
1879
2292
 
1880
2293
  end
1881
2294
 
1882
- def pretty_print_nila(input_file_contents)
2295
+ def compile_operators(input_file_contents)
1883
2296
 
1884
- #Implementation is pending
2297
+ input_file_contents = input_file_contents.collect {|element| element.sub(" and "," && ")}
1885
2298
 
1886
- end
2299
+ input_file_contents = input_file_contents.collect {|element| element.sub(" or "," || ")}
1887
2300
 
1888
- def static_analysis(input_file_contents)
2301
+ input_file_contents = input_file_contents.collect {|element| element.sub("==","===")}
1889
2302
 
1890
- #Implementation is pending
2303
+ input_file_contents = input_file_contents.collect {|element| element.sub("!=","!==")}
1891
2304
 
1892
- end
2305
+ input_file_contents = input_file_contents.collect {|element| element.sub("elsuf","else if")}
1893
2306
 
1894
- def create_self_invoking_function(input_file_contents)
2307
+ return input_file_contents
1895
2308
 
1896
- # A feature imported from Coffeescript. This makes all the function private by default
1897
- # and prevents global variables from leaking.
2309
+ end
1898
2310
 
1899
- modified_file_contents = ["(function() {\n",input_file_contents,"\n}).call(this);"].flatten
2311
+ def pretty_print_nila(input_file_contents)
1900
2312
 
1901
- return modified_file_contents
2313
+ #Implementation is pending
1902
2314
 
1903
2315
  end
1904
2316
 
@@ -1930,6 +2342,8 @@ def compile(input_file_path,*output_file_name)
1930
2342
 
1931
2343
  file_contents = compile_interpolated_strings(file_contents)
1932
2344
 
2345
+ file_contents = compile_conditional_structures(file_contents,temp_file)
2346
+
1933
2347
  file_contents = compile_arrays(file_contents)
1934
2348
 
1935
2349
  file_contents = compile_default_values(file_contents,temp_file)
@@ -1942,8 +2356,6 @@ def compile(input_file_path,*output_file_name)
1942
2356
 
1943
2357
  list_of_variables,file_contents = get_variables(file_contents,temp_file)
1944
2358
 
1945
- file_contents = compile_conditional_structures(file_contents,temp_file)
1946
-
1947
2359
  file_contents, function_names = compile_named_functions(file_contents,named_functions,nested_functions,temp_file)
1948
2360
 
1949
2361
  file_contents, ruby_functions = compile_custom_function_map(file_contents)
@@ -1958,10 +2370,10 @@ def compile(input_file_path,*output_file_name)
1958
2370
 
1959
2371
  file_contents = compile_comments(file_contents,comments,temp_file)
1960
2372
 
1961
- file_contents = create_self_invoking_function(file_contents)
1962
-
1963
2373
  file_contents = pretty_print_javascript(file_contents,temp_file)
1964
2374
 
2375
+ file_contents = compile_operators(file_contents)
2376
+
1965
2377
  output_javascript(file_contents,output_js_file,temp_file)
1966
2378
 
1967
2379
  puts "Compilation is successful!"
@@ -2028,7 +2440,7 @@ def find_file_path(input_path,file_extension)
2028
2440
 
2029
2441
  end
2030
2442
 
2031
- nilac_version = "0.0.4.0"
2443
+ nilac_version = "0.0.4.1"
2032
2444
 
2033
2445
  opts = Slop.parse do
2034
2446
  on :c, :compile=, 'Compile Nila File', as:Array, delimiter:":"