Method: RubyToAnsiC#process_if

Defined in:
lib/ruby_to_ansi_c.rb

#process_if(exp) ⇒ Object

Conditional statements

TODO: implementation is ugly as hell... PLEASE try to clean



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/ruby_to_ansi_c.rb', line 442

def process_if(exp)
  cond_part = process exp.shift

  result = "if (#{cond_part})"

  then_block = ! exp.first.nil? && exp.first.first == :block
  then_part  = process exp.shift
  else_block = ! exp.first.nil? && exp.first.first == :block
  else_part  = process exp.shift

  then_part = "" if then_part.nil?
  else_part = "" if else_part.nil?

  result += " {\n"
  
  then_part = then_part.join(";\n") if Array === then_part
  then_part += ";" unless then_part =~ /[;}]\Z/
  # HACK: um... deal with nil correctly (see unless support)
  result += then_part.to_s # + ";"
  result += ";" if then_part.nil?
  result += "\n" unless result =~ /\n\Z/
  result += "}"

  if else_part != "" then
    result += " else {\n"
    else_part = else_part.join(";\n") if Array === else_part
    else_part += ";" unless else_part =~ /[;}]\Z/
    result += else_part
    result += "\n}"
  end

  result
end