code-ruby 0.5.6 → 0.6.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 (218) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -3
  3. data/Gemfile.lock +11 -21
  4. data/README.md +2 -102
  5. data/bin/code +34 -16
  6. data/code-ruby.gemspec +5 -3
  7. data/lib/code/error.rb +16 -5
  8. data/lib/code/node/base_10.rb +4 -2
  9. data/lib/code/node/base_16.rb +3 -1
  10. data/lib/code/node/base_2.rb +3 -1
  11. data/lib/code/node/base_8.rb +3 -1
  12. data/lib/code/node/boolean.rb +4 -2
  13. data/lib/code/node/call.rb +35 -12
  14. data/lib/code/node/call_argument.rb +16 -5
  15. data/lib/code/node/code.rb +5 -4
  16. data/lib/code/node/decimal.rb +2 -0
  17. data/lib/code/node/{dictionnary.rb → dictionary.rb} +14 -5
  18. data/lib/code/node/function.rb +7 -4
  19. data/lib/code/node/function_parameter.rb +3 -1
  20. data/lib/code/node/if.rb +5 -3
  21. data/lib/code/node/left_operation.rb +63 -0
  22. data/lib/code/node/list.rb +2 -0
  23. data/lib/code/node/negation.rb +2 -0
  24. data/lib/code/node/not.rb +2 -0
  25. data/lib/code/node/nothing.rb +3 -1
  26. data/lib/code/node/number.rb +2 -0
  27. data/lib/code/node/right_operation.rb +70 -0
  28. data/lib/code/node/splat.rb +2 -0
  29. data/lib/code/node/square_bracket.rb +37 -0
  30. data/lib/code/node/statement.rb +13 -5
  31. data/lib/code/node/string.rb +3 -1
  32. data/lib/code/node/ternary.rb +5 -3
  33. data/lib/code/node/unary_minus.rb +2 -0
  34. data/lib/code/node/while.rb +6 -4
  35. data/lib/code/node.rb +11 -5
  36. data/lib/code/object/argument.rb +13 -7
  37. data/lib/code/object/boolean.rb +43 -5
  38. data/lib/code/object/class.rb +17 -0
  39. data/lib/code/object/context.rb +36 -0
  40. data/lib/code/object/decimal.rb +252 -100
  41. data/lib/code/object/dictionary.rb +641 -0
  42. data/lib/code/object/function.rb +54 -27
  43. data/lib/code/object/global.rb +65 -19
  44. data/lib/code/object/identifier_list.rb +47 -0
  45. data/lib/code/object/integer.rb +320 -137
  46. data/lib/code/object/list.rb +140 -138
  47. data/lib/code/object/nothing.rb +10 -4
  48. data/lib/code/object/number.rb +6 -1
  49. data/lib/code/object/range.rb +85 -88
  50. data/lib/code/object/ruby_function.rb +11 -6
  51. data/lib/code/object/string.rb +51 -48
  52. data/lib/code/object.rb +117 -139
  53. data/lib/code/parser/addition.rb +4 -2
  54. data/lib/code/parser/and_operator.rb +4 -2
  55. data/lib/code/parser/bitwise_and.rb +4 -2
  56. data/lib/code/parser/bitwise_or.rb +4 -2
  57. data/lib/code/parser/boolean.rb +3 -1
  58. data/lib/code/parser/call.rb +17 -11
  59. data/lib/code/parser/chained_call.rb +10 -22
  60. data/lib/code/parser/class.rb +9 -6
  61. data/lib/code/parser/code.rb +6 -4
  62. data/lib/code/parser/{dictionnary.rb → dictionary.rb} +16 -13
  63. data/lib/code/parser/equal.rb +9 -36
  64. data/lib/code/parser/equality.rb +4 -2
  65. data/lib/code/parser/function.rb +24 -9
  66. data/lib/code/parser/greater.rb +6 -3
  67. data/lib/code/parser/group.rb +4 -2
  68. data/lib/code/parser/if.rb +6 -4
  69. data/lib/code/parser/if_modifier.rb +5 -25
  70. data/lib/code/parser/left_operation.rb +40 -0
  71. data/lib/code/parser/list.rb +6 -5
  72. data/lib/code/parser/multiplication.rb +4 -2
  73. data/lib/code/parser/name.rb +19 -4
  74. data/lib/code/parser/negation.rb +4 -2
  75. data/lib/code/parser/not_keyword.rb +5 -3
  76. data/lib/code/parser/nothing.rb +3 -10
  77. data/lib/code/parser/number.rb +4 -2
  78. data/lib/code/parser/or_keyword.rb +4 -2
  79. data/lib/code/parser/or_operator.rb +4 -2
  80. data/lib/code/parser/power.rb +4 -28
  81. data/lib/code/parser/range.rb +4 -2
  82. data/lib/code/parser/rescue.rb +6 -26
  83. data/lib/code/parser/right_operation.rb +40 -0
  84. data/lib/code/parser/shift.rb +4 -2
  85. data/lib/code/parser/splat.rb +5 -3
  86. data/lib/code/parser/square_bracket.rb +48 -0
  87. data/lib/code/parser/statement.rb +3 -1
  88. data/lib/code/parser/string.rb +12 -10
  89. data/lib/code/parser/ternary.rb +10 -11
  90. data/lib/code/parser/unary_minus.rb +5 -3
  91. data/lib/code/parser/while.rb +5 -3
  92. data/lib/code/parser/whitespace.rb +2 -0
  93. data/lib/code/parser.rb +10 -3
  94. data/lib/code/ruby.rb +4 -2
  95. data/lib/code/type/hash.rb +38 -0
  96. data/lib/code/type/maybe.rb +29 -0
  97. data/lib/code/type/or.rb +38 -0
  98. data/lib/code/type/repeat.rb +38 -0
  99. data/lib/code/type/sig.rb +130 -0
  100. data/lib/code/type.rb +25 -0
  101. data/lib/code/version.rb +3 -0
  102. data/lib/code-ruby.rb +1 -2
  103. data/lib/code.rb +15 -16
  104. data/spec/code/node/call_spec.rb +39 -0
  105. data/spec/code/object/boolean_spec.rb +18 -0
  106. data/spec/code/object/decimal_spec.rb +51 -0
  107. data/spec/code/object/dictionary_spec.rb +98 -0
  108. data/spec/code/object/function_spec.rb +42 -0
  109. data/spec/code/object/integer_spec.rb +43 -0
  110. data/spec/code/object/nothing_spec.rb +14 -0
  111. data/spec/code/object/range_spec.rb +23 -0
  112. data/spec/code/parser/boolean_spec.rb +5 -10
  113. data/spec/code/parser/chained_call.rb +4 -5
  114. data/spec/code/parser/{dictionnary_spec.rb → dictionary_spec.rb} +5 -6
  115. data/spec/code/parser/function_spec.rb +4 -5
  116. data/spec/code/parser/group_spec.rb +5 -12
  117. data/spec/code/parser/if_modifier_spec.rb +18 -0
  118. data/spec/code/parser/list_spec.rb +4 -5
  119. data/spec/code/parser/number_spec.rb +4 -5
  120. data/spec/code/parser/string_spec.rb +4 -5
  121. data/spec/code/parser_spec.rb +22 -16
  122. data/spec/code/type_spec.rb +21 -0
  123. data/spec/code_spec.rb +171 -0
  124. data/spec/spec_helper.rb +1 -6
  125. metadata +63 -136
  126. data/.cherry.js +0 -21
  127. data/.editorconfig +0 -9
  128. data/.github/workflows/rspec.yml +0 -14
  129. data/.gitignore +0 -2
  130. data/.prettierrc +0 -3
  131. data/.tool-versions +0 -1
  132. data/CHANGELOG.md +0 -55
  133. data/LICENSE +0 -7
  134. data/TODO +0 -17
  135. data/bin/format +0 -3
  136. data/bin/publish +0 -19
  137. data/bin/template +0 -85
  138. data/bin/test +0 -17
  139. data/docs/class.code +0 -9
  140. data/docs/euler/1.template +0 -10
  141. data/docs/euler/2.template +0 -16
  142. data/docs/euler/3.template +0 -16
  143. data/docs/euler/4.template +0 -10
  144. data/docs/euler/5.template +0 -13
  145. data/docs/fibonnaci.template +0 -14
  146. data/docs/meetup.code +0 -12
  147. data/docs/precedence.template +0 -36
  148. data/docs/rain.code +0 -22
  149. data/docs/slack.code +0 -17
  150. data/docs/stripe.code +0 -7
  151. data/docs/twitter.code +0 -9
  152. data/language-ruby.gemspec +0 -17
  153. data/lib/code/node/chained_call.rb +0 -23
  154. data/lib/code/node/equal.rb +0 -34
  155. data/lib/code/node/if_modifier.rb +0 -47
  156. data/lib/code/node/operation.rb +0 -38
  157. data/lib/code/node/power.rb +0 -20
  158. data/lib/code/node/rescue.rb +0 -17
  159. data/lib/code/object/dictionnary.rb +0 -96
  160. data/lib/code/parser/equality_lower.rb +0 -9
  161. data/lib/code/parser/operation.rb +0 -35
  162. data/lib/language/atom.rb +0 -342
  163. data/lib/language/output.rb +0 -130
  164. data/lib/language/parser/absent/present.rb +0 -8
  165. data/lib/language/parser/absent.rb +0 -6
  166. data/lib/language/parser/end_of_input.rb +0 -6
  167. data/lib/language/parser/interuption.rb +0 -38
  168. data/lib/language/parser/not_end_of_input.rb +0 -6
  169. data/lib/language/parser/str/not_found.rb +0 -16
  170. data/lib/language/parser/str.rb +0 -6
  171. data/lib/language/parser.rb +0 -53
  172. data/lib/language-ruby.rb +0 -10
  173. data/lib/language.rb +0 -80
  174. data/lib/template/node/code_part.rb +0 -13
  175. data/lib/template/node/part.rb +0 -19
  176. data/lib/template/node/template.rb +0 -15
  177. data/lib/template/node/text_part.rb +0 -13
  178. data/lib/template/node.rb +0 -4
  179. data/lib/template/parser/template.rb +0 -39
  180. data/lib/template/parser.rb +0 -19
  181. data/lib/template/version.rb +0 -3
  182. data/lib/template-ruby.rb +0 -10
  183. data/lib/template.rb +0 -50
  184. data/spec/code/addition_spec.rb +0 -13
  185. data/spec/code/and_operator_spec.rb +0 -13
  186. data/spec/code/bitwise_and_spec.rb +0 -13
  187. data/spec/code/bitwise_or_spec.rb +0 -13
  188. data/spec/code/boolean_spec.rb +0 -13
  189. data/spec/code/call_spec.rb +0 -21
  190. data/spec/code/chained_call_spec.rb +0 -16
  191. data/spec/code/code_spec.rb +0 -29
  192. data/spec/code/dictionnary_spec.rb +0 -17
  193. data/spec/code/equal_spec.rb +0 -26
  194. data/spec/code/equality_spec.rb +0 -13
  195. data/spec/code/function_spec.rb +0 -18
  196. data/spec/code/greater_spec.rb +0 -18
  197. data/spec/code/group_spec.rb +0 -12
  198. data/spec/code/if_modifier_spec.rb +0 -20
  199. data/spec/code/if_spec.rb +0 -25
  200. data/spec/code/list_spec.rb +0 -19
  201. data/spec/code/multiplication_spec.rb +0 -18
  202. data/spec/code/negation_spec.rb +0 -20
  203. data/spec/code/not_keyword_spec.rb +0 -13
  204. data/spec/code/nothing_spec.rb +0 -17
  205. data/spec/code/number_spec.rb +0 -22
  206. data/spec/code/or_keyword_spec.rb +0 -17
  207. data/spec/code/or_operator_spec.rb +0 -16
  208. data/spec/code/parser/call_spec.rb +0 -26
  209. data/spec/code/power_spec.rb +0 -13
  210. data/spec/code/range_spec.rb +0 -16
  211. data/spec/code/rescue_spec.rb +0 -13
  212. data/spec/code/shift_spec.rb +0 -13
  213. data/spec/code/splat_spec.rb +0 -13
  214. data/spec/code/string_spec.rb +0 -27
  215. data/spec/code/ternary_spec.rb +0 -18
  216. data/spec/code/unary_minus_spec.rb +0 -13
  217. data/spec/code/while_spec.rb +0 -18
  218. data/template-ruby.gemspec +0 -19
