class Unparser::Emitter::If

Emitter if nodes

Public Instance Methods

emit_ternary() click to toggle source
# File lib/unparser/emitter/if.rb, line 11
def emit_ternary
  visit(condition)
  write(' ? ')
  visit(if_branch)
  write(' : ')
  visit(else_branch)
end

Private Instance Methods

dispatch() click to toggle source
# File lib/unparser/emitter/if.rb, line 21
def dispatch
  if postcondition?
    emit_postcondition
  else
    emit_normal
  end
end
emit_condition() click to toggle source
# File lib/unparser/emitter/if.rb, line 59
def emit_condition
  visit(condition)
end
emit_else_branch() click to toggle source
# File lib/unparser/emitter/if.rb, line 71
def emit_else_branch
  return unless else_branch

  write('else') unless unless?
  emit_body(else_branch)
end
emit_if_branch() click to toggle source
# File lib/unparser/emitter/if.rb, line 63
def emit_if_branch
  if if_branch
    emit_body(if_branch)
  end

  nl if !if_branch && !else_branch
end
emit_normal() click to toggle source
# File lib/unparser/emitter/if.rb, line 43
def emit_normal
  write(keyword, ' ')
  emit_condition
  emit_if_branch
  emit_else_branch
  k_end
end
emit_postcondition() click to toggle source
# File lib/unparser/emitter/if.rb, line 37
def emit_postcondition
  visit(if_branch || else_branch)
  write(' ', keyword, ' ')
  emit_condition
end
keyword() click to toggle source
# File lib/unparser/emitter/if.rb, line 55
def keyword
  unless? ? 'unless' : 'if'
end
postcondition?() click to toggle source
# File lib/unparser/emitter/if.rb, line 29
def postcondition?
  return false unless if_branch.nil? ^ else_branch.nil?

  body = if_branch || else_branch

  local_variable_scope.first_assignment_in?(body, condition)
end
unless?() click to toggle source
# File lib/unparser/emitter/if.rb, line 51
def unless?
  !if_branch && else_branch
end