code-ruby 1.1.3 → 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.
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 +219 -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 +141 -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 +50 -3
@@ -4,81 +4,86 @@ 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)
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?(value)
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(value, **globals)
56
+ code_map(code_value, **globals)
52
57
  when "map!"
53
58
  sig(args) { Function }
54
- code_map!(value, **globals)
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(value, **globals)
65
+ code_max_by(code_value, **globals)
61
66
  when "none?"
62
67
  sig(args) { Function.maybe }
63
- code_none?(value, **globals)
68
+ code_none?(code_value, **globals)
64
69
  when "reduce"
65
70
  sig(args) { Function }
66
- code_reduce(value, **globals)
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(value, **globals)
77
+ code_select(code_value, **globals)
73
78
  when "select!"
74
79
  sig(args) { Function }
75
- code_select!(value, **globals)
80
+ code_select!(code_value, **globals)
76
81
  when "reject"
77
82
  sig(args) { Function }
78
- code_reject(value, **globals)
83
+ code_reject(code_value, **globals)
79
84
  when "reject!"
80
85
  sig(args) { Function }
81
- code_reject!(value, **globals)
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
- argument ||= Nothing.new
102
+ code_argument = argument.to_code
103
+
98
104
  index = 0
99
105
 
100
106
  Boolean.new(
101
- raw.any? do |element|
102
- if argument.nothing?
103
- element.truthy?
107
+ raw.any? do |code_element|
108
+ if code_argument.nothing?
109
+ true
104
110
  else
105
- argument.call(
106
- arguments: List.new([element, Integer.new(index), self]),
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.value || Nothing.new
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
- raw << other
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
- raw.detect.with_index do |element, index|
125
- argument.call(
126
- arguments: List.new([element, Integer.new(index), self]),
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.value || Nothing.new
141
+ e.code_value
131
142
  end || Nothing.new
132
143
  end
133
144
 
134
145
  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]),
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.value || Nothing.new
154
+ e.code_value
142
155
  end
156
+
143
157
  self
144
158
  end
145
159
 
146
- def code_first
147
- raw.first || Nothing.new
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
- level = Integer.new(-1) if level.nil? || level.is_a?(Nothing)
152
- level = level.raw if level.is_a?(Integer)
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, element|
156
- if element.is_a?(List) && level != 0
176
+ raw.reduce([]) do |acc, code_element|
177
+ if code_element.is_a?(List) && level != 0
157
178
  if level.positive?
158
- acc + element.code_flatten(level - 1).raw
179
+ acc + code_element.code_flatten(level - 1).raw
159
180
  else
160
- acc + element.code_flatten(level).raw
181
+ acc + code_element.code_flatten(level).raw
161
182
  end
162
183
  else
163
- acc + [element]
184
+ acc + [code_element]
164
185
  end
165
186
  end
166
187
  )
167
188
  end
168
189
 
169
190
  def code_include?(other)
170
- Boolean.new(raw.include?(other))
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 |element, index|
180
- argument.call(
181
- arguments: List.new([element, Integer.new(index), self]),
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.value || Nothing.new
210
+ e.code_value
186
211
  end
187
212
  )
188
213
  end
189
214
 
190
215
  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]),
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.value || Nothing.new
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
- raw.max_by.with_index do |element, index|
209
- argument.call(
210
- arguments: List.new([element, Integer.new(index), self]),
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.value || Nothing.new
243
+ e.code_value
215
244
  end || Nothing.new
216
245
  end
217
246
 
218
247
  def code_none?(argument = nil, **globals)
219
- argument ||= Nothing.new
248
+ code_argument = argument.to_code
249
+
220
250
  index = 0
221
251
 
222
252
  Boolean.new(
223
- raw.none? do |element|
224
- if argument.nothing?
225
- element.truthy?
253
+ raw.none? do |code_element|
254
+ if code_argument.nothing?
255
+ code_element.truthy?
226
256
  else
227
- argument.call(
228
- arguments: List.new([element, Integer.new(index), self]),
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
- (e.value || Nothing.new).truthy?
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
- raw.reduce.with_index do |acc, element, index|
242
- argument.call(
243
- arguments: List.new([acc, element, Integer.new(index), self]),
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.value || Nothing.new
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 |element, index|
258
- argument.call(
259
- arguments: List.new([element, Integer.new(index), self]),
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
- (e.value || Nothing.new).truthy?
298
+ e.code_value.truthy?
264
299
  end
265
300
  )
266
301
  end
267
302
 
268
303
  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]),
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
- (e.value || Nothing.new).truthy?
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 |element, index|
284
- argument.call(
285
- arguments: List.new([element, Integer.new(index), self]),
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
- (e.value || Nothing.new).truthy?
328
+ e.code_value.truthy?
290
329
  end
291
330
  )
292
331
  end
293
332
 
294
333
  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]),
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
- (e.value || Nothing.new).truthy?
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
- separator ||= +""
349
+ code_separator = separator.to_s.to_code
309
350
 
310
- String.new(raw.join(separator.raw))
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
- delegate :any?, to: :raw
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.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