Class: CSquare::Generator::Enum
- Inherits:
-
Object
- Object
- CSquare::Generator::Enum
- Defined in:
- lib/csquare/generator/enum.rb
Defined Under Namespace
Classes: Namer, OpNamer, SparseOpNamer
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#on ⇒ Object
readonly
Returns the value of attribute on.
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
Instance Method Summary collapse
-
#enumerate ⇒ Object
Pre-set/retrieve all the enum values (e.g., OP_ADD => ‘+’.ord) for the specified ops or dtypes (an Array).
- #enumerate_for_index ⇒ Object
-
#enumerees ⇒ Object
Local symbols upon which we should enumerate.
-
#initialize(name, opts = {}) ⇒ Enum
constructor
A new instance of Enum.
- #to_c ⇒ Object
Constructor Details
#initialize(name, opts = {}) ⇒ Enum
Returns a new instance of Enum.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/csquare/generator/enum.rb', line 2 def initialize name, opts = {} @name = name @prefix = opts[:prefix] @on = opts.has_key?(:ops) ? :ops : :types @selector = opts[@on] # Don't need to use a namer if a hash was given; hash provides the names instead. if @selector.is_a?(Array) namer = opts[:with] namer ||= @on == :ops ? :OpNamer : :Namer namer_klass = begin CSquare::Generator::Enum.const_get(namer) rescue NameError Kernel.const_get(namer) end # Create a namer object with the specified prefix (if any) @namer = namer_klass.send :new, @prefix end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
26 27 28 |
# File 'lib/csquare/generator/enum.rb', line 26 def name @name end |
#on ⇒ Object (readonly)
Returns the value of attribute on.
26 27 28 |
# File 'lib/csquare/generator/enum.rb', line 26 def on @on end |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
26 27 28 |
# File 'lib/csquare/generator/enum.rb', line 26 def selector @selector end |
Instance Method Details
#enumerate ⇒ Object
Pre-set/retrieve all the enum values (e.g., OP_ADD => ‘+’.ord) for the specified ops or dtypes (an Array).
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/csquare/generator/enum.rb', line 47 def enumerate h = {} nums_used = Set.new autos = [] if @selector.is_a?(Hash) @selector.each_pair do |name, enumeree| prefixed_name = [@prefix, name].compact.join('_') autos << [enumeree, prefixed_name] end else @selector.each do |enumeree| num = @namer.num(enumeree) name = @namer.name(enumeree) next if name.nil? # don't bother if we couldn't determine a name if num == :auto autos << [enumeree, name] else h[name] = [enumeree, num] nums_used.add(num) unless num == :auto end end end count = 0 # Now go back and re-number everything marked :auto autos.each do |enumeree_and_name| enumeree, name = enumeree_and_name # Find the next unused number count += 1 while nums_used.include?(count) nums_used.add(count) h[name] = [enumeree, count] end h end |
#enumerate_for_index ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/csquare/generator/enum.rb', line 35 def enumerate_for_index ary = [] enumerees_and_c_values = enumerate.values enumerees_and_c_values.sort_by{ |ecv| ecv[1] }.each do |enumeree_and_c_value| ary <<[enumeree_and_c_value[0], enumeree_and_c_value[1]] end ary end |
#enumerees ⇒ Object
Local symbols upon which we should enumerate. If types, this will be the type symbols (e.g., i8, i16, i32). If ops, this will be operations, like :‘+’, :‘-’, etc. Returns an array.
30 31 32 |
# File 'lib/csquare/generator/enum.rb', line 30 def enumerees @selector.is_a?(Hash) ? @selector.values : @selector end |
#to_c ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/csquare/generator/enum.rb', line 92 def to_c ary = [] enumerate.each_pair do |c_symbol, enumeree_and_c_value| ary << "\t#{c_symbol} = #{enumeree_and_c_value[1]}" end "enum #{@name} {\n\t" + ary.join(",\n\t") + "\n};\n" end |