Class: Atomy::Pattern::QuasiQuote::Walker

Inherits:
Object
  • Object
show all
Defined in:
lib/atomy/pattern/quasi_quote.rb

Direct Known Subclasses

Constructor

Instance Method Summary collapse

Constructor Details

#initializeWalker

Returns a new instance of Walker.



31
32
33
# File 'lib/atomy/pattern/quasi_quote.rb', line 31

def initialize
  @depth = 0
end

Instance Method Details

#go(x) ⇒ Object



35
36
37
# File 'lib/atomy/pattern/quasi_quote.rb', line 35

def go(x)
  x.accept(self)
end

#push_literal(x) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/atomy/pattern/quasi_quote.rb', line 62

def push_literal(x)
  case x
  when Array
    x.each { |v| push_literal(v) }
    @gen.make_array(x.size)
  when String
    @gen.push_literal(x)
    @gen.string_dup
  else
    @gen.push_literal(x)
  end
end

#unquote(_) ⇒ Object

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/atomy/pattern/quasi_quote.rb', line 58

def unquote(_)
  raise NotImplementedError
end

#unsplat(pats) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/atomy/pattern/quasi_quote.rb', line 75

def unsplat(pats)
  if pats.last.is_a?(Atomy::Grammar::AST::Unquote) && \
      pats.last.node.is_a?(Atomy::Pattern::Splat)
    [pats[0..-2], pats[-1]]
  else
    [pats, nil]
  end
end

#visit_quasiquote(qq) ⇒ Object



39
40
41
42
43
44
# File 'lib/atomy/pattern/quasi_quote.rb', line 39

def visit_quasiquote(qq)
  @depth += 1
  visit(qq)
ensure
  @depth -= 1
end

#visit_unquote(x) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/atomy/pattern/quasi_quote.rb', line 46

def visit_unquote(x)
  @depth -= 1

  if @depth == 0
    unquote(x)
  else
    visit(x)
  end
ensure
  @depth += 1
end