Class: RubyHDL::High::TypeStruct
- Defined in:
- lib/HDLRuby/std/sequencer_sw.rb
Overview
Describes a struct type.
Instance Attribute Summary collapse
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
Attributes inherited from Type
Instance Method Summary collapse
-
#each(&ruby_block) ⇒ Object
Iterates over the sub name/type pair.
-
#each_key(&ruby_block) ⇒ Object
Iterates over the keys.
-
#each_name(&ruby_block) ⇒ Object
Iterates over the sub type names.
-
#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(name) ⇒ Object
Gets a sub type by
name. -
#hash ⇒ Object
Hash function.
-
#initialize(name, dir, content) ⇒ TypeStruct
constructor
Creates a new structure type named
namewith directiondirand whose hierachy is given bycontent. -
#struct? ⇒ Boolean
Tells if the type has named sub types.
-
#types? ⇒ Boolean
Tells if the type has sub types.
-
#width ⇒ Object
Gets the bitwidth of the type, nil for undefined.
Methods inherited from Type
#[], #base, #base?, #binary, #comp_operator, #constant, #define_operator, #define_operator_with_context, #each_overload, #fixed?, #float?, #hierarchical?, #htype?, #inner, #input, #leaf?, #left, #max, #min, #output, #range, #range?, #register, #regular?, #right, #signed?, #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, dir, content) ⇒ TypeStruct
Creates a new structure type named name with direction dir and
whose hierachy is given by content.
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2019 def initialize(name,dir,content) # Initialize the type. super(name) # Set the direction. @direction = dir.to_sym unless [:little, :big].include?(@direction) raise "Invalid direction for a type: #{dir}" end # Check and set the content. content = Hash[content] @types = content.map do |k,v| unless v.is_a?(Type) then raise "Invalid class for a type: #{v.class}" end [ k.to_sym, v ] end.to_h end |
Instance Attribute Details
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
2015 2016 2017 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2015 def direction @direction end |
Instance Method Details
#each(&ruby_block) ⇒ Object
Iterates over the sub name/type pair.
Returns an enumerator if no ruby block is given.
2087 2088 2089 2090 2091 2092 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2087 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_key(&ruby_block) ⇒ Object
Iterates over the keys.
Returns an enumerator if no ruby block is given.
2107 2108 2109 2110 2111 2112 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2107 def each_key(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each_key) unless ruby_block # A ruby block? Apply it on each key. @types.each_key(&ruby_block) end |
#each_name(&ruby_block) ⇒ Object
Iterates over the sub type names.
Returns an enumerator if no ruby block is given.
2117 2118 2119 2120 2121 2122 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2117 def each_name(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each_name) unless ruby_block # A ruby block? Apply it on each name. @types.each_key(&ruby_block) end |
#each_type(&ruby_block) ⇒ Object
Iterates over the sub types.
Returns an enumerator if no ruby block is given.
2097 2098 2099 2100 2101 2102 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2097 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_value(&ruby_block) end |
#each_type_deep(&ruby_block) ⇒ Object Also known as: each_deep
Iterates over the types deeply if any.
2125 2126 2127 2128 2129 2130 2131 2132 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2125 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_value { |type| type.each_type_deep(&ruby_block) } end |
#eql?(obj) ⇒ Boolean
Comparison for hash: structural comparison.
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2040 def eql?(obj) # General type comparison. # return false unless super(obj) return false unless obj.is_a?(TypeStruct) # Specific comparison. idx = 0 obj.each_key do |name| return false unless @types[name].eql?(obj.get_type(name)) 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.
2151 2152 2153 2154 2155 2156 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2151 def equivalent?(type) return (type.is_a?(TypeStruct) and !@types.to_a.zip(type.types.to_a).index do |t0,t1| t0[0] != t1[0] or !t0[1].equivalent?(t1[1]) end) end |
#get_all_types ⇒ Object
Gets an array containing all the syb types.
2070 2071 2072 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2070 def get_all_types return @types.values end |
#get_type(name) ⇒ Object
Gets a sub type by name.
NOTE: name can also be an index.
2076 2077 2078 2079 2080 2081 2082 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2076 def get_type(name) if name.respond_to?(:to_sym) then return @types[name.to_sym] else return @types.values[name.to_i] end end |
#hash ⇒ Object
Hash function.
2055 2056 2057 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2055 def hash return [super,@types].hash end |
#struct? ⇒ Boolean
Tells if the type has named sub types.
2060 2061 2062 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2060 def struct? return true end |
#types? ⇒ Boolean
Tells if the type has sub types.
2065 2066 2067 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2065 def types? return true end |
#width ⇒ Object
Gets the bitwidth of the type, nil for undefined.
NOTE: must be redefined for specific types.
2139 2140 2141 2142 2143 2144 2145 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2139 def width if @types.is_a?(Array) then return @types.reduce(0) {|sum,type| sum + type.width } else return @types.each_value.reduce(0) {|sum,type| sum + type.width } end end |