nilac 0.0.3.1 → 0.0.3.2

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.
Files changed (3) hide show
  1. data/bin/nilac +94 -131
  2. data/lib/nilac/version.rb +1 -1
  3. metadata +2 -2
data/bin/nilac CHANGED
@@ -22,42 +22,19 @@ def compile(input_file_path)
22
22
 
23
23
  end
24
24
 
25
+ def extract_parsable_file(input_file_contents)
25
26
 
26
- def extract_parsable_array(input_file_contents)
27
+ reversed_file_contents = input_file_contents.reverse
27
28
 
28
- #This method finds and removes the __END__ keyword and the following lines in a Nila file.
29
+ line_counter = 0
29
30
 
30
- modified_file_contents = input_file_contents.dup
31
-
32
- end_index = 0
33
-
34
- input_file_contents.each_with_index do |line,index|
31
+ while !reversed_file_contents[line_counter].strip.include?("__END__")
35
32
 
36
- if line.strip.eql?("__END__")
37
-
38
- end_index = index
39
-
40
- end
33
+ line_counter += 1
41
34
 
42
35
  end
43
36
 
44
- if end_index == 0
45
-
46
- output = modified_file_contents
47
-
48
- else
49
-
50
- output = modified_file_contents[0..end_index-1]
51
-
52
- while output[-1].eql?("\n")
53
-
54
- output.delete_at(-1)
55
-
56
- end
57
-
58
- end
59
-
60
- return output
37
+ return_contents = input_file_contents[0...-1*line_counter-1]
61
38
 
62
39
  end
63
40
 
@@ -156,8 +133,12 @@ def compile(input_file_path)
156
133
 
157
134
  file_id.write(modified_file_contents)
158
135
 
136
+ file_id2.write("//Written in Nila and compiled to Javascript. Have fun!\n\n")
137
+
159
138
  file_id.close()
160
139
 
140
+ file_id2.close()
141
+
161
142
  line_by_line_contents = read_file_line_by_line(temporary_nila_file)
162
143
 
163
144
  comments = multiline_comments.dup
@@ -194,7 +175,7 @@ def compile(input_file_path)
194
175
 
195
176
  replacement_line = modified_file_contents[index]
196
177
 
197
- replacement_line = replacement_line.split(";").join("\n\n")
178
+ replacement_line = replacement_line.split(";").join("\n\n") + "\n"
198
179
 
199
180
  modified_file_contents[index] = replacement_line
200
181
 
@@ -206,83 +187,6 @@ def compile(input_file_path)
206
187
 
207
188
  end
208
189
 
209
- def compile_heredoc_strings(input_file_contents,temporary_nila_file)
210
-
211
- #This method will compile all the Heredoc strings in Nila into pure
212
- #Javascript strings. Ruby has two types of Heredocs. Currently, Nila doesn't support both of them.
213
- #Here is an example of what Nila supports
214
-
215
- #long_passage = <<-ipsumtext
216
- #
217
- #Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
218
- #incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
219
- #exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
220
- #irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
221
- #pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
222
- #deserunt mollit anim id est laborum.
223
- #
224
- #ipsumtext
225
-
226
- def find_all_matching_indices(input_string,pattern)
227
-
228
- locations = []
229
-
230
- index = input_string.index(pattern)
231
-
232
- while index != nil
233
-
234
- locations << index
235
-
236
- index = input_string.index(pattern,index+1)
237
-
238
-
239
- end
240
-
241
- return locations
242
-
243
-
244
- end
245
-
246
- preserve_formatting = %w{ \n \t }
247
-
248
- joined_file_contents = input_file_contents.join
249
-
250
- modified_file_contents = joined_file_contents.dup
251
-
252
- heredoc_start_locations = find_all_matching_indices(joined_file_contents,"<<-")
253
-
254
- heredoc_start_locations.each do |location|
255
-
256
- string_extract = joined_file_contents[location..-1]
257
-
258
- end_of_first_line = string_extract.index("\n")
259
-
260
- name_of_heredoc = string_extract[3...end_of_first_line].rstrip
261
-
262
- heredoc_start,heredoc_end = find_all_matching_indices(string_extract,name_of_heredoc)
263
-
264
- heredoc = string_extract[heredoc_start..heredoc_end+name_of_heredoc.length-1]
265
-
266
- heredoc_content = heredoc.split(name_of_heredoc)
267
-
268
- heredoc_content = heredoc_content[1].lstrip.rstrip.gsub("\n","\\n").gsub("\t","\\t")
269
-
270
- modified_file_contents = modified_file_contents.sub(string_extract[heredoc_start-3..heredoc_end+name_of_heredoc.length-1],"\""+heredoc_content+"\"")
271
-
272
- end
273
-
274
- file_id = open(temporary_nila_file, 'w')
275
-
276
- file_id.write(modified_file_contents)
277
-
278
- file_id.close()
279
-
280
- line_by_line_contents = read_file_line_by_line(temporary_nila_file)
281
-
282
- return line_by_line_contents
283
-
284
- end
285
-
286
190
  def compile_interpolated_strings(input_file_contents)
