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
typeis 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
namewidthdirectionand whose sub types are given bycontent. -
#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_python_init, #to_tf_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.
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1852 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.
1907 1908 1909 1910 1911 1912 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1907 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)
1993 1994 1995 1996 1997 1998 1999 2000 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1993 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)
1985 1986 1987 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1985 def base? return regular? end |
#direction ⇒ Object
Get the direction of the type, little or big endian.
1964 1965 1966 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1964 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.
1917 1918 1919 1920 1921 1922 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1917 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.
1927 1928 1929 1930 1931 1932 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1927 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.
1935 1936 1937 1938 1939 1940 1941 1942 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1935 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.
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1872 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.
2006 2007 2008 2009 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2006 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.
1897 1898 1899 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1897 def get_all_types return @types.clone end |
#get_type(index) ⇒ Object
Gets a sub type by index.
1902 1903 1904 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1902 def get_type(index) return @types[index.to_i] end |
#hash ⇒ Object
Hash function.
1887 1888 1889 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1887 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)
1972 1973 1974 1975 1976 1977 1978 1979 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1972 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.
1949 1950 1951 1952 1953 1954 1955 1956 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1949 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.
1892 1893 1894 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1892 def types? return true end |
#width ⇒ Object
Gets the bitwidth.
1959 1960 1961 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1959 def width return @types.reduce(0) { |sum,type| sum + type.width } end |