class BOAST::If

@!parse module Functors; functorize If; end

Attributes

conditions[R]

Public Class Methods

new(conditions, &block) click to toggle source

Creates a new instance of the If construct @overload initialize(condition, &block)

Creates a simple If construct
@param [Expression] condition
@param [Proc,nil] block if given, will be evaluated when {pr} is called

@overload initialize(conditions, &block)

Creates a multi-condition If construct
@param [Hash{Expression, :else => Proc}] conditions each condition and its associated block (can be nil)
@param [Proc,nil] block else block if :else is not specified in the conditions or nil
Calls superclass method BOAST::ControlStructure::new
# File lib/BOAST/Language/If.rb, line 17
def initialize(conditions, &block)
  super()
  @conditions = []
  @blocks = []
  if conditions.is_a?(Hash) then
    else_block = conditions.delete(:else)
    else_block = block unless else_block or not block
    conditions.each { |key, value|
      @conditions.push key
      @blocks.push value
    }
    @blocks.push else_block if else_block
  else
    @conditions.push conditions
    @blocks.push block if block
  end
end

Public Instance Methods

close() click to toggle source

Closes the If construct (keyword, closing bracket in C like languages). The result is printed to the BOAST output. @return [self]

# File lib/BOAST/Language/If.rb, line 108
def close
  decrement_indent_level
  s = ""
  s << indent
  s << end_string
  output.puts s
  return self
end
get_cl_strings()
Alias for: get_c_strings
get_cuda_strings()
Alias for: get_c_strings
open(condition_number = 0) click to toggle source

Opens the If construct. The result is printed on the BOAST output. If a condition number is given, will print the corresponding condition (or else if none exist) @param [Fixnum] condition_number condition to print @return [self]

# File lib/BOAST/Language/If.rb, line 78
def open(condition_number = 0)
  decrement_indent_level if condition_number > 0
  s = ""
  s << indent
  s << to_s(condition_number)
  output.puts s
  increment_indent_level
  return self
end
pr(*args) click to toggle source

Prints the If construct to the BOAST output (see {open}). If block/blocks is/are provided during initialization, they will be printed and the construct will be closed (see {close}). @param [Array<Object>] args any number of arguments to pass to the block/blocks @return [self]

# File lib/BOAST/Language/If.rb, line 92
def pr(*args)
  args = @args if args.length == 0 and @args
  if @blocks.size > 0 then
    @blocks.each_index { |indx|
      open(indx)
      @blocks[indx].call(*args)
    }
    close
  else
    open
  end
  return self
end
to_s(condition_number = 0) click to toggle source

Returns a string representation of the If construct. @param [Fixnum] condition_number condition to print

# File lib/BOAST/Language/If.rb, line 61
def to_s(condition_number = 0)
  s = ""
  if condition_number == 0 then
    s << if_string(@conditions.first)
  else
    if @conditions[condition_number] then
      s << elsif_string(@conditions[condition_number])
    else
      s << else_string
    end
  end
  return s
end

Private Instance Methods

get_c_strings() click to toggle source
# File lib/BOAST/Language/If.rb, line 35
def get_c_strings
  return { :if => '"if (#{cond}) {"',
           :elsif => '"} else if (#{cond}) {"',
           :else => '"} else {"',
           :end => '"}"' }
end
Also aliased as: get_cl_strings, get_cuda_strings
get_fortran_strings() click to toggle source
# File lib/BOAST/Language/If.rb, line 42
def get_fortran_strings
  return { :if => '"if (#{cond}) then"',
           :elsif => '"else if (#{cond}) then"',
           :else => '"else"',
           :end => '"end if"' }
end