287
191
 
288
192
  modified_file_contents = input_file_contents.dup
@@ -391,7 +295,7 @@ def compile(input_file_path)
391
295
 
392
296
  key_word_locations << x
393
297
 
394
- elsif current_row.include?("end\n")
298
+ elsif current_row.include?("end\n") || current_row.include?("end")
395
299
 
396
300
  end_locations << x
397
301
 
@@ -919,16 +823,18 @@ def compile(input_file_path)
919
823
  line_by_line_contents = read_file_line_by_line(temporary_nila_file)
920
824
 
921
825
  return line_by_line_contents,function_names
922
-
826
+
923
827
  end
924
828
 
925
829
  def compile_custom_function_map(input_file_contents)
926
830
 
927
- function_map = ["puts"]
831
+ function_map = ["puts","print"]
928
832
 
929
833
  function_map_replacements = {
930
834
 
931
- "puts" => "console.log"
835
+ "puts" => "console.log",
836
+
837
+ "print" => "console.log"
932
838
 
933
839
  }
934
840
 
@@ -1035,7 +941,53 @@ def compile(input_file_path)
1035
941
 
1036
942
  #1. If, Elsif, Else Statement
1037
943
 
944
+ def compile_inline_conditionals(input_file_contents,temporary_nila_file)
945
+
946
+ conditionals = [/( if )/,/( while )/]
947
+
948
+ plain_conditionals = [" if "," while "]
949
+
950
+ joined_file_contents = input_file_contents.join
951
+
952
+ output_statement = ""
953
+
954
+ conditionals.each_with_index do |regex,index|
955
+
956
+ matching_lines = input_file_contents.reject {|content| !content.index(regex)}
957
+
958
+ matching_lines.each do |line|
959
+
960
+ line_split = line.split(plain_conditionals[index])
961
+
962
+ if index == 0
963
+
964
+ output_statement = "if (#{line_split[1].lstrip.rstrip}) {\n\n#{line_split[0]}\n}\n"
965
+
966
+ elsif index == 1
967
+
968
+ output_statement = "while (#{line_split[1].lstrip.rstrip}) {\n\n#{line_split[0]}\n}\n"
969
+
970
+ end
971
+
972
+ joined_file_contents = joined_file_contents.sub(line,output_statement)
973
+
974
+ end
975
+
976
+ end
977
+
978
+ file_id = open(temporary_nila_file, 'w')
979
+
980
+ file_id.write(joined_file_contents)
981
+
982
+ file_id.close()
983
+
984
+ line_by_line_contents = read_file_line_by_line(temporary_nila_file)
985
+
986
+ return line_by_line_contents
987
+
988
+ end
1038
989
 
990
+ line_by_line_contents = compile_inline_conditionals(input_file_contents,temporary_nila_file)
1039
991
 
1040
992
  end
1041
993
 
@@ -1243,7 +1195,7 @@ def compile(input_file_path)
1243
1195
 
1244
1196
  end
1245
1197
 
