eleetscript 0.0.10a → 0.0.11a

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.
@@ -10,4 +10,8 @@ class Que < List
10
10
  pop do
11
11
  shift
12
12
  end
13
+
14
+ push do |val|
15
+ unshift(val)
16
+ end
13
17
  end
@@ -24,6 +24,12 @@ module EleetScript
24
24
  "NilClass" => nil
25
25
  }
26
26
 
27
+ class << self
28
+ def define_core_methods(&block)
29
+ (@@core_definers ||= []) << block
30
+ end
31
+ end
32
+
27
33
  def initialize
28
34
  @root_namespace = NamespaceContext.new(nil, nil)
29
35
  @root_path = File.join(File.dirname(__FILE__), "eleetscript")
@@ -35,665 +41,35 @@ module EleetScript
35
41
 
36
42
  ROOT_OBJECTS.each do |obj_name, parent_class|
37
43
  if parent_class.nil?
38
- @root_namespace[obj_name] = EleetScriptClass.create(@root_namespace, obj_name)
44
+ root_namespace[obj_name] = EleetScriptClass.create(root_namespace, obj_name)
39
45
  else
40
- @root_namespace[obj_name] = EleetScriptClass.create(@root_namespace, obj_name, @root_namespace[parent_class])
46
+ root_namespace[obj_name] = EleetScriptClass.create(root_namespace, obj_name, root_namespace[parent_class])
41
47
  end
42
48
  end
43
49
 
44
- @root = @root_namespace["Object"].new
45
- @root_namespace.current_self = @root
46
- @root_namespace.current_class = @root.runtime_class
50
+ @root = root_namespace["Object"].new
51
+ root_namespace.current_self = @root
52
+ root_namespace.current_class = @root.runtime_class
47
53
 
48
- @root_namespace["true"] = @root_namespace["TrueClass"].new_with_value(true)
49
- @root_namespace["false"] = @root_namespace["FalseClass"].new_with_value(false)
50
- @root_namespace["nil"] = @root_namespace["NilClass"].new_with_value(nil)
54
+ root_namespace["true"] = root_namespace["TrueClass"].new_with_value(true)
55
+ root_namespace["false"] = root_namespace["FalseClass"].new_with_value(false)
56
+ root_namespace["nil"] = root_namespace["NilClass"].new_with_value(nil)
51
57
 
52
58
  # Global Errors Object
53
- @root_namespace["Errors"] = @root_namespace["List"].new_with_value(ListBase.new(@root_namespace.es_nil))
59
+ root_namespace["Errors"] = root_namespace["List"].new_with_value(ListBase.new(root_namespace.es_nil))
54
60
 
55
- load_object_methods
56
- load_io_methods
57
- load_string_methods
58
- load_regex_methods
59
- load_number_methods
60
- load_boolean_methods
61
- load_nil_methods
62
- load_list_methods
63
- load_lambda_methods
61
+ @@core_definers.each do |definer_block|
62
+ instance_eval(&definer_block)
63
+ end
64
64
 
65
65
  files = Dir.glob(File.join(@root_path, "**", "*.es"))
66
66
  files.each do |file|
67
67
  loader.load(file)
68
68
  end
69
69
  end
