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