Class: RubyHDL::High::TypeTuple
- Defined in:
- lib/HDLRuby/std/sequencer_sw.rb
Overview
Describes a tuple 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, #leaf?, #left, #max, #min, #name=, #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+.
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1331 def initialize(name,direction,*content) # Initialize the type. super(name) # Set the direction. @direction = direction.to_sym unless [:little, :big].include?(@direction) raise AnyError, "Invalid direction for a type: #{direction}" end # Check and set the content. content.each do |sub| unless sub.is_a?(Type) then raise AnyError, "Invalid class for a type: #{sub.class}" end end @types = content end |
Instance Method Details
#add_type(type) ⇒ Object
Adds a sub +type+.
1386 1387 1388 1389 1390 1391 1392 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1386 def add_type(type) unless type.is_a?(Type) then raise AnyError, "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)
1473 1474 1475 1476 1477 1478 1479 1480 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1473 def base if regular? then # Regular tuple, return the type of its first element. return @types[0] else raise AnyError, "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)
1465 1466 1467 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1465 def base? return regular? end |
#direction ⇒ Object
Get the direction of the type, little or big endian.
1444 1445 1446 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1444 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.
1397 1398 1399 1400 1401 1402 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1397 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.
1407 1408 1409 1410 1411 1412 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1407 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.
1415 1416 1417 1418 1419 1420 1421 1422 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1415 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.
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1351 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.
1486 1487 1488 1489 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1486 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.
1376 1377 1378 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1376 def get_all_types return @types.clone end |
#get_type(index) ⇒ Object
Gets a sub type by +index+.
1381 1382 1383 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1381 def get_type(index) return @types[index.to_i] end |
#hash ⇒ Object
Hash function.
1366 1367 1368 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1366 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)
1452 1453 1454 1455 1456 1457 1458 1459 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1452 def range if regular? then # Regular tuple, return its range as if it was an array. return 0..@types.size-1 else raise AnyError, "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.
1429 1430 1431 1432 1433 1434 1435 1436 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1429 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.
1371 1372 1373 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1371 def types? return true end |
#width ⇒ Object
Gets the bitwidth.
1439 1440 1441 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1439 def width return @types.reduce(0) { |sum,type| sum + type.width } end |