class RgGen::SystemVerilog::Common::Utility::Identifier

Public Class Methods

new(name) { |self| ... } click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 8
def initialize(name)
  @name = name
  block_given? && yield(self)
end

Public Instance Methods

[](array_index_or_lsb, lsb_or_width = nil, width = nil) click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 38
def [](array_index_or_lsb, lsb_or_width = nil, width = nil)
  if array_index_or_lsb
    __create_new_identifier__(array_index_or_lsb, lsb_or_width, width)
  else
    self
  end
end
__array_format__(array_format) click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 21
def __array_format__(array_format)
  @array_format = array_format
end
__array_size__(array_size) click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 17
def __array_size__(array_size)
  @array_size = array_size
end
__sub_identifiers__(sub_identifiers) click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 25
def __sub_identifiers__(sub_identifiers)
  Array(sub_identifiers).each do |sub_identifier|
    (@sub_identifiers ||= []) << sub_identifier
    define_singleton_method(sub_identifier) do
      Identifier.new("#{@name}.#{__method__}")
    end
  end
end
__width__(width) click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 13
def __width__(width)
  @width = width
end
to_s() click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 34
def to_s
  @name.to_s
end

Private Instance Methods

__array_select__(array_index, lsb, width) click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 65
def __array_select__(array_index, lsb, width)
  if @array_format == :serialized
    "[#{__serialized_lsb__(array_index, lsb)}+:#{width || @width}]"
  else
    [
      *array_index.map { |index| "[#{index}]" },
      lsb && __create_select__(lsb, width, nil)
    ].compact.join
  end
end
__calc_index_value__(index, factor) click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 103
def __calc_index_value__(index, factor)
  __reduce_array__([factor, index].compact, :*, 1)
end
__create_new_identifier__(array_index_or_lsb, lsb_or_width, width) click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 48
def __create_new_identifier__(array_index_or_lsb, lsb_or_width, width)
  select = __create_select__(array_index_or_lsb, lsb_or_width, width)
  self.class.new("#{@name}#{select}") do |identifier|
    identifier.__sub_identifiers__(@sub_identifiers)
  end
end
__create_select__(array_index_or_lsb, lsb_or_width, width) click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 55
def __create_select__(array_index_or_lsb, lsb_or_width, width)
  if array_index_or_lsb.is_a?(::Array)
    __array_select__(array_index_or_lsb, lsb_or_width, width)
  elsif lsb_or_width
    "[#{array_index_or_lsb}+:#{lsb_or_width}]"
  else
    "[#{array_index_or_lsb}]"
  end
end
__enclose_index_in_parenthesis(index) click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 93
def __enclose_index_in_parenthesis(index)
  integer?(index) && index || "(#{index})"
end
__index_factors__() click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 97
def __index_factors__
  Array.new(@array_size.size) do |i|
    i.zero? ? nil : __reduce_array__(@array_size[-i..-1], :*, 1)
  end
end
__reduce_array__(array, operator, initial_value) click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 107
def __reduce_array__(array, operator, initial_value)
  array = array.compact
  if array.all?(&method(:integer?))
    array.reduce(initial_value, &operator)
  else
    array.join(operator.to_s)
  end
end
__serialized_index__(array_index) click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 85
def __serialized_index__(array_index)
  array_index
    .reverse
    .zip(__index_factors__)
    .map { |i, f| __calc_index_value__(i, f) }
    .yield_self { |values| __reduce_array__(values.reverse, :+, 0) }
end
__serialized_lsb__(array_index, lsb) click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 76
def __serialized_lsb__(array_index, lsb)
  index =
    array_index
      .yield_self(&method(:__serialized_index__))
      .yield_self(&method(:__enclose_index_in_parenthesis))
  array_lsb = __reduce_array__([@width, index], :*, 1)
  __reduce_array__([array_lsb, lsb], :+, 0)
end
integer?(value) click to toggle source
# File lib/rggen/systemverilog/common/utility/identifier.rb, line 116
def integer?(value)
  value.is_a?(Integer)
end