70
-
71
- private
72
-
73
- def load_object_methods
74
- object = @root_namespace["Object"]
75
-
76
- object.class_def :new do |receiver, arguments|
77
- ins = receiver.new
78
- ins.call("init", arguments)
79
- ins
80
- end
81
-
82
- object.def :kind_of? do |receiver, arguments|
83
- t, f = @root_namespace["true"], @root_namespace["false"]
84
- if arguments.length == 0 || !arguments.first.class?
85
- f
86
- else
87
- names = []
88
- names << receiver.runtime_class.name
89
- cur_class = receiver.runtime_class
90
- while @root_namespace["Object"] != cur_class.super_class
91
- names << cur_class.super_class.name
92
- cur_class = cur_class.super_class
93
- end
94
- names << "Object" # Base of everything
95
- name = arguments.first.name
96
- names.include?(name) ? t : f
97
- end
98
- end
99
-
100
- object.def :class do |receiver, arguments|
101
- receiver.runtime_class
102
- end
103
-
104
- object.def :class_name do |receiver, arguments|
105
- @root_namespace["String"].new_with_value(receiver.runtime_class.name)
106
- end
107
-
108
- object.class_def :class_name do |receiver, arguments|
109
- @root_namespace["String"].new_with_value(receiver.name)
110
- end
111
-
112
- object.def :is do |receiver, arguments|
113
- if receiver == arguments.first
114
- @root_namespace["true"]
115
- else
116
- @root_namespace["false"]
117
- end
118
- end
119
-
120
- object.def :clone do |receiver, arguments|
121
- cls_name = receiver.runtime_class.name
122
- if ["Integer", "Float", "String", "List"].include?(cls_name)
123
- receiver.runtime_class.new_with_value(receiver.ruby_value.dup)
124
- else
125
- ins = receiver.runtime_class.call(:new)
126
- ins.ruby_value = receiver.ruby_value.dup
127
- end
128
- end
129
- end
130
-
131
- def load_io_methods
132
- io = @root_namespace["IO"]
133
-
134
- io.class_def :print do |receiver, arguments|
135
- print arguments.first.call(:to_string).ruby_value
136
- @root_namespace.es_nil
137
- end
138
-
139
- io.class_def :println do |receiver, arguments|
140
- puts arguments.first.call(:to_string).ruby_value
141
- @root_namespace.es_nil
142
- end
143
-
144
- io.class_def :new do |receiver, arguments|
145
- io
146
- end
147
- end
148
-
149
- def load_string_methods
150
- string = @root_namespace["String"]
151
-
152
- string.def :+ do |receiver, arguments|
153
- arg = arguments.first
154
- arg_str = if arg.class?
155
- arg.name
156
- elsif arg.instance? && arg.runtime_class.name == "String"
157
- arg.ruby_value
158
- else
159
- arg.call(:to_string).ruby_value
160
- end
161
- receiver.ruby_value += arg_str
162
- receiver
163
- end
164
-
165
- string.def :is do |receiver, arguments|
166
- compare_to = arguments.first.ruby_value
167
- if compare_to == receiver.ruby_value
168
- @root_namespace["true"]
169
- else
170
- @root_namespace["false"]
171
- end
172
- end
173
-
174
- string.def :substr do |receiver, arguments|
175
- if arguments.length < 2
176
- @root_namespace["nil"]
177
- else
178
- s, e = arguments
179
- if s.is_a?("Integer") && e.is_a?("Integer")
180
- range = if e.ruby_value < 0
181
- (s.ruby_value..e.ruby_value)
182
- else
183
- (s.ruby_value...e.ruby_value)
184
- end
185
- @root_namespace["String"].new_with_value(receiver.ruby_value[range])
186
- else
187
- @root_namespace["nil"]
188
- end
189
- end
190
- end
191
-
192
- string.def :length do |receiver, arguments|
193
- @root_namespace["Integer"].new_with_value(receiver.ruby_value.length)
194
- end
195
-
196
- string.def :upper_case do |receiver, arguments|
197
- string.new_with_value(receiver.ruby_value.upcase)
198
- end
199
-
200
- string.def :lower_case do |receiver, arguments|
201
- string.new_with_value(receiver.ruby_value.downcase)
202
- end
203
-
204
- string.def :[] do |receiver, arguments|
205
- index = arguments.first
206
- if index.is_a?("Integer")
207
- index = index.ruby_value
208
- if index < 0 || index >= receiver.ruby_value.length
209
- @root_namespace.es_nil
210
- else
211
- string.new_with_value(receiver.ruby_value[index])
212
- end
213
- else
214
- @root_namespace.es_nil
215
- end
216
- end
217
-
218
- string.def :[]= do |receiver, arguments|
219
- index, value = arguments
220
- if index.is_a?("Integer")
221
- index = index.ruby_value
222
- if index < 0 && index >= receiver.ruby_value.length
223
- @root_namespace.es_nil
224
- else
225
- value_str = value.call(:to_string)
226
- receiver.ruby_value[index] = value_str.ruby_value
227
- receiver
228
- end
229
- else
230
- @root_namespace.es_nil
231
- end
232
- end
233
-
234
- string.def :replace do |receiver, arguments|
235
- if arguments.length < 2
236
- string.new_with_value(receiver.ruby_value)
237
- else
238
- pattern, replacement = arguments
239
- if !pattern.is_a?("Regex")
240
- pattern = @root_namespace["Regex"].call(:new, [pattern.call(:to_string)])
241
- end
242
- if replacement.is_a?("Lambda")
243
- new_str = if pattern.ruby_value.global?
244
- receiver.ruby_value.gsub(pattern.ruby_value) do |*args|
245
- args = args.map { |arg| string.new_with_value(arg) }
246
- replacement.call(:call, args).call(:to_string).ruby_value
247
- end
248
- else
249
- receiver.ruby_value.sub(pattern.ruby_value) do |*args|
250
- args = args.map { |arg| string.new_with_value(arg) }
251
- replacement.call(:call, args).call(:to_string).ruby_value
252
- end
253
- end
254
- else
255
- new_str = if pattern.ruby_value.global?
256
- receiver.ruby_value.gsub(pattern.ruby_value, replacement.call(:to_string).ruby_value)
257
- else
258
- receiver.ruby_value.sub(pattern.ruby_value, replacement.call(:to_string).ruby_value)
259
- end
260
- string.new_with_value(new_str)
261
- end
262
- end
263
- end
264
-
265
- string.def :match do |receiver, arguments|
266
- str_cls, list_cls = @root_namespace["String"], @root_namespace["List"]
267
- rx = arguments.first
268
- if rx.is_a?("Regex")
269
- if rx.ruby_value.global?
270
- matches = receiver.ruby_value.scan(rx.ruby_value)
271
- list_args = matches.map do |match|
272
- args = match.map do |a|
273
- str_cls.new_with_value(a)
274
- end
275
- list_cls.call(:new, args)
276
- end
277
- list_cls.call(:new, args)
278
- else
279
- matches = receiver.ruby_value.match(rx.ruby_value)
280
- if matches.nil?
281
- list_cls.call(:new)
282
- else
283
- args = [str_cls.new_with_value(matches[0])]
284
- if matches.names.length > 0
285
- args += matches.names.map do |name|
286
- n, v = str_cls.new_with_value(name), str_cls.new_with_value(matches[name])
287
- @root_namespace["Pair"].call(:new, [n, v])
288
- end
289
- else
290
- group_matches = matches.to_a
291
- group_matches.shift # Remove full match
292
- args += group_matches.map do |res|
293
- str_cls.new_with_value(res)
294
- end
295
- end
296
- list_cls.call(:new, args)
297
- end
298
- end
299
- else
300
- @root_namespace["List"].call(:new)
301
- end
302
- end
303
- end
304
-
305
- def load_number_methods
306
- number = @root_namespace["Number"]
307
- int = @root_namespace["Integer"]
308
- float = @root_namespace["Float"]
309
-
310
- number.def :+ do |receiver, arguments|
311
- arg = arguments.first
312
- if arg.is_a?("Number")
313
- val = receiver.ruby_value + arg.ruby_value
314
- if val.kind_of?(Fixnum)
315
- int.new_with_value(val)
316
- else
317
- float.new_with_value(val)
318
- end
319
- elsif arg.is_a?("String")
320
- str = receiver.ruby_value.to_s + arg.ruby_value
321
- @root_namespace["String"].new_with_value(str)
322
- else
323
- receiver
324
- end
325
- end
326
-
327
- number.def :- do |receiver, arguments|
328
- arg = arguments.first
329
- if arg.is_a?("Number")
330
- val = receiver.ruby_value - arg.ruby_value
331
- if val.kind_of?(Fixnum)
332
- int.new_with_value(val)
333
- else
334
- float.new_with_value(float)
335
- end
336
- else
337
- receiver
338
- end
339
- end
340
-
341
- number.def :* do |receiver, arguments|
342
- arg = arguments.first
343
- if arg.is_a?("Number")
344
- val = receiver.ruby_value * arg.ruby_value
345
- if val.kind_of?(Fixnum)
346
- int.new_with_value(val)
347
- else
348
- float.new_with_value(float)
349
- end
350
- else
351
- receiver
352
- end
353
- end
354
-
355
- number.def :/ do |receiver, arguments|
356
- arg = arguments.first
357
- if arg.is_a?("Number")
358
- if arg.ruby_value == 0
359
- int.new_with_value(0)
360
- else
361
- val = receiver.ruby_value / arg.ruby_value
362
- if val.kind_of?(Fixnum)
363
- int.new_with_value(val)
364
- else
365
- float.new_with_value(float)
366
- end
367
- end
368
- else
369
- receiver
370
- end
371
- end
372
-
373
- number.def :% do |receiver, arguments|
374
- arg = arguments.first
375
- if arg.is_a?("Number")
376
- if arg.ruby_value == 0
377
- int.new_with_value(0)
378
- else
379
- val = receiver.ruby_value % arg.ruby_value
380
- if val.kind_of?(Fixnum)
381
- int.new_with_value(val)
382
- else
383
- float.new_with_value(float)
384
- end
385
- end
386
- else
387
- receiver
388
- end
389
- end
390
-
391
- number.def :< do |receiver, arguments|
392
- arg = arguments.first
393
- if arg.is_a?("Number")
394
- if receiver.ruby_value < arg.ruby_value
395
- @root_namespace["true"]
396
- else
397
- @root_namespace["false"]
398
- end
399
- else
400
- @root_namespace["false"]
401
- end
402
- end
403
-
404
- number.def :> do |receiver, arguments|
405
- arg = arguments.first
406
- if arg.is_a?("Number")
407
- if receiver.ruby_value > arg.ruby_value
408
- @root_namespace["true"]
409
- else
410
- @root_namespace["false"]
411
- end
412
- else
413
- @root_namespace["false"]
414
- end
415
- end
416
-
417
- number.def :is do |receiver, arguments|
418
- arg = arguments.first
419
- if arg.is_a?("Number")
420
- if receiver.ruby_value == arg.ruby_value
421
- @root_namespace["true"]
422
- else
423
- @root_namespace["false"]
424
- end
425
- else
426
- @root_namespace["false"]
427
- end
428
- end
429
-
430
- number.def :negate do |receiver, arguments|
431
- receiver.ruby_value = -receiver.ruby_value
432
- receiver
433
- end
434
-
435
- number.def :to_string do |receiver, arguments|
436
- @root_namespace["String"].new_with_value(receiver.ruby_value.to_s)
437
- end
438
-
439
- number.def :clone do |receiver, arguments|
440
- if receiver.is_a?("Integer")
441
- int.new_with_value(receiver.ruby_value)
442
- elsif receiver.is_a?("Float")
443
- float.new_with_value(receiver.ruby_value)
444
- else
445
- @root_namespace.es_nil
446
- end
447
- end
448
- end
449
-
450
- def load_regex_methods
451
- regex = @root_namespace["Regex"]
452
-
453
- regex.class_def :new do |receiver, arguments|
454
- pattern, flags = arguments
455
- pattern = (pattern ? pattern.ruby_value : "")
456
- flags = (flags ? flags.ruby_value : nil)
457
- regex.new_with_value(ESRegex.new(pattern, flags))
458
- end
459
-
460
- regex.def :pattern do |receiver, arguments|
461
- @root_namespace["String"].new_with_value(receiver.ruby_value.source)
462
- end
463
-
464
- regex.def :flags do |receiver, arguments|
465
- @root_namespace["String"].new_with_value(receiver.ruby_value.flags)
466
- end
467
-
468
- regex.def :global= do |receiver, arguments|
469
- t, f = @root_namespace["true"], @root_namespace["false"]
470
- receiver.ruby_value.global = arguments.first == t ? true : false
471
- receiver
472
- end
473
-
474
- regex.def :multiline? do |receiver, arguments|
475
- t, f = @root_namespace["true"], @root_namespace["false"]
476
- receiver.ruby_value.multiline? ? t : f
477
- end
478
-
479
- regex.def :multiline= do |receiver, arguments|
480
- t, f = @root_namespace["true"], @root_namespace["false"]
481
- rx = receiver.ruby_value
482
- if arguments.first == t
483
- receiver.ruby_value = ESRegex.new(rx.source, rx.flags + "m")
484
- else
485
- receiver.ruby_value = ESRegex.new(rx.source, rx.flags.gsub("m", ""))
486
- end
487
- receiver
488
- end
489
-
490
- regex.def :ignorecase? do |receiver, arguments|
491
- t, f = @root_namespace["true"], @root_namespace["false"]
492
- receiver.ruby_value.ignorecase? ? t : f
493
- end
494
-
495
- regex.def :ignorecase= do |receiver, arguments|
496
- t, f = @root_namespace["true"], @root_namespace["false"]
497
- rx = receiver.ruby_value
498
- if arguments.first == t
499
- receiver.ruby_value = ESRegex.new(rx.source, rx.flags + "i")
500
- else
501
- receiver.ruby_value = ESRegex.new(rx.source, rx.flags.gsub("i", ""))
502
- end
503
- receiver
504
- end
505
- end
506
-
507
- def load_boolean_methods
508
- true_cls = @root_namespace["TrueClass"]
509
- false_cls = @root_namespace["FalseClass"]
510
-
511
- true_cls.def :clone do |receiver, arguments|
512
- true_cls.new_with_value(true)
513
- end
514
-
515
- false_cls.def :clone do |receiver, arguments|
516
- false_cls.new_with_value(false)
517
- end
518
- end
519
-
520
- def load_nil_methods
521
- nil_cls = @root_namespace["NilClass"]
522
-
523
- nil_cls.def :clone do |receiver, arguments|
524
- nil_cls.new_with_value(nil)
525
- end
526
- end
527
-
528
- def load_list_methods
529
- list = @root_namespace["List"]
530
- list.class_def :new do |receiver, arguments|
531
- new_list = list.new_with_value(ListBase.new(@root_namespace.es_nil))
532
- arguments.each do |arg|
533
- if arg.instance? && arg.runtime_class.name == "Pair"
534
- new_list.ruby_value.hash_value[arg.call(:key)] = arg.call(:value)
535
- else
536
- new_list.ruby_value.array_value << arg
537
- end
538
- end
539
- new_list
540
- end
541
-
542
- list.def :[] do |receiver, arguments|
543
- lst = receiver.ruby_value
544
- arg = arguments.first
545
- if arg.instance? && arg.runtime_class.name == "Integer"
546
- index = arg.ruby_value
547
- if index < lst.array_value.length
548
- lst.array_value[index]
549
- else
550
- lst.hash_value[arg.ruby_value]
551
- end
552
- else
553
- lst.hash_value[arg]
554
- end
555
- end
556
-
557
- list.def :[]= do |receiver, arguments|
558
- lst = receiver.ruby_value
559
- key = arguments.first
560
- value = arguments[1]
561
- if key.instance? && key.runtime_class.name == "Integer"
562
- index = key.ruby_value
563
- if index < lst.array_value.length
564
- lst.array_value[index] = value
565
- else
566
- lst.hash_value[key.ruby_value] = value
567
- end
568
- else
569
- lst.hash_value[key] = value
570
- end
571
- value
572
- end
573
-
574
- list.def :merge! do |receiver, arguments|
575
- lst = receiver.ruby_value
576
- arg = arguments.first
577
- if arg.is_a?("List")
578
- lst.merge!(arg.ruby_value)
579
- end
580
- receiver
581
- end
582
-
583
- list.def :push do |receiver, arguments|
584
- receiver.ruby_value.array_value << arguments.first
585
- arguments.first
586
- end
587
-
588
- list.def :pop do |receiver, arguments|
589
- val = receiver.ruby_value.array_value.pop
590
- val.nil? ? @root_namespace.es_nil : val
591
- end
592
-
593
- list.def :shift do |receiver, arguments|
594
- val = receiver.ruby_value.array_value.shift
595
- val.nil? ? @root_namespace.es_nil : val
596
- end
597
-
598
- list.def :unshift do |receiver, arguments|
599
- receiver.ruby_value.array_value.unshift(arguments.first)
600
- arguments.first
601
- end
602
-
603
- list.def :keys do |receiver, arguments|
604
- lst = receiver.ruby_value
605
- keys = (lst.array_value.length > 0 ? (0...lst.array_value.length).to_a : []).map { |v| @root_namespace["Integer"].new_with_value(v) }
606
- keys.concat(lst.hash_value.keys)
607
- list.call(:new, keys)
608
- end
609
-
610
- list.def :values do |receiver, arguments|
611
- lst = receiver.ruby_value
612
- vals = (lst.array_value.length > 0 ? lst.array_value.dup : [])
613
- vals.concat(lst.hash_value.values)
614
- list.call(:new, vals)
615
- end
616
-
617
- list.def :delete do |receiver, arguments|
618
- val = receiver.ruby_value.hash_value.delete(arguments.first)
619
- val.nil? ? @root_namespace.es_nil : val
620
- end
621
-
622
- list.def :length do |receiver, arguments|
623
- ruby_val = receiver.ruby_value
624
- length = ruby_val.array_value.length + ruby_val.hash_value.length
625
- @root_namespace["Integer"].new_with_value(length)
626
- end
627
-
628
- list.def :first do |receiver, arguments|
629
- receiver.ruby_value.array_value.first
630
- end
631
-
632
- list.def :last do |receiver, arguments|
633
- receiver.ruby_value.array_value.last
634
- end
635
-
636
- list.def :clear do |receiver, arguments|
637
- receiver.ruby_value.clear
638
- receiver
639
- end
640
-
641
- list.def :join do |receiver, arguments|
642
- str = if arguments.length > 0
643
- arguments.first.call(:to_string).ruby_value
644
- else
645
- ", "
646
- end
647
- values = receiver.call(:values).ruby_value.array_value.map { |v| v.call(:to_string).ruby_value }
648
- @root_namespace["String"].new_with_value(values.join(str))
649
- end
650
-
651
- list.def :to_string do |receiver, arguments|
652
- arr_vals = receiver.ruby_value.array_value.map do |val|
653
- val.call(:inspect).ruby_value
654
- end
655
- arr_str = arr_vals.join(", ")
656
- hash_vals = receiver.ruby_value.hash_value.map do |k, v|
657
- if k.kind_of?(EleetScriptClassSkeleton)
658
- k = k.call(:inspect).ruby_value
659
- else
660
- k = k.inspect
661
- end
662
- "#{k}=>#{v.call(:inspect).ruby_value}"
663
- end
664
- hash_str = hash_vals.join(", ")
665
- str = if arr_str.length > 0 && hash_str.length > 0
666
- arr_str + ", " + hash_str
667
- elsif arr_str.length > 0
668
- arr_str
669
- elsif hash_str.length > 0
670
- hash_str
671
- else
672
- ""
673
- end
674
- @root_namespace["String"].new_with_value("[#{str}]")
675
- end
676
- end
677
-
678
- def load_lambda_methods
679
- lambda = @root_namespace["Lambda"]
680
-
681
- lambda.def :call do |receiver, arguments, context|
682
- receiver.ruby_value.call(nil, arguments, context)
683
- end
684
-
685
- lambda.def :apply do |receiver, arguments, context|
686
- args = arguments.first
687
- args = if args.is_a?("List")
688
- arg_list = args.ruby_value.array_value.dup
689
- arg_list + args.ruby_value.hash_value.map do |k, v|
690
- @root_namespace["Pair"].call(:new, [k, v])
691
- end
692
- else
693
- []
694
- end
695
- receiver.ruby_value.call(nil, args, context)
696
- end
697
- end
698
70
  end
71
+ end
72
+
73
+ Dir.glob(File.join(File.dirname(__FILE__), "ruby", "**", "*")).each do |file|
74
+ require file
699
75
  end
@@ -0,0 +1,14 @@
1
+ module EleetScript
2
+ Memory.define_core_methods do
3
+ true_cls = root_namespace["TrueClass"]
4
+ false_cls = root_namespace["FalseClass"]
5
+
6
+ true_cls.def :clone do |receiver, arguments|
7
+ true_cls.new_with_value(true)
8
+ end
9
+
10
+ false_cls.def :clone do |receiver, arguments|
11
+ false_cls.new_with_value(false)
12
+ end
13
+ end
14
+ end