@@ -0,0 +1,641 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Code
4
+ class Object
5
+ class Dictionary < ::Code::Object
6
+ attr_reader :raw
7
+
8
+ def initialize(raw = {})
9
+ @raw = raw
10
+ end
11
+
12
+ def self.name
13
+ "Dictionary"
14
+ end
15
+
16
+ def call(**args)
17
+ operator = args.fetch(:operator, nil)
18
+ arguments = args.fetch(:arguments, [])
19
+ globals = multi_fetch(args, *GLOBALS)
20
+ value = arguments.first&.value
21
+
22
+ case operator.to_s
23
+ when "<", "inferior"
24
+ sig(args) { Dictionary }
25
+ code_inferior(value)
26
+ when "<=", "inferior_or_equal"
27
+ sig(args) { Dictionary }
28
+ code_inferior_or_equal(value)
29
+ when "<=>", "compare"
30
+ sig(args) { Dictionary }
31
+ code_compare(value)
32
+ when ">", "superior"
33
+ sig(args) { Dictionary }
34
+ code_superior(value)
35
+ when ">=", "superior_or_equal"
36
+ sig(args) { Dictionary }
37
+ code_superior_or_equal(value)
38
+ when "[]", "at", "get"
39
+ sig(args) { Object }
40
+ code_get(value)
41
+ when "any?"
42
+ sig(args) { Function | Class }
43
+ code_any?(value, **globals)
44
+ when "clear"
45
+ sig(args)
46
+ code_clear
47
+ when "compact!"
48
+ sig(args)
49
+ code_compact!
50
+ when "compact"
51
+ sig(args)
52
+ code_compact
53
+ when "delete"
54
+ sig(args) { Object.repeat(1) }
55
+ code_delete(*arguments.map(&:value), **globals)
56
+ when "delete_if"
57
+ sig(args) { Function | Class }
58
+ code_delete_if(value, **globals)
59
+ when "delete_unless"
60
+ sig(args) { Function | Class }
61
+ code_delete_unless(value, **globals)
62
+ when "dig"
63
+ sig(args) { Object.repeat(1) }
64
+ code_dig(*arguments.map(&:value))
65
+ when "each"
66
+ sig(args) { Function }
67
+ code_each(value, **globals)
68
+ when "eight?"
69
+ sig(args)
70
+ code_eight?
71
+ when "empty?"
72
+ sig(args)
73
+ code_empty?
74
+ when "except"
75
+ sig(args) { Object.repeat(1) }
76
+ code_except(*arguments.map(&:value))
77
+ when "fetch"
78
+ sig(args) { Object.repeat(1) }
79
+ code_fetch(*arguments.map(&:value), **globals)
80
+ when "fetch_values"
81
+ sig(args) { Object.repeat(1) }
82
+ code_fetch_values(*arguments.map(&:value))
83
+ when "five?"
84
+ sig(args)
85
+ code_five?
86
+ when "flatten"
87
+ sig(args) { Integer.maybe }
88
+ code_flatten(value)
89
+ when "four?"
90
+ sig(args)
91
+ code_four?
92
+ when "has_key?"
93
+ sig(args) { Object }
94
+ code_has_key?(value)
95
+ when "has_value?"
96
+ sig(args) { Object }
97
+ code_has_value?(value)
98
+ when "invert"
99
+ sig(args)
100
+ code_invert
101
+ when "keep_if"
102
+ sig(args) { Function | Class }
103
+ code_keep_if(value, **globals)
104
+ when "keep_unless"
105
+ sig(args) { Function | Class }
106
+ code_keep_unless(value, **globals)
107
+ when "key"
108
+ sig(args) { [Object, Function.maybe] }
109
+ code_key(*arguments.map(&:value), **globals)
110
+ when "keys"
111
+ sig(args)
112
+ code_keys
113
+ when "merge"
114
+ sig(args) { [Dictionary.repeat, Function.maybe] }
115
+ code_merge(*arguments.map(&:value), **globals)
116
+ when "nine?"
117
+ sig(args)
118
+ code_nine?
119
+ when "one?"
120
+ sig(args)
121
+ code_one?
122
+ when "select!", "filter!"
123
+ sig(args) { Function | Class }
124
+ code_select!(value, **globals)
125
+ when "select", "filter"
126
+ sig(args) { Function | Class }
127
+ code_select(value, **globals)
128
+ when "seven?"
129
+ sig(args)
130
+ code_seven?
131
+ when "six?"
132
+ sig(args)
133
+ code_six?
134
+ when "size"
135
+ sig(args)
136
+ code_size
137
+ when "ten?"
138
+ sig(args)
139
+ code_ten?
140
+ when "three?"
141
+ sig(args)
142
+ code_three?
143
+ when "to_list"
144
+ sig(args)
145
+ code_to_list
146
+ when "transform_values"
147
+ sig(args) { Function }
148
+ code_transform_values(value, **globals)
149
+ when "two?"
150
+ sig(args)
151
+ code_two?
152
+ when "values"
153
+ sig(args)
154
+ code_values
155
+ when "zero?"
156
+ sig(args)
157
+ code_zero?
158
+ when ->(operator) { code_has_key?(String.new(operator)).truthy? }
159
+ result = code_fetch(operator)
160
+
161
+ if result.is_a?(Function)
162
+ result.call(**args.merge(operator: nil))
163
+ else
164
+ sig(args)
165
+ result
166
+ end
167
+ else
168
+ super
169
+ end
170
+ end
171
+
172
+ def code_any?(argument, **globals)
173
+ if argument.is_a?(Class)
174
+ Boolean.new(raw.any? { |_, value| value.is_a?(argument.raw) })
175
+ else
176
+ index = 0
177
+
178
+ Boolean.new(
179
+ raw.any? do |key, value|
180
+ argument
181
+ .call(
182
+ arguments: [
183
+ Argument.new(key),
184
+ Argument.new(value),
185
+ Argument.new(self),
186
+ Argument.new(Integer.new(index))
187
+ ],
188
+ **globals
189
+ )
190
+ .truthy?
191
+ .tap { index += 1 }
192
+ end
193
+ )
194
+ end
195
+ end
196
+
197
+ def code_clear
198
+ @raw = {}
199
+ self
200
+ end
201
+
202
+ def code_compact
203
+ self.class.new(raw.dup.delete_if { |_, value| value.falsy? })
204
+ end
205
+
206
+ def code_compact!
207
+ raw.delete_if { |_, value| value.falsy? }
208
+ self
209
+ end
210
+
211
+ def code_compare(other)
212
+ Integer.new(raw <=> other.raw)
213
+ end
214
+
215
+ def code_delete(*arguments, index: Integer.new(0), **globals)
216
+ default =
217
+ (arguments.last if arguments.last.is_a?(Function) && arguments.size > 1)
218
+
219
+ arguments = arguments[..-2] if default
220
+
221
+ if arguments.one?
222
+ raw.delete(arguments.first) do
223
+ if default
224
+ default.call(
225
+ arguments: [
226
+ Argument.new(arguments.first),
227
+ Argument.new(self),
228
+ Argument.new(index)
229
+ ],
230
+ **globals
231
+ )
232
+ else
233
+ raise(
234
+ Code::Error::KeyNotFound,
235
+ "#{arguments.first.inspect} not found on #{inspect}"
236
+ )
237
+ end
238
+ end
239
+ else
240
+ self.class.new(
241
+ arguments
242
+ .map
243
+ .with_index do |argument, index|
244
+ if default
245
+ [
246
+ argument,
247
+ code_delete(
248
+ argument,
249
+ default,
250
+ index: Integer.new(index),
251
+ **globals
252
+ )
253
+ ]
254
+ else
255
+ [
256
+ argument,
257
+ code_delete(argument, index: Integer.new(index), **globals)
258
+ ]
259
+ end
260
+ end
261
+ .to_h
262
+ )
263
+ end
264
+ end
265
+
266
+ def code_delete_if(argument, **globals)
267
+ if argument.is_a?(Class)
268
+ raw.delete_if { |_, value| value.is_a?(argument.raw) }
269
+ else
270
+ raw.delete_if.with_index do |(key, value), index|
271
+ argument.call(
272
+ arguments: [
273
+ Argument.new(key),
274
+ Argument.new(value),
275
+ Argument.new(self),
276
+ Argument.new(Integer.new(index))
277
+ ],
278
+ **globals
279
+ ).truthy?
280
+ end
281
+ end
282
+
283
+ self
284
+ end
285
+
286
+ def code_delete_unless(argument, **globals)
287
+ if argument.is_a?(Class)
288
+ raw.delete_if { |_, value| !value.is_a?(argument.raw) }
289
+ else
290
+ raw.delete_if.with_index do |(key, value), index|
291
+ argument.call(
292
+ arguments: [
293
+ Argument.new(key),
294
+ Argument.new(value),
295
+ Argument.new(self),
296
+ Argument.new(Integer.new(index))
297
+ ],
298
+ **globals
299
+ ).falsy?
300
+ end
301
+ end
302
+
303
+ self
304
+ end
305
+
306
+ def code_dig(*arguments)
307
+ raw.dig(*arguments) || Nothing.new
308
+ end
309
+
310
+ def code_each(argument, **globals)
311
+ raw.each.with_index do |(key, value), index|
312
+ argument.call(
313
+ arguments: [
314
+ Argument.new(key),
315
+ Argument.new(value),
316
+ Argument.new(self),
317
+ Argument.new(Integer.new(index))
318
+ ],
319
+ **globals
320
+ )
321
+ end
322
+
323
+ self
324
+ end
325
+
326
+ def code_eight?
327
+ code_size.code_eight?
328
+ end
329
+
330
+ def code_empty?
331
+ Boolean.new(raw.empty?)
332
+ end
333
+
334
+ def code_except(*arguments)
335
+ self.class.new(raw.except(*arguments))
336
+ end
337
+
338
+ def code_fetch(*arguments, index: Integer.new(0), **globals)
339
+ default =
340
+ (arguments.last if arguments.last.is_a?(Function) && arguments.size > 1)
341
+
342
+ arguments = arguments[..-2] if default
343
+
344
+ if arguments.one?
345
+ raw.fetch(arguments.first) do
346
+ if default
347
+ default.call(
348
+ arguments: [
349
+ Argument.new(arguments.first),
350
+ Argument.new(self),
351
+ Argument.new(index)
352
+ ],
353
+ **globals
354
+ )
355
+ else
356
+ raise(
357
+ Code::Error::KeyNotFound,
358
+ "#{arguments.first.inspect} not found on #{inspect}"
359
+ )
360
+ end
361
+ end
362
+ else
363
+ self.class.new(
364
+ arguments
365
+ .map
366
+ .with_index do |argument, index|
367
+ if default
368
+ [
369
+ argument,
370
+ code_fetch(
371
+ argument,
372
+ default,
373
+ index: Integer.new(index),
374
+ **globals
375
+ )
376
+ ]
377
+ else
378
+ [
379
+ argument,
380
+ code_fetch(argument, index: Integer.new(index), **globals)
381
+ ]
382
+ end
383
+ end
384
+ .to_h
385
+ )
386
+ end
387
+ end
388
+
389
+ def code_fetch_values(*arguments)
390
+ List.new(raw.fetch_values(*arguments))
391
+ end
392
+
393
+ def code_five?
394
+ code_size.code_five?
395
+ end
396
+
397
+ def code_flatten(level = nil)
398
+ level ||= Integer.new(-1)
399
+ code_to_list.code_flatten(level)
400
+ end
401
+
402
+ def code_four?
403
+ code_size.code_four?
404
+ end
405
+
406
+ def code_get(key)
407
+ raw[key] || Nothing.new
408
+ end
409
+
410
+ def code_has_key?(key)
411
+ Boolean.new(raw.key?(key))
412
+ end
413
+
414
+ def code_has_value?(key)
415
+ Boolean.new(raw.value?(key))
416
+ end
417
+
418
+ def code_inferior(other)
419
+ Boolean.new(raw < other.raw)
420
+ end
421
+
422
+ def code_inferior_or_equal(other)
423
+ Boolean.new(raw <= other.raw)
424
+ end
425
+
426
+ def code_invert
427
+ self.class.new(raw.invert)
428
+ end
429
+
430
+ def code_keep_if(argument, **globals)
431
+ if argument.is_a?(Class)
432
+ raw.keep_if { |_, value| value.is_a?(argument.raw) }
433
+ else
434
+ raw.keep_if.with_index do |(key, value), index|
435
+ argument.call(
436
+ arguments: [
437
+ Argument.new(key),
438
+ Argument.new(value),
439
+ Argument.new(self),
440
+ Argument.new(Integer.new(index))
441
+ ],
442
+ **globals
443
+ ).truthy?
444
+ end
445
+ end
446
+
447
+ self
448
+ end
449
+
450
+ def code_keep_unless(argument, **globals)
451
+ if argument.is_a?(Class)
452
+ raw.keep_if { |_, value| !value.is_a?(argument.raw) }
453
+ else
454
+ raw.keep_if.with_index do |(key, value), index|
455
+ argument.call(
456
+ arguments: [
457
+ Argument.new(key),
458
+ Argument.new(value),
459
+ Argument.new(self),
460
+ Argument.new(Integer.new(index))
461
+ ],
462
+ **globals
463
+ ).falsy?
464
+ end
465
+ end
466
+
467
+ self
468
+ end
469
+
470
+ def code_key(value, function = nil, **globals)
471
+ if function
472
+ raw.key(value) ||
473
+ function.call(
474
+ arguments: [Argument.new(value), Argument.new(self)],
475
+ **globals
476
+ )
477
+ else
478
+ raw.key(value) || Nothing.new
479
+ end
480
+ end
481
+
482
+ def code_keys
483
+ List.new(raw.keys)
484
+ end
485
+
486
+ def code_merge(*arguments, **globals)
487
+ conflict =
488
+ (arguments.last if arguments.last.is_a?(Function) && arguments.size > 1)
489
+
490
+ arguments = arguments[..-2] if conflict
491
+
492
+ index = 0
493
+
494
+ self.class.new(
495
+ raw.merge(*arguments.map(&:raw)) do |key, old_value, new_value|
496
+ if conflict
497
+ conflict
498
+ .call(
499
+ arguments: [
500
+ Argument.new(key),
501
+ Argument.new(old_value),
502
+ Argument.new(new_value),
503
+ Argument.new(self),
504
+ Argument.new(Integer.new(index))
505
+ ],
506
+ **globals
507
+ )
508
+ .tap { index += 1 }
509
+ else
510
+ new_value.tap { index += 1 }
511
+ end
512
+ end
513
+ )
514
+ end
515
+
516
+ def code_nine?
517
+ code_size.code_nine?
518
+ end
519
+
520
+ def code_one?
521
+ code_size.code_one?
522
+ end
523
+
524
+ def code_select!(argument, **globals)
525
+ if argument.is_a?(Class)
526
+ raw.select! { |_, value| value.is_a?(argument.raw) }
527
+ else
528
+ raw.select!.with_index do |(key, value), index|
529
+ argument.call(
530
+ arguments: [
531
+ Argument.new(key),
532
+ Argument.new(value),
533
+ Argument.new(self),
534
+ Argument.new(Integer.new(index))
535
+ ],
536
+ **globals
537
+ ).truthy?
538
+ end
539
+ end
540
+
541
+ self
542
+ end
543
+
544
+ def code_select(argument, **globals)
545
+ if argument.is_a?(Class)
546
+ self.class.new(raw.select { |_, value| value.is_a?(argument.raw) })
547
+ else
548
+ self.class.new(
549
+ raw.select.with_index do |(key, value), index|
550
+ argument.call(
551
+ arguments: [
552
+ Argument.new(key),
553
+ Argument.new(value),
554
+ Argument.new(self),
555
+ Argument.new(Integer.new(index))
556
+ ],
557
+ **globals
558
+ ).truthy?
559
+ end
560
+ )
561
+ end
562
+ end
563
+
564
+ def code_set(key, value)
565
+ raw[key] = value
566
+ value
567
+ end
568
+
569
+ def code_seven?
570
+ code_size.code_seven?
571
+ end
572
+
573
+ def code_six?
574
+ code_size.code_six?
575
+ end
576
+
577
+ def code_size
578
+ Integer.new(raw.size)
579
+ end
580
+
581
+ def code_superior(other)
582
+ Boolean.new(raw > other.raw)
583
+ end
584
+
585
+ def code_superior_or_equal(other)
586
+ Boolean.new(raw >= other.raw)
587
+ end
588
+
589
+ def code_ten?
590
+ code_size.code_ten?
591
+ end
592
+
593
+ def code_three?
594
+ code_size.code_three?
595
+ end
596
+
597
+ def code_to_context
598
+ Context.new(raw)
599
+ end
600
+
601
+ def code_to_list
602
+ List.new(raw.to_a.map { |key_value| List.new(key_value) })
603
+ end
604
+
605
+ def code_transform_values(function, **globals)
606
+ self.class.new(
607
+ raw.transform_values.with_index do |value, index|
608
+ function.call(
609
+ arguments: [
610
+ Argument.new(value),
611
+ Argument.new(self),
612
+ Argument.new(Integer.new(index))
613
+ ],
614
+ **globals
615
+ )
616
+ end
617
+ )
618
+ end
619
+
620
+ def code_two?
621
+ code_size.code_two?
622
+ end
623
+
624
+ def code_values
625
+ List.new(raw.values)
626
+ end
627
+
628
+ def code_zero?
629
+ code_size.code_zero?
630
+ end
631
+
632
+ def inspect
633
+ to_s
634
+ end
635
+
636
+ def to_s
637
+ "{#{raw.map { |key, value| "#{key.inspect} => #{value.inspect}" }.join(", ")}}"
638
+ end
639
+ end
640
+ end
641
+ end