Class: VirtualKeywords::WhileRewriter

Inherits:
SexpProcessor
  • Object
show all
Defined in:
lib/virtual_keywords/keyword_rewriter.rb

Overview

SexpProcessor subclass that rewrites “while” expressions.

Instance Method Summary collapse

Constructor Details

#initializeWhileRewriter

Returns a new instance of WhileRewriter.



153
154
155
156
# File 'lib/virtual_keywords/keyword_rewriter.rb', line 153

def initialize
  super
  self.strict = false
end

Instance Method Details

#rewrite_while(expression) ⇒ Object

Rewrite “while” expressions (automatically called by SexpProcessor#process)

Arguments:

expression: (Sexp) the :while sexp to rewrite.

Returns:

(Sexp): a sexp that instead calls REWRITTEN_KEYWORDS.call_while


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/virtual_keywords/keyword_rewriter.rb', line 165

def rewrite_while(expression)
  condition = expression[1]
  body = expression[2]
  
  # This was a true in the example I checked (in sexps_while.txt)
  # but I'm not sure what it's for.
  third = expression[3]
  if third != true # Should be true, not just a truthy object
    raise UnexpectedSexp, 'Expected true as the 3rd element in a while, ' +
        "but got #{third}, this is probably a bug."
  end

  s(:call,
    s(:colon2,
      s(:const, :VirtualKeywords),
      :REWRITTEN_KEYWORDS
    ), :call_while,
    s(:array,
      s(:self),
      s(:iter, s(:fcall, :lambda), nil, condition),
      s(:iter, s(:fcall, :lambda), nil, body)
    )
  )
end