Class: RMasm::PrimitiveDataItem

Inherits:
DataItem show all
Defined in:
lib/rmasm/data_primitive.rb

Overview




Instance Attribute Summary

Attributes inherited from DataItem

#array_size, #default_value, #symbol, #type

Attributes inherited from Directive

#id

Instance Method Summary collapse

Methods inherited from DataItem

decode, decode_array_size, fetch

Methods included from IBinIOWriter

#_binary_read, #_binary_write, #binary_read, #binary_write, #from_binary, #to_binary

Methods inherited from Directive

#fetch

Constructor Details

#initialize(type, symbol, array_size, default_value, value) ⇒ PrimitiveDataItem

Returns a new instance of PrimitiveDataItem.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rmasm/data_primitive.rb', line 121

def initialize(type, symbol, array_size, default_value, value)
  super type, symbol, array_size, default_value
  @values = []
  @hash_single = {}
  @hash_range = {}
  @max_hash_array_index = -1
  @errors = 0

  # If the value is not an array, put it in an array to use common process_array_values() function
  if !value.is_a?(Array)
    process_single_value(value)
  elsif !@array_size.nil?
    process_array_values(value)

    # Update the real size according to the values and the hash ranges
    if @values.length > @array_size
      if @array_size == 0
        @array_size = @values.length
      else
        report_error(:R4A83, @type, @array_size, @values.length)
      end
    end

    if (@max_hash_array_index+1) > @array_size
      @array_size = @max_hash_array_index + 1
    end
  else
    report_error(:R4A72, value, @type)
  end

end

Instance Method Details

#index_invalid?(index) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rmasm/data_primitive.rb', line 202

def index_invalid?(index)
  if index < 0
    return true
  elsif @array_size == 0
    @max_hash_array_index = index if index > @max_hash_array_index
    return false
  elsif index < @array_size
    return false
  end
  return true
end

#process_array_values(values) ⇒ Object

process_array_values process an array of initialization values for a primitive type This function handles simples numeric ans string valeus, as well as nestesd arrays, hash with single mapping and range mapping



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rmasm/data_primitive.rb', line 186

def process_array_values(values)
  for value in values
    # Try to process as a single value
    if !process_single_value(value)
      if value.is_a?(Array)  # Handle ARRAY value (follow it)
        process_array_values(value)
      elsif value.is_a?(Hash)   # Handle HASH value (follow it)
        process_hash_values(value)
      else
        # Unexpected datatype [%s] with value [%s]. Value must be [%s] in the range [%s]"
        report_error(:R4A14, value.class, value, @type, @type.range)
      end
    end
  end
end

#process_hash_values(hash) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/rmasm/data_primitive.rb', line 214

def process_hash_values(hash)
  hash.each do |index,value|

    # Check that the value is a single char. If yes, take it as a byte value
    if value.is_a?(String)
      value_str = @type.from_str(value)
      # Check that the value is converted?
      if value_str.nil?
        return report_error(:R4A23, value, @type, @type)
      end
      # Check that inside a hash we don't have any more than a single value
      if value_str.length != 1
        return report_error(:R4A94, value, value.length, @type, @type.sizeof)
      end
      # Get only a single value          
      value = value_str[0]
    elsif !@type.valid?(value)
      # return from the do...end, but do not return from the hash.each. We want to list all errors
      return report_error(:R4A03, value, @type, @type.range)
    end

    # Check the index value
    if index.is_a?(Integer)
      # If numeric check that the range is valid (take into account the variable array)
      if index_invalid?(index)
        return report_error(:R4A33, index, @type, @array_size)
      end
      # If index is ok, then register this index => value
      @hash_single[index] = value
    elsif index.is_a?(Range)

      # Check that the range is using integers
      if !(index.begin.is_a?(Integer) && index.end.is_a?(Integer))
        return report_error(:R4A43, index, @type, @array_size)
      end

      # Normalize range value
      r_start = index.begin<0?0:index.begin;
      r_end = index.end<0?@array_size-1:index.end;
      # invert begin,end if begin > end
      if (r_start > r_end )
        r_start, r_end = [r_end, r_start]
      end
      if index_invalid?(r_start)
        return report_error(:R4A53, r_start, index, @type, @array_size)
      end
      if index_invalid?(r_end)
        return report_error(:R4A53, r_end, index, @type, @array_size)
      end
      @hash_range[Range.new(r_start,r_end)] = value
    else
      return report_error(:R4A63, index, @type, @array_size)
    end
  end
end

#process_single_value(value) ⇒ Object

Return true if the value was processed. “true” does not imply that the value is in the correct range.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rmasm/data_primitive.rb', line 159

def process_single_value(value)
  if value.is_a?(Numeric)  # Handle NUMERIC value
    # Does the type accept this value?
    if @type.valid?(value)
      # add this value
      @values << value
    else
      # If not, report an error and continue
      report_error(:R4A03, value, @type, @type.range)
    end
    return true
  elsif value.is_a?(String) # Handle STRING value
    # Does the type accept a conversion from a string?
    value_str = @type.from_str(value)
    if value_str.nil?
      report_error(:R4A23, value, @type, @type)
    else
      @values = @values + value_str
    end
    return true
  end
  return false
end

#report_error(symbol, *args) ⇒ Object



153
154
155
156
# File 'lib/rmasm/data_primitive.rb', line 153

def report_error(symbol,*args)
  @errors += 1
  Report.error(symbol, *args)
end