code-ruby 1.1.1 → 1.2.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.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +13 -13
- data/.github/workflows/ci.yml +25 -24
- data/.npm-version +1 -0
- data/.rubocop.yml +14 -11
- data/Gemfile +5 -4
- data/Gemfile.lock +134 -28
- data/VERSION +1 -1
- data/bin/console +6 -0
- data/bin/dorian +31 -0
- data/code-ruby.gemspec +6 -1
- data/lib/code/error.rb +4 -25
- data/lib/code/node/code.rb +1 -1
- data/lib/code/node/function_parameter.rb +10 -8
- data/lib/code/node/while.rb +1 -1
- data/lib/code/object/boolean.rb +20 -16
- data/lib/code/object/class.rb +10 -4
- data/lib/code/object/code.rb +7 -3
- data/lib/code/object/context.rb +8 -8
- data/lib/code/object/date.rb +41 -7
- data/lib/code/object/decimal.rb +101 -56
- data/lib/code/object/dictionary.rb +245 -191
- data/lib/code/object/duration.rb +11 -7
- data/lib/code/object/function.rb +38 -25
- data/lib/code/object/global.rb +95 -42
- data/lib/code/object/html.rb +12 -14
- data/lib/code/object/http.rb +219 -0
- data/lib/code/object/identifier_list.rb +16 -16
- data/lib/code/object/integer.rb +129 -89
- data/lib/code/object/json.rb +18 -22
- data/lib/code/object/list.rb +141 -92
- data/lib/code/object/parameter.rb +9 -13
- data/lib/code/object/range.rb +77 -45
- data/lib/code/object/string.rb +15 -34
- data/lib/code/object/time.rb +17 -16
- data/lib/code/object.rb +126 -93
- data/lib/code/parser/string.rb +2 -1
- data/lib/code/type/sig.rb +3 -3
- data/lib/code-ruby.rb +119 -0
- data/package-lock.json +1 -1
- data/package.json +1 -1
- data/spec/code/object/http_spec.rb +91 -0
- data/spec/code/type_spec.rb +1 -1
- data/spec/code_spec.rb +10 -5
- data/spec/spec_helper.rb +18 -0
- metadata +50 -3
data/lib/code/object/list.rb
CHANGED
@@ -4,81 +4,86 @@ class Code
|
|
4
4
|
class Object
|
5
5
|
class List < Object
|
6
6
|
def initialize(*args, **_kargs, &)
|
7
|
-
raw =
|
8
|
-
|
9
|
-
|
7
|
+
@raw =
|
8
|
+
if args.first.is_a?(List)
|
9
|
+
args.first.raw.map(&:to_code)
|
10
|
+
elsif args.first.is_an?(::Array)
|
11
|
+
args.first.map(&:to_code)
|
12
|
+
else
|
13
|
+
[]
|
14
|
+
end
|
10
15
|
end
|
11
16
|
|
12
17
|
def call(**args)
|
13
|
-
|
14
|
-
|
18
|
+
code_operator = args.fetch(:operator, nil).to_code
|
19
|
+
code_arguments = args.fetch(:arguments, []).to_code
|
15
20
|
globals = multi_fetch(args, *GLOBALS)
|
16
|
-
|
21
|
+
code_value = code_arguments.code_first
|
17
22
|
|
18
|
-
case
|
23
|
+
case code_operator.to_s
|
19
24
|
when "join"
|
20
25
|
sig(args) { String.maybe }
|
21
|
-
code_join(
|
26
|
+
code_join(code_value)
|
22
27
|
when "sort"
|
23
28
|
sig(args)
|
24
29
|
code_sort
|
25
30
|
when "<<", "append"
|
26
31
|
sig(args) { Object }
|
27
|
-
code_append(
|
32
|
+
code_append(code_value)
|
28
33
|
when "any?"
|
29
34
|
sig(args) { Function.maybe }
|
30
|
-
code_any?(
|
35
|
+
code_any?(code_value, **globals)
|
31
36
|
when "detect"
|
32
37
|
sig(args) { Function }
|
33
|
-
code_detect(
|
38
|
+
code_detect(code_value, **globals)
|
34
39
|
when "each"
|
35
40
|
sig(args) { Function }
|
36
|
-
code_each(
|
41
|
+
code_each(code_value, **globals)
|
37
42
|
when "first"
|
38
|
-
sig(args)
|
39
|
-
code_first
|
43
|
+
sig(args) { Integer.maybe }
|
44
|
+
code_first(code_value)
|
40
45
|
when "flatten"
|
41
46
|
sig(args) { Integer.maybe }
|
42
47
|
code_flatten
|
43
48
|
when "include?"
|
44
49
|
sig(args) { Object }
|
45
|
-
code_include?(
|
50
|
+
code_include?(code_value)
|
46
51
|
when "last"
|
47
52
|
sig(args)
|
48
53
|
code_last
|
49
54
|
when "map"
|
50
55
|
sig(args) { Function }
|
51
|
-
code_map(
|
56
|
+
code_map(code_value, **globals)
|
52
57
|
when "map!"
|
53
58
|
sig(args) { Function }
|
54
|
-
code_map!(
|
59
|
+
code_map!(code_value, **globals)
|
55
60
|
when "max"
|
56
61
|
sig(args)
|
57
62
|
code_max
|
58
63
|
when "max_by"
|
59
64
|
sig(args) { Function }
|
60
|
-
code_max_by(
|
65
|
+
code_max_by(code_value, **globals)
|
61
66
|
when "none?"
|
62
67
|
sig(args) { Function.maybe }
|
63
|
-
code_none?(
|
68
|
+
code_none?(code_value, **globals)
|
64
69
|
when "reduce"
|
65
70
|
sig(args) { Function }
|
66
|
-
code_reduce(
|
71
|
+
code_reduce(code_value, **globals)
|
67
72
|
when "reverse"
|
68
73
|
sig(args)
|
69
74
|
code_reverse
|
70
75
|
when "select"
|
71
76
|
sig(args) { Function }
|
72
|
-
code_select(
|
77
|
+
code_select(code_value, **globals)
|
73
78
|
when "select!"
|
74
79
|
sig(args) { Function }
|
75
|
-
code_select!(
|
80
|
+
code_select!(code_value, **globals)
|
76
81
|
when "reject"
|
77
82
|
sig(args) { Function }
|
78
|
-
code_reject(
|
83
|
+
code_reject(code_value, **globals)
|
79
84
|
when "reject!"
|
80
85
|
sig(args) { Function }
|
81
|
-
code_reject!(
|
86
|
+
code_reject!(code_value, **globals)
|
82
87
|
when "size"
|
83
88
|
sig(args)
|
84
89
|
code_size
|
@@ -94,21 +99,22 @@ class Code
|
|
94
99
|
end
|
95
100
|
|
96
101
|
def code_any?(argument = nil, **globals)
|
97
|
-
|
102
|
+
code_argument = argument.to_code
|
103
|
+
|
98
104
|
index = 0
|
99
105
|
|
100
106
|
Boolean.new(
|
101
|
-
raw.any? do |
|
102
|
-
if
|
103
|
-
|
107
|
+
raw.any? do |code_element|
|
108
|
+
if code_argument.nothing?
|
109
|
+
true
|
104
110
|
else
|
105
|
-
|
106
|
-
arguments: List.new([
|
111
|
+
code_argument.call(
|
112
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
107
113
|
**globals
|
108
114
|
).truthy?
|
109
115
|
end
|
110
116
|
rescue Error::Next => e
|
111
|
-
e.
|
117
|
+
e.code_value
|
112
118
|
ensure
|
113
119
|
index += 1
|
114
120
|
end
|
@@ -116,58 +122,75 @@ class Code
|
|
116
122
|
end
|
117
123
|
|
118
124
|
def code_append(other)
|
119
|
-
|
125
|
+
code_other = other.to_code
|
126
|
+
|
127
|
+
raw << code_other
|
128
|
+
|
120
129
|
self
|
121
130
|
end
|
122
131
|
|
123
132
|
def code_detect(argument, **globals)
|
124
|
-
|
125
|
-
|
126
|
-
|
133
|
+
code_argument = argument.to_code
|
134
|
+
|
135
|
+
raw.detect.with_index do |code_element, index|
|
136
|
+
code_argument.call(
|
137
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
127
138
|
**globals
|
128
139
|
).truthy?
|
129
140
|
rescue Error::Next => e
|
130
|
-
e.
|
141
|
+
e.code_value
|
131
142
|
end || Nothing.new
|
132
143
|
end
|
133
144
|
|
134
145
|
def code_each(argument, **globals)
|
135
|
-
|
136
|
-
|
137
|
-
|
146
|
+
code_argument = argument.to_code
|
147
|
+
|
148
|
+
raw.each.with_index do |code_element, index|
|
149
|
+
code_argument.call(
|
150
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
138
151
|
**globals
|
139
152
|
)
|
140
153
|
rescue Error::Next => e
|
141
|
-
e.
|
154
|
+
e.code_value
|
142
155
|
end
|
156
|
+
|
143
157
|
self
|
144
158
|
end
|
145
159
|
|
146
|
-
def code_first
|
147
|
-
|
160
|
+
def code_first(value = nil)
|
161
|
+
code_value = value.to_code
|
162
|
+
|
163
|
+
if code_value.nothing?
|
164
|
+
raw.first || Nothing.new
|
165
|
+
else
|
166
|
+
List.new(raw.first(code_value.raw))
|
167
|
+
end
|
148
168
|
end
|
149
169
|
|
150
170
|
def code_flatten(level = nil)
|
151
|
-
|
152
|
-
|
171
|
+
code_level = level.to_code
|
172
|
+
code_level = Integer.new(-1) if code_level.nothing?
|
173
|
+
level = code_level.raw
|
153
174
|
|
154
175
|
List.new(
|
155
|
-
raw.reduce([]) do |acc,
|
156
|
-
if
|
176
|
+
raw.reduce([]) do |acc, code_element|
|
177
|
+
if code_element.is_a?(List) && level != 0
|
157
178
|
if level.positive?
|
158
|
-
acc +
|
179
|
+
acc + code_element.code_flatten(level - 1).raw
|
159
180
|
else
|
160
|
-
acc +
|
181
|
+
acc + code_element.code_flatten(level).raw
|
161
182
|
end
|
162
183
|
else
|
163
|
-
acc + [
|
184
|
+
acc + [code_element]
|
164
185
|
end
|
165
186
|
end
|
166
187
|
)
|
167
188
|
end
|
168
189
|
|
169
190
|
def code_include?(other)
|
170
|
-
|
191
|
+
code_other = other.to_code
|
192
|
+
|
193
|
+
Boolean.new(raw.include?(code_other))
|
171
194
|
end
|
172
195
|
|
173
196
|
def code_last
|
@@ -175,26 +198,30 @@ class Code
|
|
175
198
|
end
|
176
199
|
|
177
200
|
def code_map(argument, **globals)
|
201
|
+
code_argument = argument.to_code
|
202
|
+
|
178
203
|
List.new(
|
179
|
-
raw.map.with_index do |
|
180
|
-
|
181
|
-
arguments: List.new([
|
204
|
+
raw.map.with_index do |code_element, index|
|
205
|
+
code_argument.call(
|
206
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
182
207
|
**globals
|
183
208
|
)
|
184
209
|
rescue Error::Next => e
|
185
|
-
e.
|
210
|
+
e.code_value
|
186
211
|
end
|
187
212
|
)
|
188
213
|
end
|
189
214
|
|
190
215
|
def code_map!(argument, **globals)
|
191
|
-
|
192
|
-
|
193
|
-
|
216
|
+
code_argument = argument.to_code
|
217
|
+
|
218
|
+
raw.map!.with_index do |code_element, index|
|
219
|
+
code_argument.call(
|
220
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
194
221
|
**globals
|
195
222
|
)
|
196
223
|
rescue Error::Next => e
|
197
|
-
e.
|
224
|
+
e.code_value
|
198
225
|
end
|
199
226
|
|
200
227
|
self
|
@@ -205,32 +232,35 @@ class Code
|
|
205
232
|
end
|
206
233
|
|
207
234
|
def code_max_by(argument, **globals)
|
208
|
-
|
209
|
-
|
210
|
-
|
235
|
+
code_argument = argument.to_code
|
236
|
+
|
237
|
+
raw.max_by.with_index do |code_element, index|
|
238
|
+
code_argument.call(
|
239
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
211
240
|
**globals
|
212
241
|
)
|
213
242
|
rescue Error::Next => e
|
214
|
-
e.
|
243
|
+
e.code_value
|
215
244
|
end || Nothing.new
|
216
245
|
end
|
217
246
|
|
218
247
|
def code_none?(argument = nil, **globals)
|
219
|
-
|
248
|
+
code_argument = argument.to_code
|
249
|
+
|
220
250
|
index = 0
|
221
251
|
|
222
252
|
Boolean.new(
|
223
|
-
raw.none? do |
|
224
|
-
if
|
225
|
-
|
253
|
+
raw.none? do |code_element|
|
254
|
+
if code_argument.nothing?
|
255
|
+
code_element.truthy?
|
226
256
|
else
|
227
|
-
|
228
|
-
arguments: List.new([
|
257
|
+
code_argument.call(
|
258
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
229
259
|
**globals
|
230
260
|
).truthy?
|
231
261
|
end
|
232
262
|
rescue Error::Next => e
|
233
|
-
|
263
|
+
e.code_value.truthy?
|
234
264
|
ensure
|
235
265
|
index += 1
|
236
266
|
end
|
@@ -238,13 +268,16 @@ class Code
|
|
238
268
|
end
|
239
269
|
|
240
270
|
def code_reduce(argument, **globals)
|
241
|
-
|
242
|
-
|
243
|
-
|
271
|
+
code_argument = argument.to_code
|
272
|
+
|
273
|
+
raw.reduce.with_index do |code_acc, code_element, index|
|
274
|
+
code_argument.call(
|
275
|
+
arguments:
|
276
|
+
List.new([code_acc, code_element, Integer.new(index), self]),
|
244
277
|
**globals
|
245
278
|
)
|
246
279
|
rescue Error::Next => e
|
247
|
-
e.
|
280
|
+
e.code_value
|
248
281
|
end || Nothing.new
|
249
282
|
end
|
250
283
|
|
@@ -253,61 +286,69 @@ class Code
|
|
253
286
|
end
|
254
287
|
|
255
288
|
def code_select(argument, **globals)
|
289
|
+
code_argument = argument.to_code
|
290
|
+
|
256
291
|
List.new(
|
257
|
-
raw.select.with_index do |
|
258
|
-
|
259
|
-
arguments: List.new([
|
292
|
+
raw.select.with_index do |code_element, index|
|
293
|
+
code_argument.call(
|
294
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
260
295
|
**globals
|
261
296
|
).truthy?
|
262
297
|
rescue Error::Next => e
|
263
|
-
|
298
|
+
e.code_value.truthy?
|
264
299
|
end
|
265
300
|
)
|
266
301
|
end
|
267
302
|
|
268
303
|
def code_select!(argument, **globals)
|
269
|
-
|
270
|
-
|
271
|
-
|
304
|
+
code_argument = argument.to_code
|
305
|
+
|
306
|
+
raw.select!.with_index do |code_element, index|
|
307
|
+
code_argument.call(
|
308
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
272
309
|
**globals
|
273
310
|
).truthy?
|
274
311
|
rescue Error::Next => e
|
275
|
-
|
312
|
+
e.code_value.truthy?
|
276
313
|
end
|
277
314
|
|
278
315
|
self
|
279
316
|
end
|
280
317
|
|
281
318
|
def code_reject(argument, **globals)
|
319
|
+
code_argument = argument.to_code
|
320
|
+
|
282
321
|
List.new(
|
283
|
-
raw.reject.with_index do |
|
284
|
-
|
285
|
-
arguments: List.new([
|
322
|
+
raw.reject.with_index do |code_element, index|
|
323
|
+
code_argument.call(
|
324
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
286
325
|
**globals
|
287
326
|
).truthy?
|
288
327
|
rescue Error::Next => e
|
289
|
-
|
328
|
+
e.code_value.truthy?
|
290
329
|
end
|
291
330
|
)
|
292
331
|
end
|
293
332
|
|
294
333
|
def code_reject!(argument, **globals)
|
295
|
-
|
296
|
-
|
297
|
-
|
334
|
+
code_argument = argument.to_code
|
335
|
+
|
336
|
+
raw.reject!.with_index do |code_element, index|
|
337
|
+
code_argument.call(
|
338
|
+
arguments: List.new([code_element, Integer.new(index), self]),
|
298
339
|
**globals
|
299
340
|
).truthy?
|
300
341
|
rescue Error::Next => e
|
301
|
-
|
342
|
+
e.code_value.truthy?
|
302
343
|
end
|
303
344
|
|
304
345
|
self
|
305
346
|
end
|
306
347
|
|
307
348
|
def code_join(separator = nil)
|
308
|
-
|
349
|
+
code_separator = separator.to_s.to_code
|
309
350
|
|
310
|
-
String.new(raw.join(
|
351
|
+
String.new(raw.join(code_separator.raw))
|
311
352
|
end
|
312
353
|
|
313
354
|
def code_sort
|
@@ -326,7 +367,15 @@ class Code
|
|
326
367
|
raw.inject(&:code_plus) || Nothing.new
|
327
368
|
end
|
328
369
|
|
329
|
-
|
370
|
+
def code_get(argument)
|
371
|
+
code_argument = argument.to_code
|
372
|
+
|
373
|
+
raw[code_argument] || Nothing.new
|
374
|
+
end
|
375
|
+
|
376
|
+
def any?
|
377
|
+
code_any?.truthy?
|
378
|
+
end
|
330
379
|
end
|
331
380
|
end
|
332
381
|
end
|
@@ -4,35 +4,35 @@ class Code
|
|
4
4
|
class Object
|
5
5
|
class Parameter < Object
|
6
6
|
def initialize(*args, **_kargs, &)
|
7
|
-
@raw = Dictionary.new(args.first
|
7
|
+
@raw = Dictionary.new(args.first)
|
8
8
|
end
|
9
9
|
|
10
10
|
def code_name
|
11
|
-
String.new(raw.code_get(
|
11
|
+
String.new(raw.code_get(:name))
|
12
12
|
end
|
13
13
|
|
14
14
|
def code_regular?
|
15
|
-
Boolean.new(raw.code_get(
|
15
|
+
Boolean.new(raw.code_get(:regular?))
|
16
16
|
end
|
17
17
|
|
18
18
|
def code_keyword?
|
19
|
-
Boolean.new(raw.code_get(
|
19
|
+
Boolean.new(raw.code_get(:keyword?))
|
20
20
|
end
|
21
21
|
|
22
22
|
def code_regular_splat?
|
23
|
-
Boolean.new(raw.code_get(
|
23
|
+
Boolean.new(raw.code_get(:regular_splat?))
|
24
24
|
end
|
25
25
|
|
26
26
|
def code_keyword_splat?
|
27
|
-
Boolean.new(raw.code_get(
|
27
|
+
Boolean.new(raw.code_get(:keyword_splat?))
|
28
28
|
end
|
29
29
|
|
30
30
|
def code_default
|
31
|
-
Code.new(raw.code_get(
|
31
|
+
Code.new(raw.code_get(:default))
|
32
32
|
end
|
33
33
|
|
34
|
-
def
|
35
|
-
|
34
|
+
def code_evaluate(...)
|
35
|
+
code_default.code_evaluate(...)
|
36
36
|
end
|
37
37
|
|
38
38
|
def regular?
|
@@ -50,10 +50,6 @@ class Code
|
|
50
50
|
def keyword_splat?
|
51
51
|
code_keyword_splat?.truthy?
|
52
52
|
end
|
53
|
-
|
54
|
-
def to_h
|
55
|
-
raw.raw
|
56
|
-
end
|
57
53
|
end
|
58
54
|
end
|
59
55
|
end
|