Class: VirtualKeywords::UntilRewriter

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

Overview

SexpProcessor subclass that rewrites “until” expressions.

Instance Method Summary collapse

Constructor Details

#initializeUntilRewriter

Returns a new instance of UntilRewriter.



193
194
195
196
# File 'lib/virtual_keywords/keyword_rewriter.rb', line 193

def initialize
  super
  self.strict = false
end

Instance Method Details

#rewrite_until(expression) ⇒ Object

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

Arguments:

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

Returns:

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


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/virtual_keywords/keyword_rewriter.rb', line 205

def rewrite_until(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_until,
    s(:array,
      s(:self),
      s(:iter, s(:fcall, :lambda), nil, condition),
      s(:iter, s(:fcall, :lambda), nil, body)
    )
  )
end