Class: ArrayAccess

Inherits:
InstanceCallContainer show all
Includes:
VariableIncluded
Defined in:
lib/core/statement/ArrayAccess.rb

Overview

TODO I am duplicating variables here with array, index, subject and method_call

Instance Attribute Summary collapse

Attributes inherited from InstanceCallContainer

#method_call, #subject, #value

Attributes inherited from CallContainer

#parameters

Instance Method Summary collapse

Methods included from VariableIncluded

#find_actual_variable

Methods inherited from InstanceCallContainer

#container=, #contains?, #contains_self?, #copy_contextual_variable, #declaration_statement, #find_actual_variable, #find_variable, #literalisable?, #literalise, #replace_variable_if, #replace_variables!, #subst!, #subst_self_for, #to_declaration, #variable_id, #variables, #write_in_context, #write_with_uniq_id

Methods included from PrintVariables

#display_name_for

Methods inherited from CallContainer

#has?, #map_to

Methods included from WriteParameters

#describe_params, #write_params

Methods inherited from Array

#contains?, #to_declaration, #to_intrinsic, #to_literal, #to_var

Constructor Details

#initialize(array, index) ⇒ ArrayAccess

Returns a new instance of ArrayAccess.



9
10
11
12
13
# File 'lib/core/statement/ArrayAccess.rb', line 9

def initialize(array,index)
  @array = array    
  @index = index
  super(@array,'[Array Access]',index)
end

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



7
8
9
# File 'lib/core/statement/ArrayAccess.rb', line 7

def array
  @array
end

#indexObject (readonly)

Returns the value of attribute index.



7
8
9
# File 'lib/core/statement/ArrayAccess.rb', line 7

def index
  @index
end

Instance Method Details

#cauldron_method_callsObject



27
28
29
# File 'lib/core/statement/ArrayAccess.rb', line 27

def cauldron_method_calls
  return @array[@index].cauldron_method_calls
end

#closureObject



39
40
41
# File 'lib/core/statement/ArrayAccess.rb', line 39

def closure
  return ['[',']']
end

#copyObject



31
32
33
# File 'lib/core/statement/ArrayAccess.rb', line 31

def copy
  return ArrayAccess.new(@array.copy,@index.copy)
end

#describeObject



19
20
21
# File 'lib/core/statement/ArrayAccess.rb', line 19

def describe
  return @array.describe+'['+@index.describe+']'    
end

#equivalent?(to) ⇒ Boolean

Returns true if the passed argument is the same kind of ArrayAccess - in that it is the same class, array and index.

Parameters:

  • to

    The array access that is being compared with this array access instance to determine if their equivalent.

Returns:



69
70
71
72
73
74
# File 'lib/core/statement/ArrayAccess.rb', line 69

def equivalent?(to)
  return false if to.class != self.class
  return false unless to.array.equivalent?(@array)
  return false unless to.index.equivalent?(@index)
  return true
end

#replace_theory_variables!(mapping) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/core/statement/ArrayAccess.rb', line 82

def replace_theory_variables!(mapping)
  if @array.kind_of?(TheoryVariable) && mapping.has_key?(@array.theory_variable_id)
    @array = mapping[@array.theory_variable_id].copy
  elsif @array.respond_to?(:replace_theory_variables!)
    @array.replace_theory_variables!(mapping)
  end  
  if @index.kind_of?(TheoryVariable) && mapping.has_key?(@index.theory_variable_id)
    @index = mapping[@index.theory_variable_id].copy
  elsif @index.respond_to?(:replace_theory_variables!)
    @index.replace_theory_variables!(mapping)
  end
  
end

#replace_variables_alt!(map) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/core/statement/ArrayAccess.rb', line 96

def replace_variables_alt!(map)
  if @array.kind_of?(TheoryVariable)
    map.each do |key,value|
      if @array.theory_variable_id == key.theory_variable_id
        @array = value
        break
      end
    end
  else
    @array.replace_variables_alt!(map)
  end
  
  #
  if @index.kind_of?(TheoryVariable)
    map.each do |key,value|
      if @index.theory_variable_id == key.theory_variable_id
        @index = value
        break
      end
    end
  elsif(@index.kind_of?(Literal))
  else
    @index.replace_variables_alt!(map)
  end        
end

#responseObject



23
24
25
# File 'lib/core/statement/ArrayAccess.rb', line 23

def response
  return eval(@array.literalise.write+'['+@index.write+']').to_var
end

#select_all(results = [], &block) ⇒ Object

Returns all the elements that match the selection criteria just like the select method. However is this contains an array it will seach the array.

Parameters:

  • results (defaults to: [])

    An array of current results that match the requirement of the block.

  • block

    .



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/core/statement/ArrayAccess.rb', line 51

def select_all(results=[],&block)
   results.push(@array) if block.call(@array)
   results.push(@index) if block.call(@index)
   if @array.respond_to?(:select_all) 
     @array.select_all(results,&block)
   end
   if @index.respond_to?(:select_all)
     @index.select_all(results,&block)
   end
   return results
end

#to_literal_stringObject

Returns a string calling the specfied index. e.g. “[4]”



78
79
80
# File 'lib/core/statement/ArrayAccess.rb', line 78

def to_literal_string
  return write    
end

#variableObject



35
36
37
# File 'lib/core/statement/ArrayAccess.rb', line 35

def variable
  return @array.copy
end

#writeObject



15
16
17
# File 'lib/core/statement/ArrayAccess.rb', line 15

def write
  return @array.write+'['+@index.write+']'
end