1246
- javascript_regexp = /(function |function\()/
1198
+ javascript_regexp = /(if |while |function |function\()/
1247
1199
 
1248
1200
  locations = []
1249
1201
 
@@ -1257,8 +1209,6 @@ def compile(input_file_path)
1257
1209
 
1258
1210
  combined_location = [code_block_starting_locations,code_block_ending_locations.dup].flatten.sort
1259
1211
 
1260
- last_matching_location = 0
1261
-
1262
1212
  self_invoking_function_extract = joined_file_contents[code_block_starting_locations[0]..code_block_ending_locations[-1]]
1263
1213
 
1264
1214
  self_invoking_function_array = convert_string_to_array(self_invoking_function_extract,temporary_nila_file)
@@ -1275,29 +1225,43 @@ def compile(input_file_path)
1275
1225
 
1276
1226
  modified_self_invoking_array[-1] = "\n\n" + modified_self_invoking_array[-1]
1277
1227
 
1228
+ nested_elements = []
1229
+
1230
+ nested_indices = []
1231
+
1232
+ modified_starting_locations = code_block_starting_locations.dup
1233
+
1278
1234
  while code_block_ending_locations.length > 0
1279
1235
 
1280
1236
  matching_location = combined_location[combined_location.index(code_block_ending_locations[0])-1]
1281
1237
 
1238
+ combined_location.delete(matching_location)
1239
+
1282
1240
  location_among_start_locations = code_block_starting_locations.index(matching_location)
1283
1241
 
1284
- locations << [[matching_location,combined_location[combined_location.index(code_block_ending_locations[0])]]]
1242
+ if location_among_start_locations == 0
1285
1243
 
1286
- code_block_ending_locations.delete_at(0)
1244
+ locations << [[matching_location,combined_location[combined_location.index(code_block_ending_locations[0])]]]
1287
1245
 
1288
- if code_block_ending_locations.length > 0
1246
+ else
1289
1247
 
1290
- for x in location_among_start_locations-1..last_matching_location
1248
+ nested_elements << [matching_location,combined_location[combined_location.index(code_block_ending_locations[0])]]
1291
1249
 
1292
- locations[-1] << [code_block_starting_locations[x],code_block_ending_locations[0]]
1250
+ nested_indices << modified_starting_locations.index(matching_location)
1293
1251
 
1294
- code_block_ending_locations.delete_at(0)
1252
+ end
1295
1253
 
1296
- end
1254
+ combined_location.delete(code_block_ending_locations[0])
1297
1255
 
1298
- end
1256
+ code_block_ending_locations.delete_at(0)
1299
1257
 
1300
- last_matching_location = location_among_start_locations + 1
1258
+ code_block_starting_locations.delete(matching_location)
1259
+
1260
+ end
1261
+
1262
+ nested_indices.each_with_index do |loc,index|
1263
+
1264
+ locations[loc-1] << nested_elements[index]
1301
1265
 
1302
1266
  end
1303
1267
 
@@ -1379,7 +1343,7 @@ def compile(input_file_path)
1379
1343
 
1380
1344
  def create_self_invoking_function(input_file_contents)
1381
1345
 
1382
- # A feature imported from Coffeescript 1.6.1. This makes all the function private by default
1346
+ # A feature imported from Coffeescript. This makes all the function private by default
1383
1347
  # and prevents global variables from leaking.
1384
1348
 
1385
1349
  modified_file_contents = ["(function() {\n\n",input_file_contents,"\n\n}).call(this);\n"].flatten
@@ -1406,14 +1370,12 @@ def compile(input_file_path)
1406
1370
 
1407
1371
  file_contents = read_file_line_by_line(input_file_path)
1408
1372
 
1409
- file_contents = extract_parsable_array(file_contents)
1373
+ file_contents = extract_parsable_file(file_contents)
1410
1374
 
1411
1375
  file_contents,multiline_comments,temp_file,output_js_file = replace_multiline_comments(file_contents,input_file_path)
1412
1376
 
1413
1377
  file_contents = split_semicolon_seperated_expressions(file_contents)
1414
1378
 
1415
- file_contents = compile_heredoc_strings(file_contents,temp_file)
1416
-
1417
1379
  file_contents = compile_interpolated_strings(file_contents)
1418
1380
 
1419
1381
  file_contents,singleline_comments = replace_singleline_comments(file_contents)
@@ -1426,6 +1388,8 @@ def compile(input_file_path)
1426
1388
 
1427
1389
  file_contents = compile_arrays(file_contents)
1428
1390
 
1391
+ file_contents = compile_conditional_structures(file_contents,temp_file)
1392
+
1429
1393
  file_contents, function_names = compile_named_functions(file_contents,named_functions,nested_functions,temp_file)
1430
1394
 
1431
1395
  file_contents, ruby_functions = compile_custom_function_map(file_contents)
@@ -1481,7 +1445,7 @@ def create_mac_executable(input_file)
1481
1445
 
1482
1446
  end
1483
1447
 
1484
- mac_file_contents = ["#!/usr/bin/env ruby"] + read_file_line_by_line(input_file)
1448
+ mac_file_contents = ["#!/usr/bin/env ruby\n\n"] + read_file_line_by_line(input_file)
1485
1449
 
1486
1450
  mac_file_path = input_file.sub(".rb","")
1487
1451
 
@@ -1535,7 +1499,7 @@ OptionParser.new do |opts|
1535
1499
 
1536
1500
  end
1537
1501
 
1538
- opts.on("-b", "--build", "Builds Itself") do
1502
+ opts.on("-b", "--build FILE", "Builds Itself") do |file|
1539
1503
 
1540
1504
  file_path = Dir.pwd + "/nilac.rb"
1541
1505
 
@@ -1545,7 +1509,7 @@ OptionParser.new do |opts|
1545
1509
 
1546
1510
  end
1547
1511
 
1548
- opts.on("-m", "--buildmac", "Builds Mac Executables") do
1512
+ opts.on("-m", "--buildmac FILE", "Builds Mac Executables") do |macfile|
1549
1513
 
1550
1514
  file_path = Dir.pwd + "/nilac.rb"
1551
1515
 
@@ -1555,5 +1519,4 @@ OptionParser.new do |opts|
1555
1519
 
1556
1520
  end
1557
1521
 
1558
-
1559
1522
  end.parse!
data/lib/nilac/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Nilac
2
- VERSION = "0.0.3.1"
2
+ VERSION = "0.0.3.2"
3
3
  end
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.3.1
4
+ version: 0.0.3.2
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-05-06 00:00:00.000000000 Z
12
+ date: 2013-05-10 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Nilac is the official compiler of Nila language
15
15
  email: