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,73 +4,78 @@ class Code
4
4
  class Object
5
5
  class Integer < Object
6
6
  def initialize(*args, **_kargs, &)
7
- whole = args.first || 0
8
- exponent = args.second || 0
9
- whole = whole.raw if whole.is_an?(Object)
10
- exponent = exponent.raw if exponent.is_an?(Object)
11
- @raw = whole.to_i * (10**exponent)
7
+ @raw =
8
+ if args.first.class.in?(NUMBER_CLASSES)
9
+ if args.second.class.in?(NUMBER_CLASSES)
10
+ (args.first.to_s.to_d * (10**args.second.to_s.to_d)).to_i
11
+ else
12
+ args.first.to_s.to_i
13
+ end
14
+ else
15
+ 0
16
+ end
12
17
  rescue FloatDomainError
13
- raise Error, "#{decimal.inspect} * 10**#{exponent.inspect} is invalid"
18
+ @raw = 0
14
19
  end
15
20
 
16
21
  def call(**args)
17
- operator = args.fetch(:operator, nil)
18
- arguments = args.fetch(:arguments, List.new)
22
+ code_operator = args.fetch(:operator, nil).to_code
23
+ code_arguments = args.fetch(:arguments, []).to_code
19
24
  globals = multi_fetch(args, *GLOBALS)
20
- value = arguments.code_first
25
+ code_value = code_arguments.code_first
21
26
 
22
- case operator.to_s
27
+ case code_operator.to_s
23
28
  when "%", "modulo"
24
29
  sig(args) { Integer | Decimal }
25
- code_modulo(value)
30
+ code_modulo(code_value)
26
31
  when "&", "bitwise_and"
27
32
  sig(args) { Integer | Decimal }
28
- code_bitwise_and(value)
33
+ code_bitwise_and(code_value)
29
34
  when "*", "multiplication", "×"
30
35
  sig(args) { Integer | Decimal | String }
31
- code_multiplication(value)
36
+ code_multiplication(code_value)
32
37
  when "**", "power"
33
38
  sig(args) { Integer | Decimal }
34
- code_power(value)
39
+ code_power(code_value)
35
40
  when "+", "plus", "self"
36
41
  sig(args) { Object.maybe }
37
- arguments.any? ? code_plus(value) : code_self
42
+ code_arguments.any? ? code_plus(code_value) : code_self
38
43
  when "-", "minus", "unary_minus"
39
44
  sig(args) { Integer | Decimal.maybe }
40
- arguments.any? ? code_minus(value) : code_unary_minus
45
+ code_arguments.any? ? code_minus(code_value) : code_unary_minus
41
46
  when "/", "division", "÷"
42
47
  sig(args) { Integer | Decimal }
43
- code_division(value)
48
+ code_division(code_value)
44
49
  when "<", "inferior"
45
50
  sig(args) { Integer | Decimal }
46
- code_inferior(value)
51
+ code_inferior(code_value)
47
52
  when "<<", "left_shift"
48
53
  sig(args) { Integer | Decimal }
49
- code_left_shift(value)
54
+ code_left_shift(code_value)
50
55
  when "<=", "inferior_or_equal"
51
56
  sig(args) { Integer | Decimal }
52
- code_inferior_or_equal(value)
57
+ code_inferior_or_equal(code_value)
53
58
  when "<=>", "compare"
54
59
  sig(args) { Integer | Decimal }
55
- code_compare(value)
60
+ code_compare(code_value)
56
61
  when ">", "superior"
57
62
  sig(args) { Integer | Decimal }
58
- code_superior(value)
63
+ code_superior(code_value)
59
64
  when ">=", "superior_or_equal"
60
65
  sig(args) { Integer | Decimal }
61
- code_superior_or_equal(value)
66
+ code_superior_or_equal(code_value)
62
67
  when ">>", "right_shift"
63
68
  sig(args) { Integer | Decimal }
64
- code_right_shift(value)
69
+ code_right_shift(code_value)
65
70
  when "^", "bitwise_xor"
66
71
  sig(args) { Integer | Decimal }
67
- code_bitwise_xor(value)
72
+ code_bitwise_xor(code_value)
68
73
  when "abs"
69
74
  sig(args)
70
75
  code_abs
71
76
  when "ceil"
72
77
  sig(args) { Integer.maybe }
73
- code_ceil(value)
78
+ code_ceil(code_value)
74
79
  when "clone"
75
80
  sig(args)
76
81
  code_clone
@@ -79,10 +84,10 @@ class Code
79
84
  code_days
80
85
  when "decrement!"
81
86
  sig(args) { Integer.maybe }
82
- code_decrement!(value)
87
+ code_decrement!(code_value)
83
88
  when "decrement"
84
89
  sig(args) { Integer.maybe }
85
- code_decrement(value)
90
+ code_decrement(code_value)
86
91
  when "eight?"
87
92
  sig(args)
88
93
  code_eight?
@@ -94,7 +99,7 @@ class Code
94
99
  code_five?
95
100
  when "floor"
96
101
  sig(args) { Integer.maybe }
97
- code_floor(value)
102
+ code_floor(code_value)
98
103
  when "four?"
99
104
  sig(args)
100
105
  code_four?
@@ -103,10 +108,10 @@ class Code
103
108
  code_hours
104
109
  when "increment!"
105
110
  sig(args) { Integer.maybe }
106
- code_increment!(value)
111
+ code_increment!(code_value)
107
112
  when "increment"
108
113
  sig(args) { Integer.maybe }
109
- code_increment(value)
114
+ code_increment(code_value)
110
115
  when "nine?"
111
116
  sig(args)
112
117
  code_nine?
@@ -118,7 +123,7 @@ class Code
118
123
  code_one?
119
124
  when "round"
120
125
  sig(args) { Integer.maybe }
121
- code_round(value)
126
+ code_round(code_value)
122
127
  when "seven?"
123
128
  sig(args)
124
129
  code_seven?
@@ -136,10 +141,10 @@ class Code
136
141
  code_three?
137
142
  when "times"
138
143
  sig(args) { Function }
139
- code_times(value, **globals)
144
+ code_times(code_value, **globals)
140
145
  when "truncate"
141
146
  sig(args) { Integer.maybe }
142
- code_truncate(value)
147
+ code_truncate(code_value)
143
148
  when "two?"
144
149
  sig(args)
145
150
  code_two?
@@ -148,7 +153,7 @@ class Code
148
153
  code_zero?
149
154
  when "|", "bitwise_or"
150
155
  sig(args) { Integer | Decimal }
151
- code_bitwise_or(value)
156
+ code_bitwise_or(code_value)
152
157
  else
153
158
  super
154
159
  end
@@ -159,20 +164,24 @@ class Code
159
164
  end
160
165
 
161
166
  def code_bitwise_and(other)
162
- Integer.new(raw & other.raw.to_i)
167
+ code_other = other.to_code
168
+ Integer.new(raw & code_other.raw.to_i)
163
169
  end
164
170
 
165
171
  def code_bitwise_or(other)
166
- Integer.new(raw | other.raw.to_i)
172
+ code_other = other.to_code
173
+ Integer.new(raw | code_other.raw.to_i)
167
174
  end
168
175
 
169
176
  def code_bitwise_xor(other)
170
- Integer.new(raw ^ other.raw.to_i)
177
+ code_other = other.to_code
178
+ Integer.new(raw ^ code_other.raw.to_i)
171
179
  end
172
180
 
173
181
  def code_ceil(n = nil)
174
- n = Integer.new(0) if n.nil? || n.is_a?(Nothing)
175
- Integer.new(raw.ceil(n.raw))
182
+ code_n = n.to_code
183
+ code_n = Integer.new(0) if code_n.nothing?
184
+ Integer.new(raw.ceil(code_n.raw))
176
185
  end
177
186
 
178
187
  def code_clone
@@ -180,26 +189,30 @@ class Code
180
189
  end
181
190
 
182
191
  def code_compare(other)
183
- Integer.new(raw <=> other.raw)
192
+ code_other = other.to_code
193
+ Integer.new(raw <=> code_other.raw)
184
194
  end
185
195
 
186
196
  def code_decrement!(n = nil)
187
- n = Integer.new(1) if n.nil? || n.is_a?(Nothing)
188
- @raw -= n.raw
197
+ code_n = n.to_code
198
+ code_n = Integer.new(1) if code_n.nothing?
199
+ @raw -= code_n.raw
189
200
  self
190
201
  end
191
202
 
192
203
  def code_decrement(n = nil)
193
- n = Integer.new(1) if n.nil? || n.is_a?(Nothing)
194
- Integer.new(raw - n.raw)
204
+ code_n = n.to_code
205
+ code_n = Integer.new(1) if code_n.nothing?
206
+ Integer.new(raw - code_n.raw)
195
207
  end
196
208
 
197
209
  def code_division(other)
198
- Decimal.new(BigDecimal(raw) / other.raw)
210
+ code_other = other.to_code
211
+ Decimal.new(BigDecimal(raw) / code_other.raw)
199
212
  end
200
213
 
201
214
  def code_eight?
202
- Boolean.new(raw == 8)
215
+ Boolean.new(raw.eight?)
203
216
  end
204
217
 
205
218
  def code_even?
@@ -207,69 +220,81 @@ class Code
207
220
  end
208
221
 
209
222
  def code_five?
210
- Boolean.new(raw == 5)
223
+ Boolean.new(raw.five?)
211
224
  end
212
225
 
213
226
  def code_floor(n = nil)
214
- n = Integer.new(0) if n.nil? || n.is_a?(Nothing)
215
- Integer.new(raw.floor(n.raw))
227
+ code_n = n.to_code
228
+ code_n = Integer.new(0) if code_n.nothing?
229
+ Integer.new(raw.floor(code_n.raw))
216
230
  end
217
231
 
218
232
  def code_four?
219
- Boolean.new(raw == 4)
233
+ Boolean.new(raw.four?)
220
234
  end
221
235
 
222
236
  def code_increment!(n = nil)
223
- n = Integer.new(1) if n.nil? || n.is_a?(Nothing)
224
- @raw += n.raw
237
+ code_n = n.to_code
238
+ code_n = Integer.new(1) if code_n.nothing?
239
+ @raw += code_n.raw
225
240
  self
226
241
  end
227
242
 
228
243
  def code_increment(n = nil)
229
- n = Integer.new(1) if n.nil? || n.is_a?(Nothing)
230
- Integer.new(raw + n.raw)
244
+ code_n = n.to_code
245
+ code_n = Integer.new(1) if code_n.nothing?
246
+ Integer.new(raw + code_n.raw)
231
247
  end
232
248
 
233
249
  def code_inferior(other)
234
- Boolean.new(raw < other.raw)
250
+ code_other = other.to_code
251
+ Boolean.new(raw < code_other.raw)
235
252
  end
236
253
 
237
254
  def code_inferior_or_equal(other)
238
- Boolean.new(raw <= other.raw)
255
+ code_other = other.to_code
256
+ Boolean.new(raw <= code_other.raw)
239
257
  end
240
258
 
241
259
  def code_left_shift(other)
242
- Integer.new(raw << other.raw.to_i)
260
+ code_other = other.to_code
261
+ Integer.new(raw << code_other.raw.to_i)
243
262
  end
244
263
 
245
264
  def code_minus(other)
246
- if other.is_a?(Integer)
247
- Integer.new(raw - other.raw)
265
+ code_other = other.to_code
266
+
267
+ if code_other.is_a?(Integer)
268
+ Integer.new(raw - code_other.raw)
248
269
  else
249
- Decimal.new(raw - other.raw)
270
+ Decimal.new(raw - code_other.raw)
250
271
  end
251
272
  end
252
273
 
253
274
  def code_modulo(other)
254
- if other.is_a?(Integer)
255
- Integer.new(raw % other.raw)
275
+ code_other = other.to_code
276
+
277
+ if code_other.is_a?(Integer)
278
+ Integer.new(raw % code_other.raw)
256
279
  else
257
- Decimal.new(raw % other.raw)
280
+ Decimal.new(raw % code_other.raw)
258
281
  end
259
282
  end
260
283
 
261
284
  def code_multiplication(other)
262
- if other.is_a?(Integer)
263
- Integer.new(raw * other.raw)
264
- elsif other.is_a?(Decimal)
265
- Decimal.new(raw * other.raw)
285
+ code_other = other.to_code
286
+
287
+ if code_other.is_a?(Integer)
288
+ Integer.new(raw * code_other.raw)
289
+ elsif code_other.is_a?(Decimal)
290
+ Decimal.new(raw * code_other.raw)
266
291
  else
267
- String.new(other.raw * raw)
292
+ String.new(code_other.raw * raw)
268
293
  end
269
294
  end
270
295
 
271
296
  def code_nine?
272
- Boolean.new(raw == 9)
297
+ Boolean.new(raw.nine?)
273
298
  end
274
299
 
275
300
  def code_odd?
@@ -281,17 +306,21 @@ class Code
281
306
  end
282
307
 
283
308
  def code_plus(other)
284
- if other.is_a?(Integer)
285
- Integer.new(raw + other.raw)
286
- elsif other.is_a?(Decimal)
287
- Decimal.new(raw + other.raw)
309
+ code_other = other.to_code
310
+
311
+ if code_other.is_a?(Integer)
312
+ Integer.new(raw + code_other.raw)
313
+ elsif code_other.is_a?(Decimal)
314
+ Decimal.new(raw + code_other.raw)
288
315
  else
289
- String.new(to_s + other.to_s)
316
+ String.new(to_s + code_other.to_s)
290
317
  end
291
318
  end
292
319
 
293
320
  def code_power(other)
294
- if other.is_a?(Integer)
321
+ code_other = other.to_code
322
+
323
+ if code_other.is_a?(Integer)
295
324
  Integer.new(raw**other.raw)
296
325
  else
297
326
  Decimal.new(raw**other.raw)
@@ -299,20 +328,23 @@ class Code
299
328
  end
300
329
 
301
330
  def code_right_shift(other)
302
- Integer.new(raw >> other.raw.to_i)
331
+ code_other = other.to_code
332
+ Integer.new(raw >> code_other.raw.to_i)
303
333
  end
304
334
 
305
335
  def code_round(n = nil)
306
- n = Integer.new(0) if n.nil? || n.is_a?(Nothing)
307
- Integer.new(raw.round(n.raw))
336
+ code_n = n.to_code
337
+ code_n = Integer.new(0) if code_n.nothing?
338
+
339
+ Integer.new(raw.round(code_n.raw))
308
340
  end
309
341
 
310
342
  def code_seven?
311
- Boolean.new(raw == 7)
343
+ Boolean.new(raw.seven?)
312
344
  end
313
345
 
314
346
  def code_six?
315
- Boolean.new(raw == 6)
347
+ Boolean.new(raw.six?)
316
348
  end
317
349
 
318
350
  def code_sqrt
@@ -320,19 +352,23 @@ class Code
320
352
  end
321
353
 
322
354
  def code_superior(other)
323
- Boolean.new(raw > other.raw)
355
+ code_other = other.to_code
356
+
357
+ Boolean.new(raw > code_other.raw)
324
358
  end
325
359
 
326
360
  def code_superior_or_equal(other)
327
- Boolean.new(raw >= other.raw)
361
+ code_other = other.to_code
362
+
363
+ Boolean.new(raw >= code_other.raw)
328
364
  end
329
365
 
330
366
  def code_ten?
331
- Boolean.new(raw == 10)
367
+ Boolean.new(raw.ten?)
332
368
  end
333
369
 
334
370
  def code_three?
335
- Boolean.new(raw == 3)
371
+ Boolean.new(raw.three?)
336
372
  end
337
373
 
338
374
  def code_to_decimal
@@ -344,8 +380,10 @@ class Code
344
380
  end
345
381
 
346
382
  def code_times(argument, **globals)
383
+ code_argument = argument.to_code
384
+
347
385
  raw.times do |element|
348
- argument.call(
386
+ code_argument.call(
349
387
  arguments: List.new([Integer.new(element), self]),
350
388
  **globals
351
389
  )
@@ -355,12 +393,14 @@ class Code
355
393
  end
356
394
 
357
395
  def code_truncate(n = nil)
358
- n = Integer.new(0) if n.nil? || n.is_a?(Nothing)
359
- Integer.new(raw.truncate(n.raw))
396
+ code_n = n.to_code
397
+ code_n = Integer.new(0) if code_n.nothing?
398
+
399
+ Integer.new(raw.truncate(code_n.raw))
360
400
  end
361
401
 
362
402
  def code_two?
363
- Boolean.new(raw == 2)
403
+ Boolean.new(raw.two?)
364
404
  end
365
405
 
366
406
  def code_unary_minus
@@ -3,31 +3,27 @@
3
3
  class Code
4
4
  class Object
5
5
  class Json < Object
6
- def self.to_code(json)
7
- if json.is_an?(Object)
8
- json
9
- elsif json.is_a?(::Hash)
10
- Dictionary.new(
11
- json
12
- .transform_keys { |key| Json.to_code(key) }
13
- .transform_values { |value| Json.to_code(value) }
14
- )
15
- elsif json.is_a?(::Array)
16
- List.new(json.map { |element| Json.to_code(element) })
17
- elsif json.is_a?(::String)
18
- String.new(json)
19
- elsif json.is_a?(::Float)
20
- Decimal.new(json)
21
- elsif json.is_an?(::Integer)
22
- Integer.new(json)
23
- elsif json.is_a?(::TrueClass) || json.is_a?(::FalseClass)
24
- Boolean.new(json)
25
- elsif json.is_a?(::NilClass)
26
- Nothing.new(json)
6
+ def self.call(**args)
7
+ code_operator = args.fetch(:operator, nil).to_code
8
+ code_arguments = args.fetch(:arguments, []).to_code
9
+ code_value = code_arguments.code_first
10
+
11
+ case code_operator.to_s
12
+ when "parse"
13
+ sig(args) { String }
14
+ code_parse(code_value)
27
15
  else
28
- raise Error, "#{json.inspect} not supported"
16
+ super
29
17
  end
30
18
  end
19
+
20
+ def self.code_parse(value)
21
+ code_value = value.to_code
22
+
23
+ ::JSON.parse(code_value.raw).to_code
24
+ rescue JSON::ParserError
25
+ Nothing.new
26
+ end
31
27
  end
32
28
  end
33
29
  end