Module: RLSL::Prism::IR::Traversal

Defined in:
lib/rlsl/prism/ir/traversal.rb

Class Method Summary collapse

Class Method Details

.child_nodes(node) ⇒ Object



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
57
58
# File 'lib/rlsl/prism/ir/traversal.rb', line 17

def child_nodes(node)
  case node
  when Block
    node.statements
  when VarDecl
    [node.initializer]
  when BinaryOp
    [node.left, node.right]
  when UnaryOp
    [node.operand]
  when FuncCall
    [node.receiver, *node.args]
  when FieldAccess, Swizzle
    [node.receiver]
  when IfStatement
    [node.condition, node.then_branch, node.else_branch]
  when Ternary
    [node.condition, node.then_expr, node.else_expr]
  when Return
    [node.expression]
  when Assignment
    [node.target, node.value]
  when ForLoop
    [node.range_start, node.range_end, node.body]
  when WhileLoop
    [node.condition, node.body]
  when Parenthesized
    [node.expression]
  when ArrayLiteral
    node.elements
  when ArrayIndex
    [node.array, node.index]
  when GlobalDecl
    [node.initializer]
  when FunctionDefinition
    [node.body]
  when MultipleAssignment
    [*node.targets, node.value]
  else
    []
  end.compact
end

.each(node) {|node| ... } ⇒ Object

Yields:

  • (node)


9
10
11
12
13
14
15
# File 'lib/rlsl/prism/ir/traversal.rb', line 9

def each(node, &block)
  return enum_for(:each, node) unless block_given?
  return if node.nil?

  yield node
  child_nodes(node).each { |child| each(child, &block) }
end