/
atom_subcompiler.rb
56 lines (48 loc) · 1.46 KB
/
atom_subcompiler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true
module RuboCop
module AST
class NodePattern
class Compiler
# Generates code that evaluates to a value (Ruby object)
# This value responds to `===`.
#
# Doc on how this fits in the compiling process:
# /doc/modules/ROOT/pages/node_pattern.md
class AtomSubcompiler < Subcompiler
private
def visit_unify
compiler.bind(node.child) do
raise Invalid, 'unified variables can not appear first as argument'
end
end
def visit_symbol
node.child.inspect
end
alias visit_number visit_symbol
alias visit_string visit_symbol
alias visit_regexp visit_symbol
def visit_const
node.child
end
def visit_named_parameter
compiler.named_parameter(node.child)
end
def visit_positional_parameter
compiler.positional_parameter(node.child)
end
def visit_set
set = node.children.map(&:child).to_set.freeze
NodePattern::Sets[set]
end
# Assumes other types are node patterns.
def visit_other_type
compiler.with_temp_variables do |compare|
code = compiler.compile_as_node_pattern(node, var: compare)
"->(#{compare}) { #{code} }"
end
end
end
end
end
end
end