Class: BOAST::Case

Inherits:
Object show all
Defined in:
lib/BOAST/Algorithm.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression, *control) ⇒ Case

Returns a new instance of Case.



1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
# File 'lib/BOAST/Algorithm.rb', line 1350

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_listObject (readonly)

Returns the value of attribute constants_list.



1348
1349
1350
# File 'lib/BOAST/Algorithm.rb', line 1348

def constants_list
  @constants_list
end

#expressionObject (readonly)

Returns the value of attribute expression.



1347
1348
1349
# File 'lib/BOAST/Algorithm.rb', line 1347

def expression
  @expression
end

Class Method Details

.parens(*args, &block) ⇒ Object



1343
1344
1345
# File 'lib/BOAST/Algorithm.rb', line 1343

def self.parens(*args,&block)
  return self::new(*args,&block)
end

Instance Method Details

#close(final = true) ⇒ Object



1429
1430
1431
1432
# File 'lib/BOAST/Algorithm.rb', line 1429

def close(final=true)
  return self.close_fortran(final) if BOAST::get_lang == FORTRAN
  return self.close_c(final) if [C, CL, CUDA].include?( BOAST::get_lang )
end

#close_c(final = true) ⇒ Object



1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
# File 'lib/BOAST/Algorithm.rb', line 1433

def close_c(final=true)
  s = ""
  s += " "*BOAST::get_indent_level if final
  s += "break;\n"
  BOAST::decrement_indent_level      
  s += " "*BOAST::get_indent_level if final
  s += "}"
  BOAST::decrement_indent_level      
  BOAST::get_output.puts s if final
  return s
end

#close_fortran(final = true) ⇒ Object



1444
1445
1446
1447
1448
1449
1450
1451
1452
# File 'lib/BOAST/Algorithm.rb', line 1444

def close_fortran(final=true)
  s = ""
  BOAST::decrement_indent_level      
  s += " "*BOAST::get_indent_level if final
  s += "end select"
  BOAST::decrement_indent_level      
  BOAST::get_output.puts s if final
  return s
end


1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
# File 'lib/BOAST/Algorithm.rb', line 1418

def print(*args)
  first = true
  @blocks.each_index { |indx|
    s = self.to_str(@constants_list[indx],first)
    BOAST::get_output.puts s
    @blocks[indx].call(*args)
    first = false
  }
  self.close
  return self
end

#to_s(*args) ⇒ Object



1370
1371
1372
# File 'lib/BOAST/Algorithm.rb', line 1370

def to_s(*args)
  self.to_str(*args)
end

#to_str(constants, first = true) ⇒ Object



1374
1375
1376
1377
# File 'lib/BOAST/Algorithm.rb', line 1374

def to_str(constants, first= true)
  return self.to_str_fortran(constants, first) if BOAST::get_lang == FORTRAN
  return self.to_str_c(constants, first) if [C, CL, CUDA].include?( BOAST::get_lang )
end

#to_str_c(constants, first) ⇒ Object



1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
# File 'lib/BOAST/Algorithm.rb', line 1398

def to_str_c(constants, first)
  s = ""
  if first then
    s += " "*BOAST::get_indent_level
    s += "switch(#{@expression}){\n"
    BOAST::increment_indent_level
  else
    s += " "*BOAST::get_indent_level + "break;\n"
    BOAST::decrement_indent_level
  end
  s += " "*BOAST::get_indent_level
  if constants and constants.size>0 then
    s += "case #{constants.join(" : case")} :"
  else
    s += "default :"
  end
  BOAST::increment_indent_level
  return s
end

#to_str_fortran(constants, first) ⇒ Object



1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
# File 'lib/BOAST/Algorithm.rb', line 1379

def to_str_fortran(constants, first)
  s = ""
  if first then
    s += " "*BOAST::get_indent_level
    s += "select case (#{@expression})\n"
    BOAST::increment_indent_level
  else
    BOAST::decrement_indent_level
  end
  s += " "*BOAST::get_indent_level
  if constants and constants.size>0 then
    s += "case (#{constants.join(" : ")})"
  else
    s += "case default"
  end
  BOAST::increment_indent_level
  return s
end