rubyx/lib/vool/statements/if_statement.rb
Torsten Ruger dab4e74659 Reworking if statement
Using 2 phase approach
Flattening tbd
2017-09-04 21:00:08 +03:00

49 lines
1.2 KiB
Ruby

module Vool
class IfStatement < Statement
attr_reader :condition , :if_true , :if_false
def initialize( cond , if_true , if_false = nil)
@condition = cond
@if_true = if_true
@if_false = if_false
simplify_condition
end
def to_mom( method )
if_true = @if_true.to_mom( method )
if_false = @if_false.to_mom( method )
condition , hoisted = hoist_condition( method )
check = Mom::IfStatement.new( Mom::TruthCheck.new(condition) , if_true , if_false )
check.hoisted = hoisted.to_mom(method) if hoisted
check
end
def hoist_condition( method )
return [@condition] if @condition.is_a?(Vool::Named)
local = method.create_tmp
assign = LocalAssignment.new( local , @condition)
[Vool::LocalVariable.new(local) , assign]
end
def collect(arr)
@if_true.collect(arr)
@if_false.collect(arr)
super
end
def simplify_condition
return unless @condition.is_a?(ScopeStatement)
@condition = @condition.first if @condition.single?
end
def has_false?
@if_false != nil
end
def has_true?
@if_true != nil
end
end
end