Class: VirtualKeywords::IfRewriter
- Inherits:
-
SexpProcessor
- Object
- SexpProcessor
- VirtualKeywords::IfRewriter
- Defined in:
- lib/virtual_keywords/keyword_rewriter.rb
Overview
SexpProcessor subclass that rewrites “if” expressions. Calls VirtualKeywords::REWRITTEN_KEYWORDS.call_if and lets it pick a virtual implementation.
Instance Method Summary collapse
-
#initialize ⇒ IfRewriter
constructor
Initialize an IfRewriter (self.strict is false).
-
#rewrite_if(expression) ⇒ Object
Rewrite an :if sexp.
Constructor Details
#initialize ⇒ IfRewriter
Initialize an IfRewriter (self.strict is false)
7 8 9 10 |
# File 'lib/virtual_keywords/keyword_rewriter.rb', line 7 def initialize super self.strict = false end |
Instance Method Details
#rewrite_if(expression) ⇒ Object
Rewrite an :if sexp. SexpProcessor#process is a template method that will call this method every time it encounters an :if.
Arguments:
expression: (Sexp) the :if sexp to be rewritten.
Returns:
(Sexp) A rewritten sexp that calls
VirtualKeywords::REWRITTEN_KEYWORDS.call_if with the condition,
then clause, and else clause as arguments, all wrapped in lambdas.
It must also pass self to call_if, so REWRITTEN_KEYWORDS can decide
which of the lambdas registered with it should be called.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/virtual_keywords/keyword_rewriter.rb', line 24 def rewrite_if(expression) # The sexp for the condition passed to if is inside expression[1] # We can further process this sexp if it has and/or in it. condition = expression[1] then_do = expression[2] else_do = expression[3] # This ugly sexp turns into the following Ruby code: # VirtualKeywords::REWRITTEN_KEYWORDS.call_if( # self, lambda { condition }, lambda { then_do }, lambda { else_do }) s(:call, s(:colon2, s(:const, :VirtualKeywords), :REWRITTEN_KEYWORDS ), :call_if, s(:array, s(:self), s(:iter, s(:fcall, :lambda), nil, condition), s(:iter, s(:fcall, :lambda), nil, then_do), s(:iter, s(:fcall, :lambda), nil, else_do) ) ) end |