Class: Bones::Variable
Overview
This class represents individual variables. Variables have a name, a type, a direction and an id. They might also have an algorithmic species coupled if they represent an array. The variable type is in AST form and holds information which can be extracted through methods implemented for the Type class.
The provided methods are able to obtain information from the variables, such as the number of dimensions and the definition. Other methods query variables for properties, such as whether a variable is an array or not. Several methods should only be executed if the variable is an array - this is not checked within the methods itself.
Instance Attribute Summary collapse
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#factors ⇒ Object
readonly
Returns the value of attribute factors.
-
#guess ⇒ Object
Returns the value of attribute guess.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#size ⇒ Object
Returns the value of attribute size.
-
#species ⇒ Object
Returns the value of attribute species.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#definition ⇒ Object
Method to obtain the full defintion of a variable.
-
#device_definition ⇒ Object
Method to return the device version of a variable’s definition.
-
#device_name ⇒ Object
This method returns the device name of the variable.
-
#device_pointer ⇒ Object
This method returns a star (‘*’) if the variable is an array or an empty string if it is not.
-
#dimensions ⇒ Object
Method to obtain the number of dimensions of a variable.
-
#dynamic? ⇒ Boolean
This method detects whether the variable is dynamically allocated or not.
-
#flatindex ⇒ Object
Method to return the full flattened address.
-
#flatten ⇒ Object
Method to flatten an array into a one dimensional array.
-
#golden_name ⇒ Object
This method returns the ‘golden’ name of a variable.
-
#initialization ⇒ Object
This method returns the initialization code for a varia- ble, formatted as a string.
-
#initialize(name, type, size, direction, id, shared) ⇒ Variable
constructor
Method to initilize the variable class with a name, type and a direction.
-
#input? ⇒ Boolean
Method to find out whether a variable is used as an in- put.
-
#output? ⇒ Boolean
Method to find out whether a variable is used as an out- put.
-
#set_factors ⇒ Object
Method to return an array of multiplication factors for multi-dimensional arrays that need to be flattened.
-
#type_name ⇒ Object
Method to obtain the type of a variable, omitting any information about arrays or pointers.
Methods inherited from Common
#flatten_hash, #from, #replace_defines, #search_and_replace, #search_and_replace!, #sum, #sum_and_from, #to
Constructor Details
#initialize(name, type, size, direction, id, shared) ⇒ Variable
Method to initilize the variable class with a name, type and a direction.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/bones/variable.rb', line 21 def initialize(name,type,size,direction,id,shared) @name = name @type = type @size = size @direction = direction @id = id @shared = shared @guess = false @species = nil end |
Instance Attribute Details
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
16 17 18 |
# File 'lib/bones/variable.rb', line 16 def direction @direction end |
#factors ⇒ Object (readonly)
Returns the value of attribute factors.
16 17 18 |
# File 'lib/bones/variable.rb', line 16 def factors @factors end |
#guess ⇒ Object
Returns the value of attribute guess.
17 18 19 |
# File 'lib/bones/variable.rb', line 17 def guess @guess end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/bones/variable.rb', line 16 def name @name end |
#size ⇒ Object
Returns the value of attribute size.
17 18 19 |
# File 'lib/bones/variable.rb', line 17 def size @size end |
#species ⇒ Object
Returns the value of attribute species.
17 18 19 |
# File 'lib/bones/variable.rb', line 17 def species @species end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
16 17 18 |
# File 'lib/bones/variable.rb', line 16 def type @type end |
Instance Method Details
#definition ⇒ Object
90 91 92 |
# File 'lib/bones/variable.rb', line 90 def definition definition_prefix + ' ' + @name + definition_suffix end |
#device_definition ⇒ Object
Method to return the device version of a variable’s definition. This includes the variable’s type, zero or more stars and the variable’s name, all returned as a single string.
79 80 81 |
# File 'lib/bones/variable.rb', line 79 def device_definition type_name + device_pointer + ' ' + @name end |
#device_name ⇒ Object
This method returns the device name of the variable.
33 34 35 |
# File 'lib/bones/variable.rb', line 33 def device_name DEVICE+@name end |
#device_pointer ⇒ Object
This method returns a star (‘*’) if the variable is an array or an empty string if it is not. It will not be able to return more than a single star, as device varia- bles are assumed to be flattened.
98 99 100 |
# File 'lib/bones/variable.rb', line 98 def device_pointer (@type.array_or_pointer?) ? '*' : '' end |
#dimensions ⇒ Object
Method to obtain the number of dimensions of a variable. This method returns a positive integer. The functionality is implemented as a recursive search in the Type class.
71 72 73 |
# File 'lib/bones/variable.rb', line 71 def dimensions @type.dimensions end |
#dynamic? ⇒ Boolean
This method detects whether the variable is dynamically allocated or not. It returns either true or false.
119 120 121 |
# File 'lib/bones/variable.rb', line 119 def dynamic? (definition_prefix.count('*') > 0) end |
#flatindex ⇒ Object
Method to return the full flattened address.
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/bones/variable.rb', line 136 def flatindex indices = [] @species.dimensions.each_with_index do |dimension,num_dimension| index_reverse = !(@species.reverse?) ? num_dimension : @species.dimensions.length-num_dimension-1 # FIXME: REVERSED if (from(dimension) != to(dimension)) data = "#{GLOBAL_ID}_#{index_reverse}" else data = from(dimension) end indices.push(data + @factors[index_reverse]) end return indices.join(' + ') end |
#flatten ⇒ Object
Method to flatten an array into a one dimensional array. If an array has multiple dimensions, the method will return a ‘[0]’ for each additional dimension. This method assumes that the variable is an array.
106 107 108 |
# File 'lib/bones/variable.rb', line 106 def flatten ''+'[0]'*(dimensions-1) end |
#golden_name ⇒ Object
This method returns the ‘golden’ name of a variable. This is used to generate verification code.
39 40 41 |
# File 'lib/bones/variable.rb', line 39 def golden_name GOLDEN + '_' + @name + '_' + @id end |
#initialization ⇒ Object
This method returns the initialization code for a varia- ble, formatted as a string. This is used to generate the verification code.
113 114 115 |
# File 'lib/bones/variable.rb', line 113 def initialization (dynamic?) ? " = (#{definition_prefix})malloc(#{@size.join('*')}*sizeof(#{type_name}));"+NL : ';'+NL end |
#input? ⇒ Boolean
Method to find out whether a variable is used as an in- put. If the current algorithm uses the ‘shared’ pattern, then the variable must be input only, otherwise it can be both input or inout to be considered input. The return value is boolean.
48 49 50 |
# File 'lib/bones/variable.rb', line 48 def input? return (@shared) ? (@direction == INPUT) : (@direction == INPUT || @direction == INOUT) end |
#output? ⇒ Boolean
Method to find out whether a variable is used as an out- put. The variable can be both output or inout to be con- sidered input. The return value is boolean.
55 56 57 |
# File 'lib/bones/variable.rb', line 55 def output? return (@direction == OUTPUT || @direction == INOUT) end |
#set_factors ⇒ Object
Method to return an array of multiplication factors for multi-dimensional arrays that need to be flattened. For every dimension, one factor is generated.
126 127 128 129 130 131 132 133 |
# File 'lib/bones/variable.rb', line 126 def set_factors raise_error("Species dimensions (#{@species.dimensions.length}) and array dimensions (#{@size.length}) mismatch for array '#{@name}'") if @species.dimensions.length != @size.length sizes, @factors = [], [] @species.dimensions.each_with_index do |dimension,num_dimension| (sizes.empty?) ? @factors.push('') : @factors.push('*'+sizes.join('*')) sizes.push(simplify(@size[dimensions-num_dimension-1])) end end |
#type_name ⇒ Object
Method to obtain the type of a variable, omitting any information about arrays or pointers. Examples of out- puts are: int, float or char. This functionality is implemented as a recursive search in the Type class, since the type is in AST form.
64 65 66 |
# File 'lib/bones/variable.rb', line 64 def type_name @type.type_name.to_s end |