playoffs 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. checksums.yaml +7 -0
  2. data/.bundle/config +2 -0
  3. data/.editorconfig +8 -0
  4. data/.github/workflows/ci.yaml +29 -0
  5. data/.gitignore +8 -0
  6. data/.rubocop.yml +34 -0
  7. data/.tool-versions +1 -0
  8. data/.vscode/settings.json +5 -0
  9. data/CHANGELOG.md +3 -0
  10. data/CODE_OF_CONDUCT.md +73 -0
  11. data/Gemfile +5 -0
  12. data/Guardfile +17 -0
  13. data/LICENSE +5 -0
  14. data/README.md +245 -0
  15. data/Rakefile +11 -0
  16. data/bin/_guard-core +27 -0
  17. data/bin/bundle +109 -0
  18. data/bin/bundle-audit +27 -0
  19. data/bin/bundler-audit +27 -0
  20. data/bin/coderay +27 -0
  21. data/bin/console +11 -0
  22. data/bin/guard +27 -0
  23. data/bin/htmldiff +27 -0
  24. data/bin/ldiff +27 -0
  25. data/bin/listen +27 -0
  26. data/bin/playoffs +27 -0
  27. data/bin/pry +27 -0
  28. data/bin/racc +27 -0
  29. data/bin/rake +27 -0
  30. data/bin/rspec +27 -0
  31. data/bin/rubocop +27 -0
  32. data/bin/ruby-parse +27 -0
  33. data/bin/ruby-rewrite +27 -0
  34. data/bin/spoom +27 -0
  35. data/bin/srb +27 -0
  36. data/bin/srb-rbi +27 -0
  37. data/bin/tapioca +27 -0
  38. data/bin/thor +27 -0
  39. data/bin/yard +27 -0
  40. data/bin/yardoc +27 -0
  41. data/bin/yri +27 -0
  42. data/exe/playoffs +7 -0
  43. data/lib/playoffs/basketball.rb +120 -0
  44. data/lib/playoffs/best_of.rb +38 -0
  45. data/lib/playoffs/cli.rb +201 -0
  46. data/lib/playoffs/contestant.rb +8 -0
  47. data/lib/playoffs/round.rb +42 -0
  48. data/lib/playoffs/series.rb +167 -0
  49. data/lib/playoffs/team.rb +7 -0
  50. data/lib/playoffs/tournament/bracketable.rb +93 -0
  51. data/lib/playoffs/tournament/roundable.rb +53 -0
  52. data/lib/playoffs/tournament.rb +61 -0
  53. data/lib/playoffs/version.rb +6 -0
  54. data/lib/playoffs.rb +26 -0
  55. data/playoffs.gemspec +49 -0
  56. data/sorbet/config +4 -0
  57. data/sorbet/rbi/annotations/.gitattributes +1 -0
  58. data/sorbet/rbi/annotations/rainbow.rbi +269 -0
  59. data/sorbet/rbi/gems/.gitattributes +1 -0
  60. data/sorbet/rbi/gems/ansi@1.5.0.rbi +688 -0
  61. data/sorbet/rbi/gems/ast@2.4.2.rbi +585 -0
  62. data/sorbet/rbi/gems/bundler-audit@0.9.1.rbi +309 -0
  63. data/sorbet/rbi/gems/coderay@1.1.3.rbi +3426 -0
  64. data/sorbet/rbi/gems/diff-lcs@1.5.1.rbi +1131 -0
  65. data/sorbet/rbi/gems/docile@1.4.0.rbi +377 -0
  66. data/sorbet/rbi/gems/erubi@1.12.0.rbi +145 -0
  67. data/sorbet/rbi/gems/ffi@1.16.3.rbi +9 -0
  68. data/sorbet/rbi/gems/formatador@1.1.0.rbi +9 -0
  69. data/sorbet/rbi/gems/guard-compat@1.2.1.rbi +67 -0
  70. data/sorbet/rbi/gems/guard-rspec@4.7.3.rbi +563 -0
  71. data/sorbet/rbi/gems/guard@2.18.1.rbi +9 -0
  72. data/sorbet/rbi/gems/json@2.7.2.rbi +1562 -0
  73. data/sorbet/rbi/gems/language_server-protocol@3.17.0.3.rbi +14238 -0
  74. data/sorbet/rbi/gems/listen@3.9.0.rbi +9 -0
  75. data/sorbet/rbi/gems/lumberjack@1.2.10.rbi +9 -0
  76. data/sorbet/rbi/gems/method_source@1.1.0.rbi +304 -0
  77. data/sorbet/rbi/gems/nenv@0.3.0.rbi +9 -0
  78. data/sorbet/rbi/gems/netrc@0.11.0.rbi +158 -0
  79. data/sorbet/rbi/gems/notiffany@0.1.3.rbi +9 -0
  80. data/sorbet/rbi/gems/parallel@1.24.0.rbi +280 -0
  81. data/sorbet/rbi/gems/parser@3.3.1.0.rbi +7238 -0
  82. data/sorbet/rbi/gems/primitive@1.0.0.rbi +58 -0
  83. data/sorbet/rbi/gems/prism@0.29.0.rbi +37987 -0
  84. data/sorbet/rbi/gems/pry@0.14.2.rbi +10069 -0
  85. data/sorbet/rbi/gems/racc@1.7.3.rbi +162 -0
  86. data/sorbet/rbi/gems/rainbow@3.1.1.rbi +403 -0
  87. data/sorbet/rbi/gems/rake@13.2.1.rbi +3028 -0
  88. data/sorbet/rbi/gems/rb-fsevent@0.11.2.rbi +9 -0
  89. data/sorbet/rbi/gems/rb-inotify@0.10.1.rbi +9 -0
  90. data/sorbet/rbi/gems/rbi@0.1.13.rbi +3078 -0
  91. data/sorbet/rbi/gems/regexp_parser@2.9.2.rbi +3772 -0
  92. data/sorbet/rbi/gems/rexml@3.2.8.rbi +4794 -0
  93. data/sorbet/rbi/gems/rspec-core@3.13.0.rbi +10874 -0
  94. data/sorbet/rbi/gems/rspec-expectations@3.13.0.rbi +8154 -0
  95. data/sorbet/rbi/gems/rspec-mocks@3.13.1.rbi +5341 -0
  96. data/sorbet/rbi/gems/rspec-support@3.13.1.rbi +1630 -0
  97. data/sorbet/rbi/gems/rspec@3.13.0.rbi +83 -0
  98. data/sorbet/rbi/gems/rubocop-ast@1.31.3.rbi +7159 -0
  99. data/sorbet/rbi/gems/rubocop-capybara@2.20.0.rbi +1208 -0
  100. data/sorbet/rbi/gems/rubocop-factory_bot@2.25.1.rbi +928 -0
  101. data/sorbet/rbi/gems/rubocop-rake@0.6.0.rbi +329 -0
  102. data/sorbet/rbi/gems/rubocop-rspec@2.29.2.rbi +8247 -0
  103. data/sorbet/rbi/gems/rubocop-rspec_rails@2.28.3.rbi +911 -0
  104. data/sorbet/rbi/gems/rubocop-sorbet@0.8.3.rbi +1607 -0
  105. data/sorbet/rbi/gems/rubocop@1.63.5.rbi +57788 -0
  106. data/sorbet/rbi/gems/ruby-progressbar@1.13.0.rbi +1318 -0
  107. data/sorbet/rbi/gems/shellany@0.0.1.rbi +9 -0
  108. data/sorbet/rbi/gems/simplecov-console@0.9.1.rbi +103 -0
  109. data/sorbet/rbi/gems/simplecov-html@0.12.3.rbi +217 -0
  110. data/sorbet/rbi/gems/simplecov@0.22.0.rbi +2149 -0
  111. data/sorbet/rbi/gems/simplecov_json_formatter@0.1.4.rbi +9 -0
  112. data/sorbet/rbi/gems/sorbet-runtime-stub@0.2.0.rbi +8 -0
  113. data/sorbet/rbi/gems/spoom@1.3.2.rbi +4420 -0
  114. data/sorbet/rbi/gems/strscan@3.1.0.rbi +9 -0
  115. data/sorbet/rbi/gems/tapioca@0.14.2.rbi +3539 -0
  116. data/sorbet/rbi/gems/terminal-table@3.0.2.rbi +9 -0
  117. data/sorbet/rbi/gems/thor@1.3.1.rbi +4318 -0
  118. data/sorbet/rbi/gems/unicode-display_width@2.5.0.rbi +66 -0
  119. data/sorbet/rbi/gems/yard-sorbet@0.8.1.rbi +428 -0
  120. data/sorbet/rbi/gems/yard@0.9.36.rbi +18085 -0
  121. data/sorbet/tapioca/config.yml +13 -0
  122. data/sorbet/tapioca/require.rb +4 -0
  123. metadata +383 -0
