実⽤例: AST
def print_tree(node, indent = 0)
print '| ' * indent
case [node&.type, node&.children]
in [:SCOPE, [_, _, n1]]
puts 'scope'; print_tree(n1, indent + 1)
in [:OPCALL, [n1, op, n2]]
puts op.to_s; print_tree(n1, indent + 1); print_tree(n2,
in [:LIST, [h, t]]
puts 'cons'; print_tree(h, indent + 1); print_tree(t, ind
in [:LIT, [lit]]
puts lit.to_s
in [nil, _]
puts 'nil'
end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
https://github.com/kokuyouwind/pattern_match_demo/blob/master/src/05_ast.rb