Class: BOAST::Case
- Inherits:
-
ControlStructure
- Object
- ControlStructure
- BOAST::Case
- Defined in:
- lib/BOAST/Case.rb
Constant Summary collapse
- @@c_strings =
{ :switch => '"switch (#{expr}) {"', :case => '"case #{constants.join(" : case")} :"', :default => '"default :"', :break => '"break;"', :end => '"}"' }
- @@f_strings =
{ :switch => '"select case (#{expr})"', :case => '"case (#{constants.join(" : ")})"', :default => '"case default"', :break => 'nil', :end => '"end select"' }
- @@strings =
{ C => @@c_strings, CL => @@c_strings, CUDA => @@c_strings, FORTRAN => @@f_strings }
Instance Attribute Summary collapse
-
#constants_list ⇒ Object
readonly
Returns the value of attribute constants_list.
-
#expression ⇒ Object
readonly
Returns the value of attribute expression.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(expression, *control) ⇒ Case
constructor
A new instance of Case.
- #open ⇒ Object
- #pr(*args) ⇒ Object
- #to_s(block_number = nil) ⇒ Object
Methods inherited from ControlStructure
inherited, token_string_generator
Methods included from Inspectable
Methods included from PrivateStateAccessor
private_boolean_state_accessor, private_state_accessor
Constructor Details
#initialize(expression, *control) ⇒ Case
Returns a new instance of Case.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/BOAST/Case.rb', line 8 def initialize(expression, *control) @expression = expression @constants_list = [] @blocks = [] if control.size < 1 then raise "No block given!" elsif control.size.even? then (0..control.size-1).step(2) { |i| @constants_list[i/2] = [control[i]].flatten @blocks[i/2] = control[i+1] } else (0..control.size-2).step(2) { |i| @constants_list[i/2] = [control[i]].flatten @blocks[i/2] = control[i+1] } @blocks.push(control.last) end end |
Instance Attribute Details
#constants_list ⇒ Object (readonly)
Returns the value of attribute constants_list.
6 7 8 |
# File 'lib/BOAST/Case.rb', line 6 def constants_list @constants_list end |
#expression ⇒ Object (readonly)
Returns the value of attribute expression.
5 6 7 |
# File 'lib/BOAST/Case.rb', line 5 def expression @expression end |
Instance Method Details
#close ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/BOAST/Case.rb', line 96 def close s = "" s += indent + break_string + "\n" if break_string decrement_indent_level s += indent s += end_string decrement_indent_level output.puts s return self end |
#open ⇒ Object
78 79 80 81 |
# File 'lib/BOAST/Case.rb', line 78 def open output.puts to_s return self end |
#pr(*args) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/BOAST/Case.rb', line 83 def pr(*args) open if @blocks.size > 0 then @blocks.each_index { |indx| s = to_s(indx) output.puts s @blocks[indx].call(*args) } close end return self end |
#to_s(block_number = nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/BOAST/Case.rb', line 57 def to_s(block_number = nil) s = "" if block_number then if block_number != 0 then s += indent + break_string + "\n" if break_string decrement_indent_level end s += indent if @constants_list[block_number] and @constants_list[block_number].size > 0 then s += case_string(@constants_list[block_number]) else s += default_string end else s += indent s += switch_string(@expression) end increment_indent_level return s end |