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.
- checksums.yaml +4 -4
- data/lib/lang/grammar.y +3 -1
- data/lib/lang/parser.rb +564 -547
- data/lib/lang/runtime/eleetscript/que.es +4 -0
- data/lib/lang/runtime/memory.rb +22 -646
- data/lib/lang/runtime/ruby/boolean_methods.rb +14 -0
- data/lib/lang/runtime/ruby/io_methods.rb +19 -0
- data/lib/lang/runtime/ruby/lambda_methods.rb +22 -0
- data/lib/lang/runtime/ruby/list_methods.rb +152 -0
- data/lib/lang/runtime/ruby/nil_methods.rb +9 -0
- data/lib/lang/runtime/ruby/number_methods.rb +146 -0
- data/lib/lang/runtime/ruby/object_methods.rb +59 -0
- data/lib/lang/runtime/ruby/regex_methods.rb +58 -0
- data/lib/lang/runtime/ruby/string_methods.rb +159 -0
- metadata +10 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module EleetScript
|
|
2
|
+
Memory.define_core_methods do
|
|
3
|
+
io = root_namespace["IO"]
|
|
4
|
+
|
|
5
|
+
io.class_def :print do |receiver, arguments|
|
|
6
|
+
print arguments.first.call(:to_string).ruby_value
|
|
7
|
+
root_namespace.es_nil
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
io.class_def :println do |receiver, arguments|
|
|
11
|
+
puts arguments.first.call(:to_string).ruby_value
|
|
12
|
+
root_namespace.es_nil
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
io.class_def :new do |receiver, arguments|
|
|
16
|
+
io
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module EleetScript
|
|
2
|
+
Memory.define_core_methods do
|
|
3
|
+
lambda = root_namespace["Lambda"]
|
|
4
|
+
|
|
5
|
+
lambda.def :call do |receiver, arguments, context|
|
|
6
|
+
receiver.ruby_value.call(nil, arguments, context)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
lambda.def :apply do |receiver, arguments, context|
|
|
10
|
+
args = arguments.first
|
|
11
|
+
args = if args.is_a?("List")
|
|
12
|
+
arg_list = args.ruby_value.array_value.dup
|
|
13
|
+
arg_list + args.ruby_value.hash_value.map do |k, v|
|
|
14
|
+
root_namespace["Pair"].call(:new, [k, v])
|
|
15
|
+
end
|
|
16
|
+
else
|
|
17
|
+
[]
|
|
18
|
+
end
|
|
19
|
+
receiver.ruby_value.call(nil, args, context)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
module EleetScript
|
|
2
|
+
Memory.define_core_methods do
|
|
3
|
+
list = root_namespace["List"]
|
|
4
|
+
|
|
5
|
+
list.class_def :new do |receiver, arguments|
|
|
6
|
+
new_list = receiver.new_with_value(ListBase.new(root_namespace.es_nil))
|
|
7
|
+
arguments.each do |arg|
|
|
8
|
+
if arg.instance? && arg.runtime_class.name == "Pair"
|
|
9
|
+
new_list.ruby_value.hash_value[arg.call(:key)] = arg.call(:value)
|
|
10
|
+
else
|
|
11
|
+
new_list.ruby_value.array_value << arg
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
new_list
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
list.def :[] do |receiver, arguments|
|
|
18
|
+
lst = receiver.ruby_value
|
|
19
|
+
arg = arguments.first
|
|
20
|
+
if arg.instance? && arg.runtime_class.name == "Integer"
|
|
21
|
+
index = arg.ruby_value
|
|
22
|
+
if index < lst.array_value.length
|
|
23
|
+
lst.array_value[index]
|
|
24
|
+
else
|
|
25
|
+
lst.hash_value[arg.ruby_value]
|
|
26
|
+
end
|
|
27
|
+
else
|
|
28
|
+
lst.hash_value[arg]
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
list.def :[]= do |receiver, arguments|
|
|
33
|
+
lst = receiver.ruby_value
|
|
34
|
+
key = arguments.first
|
|
35
|
+
value = arguments[1]
|
|
36
|
+
if key.instance? && key.runtime_class.name == "Integer"
|
|
37
|
+
index = key.ruby_value
|
|
38
|
+
if index < lst.array_value.length
|
|
39
|
+
lst.array_value[index] = value
|
|
40
|
+
else
|
|
41
|
+
lst.hash_value[key.ruby_value] = value
|
|
42
|
+
end
|
|
43
|
+
else
|
|
44
|
+
lst.hash_value[key] = value
|
|
45
|
+
end
|
|
46
|
+
value
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
list.def :merge! do |receiver, arguments|
|
|
50
|
+
lst = receiver.ruby_value
|
|
51
|
+
arg = arguments.first
|
|
52
|
+
if arg.is_a?("List")
|
|
53
|
+
lst.merge!(arg.ruby_value)
|
|
54
|
+
end
|
|
55
|
+
receiver
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
list.def :push do |receiver, arguments|
|
|
59
|
+
receiver.ruby_value.array_value << arguments.first
|
|
60
|
+
arguments.first
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
list.def :pop do |receiver, arguments|
|
|
64
|
+
val = receiver.ruby_value.array_value.pop
|
|
65
|
+
val.nil? ? root_namespace.es_nil : val
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
list.def :shift do |receiver, arguments|
|
|
69
|
+
val = receiver.ruby_value.array_value.shift
|
|
70
|
+
val.nil? ? root_namespace.es_nil : val
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
list.def :unshift do |receiver, arguments|
|
|
74
|
+
receiver.ruby_value.array_value.unshift(arguments.first)
|
|
75
|
+
arguments.first
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
list.def :keys do |receiver, arguments|
|
|
79
|
+
lst = receiver.ruby_value
|
|
80
|
+
keys = (lst.array_value.length > 0 ? (0...lst.array_value.length).to_a : []).map { |v| root_namespace["Integer"].new_with_value(v) }
|
|
81
|
+
keys.concat(lst.hash_value.keys)
|
|
82
|
+
list.call(:new, keys)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
list.def :values do |receiver, arguments|
|
|
86
|
+
lst = receiver.ruby_value
|
|
87
|
+
vals = (lst.array_value.length > 0 ? lst.array_value.dup : [])
|
|
88
|
+
vals.concat(lst.hash_value.values)
|
|
89
|
+
list.call(:new, vals)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
list.def :delete do |receiver, arguments|
|
|
93
|
+
val = receiver.ruby_value.hash_value.delete(arguments.first)
|
|
94
|
+
val.nil? ? root_namespace.es_nil : val
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
list.def :length do |receiver, arguments|
|
|
98
|
+
ruby_val = receiver.ruby_value
|
|
99
|
+
length = ruby_val.array_value.length + ruby_val.hash_value.length
|
|
100
|
+
root_namespace["Integer"].new_with_value(length)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
list.def :first do |receiver, arguments|
|
|
104
|
+
receiver.ruby_value.array_value.first
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
list.def :last do |receiver, arguments|
|
|
108
|
+
receiver.ruby_value.array_value.last
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
list.def :clear do |receiver, arguments|
|
|
112
|
+
receiver.ruby_value.clear
|
|
113
|
+
receiver
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
list.def :join do |receiver, arguments|
|
|
117
|
+
str = if arguments.length > 0
|
|
118
|
+
arguments.first.call(:to_string).ruby_value
|
|
119
|
+
else
|
|
120
|
+
", "
|
|
121
|
+
end
|
|
122
|
+
values = receiver.call(:values).ruby_value.array_value.map { |v| v.call(:to_string).ruby_value }
|
|
123
|
+
root_namespace["String"].new_with_value(values.join(str))
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
list.def :to_string do |receiver, arguments|
|
|
127
|
+
arr_vals = receiver.ruby_value.array_value.map do |val|
|
|
128
|
+
val.call(:inspect).ruby_value
|
|
129
|
+
end
|
|
130
|
+
arr_str = arr_vals.join(", ")
|
|
131
|
+
hash_vals = receiver.ruby_value.hash_value.map do |k, v|
|
|
132
|
+
if k.kind_of?(EleetScriptClassSkeleton)
|
|
133
|
+
k = k.call(:inspect).ruby_value
|
|
134
|
+
else
|
|
135
|
+
k = k.inspect
|
|
136
|
+
end
|
|
137
|
+
"#{k}=>#{v.call(:inspect).ruby_value}"
|
|
138
|
+
end
|
|
139
|
+
hash_str = hash_vals.join(", ")
|
|
140
|
+
str = if arr_str.length > 0 && hash_str.length > 0
|
|
141
|
+
arr_str + ", " + hash_str
|
|
142
|
+
elsif arr_str.length > 0
|
|
143
|
+
arr_str
|
|
144
|
+
elsif hash_str.length > 0
|
|
145
|
+
hash_str
|
|
146
|
+
else
|
|
147
|
+
""
|
|
148
|
+
end
|
|
149
|
+
root_namespace["String"].new_with_value("[#{str}]")
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
module EleetScript
|
|
2
|
+
Memory.define_core_methods do
|
|
3
|
+
number = root_namespace["Number"]
|
|
4
|
+
int = root_namespace["Integer"]
|
|
5
|
+
float = root_namespace["Float"]
|
|
6
|
+
|
|
7
|
+
number.def :+ do |receiver, arguments|
|
|
8
|
+
arg = arguments.first
|
|
9
|
+
if arg.is_a?("Number")
|
|
10
|
+
val = receiver.ruby_value + arg.ruby_value
|
|
11
|
+
if val.kind_of?(Fixnum)
|
|
12
|
+
int.new_with_value(val)
|
|
13
|
+
else
|
|
14
|
+
float.new_with_value(val)
|
|
15
|
+
end
|
|
16
|
+
elsif arg.is_a?("String")
|
|
17
|
+
str = receiver.ruby_value.to_s + arg.ruby_value
|
|
18
|
+
root_namespace["String"].new_with_value(str)
|
|
19
|
+
else
|
|
20
|
+
receiver
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
number.def :- do |receiver, arguments|
|
|
25
|
+
arg = arguments.first
|
|
26
|
+
if arg.is_a?("Number")
|
|
27
|
+
val = receiver.ruby_value - arg.ruby_value
|
|
28
|
+
if val.kind_of?(Fixnum)
|
|
29
|
+
int.new_with_value(val)
|
|
30
|
+
else
|
|
31
|
+
float.new_with_value(float)
|
|
32
|
+
end
|
|
33
|
+
else
|
|
34
|
+
receiver
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
number.def :* do |receiver, arguments|
|
|
39
|
+
arg = arguments.first
|
|
40
|
+
if arg.is_a?("Number")
|
|
41
|
+
val = receiver.ruby_value * arg.ruby_value
|
|
42
|
+
if val.kind_of?(Fixnum)
|
|
43
|
+
int.new_with_value(val)
|
|
44
|
+
else
|
|
45
|
+
float.new_with_value(float)
|
|
46
|
+
end
|
|
47
|
+
else
|
|
48
|
+
receiver
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
number.def :/ do |receiver, arguments|
|
|
53
|
+
arg = arguments.first
|
|
54
|
+
if arg.is_a?("Number")
|
|
55
|
+
if arg.ruby_value == 0
|
|
56
|
+
int.new_with_value(0)
|
|
57
|
+
else
|
|
58
|
+
val = receiver.ruby_value / arg.ruby_value
|
|
59
|
+
if val.kind_of?(Fixnum)
|
|
60
|
+
int.new_with_value(val)
|
|
61
|
+
else
|
|
62
|
+
float.new_with_value(float)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
else
|
|
66
|
+
receiver
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
number.def :% do |receiver, arguments|
|
|
71
|
+
arg = arguments.first
|
|
72
|
+
if arg.is_a?("Number")
|
|
73
|
+
if arg.ruby_value == 0
|
|
74
|
+
int.new_with_value(0)
|
|
75
|
+
else
|
|
76
|
+
val = receiver.ruby_value % arg.ruby_value
|
|
77
|
+
if val.kind_of?(Fixnum)
|
|
78
|
+
int.new_with_value(val)
|
|
79
|
+
else
|
|
80
|
+
float.new_with_value(float)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
else
|
|
84
|
+
receiver
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
number.def :< do |receiver, arguments|
|
|
89
|
+
arg = arguments.first
|
|
90
|
+
if arg.is_a?("Number")
|
|
91
|
+
if receiver.ruby_value < arg.ruby_value
|
|
92
|
+
root_namespace["true"]
|
|
93
|
+
else
|
|
94
|
+
root_namespace["false"]
|
|
95
|
+
end
|
|
96
|
+
else
|
|
97
|
+
root_namespace["false"]
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
number.def :> do |receiver, arguments|
|
|
102
|
+
arg = arguments.first
|
|
103
|
+
if arg.is_a?("Number")
|
|
104
|
+
if receiver.ruby_value > arg.ruby_value
|
|
105
|
+
root_namespace["true"]
|
|
106
|
+
else
|
|
107
|
+
root_namespace["false"]
|
|
108
|
+
end
|
|
109
|
+
else
|
|
110
|
+
root_namespace["false"]
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
number.def :is do |receiver, arguments|
|
|
115
|
+
arg = arguments.first
|
|
116
|
+
if arg.is_a?("Number")
|
|
117
|
+
if receiver.ruby_value == arg.ruby_value
|
|
118
|
+
root_namespace["true"]
|
|
119
|
+
else
|
|
120
|
+
root_namespace["false"]
|
|
121
|
+
end
|
|
122
|
+
else
|
|
123
|
+
root_namespace["false"]
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
number.def :negate do |receiver, arguments|
|
|
128
|
+
receiver.ruby_value = -receiver.ruby_value
|
|
129
|
+
receiver
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
number.def :to_string do |receiver, arguments|
|
|
133
|
+
root_namespace["String"].new_with_value(receiver.ruby_value.to_s)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
number.def :clone do |receiver, arguments|
|
|
137
|
+
if receiver.is_a?("Integer")
|
|
138
|
+
int.new_with_value(receiver.ruby_value)
|
|
139
|
+
elsif receiver.is_a?("Float")
|
|
140
|
+
float.new_with_value(receiver.ruby_value)
|
|
141
|
+
else
|
|
142
|
+
root_namespace.es_nil
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
module EleetScript
|
|
2
|
+
Memory.define_core_methods do
|
|
3
|
+
object = root_namespace["Object"]
|
|
4
|
+
|
|
5
|
+
object.class_def :new do |receiver, arguments|
|
|
6
|
+
ins = receiver.new
|
|
7
|
+
ins.call("init", arguments)
|
|
8
|
+
ins
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
object.def :kind_of? do |receiver, arguments|
|
|
12
|
+
t, f = root_namespace["true"], root_namespace["false"]
|
|
13
|
+
if arguments.length == 0 || !arguments.first.class?
|
|
14
|
+
f
|
|
15
|
+
else
|
|
16
|
+
names = []
|
|
17
|
+
names << receiver.runtime_class.name
|
|
18
|
+
cur_class = receiver.runtime_class
|
|
19
|
+
while root_namespace["Object"] != cur_class.super_class
|
|
20
|
+
names << cur_class.super_class.name
|
|
21
|
+
cur_class = cur_class.super_class
|
|
22
|
+
end
|
|
23
|
+
names << "Object" # Base of everything
|
|
24
|
+
name = arguments.first.name
|
|
25
|
+
names.include?(name) ? t : f
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
object.def :class do |receiver, arguments|
|
|
30
|
+
receiver.runtime_class
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
object.def :class_name do |receiver, arguments|
|
|
34
|
+
root_namespace["String"].new_with_value(receiver.runtime_class.name)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
object.class_def :class_name do |receiver, arguments|
|
|
38
|
+
root_namespace["String"].new_with_value(receiver.name)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
object.def :is do |receiver, arguments|
|
|
42
|
+
if receiver == arguments.first
|
|
43
|
+
root_namespace["true"]
|
|
44
|
+
else
|
|
45
|
+
root_namespace["false"]
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
object.def :clone do |receiver, arguments|
|
|
50
|
+
cls_name = receiver.runtime_class.name
|
|
51
|
+
if ["Integer", "Float", "String", "List"].include?(cls_name)
|
|
52
|
+
receiver.runtime_class.new_with_value(receiver.ruby_value.dup)
|
|
53
|
+
else
|
|
54
|
+
ins = receiver.runtime_class.call(:new)
|
|
55
|
+
ins.ruby_value = receiver.ruby_value.dup
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
module EleetScript
|
|
2
|
+
Memory.define_core_methods do
|
|
3
|
+
regex = root_namespace["Regex"]
|
|
4
|
+
|
|
5
|
+
regex.class_def :new do |receiver, arguments|
|
|
6
|
+
pattern, flags = arguments
|
|
7
|
+
pattern = (pattern ? pattern.ruby_value : "")
|
|
8
|
+
flags = (flags ? flags.ruby_value : nil)
|
|
9
|
+
receiver.new_with_value(ESRegex.new(pattern, flags))
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
regex.def :pattern do |receiver, arguments|
|
|
13
|
+
root_namespace["String"].new_with_value(receiver.ruby_value.source)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
regex.def :flags do |receiver, arguments|
|
|
17
|
+
root_namespace["String"].new_with_value(receiver.ruby_value.flags)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
regex.def :global= do |receiver, arguments|
|
|
21
|
+
t, f = root_namespace["true"], root_namespace["false"]
|
|
22
|
+
receiver.ruby_value.global = arguments.first == t ? true : false
|
|
23
|
+
receiver
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
regex.def :multiline? do |receiver, arguments|
|
|
27
|
+
t, f = root_namespace["true"], root_namespace["false"]
|
|
28
|
+
receiver.ruby_value.multiline? ? t : f
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
regex.def :multiline= do |receiver, arguments|
|
|
32
|
+
t, f = root_namespace["true"], root_namespace["false"]
|
|
33
|
+
rx = receiver.ruby_value
|
|
34
|
+
if arguments.first == t
|
|
35
|
+
receiver.ruby_value = ESRegex.new(rx.source, rx.flags + "m")
|
|
36
|
+
else
|
|
37
|
+
receiver.ruby_value = ESRegex.new(rx.source, rx.flags.gsub("m", ""))
|
|
38
|
+
end
|
|
39
|
+
receiver
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
regex.def :ignorecase? do |receiver, arguments|
|
|
43
|
+
t, f = root_namespace["true"], root_namespace["false"]
|
|
44
|
+
receiver.ruby_value.ignorecase? ? t : f
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
regex.def :ignorecase= do |receiver, arguments|
|
|
48
|
+
t, f = root_namespace["true"], root_namespace["false"]
|
|
49
|
+
rx = receiver.ruby_value
|
|
50
|
+
if arguments.first == t
|
|
51
|
+
receiver.ruby_value = ESRegex.new(rx.source, rx.flags + "i")
|
|
52
|
+
else
|
|
53
|
+
receiver.ruby_value = ESRegex.new(rx.source, rx.flags.gsub("i", ""))
|
|
54
|
+
end
|
|
55
|
+
receiver
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
module EleetScript
|
|
2
|
+
Memory.define_core_methods do
|
|
3
|
+
string = root_namespace["String"]
|
|
4
|
+
|
|
5
|
+
string.def :+ do |receiver, arguments|
|
|
6
|
+
arg = arguments.first
|
|
7
|
+
arg_str = if arg.class?
|
|
8
|
+
arg.name
|
|
9
|
+
elsif arg.instance? && arg.runtime_class.name == "String"
|
|
10
|
+
arg.ruby_value
|
|
11
|
+
else
|
|
12
|
+
arg.call(:to_string).ruby_value
|
|
13
|
+
end
|
|
14
|
+
receiver.ruby_value += arg_str
|
|
15
|
+
receiver
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
string.def :is do |receiver, arguments|
|
|
19
|
+
compare_to = arguments.first.ruby_value
|
|
20
|
+
if compare_to == receiver.ruby_value
|
|
21
|
+
root_namespace["true"]
|
|
22
|
+
else
|
|
23
|
+
root_namespace["false"]
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
string.def :substr do |receiver, arguments|
|
|
28
|
+
if arguments.length < 2
|
|
29
|
+
root_namespace["nil"]
|
|
30
|
+
else
|
|
31
|
+
s, e = arguments
|
|
32
|
+
if s.is_a?("Integer") && e.is_a?("Integer")
|
|
33
|
+
range = if e.ruby_value < 0
|
|
34
|
+
(s.ruby_value..e.ruby_value)
|
|
35
|
+
else
|
|
36
|
+
(s.ruby_value...e.ruby_value)
|
|
37
|
+
end
|
|
38
|
+
root_namespace["String"].new_with_value(receiver.ruby_value[range])
|
|
39
|
+
else
|
|
40
|
+
root_namespace["nil"]
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
string.def :length do |receiver, arguments|
|
|
46
|
+
root_namespace["Integer"].new_with_value(receiver.ruby_value.length)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
string.def :upper_case do |receiver, arguments|
|
|
50
|
+
string.new_with_value(receiver.ruby_value.upcase)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
string.def :lower_case do |receiver, arguments|
|
|
54
|
+
string.new_with_value(receiver.ruby_value.downcase)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
string.def :[] do |receiver, arguments|
|
|
58
|
+
index = arguments.first
|
|
59
|
+
if index.is_a?("Integer")
|
|
60
|
+
index = index.ruby_value
|
|
61
|
+
if index < 0 || index >= receiver.ruby_value.length
|
|
62
|
+
root_namespace.es_nil
|
|
63
|
+
else
|
|
64
|
+
string.new_with_value(receiver.ruby_value[index])
|
|
65
|
+
end
|
|
66
|
+
else
|
|
67
|
+
root_namespace.es_nil
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
string.def :[]= do |receiver, arguments|
|
|
72
|
+
index, value = arguments
|
|
73
|
+
if index.is_a?("Integer")
|
|
74
|
+
index = index.ruby_value
|
|
75
|
+
if index < 0 && index >= receiver.ruby_value.length
|
|
76
|
+
root_namespace.es_nil
|
|
77
|
+
else
|
|
78
|
+
value_str = value.call(:to_string)
|
|
79
|
+
receiver.ruby_value[index] = value_str.ruby_value
|
|
80
|
+
receiver
|
|
81
|
+
end
|
|
82
|
+
else
|
|
83
|
+
root_namespace.es_nil
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
string.def :replace do |receiver, arguments|
|
|
88
|
+
if arguments.length < 2
|
|
89
|
+
string.new_with_value(receiver.ruby_value)
|
|
90
|
+
else
|
|
91
|
+
pattern, replacement = arguments
|
|
92
|
+
if !pattern.is_a?("Regex")
|
|
93
|
+
pattern = root_namespace["Regex"].call(:new, [pattern.call(:to_string)])
|
|
94
|
+
end
|
|
95
|
+
if replacement.is_a?("Lambda")
|
|
96
|
+
new_str = if pattern.ruby_value.global?
|
|
97
|
+
receiver.ruby_value.gsub(pattern.ruby_value) do
|
|
98
|
+
args = Regexp.last_match[1..-1].map { |match| string.new_with_value(match) }
|
|
99
|
+
replacement.call(:call, args).call(:to_string).ruby_value
|
|
100
|
+
end
|
|
101
|
+
else
|
|
102
|
+
receiver.ruby_value.sub(pattern.ruby_value) do
|
|
103
|
+
args = Regexp.last_match[1..-1].map { |match| string.new_with_value(match) }
|
|
104
|
+
replacement.call(:call, args).call(:to_string).ruby_value
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
string.new_with_value(new_str)
|
|
108
|
+
else
|
|
109
|
+
new_str = if pattern.ruby_value.global?
|
|
110
|
+
receiver.ruby_value.gsub(pattern.ruby_value, replacement.call(:to_string).ruby_value)
|
|
111
|
+
else
|
|
112
|
+
receiver.ruby_value.sub(pattern.ruby_value, replacement.call(:to_string).ruby_value)
|
|
113
|
+
end
|
|
114
|
+
string.new_with_value(new_str)
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
string.def :match do |receiver, arguments|
|
|
120
|
+
str_cls, list_cls = root_namespace["String"], root_namespace["List"]
|
|
121
|
+
rx = arguments.first
|
|
122
|
+
args = []
|
|
123
|
+
if rx.is_a?("Regex")
|
|
124
|
+
if rx.ruby_value.global?
|
|
125
|
+
matches = receiver.ruby_value.scan(rx.ruby_value)
|
|
126
|
+
list_args = matches.map do |match|
|
|
127
|
+
args = match.map do |a|
|
|
128
|
+
str_cls.new_with_value(a)
|
|
129
|
+
end
|
|
130
|
+
list_cls.call(:new, args)
|
|
131
|
+
end
|
|
132
|
+
list_cls.call(:new, list_args)
|
|
133
|
+
else
|
|
134
|
+
matches = receiver.ruby_value.match(rx.ruby_value)
|
|
135
|
+
if matches.nil?
|
|
136
|
+
list_cls.call(:new)
|
|
137
|
+
else
|
|
138
|
+
args = [str_cls.new_with_value(matches[0])]
|
|
139
|
+
if matches.names.length > 0
|
|
140
|
+
args += matches.names.map do |name|
|
|
141
|
+
n, v = str_cls.new_with_value(name), str_cls.new_with_value(matches[name])
|
|
142
|
+
root_namespace["Pair"].call(:new, [n, v])
|
|
143
|
+
end
|
|
144
|
+
else
|
|
145
|
+
group_matches = matches.to_a
|
|
146
|
+
group_matches.shift # Remove full match
|
|
147
|
+
args += group_matches.map do |res|
|
|
148
|
+
str_cls.new_with_value(res)
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
list_cls.call(:new, args)
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
else
|
|
155
|
+
root_namespace["List"].call(:new)
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: eleetscript
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.11a
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Brandon Buck
|
|
@@ -53,6 +53,15 @@ files:
|
|
|
53
53
|
- lib/lang/runtime/memory.rb
|
|
54
54
|
- lib/lang/runtime/method.rb
|
|
55
55
|
- lib/lang/runtime/method_hash.rb
|
|
56
|
+
- lib/lang/runtime/ruby/boolean_methods.rb
|
|
57
|
+
- lib/lang/runtime/ruby/io_methods.rb
|
|
58
|
+
- lib/lang/runtime/ruby/lambda_methods.rb
|
|
59
|
+
- lib/lang/runtime/ruby/list_methods.rb
|
|
60
|
+
- lib/lang/runtime/ruby/nil_methods.rb
|
|
61
|
+
- lib/lang/runtime/ruby/number_methods.rb
|
|
62
|
+
- lib/lang/runtime/ruby/object_methods.rb
|
|
63
|
+
- lib/lang/runtime/ruby/regex_methods.rb
|
|
64
|
+
- lib/lang/runtime/ruby/string_methods.rb
|
|
56
65
|
- lib/util/processed_key_hash.rb
|
|
57
66
|
- bin/eleet
|
|
58
67
|
homepage: http://github.com/bbuck/eleetscript
|