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.



1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
# File 'lib/BOAST/Algorithm.rb', line 1342

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.



1340
1341
1342
# File 'lib/BOAST/Algorithm.rb', line 1340

def constants_list
  @constants_list
end

#expressionObject (readonly)

Returns the value of attribute expression.



1339
1340
1341
# File 'lib/BOAST/Algorithm.rb', line 1339

def expression
  @expression
end

Class Method Details

.parens(*args, &block) ⇒ Object



1335
1336
1337
# File 'lib/BOAST/Algorithm.rb', line 1335

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

Instance Method Details

#close(final = true) ⇒ Object



1420
1421
1422
1423
# File 'lib/BOAST/Algorithm.rb', line 1420

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



1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
# File 'lib/BOAST/Algorithm.rb', line 1424

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



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

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


1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
# File 'lib/BOAST/Algorithm.rb', line 1409

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



1362
1363
1364
# File 'lib/BOAST/Algorithm.rb', line 1362

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

#to_str(constants, first = true) ⇒ Object



1366
1367
1368
1369
# File 'lib/BOAST/Algorithm.rb', line 1366

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



1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
# File 'lib/BOAST/Algorithm.rb', line 1389

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



1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
# File 'lib/BOAST/Algorithm.rb', line 1371

def to_str_fortran(constants, first)
  s = ""
  if first then
    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