Class: RubyHDL::High::TypeTuple
- Defined in:
- lib/HDLRuby/std/sequencer_sw.rb
Overview
Describes a tuple type.
Instance Attribute Summary
Attributes inherited from Type
Instance Method Summary collapse
-
#add_type(type) ⇒ Object
Adds a sub +type+.
-
#base ⇒ Object
Gets the base type.
-
#base? ⇒ Boolean
Tells if the type has a base.
-
#direction ⇒ Object
Get the direction of the type, little or big endian.
-
#each(&ruby_block) ⇒ Object
Iterates over the sub name/type pair.
-
#each_type(&ruby_block) ⇒ Object
Iterates over the sub types.
-
#each_type_deep(&ruby_block) ⇒ Object
(also: #each_deep)
Iterates over the types deeply if any.
-
#eql?(obj) ⇒ Boolean
Comparison for hash: structural comparison.
-
#equivalent?(type) ⇒ Boolean
Tell if +type+ is equivalent to current type.
-
#get_all_types ⇒ Object
Gets an array containing all the syb types.
-
#get_type(index) ⇒ Object
Gets a sub type by +index+.
-
#hash ⇒ Object
Hash function.
-
#initialize(name, direction, *content) ⇒ TypeTuple
constructor
Creates a new tuple type named +name+ width +direction+ and whose sub types are given by +content+.
-
#range ⇒ Object
Gets the range of the type.
-
#regular? ⇒ Boolean
Tell if the tuple is regular, i.e., all its sub types are equivalent.
-
#types? ⇒ Boolean
Tells if the type has sub types.
-
#width ⇒ Object
Gets the bitwidth.
Methods inherited from Type
#[], #binary, #comp_operator, #constant, #define_operator, #define_operator_with_context, #each_overload, #fixed?, #float?, #hierarchical?, #htype?, #inner, #input, #leaf?, #left, #max, #min, #output, #range?, #register, #right, #signed?, #struct?, #to_c, #to_c_init, #to_type, #to_vector, #typedef, #unary, #unsigned?, #vector?
Methods included from HDLRuby::Tprocess
#&, #*, #+, #+@, #-@, #/, #<<, #==, #abs, #lr, #make, #resolve, #slice, #~
Constructor Details
#initialize(name, direction, *content) ⇒ TypeTuple
Creates a new tuple type named +name+ width +direction+ and whose sub types are given by +content+.
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1386 def initialize(name,direction,*content) # Initialize the type. super(name) # Set the direction. @direction = direction.to_sym unless [:little, :big].include?(@direction) raise "Invalid direction for a type: #{direction}" end # Check and set the content. content.each do |sub| unless sub.is_a?(Type) then raise "Invalid class for a type: #{sub.class}" end end @types = content end |
Instance Method Details
#add_type(type) ⇒ Object
Adds a sub +type+.
1441 1442 1443 1444 1445 1446 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1441 def add_type(type) unless type.is_a?(Type) then raise "Invalid class for a type: #{type.class} (#{type})" end @types << type end |
#base ⇒ Object
Gets the base type.
NOTE: only valid if the tuple is regular (i.e., all its sub types are identical)
1527 1528 1529 1530 1531 1532 1533 1534 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1527 def base if regular? then # Regular tuple, return the type of its first element. return @types[0] else raise "No base type for type #{self}" end end |
#base? ⇒ Boolean
Tells if the type has a base.
NOTE: only if the tuple is regular (i.e., all its sub types are identical)
1519 1520 1521 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1519 def base? return regular? end |
#direction ⇒ Object
Get the direction of the type, little or big endian.
1498 1499 1500 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1498 def direction return @direction end |
#each(&ruby_block) ⇒ Object
Iterates over the sub name/type pair.
Returns an enumerator if no ruby block is given.
1451 1452 1453 1454 1455 1456 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1451 def each(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each) unless ruby_block # A ruby block? Apply it on each sub name/type pair. @types.each(&ruby_block) end |
#each_type(&ruby_block) ⇒ Object
Iterates over the sub types.
Returns an enumerator if no ruby block is given.
1461 1462 1463 1464 1465 1466 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1461 def each_type(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each_type) unless ruby_block # A ruby block? Apply it on each sub type. @types.each(&ruby_block) end |
#each_type_deep(&ruby_block) ⇒ Object Also known as: each_deep
Iterates over the types deeply if any.
1469 1470 1471 1472 1473 1474 1475 1476 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1469 def each_type_deep(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each_type_deep) unless ruby_block # A ruby block? First apply it to current. ruby_block.call(self) # And recurse on the sub types. @types.each { |type| type.each_type_deep(&ruby_block) } end |
#eql?(obj) ⇒ Boolean
Comparison for hash: structural comparison.
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1406 def eql?(obj) # # General type comparison. # return false unless super(obj) return false unless obj.is_a?(TypeTuple) # Specific comparison. idx = 0 obj.each_type do |type| return false unless @types[idx].eql?(type) idx += 1 end return false unless idx == @types.size return true end |
#equivalent?(type) ⇒ Boolean
Tell if +type+ is equivalent to current type.
NOTE: type can be compatible while not being equivalent, please
refer to hruby_types.rb
for type compatibility.
1540 1541 1542 1543 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1540 def equivalent?(type) return (type.is_a?(TypeTuple) and !@types.zip(type.types).index {|t0,t1| !t0.equivalent?(t1) }) end |
#get_all_types ⇒ Object
Gets an array containing all the syb types.
1431 1432 1433 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1431 def get_all_types return @types.clone end |
#get_type(index) ⇒ Object
Gets a sub type by +index+.
1436 1437 1438 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1436 def get_type(index) return @types[index.to_i] end |
#hash ⇒ Object
Hash function.
1421 1422 1423 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1421 def hash return [super,@types].hash end |
#range ⇒ Object
Gets the range of the type.
NOTE: only valid if the tuple is regular (i.e., all its sub types are identical)
1506 1507 1508 1509 1510 1511 1512 1513 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1506 def range if regular? then # Regular tuple, return its range as if it was an array. return 0..@types.size-1 else raise "No range for type #{self}" end end |
#regular? ⇒ Boolean
Tell if the tuple is regular, i.e., all its sub types are equivalent.
NOTE: empty tuples are assumed not to be regular.
1483 1484 1485 1486 1487 1488 1489 1490 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1483 def regular? return false if @types.empty? t0 = @types[0] @types[1..-1].each do |type| return false unless t0.equivalent?(type) end return true end |
#types? ⇒ Boolean
Tells if the type has sub types.
1426 1427 1428 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1426 def types? return true end |
#width ⇒ Object
Gets the bitwidth.
1493 1494 1495 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1493 def width return @types.reduce(0) { |sum,type| sum + type.width } end |