Class: Ifdef::Rewriter

Inherits:
Parser::Rewriter
  • Object
show all
Defined in:
lib/ifdef/rewriter.rb

Instance Method Summary collapse

Constructor Details

#initialize(truth) ⇒ Rewriter

Returns a new instance of Rewriter.



3
4
5
6
# File 'lib/ifdef/rewriter.rb', line 3

def initialize(truth)
  @logic_processor = LogicProcessor.new(truth)
  super()
end

Instance Method Details

#on_if(node) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ifdef/rewriter.rb', line 8

def on_if(node)
  _if, _then, _else = *node.children

  case @logic_processor.result(_if)
  when :true
    process(_then) if _then
    replace(_else.loc.expression, "nil") if _else
  when :false
    process(_else) if _else
    replace(_then.loc.expression, "nil") if _then
  else
    process(_then) if _then
    process(_else) if _else
  end
end