@@ -0,0 +1,585 @@
1
+ # typed: true
2
+
3
+ # DO NOT EDIT MANUALLY
4
+ # This is an autogenerated file for types exported from the `ast` gem.
5
+ # Please instead update this file by running `bin/tapioca gem ast`.
6
+
7
+
8
+ # {AST} is a library for manipulating abstract syntax trees.
9
+ #
10
+ # It embraces immutability; each AST node is inherently frozen at
11
+ # creation, and updating a child node requires recreating that node
12
+ # and its every parent, recursively.
13
+ # This is a design choice. It does create some pressure on
14
+ # garbage collector, but completely eliminates all concurrency
15
+ # and aliasing problems.
16
+ #
17
+ # See also {AST::Node}, {AST::Processor::Mixin} and {AST::Sexp} for
18
+ # additional recommendations and design patterns.
19
+ #
20
+ # source://ast//lib/ast.rb#13
21
+ module AST; end
22
+
23
+ # Node is an immutable class, instances of which represent abstract
24
+ # syntax tree nodes. It combines semantic information (i.e. anything
25
+ # that affects the algorithmic properties of a program) with
26
+ # meta-information (line numbers or compiler intermediates).
27
+ #
28
+ # Notes on inheritance
29
+ # ====================
30
+ #
31
+ # The distinction between semantics and metadata is important. Complete
32
+ # semantic information should be contained within just the {#type} and
33
+ # {#children} of a Node instance; in other words, if an AST was to be
34
+ # stripped of all meta-information, it should remain a valid AST which
35
+ # could be successfully processed to yield a result with the same
36
+ # algorithmic properties.
37
+ #
38
+ # Thus, Node should never be inherited in order to define methods which
39
+ # affect or return semantic information, such as getters for `class_name`,
40
+ # `superclass` and `body` in the case of a hypothetical `ClassNode`. The
41
+ # correct solution is to use a generic Node with a {#type} of `:class`
42
+ # and three children. See also {Processor} for tips on working with such
43
+ # ASTs.
44
+ #
45
+ # On the other hand, Node can and should be inherited to define
46
+ # application-specific metadata (see also {#initialize}) or customize the
47
+ # printing format. It is expected that an application would have one or two
48
+ # such classes and use them across the entire codebase.
49
+ #
50
+ # The rationale for this pattern is extensibility and maintainability.
51
+ # Unlike static ones, dynamic languages do not require the presence of a
52
+ # predefined, rigid structure, nor does it improve dispatch efficiency,
53
+ # and while such a structure can certainly be defined, it does not add
54
+ # any value but incurs a maintaining cost.
55
+ # For example, extending the AST even with a transformation-local
56
+ # temporary node type requires making globally visible changes to
57
+ # the codebase.
58
+ #
59
+ # source://ast//lib/ast/node.rb#40
60
+ class AST::Node
61
+ # Constructs a new instance of Node.
62
+ #
63
+ # The arguments `type` and `children` are converted with `to_sym` and
64
+ # `to_a` respectively. Additionally, the result of converting `children`
65
+ # is frozen. While mutating the arguments is generally considered harmful,
66
+ # the most common case is to pass an array literal to the constructor. If
67
+ # your code does not expect the argument to be frozen, use `#dup`.
68
+ #
69
+ # The `properties` hash is passed to {#assign_properties}.
70
+ #
71
+ # @return [Node] a new instance of Node
72
+ #
73
+ # source://ast//lib/ast/node.rb#72
74
+ def initialize(type, children = T.unsafe(nil), properties = T.unsafe(nil)); end
75
+
76
+ # Concatenates `array` with `children` and returns the resulting node.
77
+ #
78
+ # @return [AST::Node]
79
+ #
80
+ # source://ast//lib/ast/node.rb#168
81
+ def +(array); end
82
+
83
+ # Appends `element` to `children` and returns the resulting node.
84
+ #
85
+ # @return [AST::Node]
86
+ #
87
+ # source://ast//lib/ast/node.rb#177
88
+ def <<(element); end
89
+
90
+ # Compares `self` to `other`, possibly converting with `to_ast`. Only
91
+ # `type` and `children` are compared; metadata is deliberately ignored.
92
+ #
93
+ # @return [Boolean]
94
+ #
95
+ # source://ast//lib/ast/node.rb#153
96
+ def ==(other); end
97
+
98
+ # Appends `element` to `children` and returns the resulting node.
99
+ #
100
+ # @return [AST::Node]
101
+ #
102
+ # source://ast//lib/ast/node.rb#177
103
+ def append(element); end
104
+
105
+ # Returns the children of this node.
106
+ # The returned value is frozen.
107
+ # The to_a alias is useful for decomposing nodes concisely.
108
+ # For example:
109
+ #
110
+ # node = s(:gasgn, :$foo, s(:integer, 1))
111
+ # var_name, value = *node
112
+ # p var_name # => :$foo
113
+ # p value # => (integer 1)
114
+ #
115
+ # @return [Array]
116
+ #
117
+ # source://ast//lib/ast/node.rb#56
118
+ def children; end
119
+
120
+ # Nodes are already frozen, so there is no harm in returning the
121
+ # current node as opposed to initializing from scratch and freezing
122
+ # another one.
123
+ #
124
+ # @return self
125
+ #
126
+ # source://ast//lib/ast/node.rb#115
127
+ def clone; end
128
+
129
+ # Concatenates `array` with `children` and returns the resulting node.
130
+ #
131
+ # @return [AST::Node]
132
+ #
133
+ # source://ast//lib/ast/node.rb#168
134
+ def concat(array); end
135
+
136
+ # Enables matching for Node, where type is the first element
137
+ # and the children are remaining items.
138
+ #
139
+ # @return [Array]
140
+ #
141
+ # source://ast//lib/ast/node.rb#253
142
+ def deconstruct; end
143
+
144
+ # Nodes are already frozen, so there is no harm in returning the
145
+ # current node as opposed to initializing from scratch and freezing
146
+ # another one.
147
+ #
148
+ # @return self
149
+ #
150
+ # source://ast//lib/ast/node.rb#115
151
+ def dup; end
152
+
153
+ # Test if other object is equal to
154
+ #
155
+ # @param other [Object]
156
+ # @return [Boolean]
157
+ #
158
+ # source://ast//lib/ast/node.rb#85
159
+ def eql?(other); end
160
+
161
+ # Returns the precomputed hash value for this node
162
+ #
163
+ # @return [Fixnum]
164
+ #
165
+ # source://ast//lib/ast/node.rb#61
166
+ def hash; end
167
+
168
+ # Converts `self` to a s-expression ruby string.
169
+ # The code return will recreate the node, using the sexp module s()
170
+ #
171
+ # @param indent [Integer] Base indentation level.
172
+ # @return [String]
173
+ #
174
+ # source://ast//lib/ast/node.rb#211
175
+ def inspect(indent = T.unsafe(nil)); end
176
+
177
+ # Returns the children of this node.
178
+ # The returned value is frozen.
179
+ # The to_a alias is useful for decomposing nodes concisely.
180
+ # For example:
181
+ #
182
+ # node = s(:gasgn, :$foo, s(:integer, 1))
183
+ # var_name, value = *node
184
+ # p var_name # => :$foo
185
+ # p value # => (integer 1)
186
+ #
187
+ # @return [Array]
188
+ #
189
+ # source://ast//lib/ast/node.rb#56
190
+ def to_a; end
191
+
192
+ # @return [AST::Node] self
193
+ #
194
+ # source://ast//lib/ast/node.rb#229
195
+ def to_ast; end
196
+
197
+ # Converts `self` to a pretty-printed s-expression.
198
+ #
199
+ # @param indent [Integer] Base indentation level.
200
+ # @return [String]
201
+ #
202
+ # source://ast//lib/ast/node.rb#187
203
+ def to_s(indent = T.unsafe(nil)); end
204
+
205
+ # Converts `self` to a pretty-printed s-expression.
206
+ #
207
+ # @param indent [Integer] Base indentation level.
208
+ # @return [String]
209
+ #
210
+ # source://ast//lib/ast/node.rb#187
211
+ def to_sexp(indent = T.unsafe(nil)); end
212
+
213
+ # Converts `self` to an Array where the first element is the type as a Symbol,
214
+ # and subsequent elements are the same representation of its children.
215
+ #
216
+ # @return [Array<Symbol, [...Array]>]
217
+ #
218
+ # source://ast//lib/ast/node.rb#237
219
+ def to_sexp_array; end
220
+
221
+ # Returns the type of this node.
222
+ #
223
+ # @return [Symbol]
224
+ #
225
+ # source://ast//lib/ast/node.rb#43
226
+ def type; end
227
+
228
+ # Returns a new instance of Node where non-nil arguments replace the
229
+ # corresponding fields of `self`.
230
+ #
231
+ # For example, `Node.new(:foo, [ 1, 2 ]).updated(:bar)` would yield
232
+ # `(bar 1 2)`, and `Node.new(:foo, [ 1, 2 ]).updated(nil, [])` would
233
+ # yield `(foo)`.
234
+ #
235
+ # If the resulting node would be identical to `self`, does nothing.
236
+ #
237
+ # @param type [Symbol, nil]
238
+ # @param children [Array, nil]
239
+ # @param properties [Hash, nil]
240
+ # @return [AST::Node]
241
+ #
242
+ # source://ast//lib/ast/node.rb#133
243
+ def updated(type = T.unsafe(nil), children = T.unsafe(nil), properties = T.unsafe(nil)); end
244
+
245
+ protected
246
+
247
+ # By default, each entry in the `properties` hash is assigned to
248
+ # an instance variable in this instance of Node. A subclass should define
249
+ # attribute readers for such variables. The values passed in the hash
250
+ # are not frozen or whitelisted; such behavior can also be implemented
251
+ # by subclassing Node and overriding this method.
252
+ #
253
+ # @return [nil]
254
+ #
255
+ # source://ast//lib/ast/node.rb#98
256
+ def assign_properties(properties); end
257
+
258
+ # Returns `@type` with all underscores replaced by dashes. This allows
259
+ # to write symbol literals without quotes in Ruby sources and yet have
260
+ # nicely looking s-expressions.
261
+ #
262
+ # @return [String]
263
+ #
264
+ # source://ast//lib/ast/node.rb#264
265
+ def fancy_type; end
266
+
267
+ private
268
+
269
+ def original_dup; end
270
+ end
271
+
272
+ # This class includes {AST::Processor::Mixin}; however, it is
273
+ # deprecated, since the module defines all of the behaviors that
274
+ # the processor includes. Any new libraries should use
275
+ # {AST::Processor::Mixin} instead of subclassing this.
276
+ #
277
+ # @deprecated Use {AST::Processor::Mixin} instead.
278
+ #
279
+ # source://ast//lib/ast/processor.rb#8
280
+ class AST::Processor
281
+ include ::AST::Processor::Mixin
282
+ end
283
+
284
+ # The processor module is a module which helps transforming one
285
+ # AST into another. In a nutshell, the {#process} method accepts
286
+ # a {Node} and dispatches it to a handler corresponding to its
287
+ # type, and returns a (possibly) updated variant of the node.
288
+ #
289
+ # The processor module has a set of associated design patterns.
290
+ # They are best explained with a concrete example. Let's define a
291
+ # simple arithmetic language and an AST format for it:
292
+ #
293
+ # Terminals (AST nodes which do not have other AST nodes inside):
294
+ #
295
+ # * `(integer <int-literal>)`,
296
+ #
297
+ # Nonterminals (AST nodes with other nodes as children):
298
+ #
299
+ # * `(add <node> <node>)`,
300
+ # * `(multiply <node> <node>)`,
301
+ # * `(divide <node> <node>)`,
302
+ # * `(negate <node>)`,
303
+ # * `(store <node> <string-literal>)`: stores value of `<node>`
304
+ # into a variable named `<string-literal>`,
305
+ # * `(load <string-literal>)`: loads value of a variable named
306
+ # `<string-literal>`,
307
+ # * `(each <node> ...)`: computes each of the `<node>`s and
308
+ # prints the result.
309
+ #
310
+ # All AST nodes have the same Ruby class, and therefore they don't
311
+ # know how to traverse themselves. (A solution which dynamically
312
+ # checks the type of children is possible, but is slow and
313
+ # error-prone.) So, a class including the module which knows how
314
+ # to traverse the entire tree should be defined. Such classes
315
+ # have a handler for each nonterminal node which recursively
316
+ # processes children nodes:
317
+ #
318
+ # require 'ast'
319
+ #
320
+ # class ArithmeticsProcessor
321
+ # include AST::Processor::Mixin
322
+ # # This method traverses any binary operators such as (add)
323
+ # # or (multiply).
324
+ # def process_binary_op(node)
325
+ # # Children aren't decomposed automatically; it is
326
+ # # suggested to use Ruby multiple assignment expansion,
327
+ # # as it is very convenient here.
328
+ # left_expr, right_expr = *node
329
+ #
330
+ # # AST::Node#updated won't change node type if nil is
331
+ # # passed as a first argument, which allows to reuse the
332
+ # # same handler for multiple node types using `alias'
333
+ # # (below).
334
+ # node.updated(nil, [
335
+ # process(left_expr),
336
+ # process(right_expr)
337
+ # ])
338
+ # end
339
+ # alias_method :on_add, :process_binary_op
340
+ # alias_method :on_multiply, :process_binary_op
341
+ # alias_method :on_divide, :process_binary_op
342
+ #
343
+ # def on_negate(node)
344
+ # # It is also possible to use #process_all for more
345
+ # # compact code if every child is a Node.
346
+ # node.updated(nil, process_all(node))
347
+ # end
348
+ #
349
+ # def on_store(node)
350
+ # expr, variable_name = *node
351
+ #
352
+ # # Note that variable_name is not a Node and thus isn't
353
+ # # passed to #process.
354
+ # node.updated(nil, [
355
+ # process(expr),
356
+ # variable_name
357
+ # ])
358
+ # end
359
+ #
360
+ # # (load) is effectively a terminal node, and so it does
361
+ # # not need an explicit handler, as the following is the
362
+ # # default behavior. Essentially, for any nodes that don't
363
+ # # have a defined handler, the node remains unchanged.
364
+ # def on_load(node)
365
+ # nil
366
+ # end
367
+ #
368
+ # def on_each(node)
369
+ # node.updated(nil, process_all(node))
370
+ # end
371
+ # end
372
+ #
373
+ # Let's test our ArithmeticsProcessor:
374
+ #
375
+ # include AST::Sexp
376
+ # expr = s(:add, s(:integer, 2), s(:integer, 2))
377
+ #
378
+ # p ArithmeticsProcessor.new.process(expr) == expr # => true
379
+ #
380
+ # As expected, it does not change anything at all. This isn't
381
+ # actually very useful, so let's now define a Calculator, which
382
+ # will compute the expression values:
383
+ #
384
+ # # This Processor folds nonterminal nodes and returns an
385
+ # # (integer) terminal node.
386
+ # class ArithmeticsCalculator < ArithmeticsProcessor
387
+ # def compute_op(node)
388
+ # # First, node children are processed and then unpacked
389
+ # # to local variables.
390
+ # nodes = process_all(node)
391
+ #
392
+ # if nodes.all? { |node| node.type == :integer }
393
+ # # If each of those nodes represents a literal, we can
394
+ # # fold this node!
395
+ # values = nodes.map { |node| node.children.first }
396
+ # AST::Node.new(:integer, [
397
+ # yield(values)
398
+ # ])
399
+ # else
400
+ # # Otherwise, we can just leave the current node in the
401
+ # # tree and only update it with processed children
402
+ # # nodes, which can be partially folded.
403
+ # node.updated(nil, nodes)
404
+ # end
405
+ # end
406
+ #
407
+ # def on_add(node)
408
+ # compute_op(node) { |left, right| left + right }
409
+ # end
410
+ #
411
+ # def on_multiply(node)
412
+ # compute_op(node) { |left, right| left * right }
413
+ # end
414
+ # end
415
+ #
416
+ # Let's check:
417
+ #
418
+ # p ArithmeticsCalculator.new.process(expr) # => (integer 4)
419
+ #
420
+ # Excellent, the calculator works! Now, a careful reader could
421
+ # notice that the ArithmeticsCalculator does not know how to
422
+ # divide numbers. What if we pass an expression with division to
423
+ # it?
424
+ #
425
+ # expr_with_division = \
426
+ # s(:add,
427
+ # s(:integer, 1),
428
+ # s(:divide,
429
+ # s(:add, s(:integer, 8), s(:integer, 4)),
430
+ # s(:integer, 3))) # 1 + (8 + 4) / 3
431
+ #
432
+ # folded_expr_with_division = ArithmeticsCalculator.new.process(expr_with_division)
433
+ # p folded_expr_with_division
434
+ # # => (add
435
+ # # (integer 1)
436
+ # # (divide
437
+ # # (integer 12)
438
+ # # (integer 3)))
439
+ #
440
+ # As you can see, the expression was folded _partially_: the inner
441
+ # `(add)` node which could be computed was folded to
442
+ # `(integer 12)`, the `(divide)` node is left as-is because there
443
+ # is no computing handler for it, and the root `(add)` node was
444
+ # also left as it is because some of its children were not
445
+ # literals.
446
+ #
447
+ # Note that this partial folding is only possible because the
448
+ # _data_ format, i.e. the format in which the computed values of
449
+ # the nodes are represented, is the same as the AST itself.
450
+ #
451
+ # Let's extend our ArithmeticsCalculator class further.
452
+ #
453
+ # class ArithmeticsCalculator
454
+ # def on_divide(node)
455
+ # compute_op(node) { |left, right| left / right }
456
+ # end
457
+ #
458
+ # def on_negate(node)
459
+ # # Note how #compute_op works regardless of the operator
460
+ # # arity.
461
+ # compute_op(node) { |value| -value }
462
+ # end
463
+ # end
464
+ #
465
+ # Now, let's apply our renewed ArithmeticsCalculator to a partial
466
+ # result of previous evaluation:
467
+ #
468
+ # p ArithmeticsCalculator.new.process(expr_with_division) # => (integer 5)
469
+ #
470
+ # Five! Excellent. This is also pretty much how CRuby 1.8 executed
471
+ # its programs.
472
+ #
473
+ # Now, let's do some automated bug searching. Division by zero is
474
+ # an error, right? So if we could detect that someone has divided
475
+ # by zero before the program is even run, that could save some
476
+ # debugging time.
477
+ #
478
+ # class DivisionByZeroVerifier < ArithmeticsProcessor
479
+ # class VerificationFailure < Exception; end
480
+ #
481
+ # def on_divide(node)
482
+ # # You need to process the children to handle nested divisions
483
+ # # such as:
484
+ # # (divide
485
+ # # (integer 1)
486
+ # # (divide (integer 1) (integer 0))
487
+ # left, right = process_all(node)
488
+ #
489
+ # if right.type == :integer &&
490
+ # right.children.first == 0
491
+ # raise VerificationFailure, "Ouch! This code divides by zero."
492
+ # end
493
+ # end
494
+ #
495
+ # def divides_by_zero?(ast)
496
+ # process(ast)
497
+ # false
498
+ # rescue VerificationFailure
499
+ # true
500
+ # end
501
+ # end
502
+ #
503
+ # nice_expr = \
504
+ # s(:divide,
505
+ # s(:add, s(:integer, 10), s(:integer, 2)),
506
+ # s(:integer, 4))
507
+ #
508
+ # p DivisionByZeroVerifier.new.divides_by_zero?(nice_expr)
509
+ # # => false. Good.
510
+ #
511
+ # bad_expr = \
512
+ # s(:add, s(:integer, 10),
513
+ # s(:divide, s(:integer, 1), s(:integer, 0)))
514
+ #
515
+ # p DivisionByZeroVerifier.new.divides_by_zero?(bad_expr)
516
+ # # => true. WHOOPS. DO NOT RUN THIS.
517
+ #
518
+ # Of course, this won't detect more complex cases... unless you
519
+ # use some partial evaluation before! The possibilites are
520
+ # endless. Have fun.
521
+ #
522
+ # source://ast//lib/ast/processor/mixin.rb#240
523
+ module AST::Processor::Mixin
524
+ # Default handler. Does nothing.
525
+ #
526
+ # @param node [AST::Node]
527
+ # @return [AST::Node, nil]
528
+ #
529
+ # source://ast//lib/ast/processor/mixin.rb#284
530
+ def handler_missing(node); end
531
+
532
+ # Dispatches `node`. If a node has type `:foo`, then a handler
533
+ # named `on_foo` is invoked with one argument, the `node`; if
534
+ # there isn't such a handler, {#handler_missing} is invoked
535
+ # with the same argument.
536
+ #
537
+ # If the handler returns `nil`, `node` is returned; otherwise,
538
+ # the return value of the handler is passed along.
539
+ #
540
+ # @param node [AST::Node, nil]
541
+ # @return [AST::Node, nil]
542
+ #
543
+ # source://ast//lib/ast/processor/mixin.rb#251
544
+ def process(node); end
545
+
546
+ # {#process}es each node from `nodes` and returns an array of
547
+ # results.
548
+ #
549
+ # @param nodes [Array<AST::Node>]
550
+ # @return [Array<AST::Node>]
551
+ #
552
+ # source://ast//lib/ast/processor/mixin.rb#274
553
+ def process_all(nodes); end
554
+ end
555
+
556
+ # This simple module is very useful in the cases where one needs
557
+ # to define deeply nested ASTs from Ruby code, for example, in
558
+ # tests. It should be used like this:
559
+ #
560
+ # describe YourLanguage::AST do
561
+ # include Sexp
562
+ #
563
+ # it "should correctly parse expressions" do
564
+ # YourLanguage.parse("1 + 2 * 3").should ==
565
+ # s(:add,
566
+ # s(:integer, 1),
567
+ # s(:multiply,
568
+ # s(:integer, 2),
569
+ # s(:integer, 3)))
570
+ # end
571
+ # end
572
+ #
573
+ # This way the amount of boilerplate code is greatly reduced.
574
+ #
575
+ # source://ast//lib/ast/sexp.rb#20
576
+ module AST::Sexp
577
+ # Creates a {Node} with type `type` and children `children`.
578
+ # Note that the resulting node is of the type AST::Node and not a
579
+ # subclass.
580
+ # This would not pose a problem with comparisons, as {Node#==}
581
+ # ignores metadata.
582
+ #
583
+ # source://ast//lib/ast/sexp.rb#26
584
+ def s(type, *children); end
585
+ end