Class: InstanceCallContainer

Inherits:
CallContainer show all
Includes:
PrintVariables, VariableIncluded
Defined in:
lib/core/InstanceCallContainer.rb

Overview

This class represents any instance method call. For example:

‘test’.length foo.class bar.nil?

TODO The InstanceCallContainer should may be be replaced with just statement - the parser

just has :call types.

Direct Known Subclasses

ArrayAccess

Instance Attribute Summary collapse

Attributes inherited from CallContainer

#parameters

Instance Method Summary collapse

Methods included from PrintVariables

#display_name_for

Methods inherited from CallContainer

#describe, #has?, #map_to, #select_all, #write

Methods included from WriteParameters

#describe_params, #write_params

Methods inherited from Array

#cauldron_method_calls, #select_all, #to_intrinsic, #to_literal, #to_var, #write

Constructor Details

#initialize(subject, method_call, *params) ⇒ InstanceCallContainer

Returns a new instance of InstanceCallContainer.

Parameters:

  • subject

    The variable or literal that is being acted upon

  • method_call

    The method call being made against the variable or literal

Raises:

  • (StandardError)


22
23
24
25
# File 'lib/core/InstanceCallContainer.rb', line 22

def initialize(subject,method_call,*params)
  raise StandardError.new('Use ClassMethodCallContainer here') if subject.kind_of?(ClassName)
  super(subject,method_call,*params)        
end

Instance Attribute Details

#method_callObject (readonly)

Returns the value of attribute method_call.



14
15
16
# File 'lib/core/InstanceCallContainer.rb', line 14

def method_call
  @method_call
end

#subjectObject

Returns the value of attribute subject.



14
15
16
# File 'lib/core/InstanceCallContainer.rb', line 14

def subject
  @subject
end

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#container=(val) ⇒ Object

Set the value of the container for the subject



151
152
153
154
# File 'lib/core/InstanceCallContainer.rb', line 151

def container=(val)
  @container = val
  @subject.container = @container
end

#contains?(&block) ⇒ Boolean

Returns:



87
88
89
90
91
92
# File 'lib/core/InstanceCallContainer.rb', line 87

def contains?(&block)
  return true if @parameters.contains?(&block)
  return true if block.call(@subject)  
  return true if block.call(@method_call)      
  return false
end

#contains_self?Boolean

Determines whether the is an imediate ‘self’ in the instance call.

Returns:



158
159
160
161
# File 'lib/core/InstanceCallContainer.rb', line 158

def contains_self?
  return true if subject.kind_of? This
  return false
end

#copyObject



98
99
100
101
# File 'lib/core/InstanceCallContainer.rb', line 98

def copy
  copied_subject = @subject.copy
  return InstanceCallContainer.new(copied_subject,@method_call.copy,*@parameters.collect {|x| x.copy})
end

#copy_contextual_variableObject



199
200
201
# File 'lib/core/InstanceCallContainer.rb', line 199

def copy_contextual_variable
  return contextual_subject  
end

#declaration_statementObject



203
204
205
206
# File 'lib/core/InstanceCallContainer.rb', line 203

def declaration_statement
  new_instance_call = ClassMethodCallContainer.new(InstanceCallContainerClass.new,New.new,@subject.copy.declaration_statement,@method_call.copy.declaration_statement)    
  return Statement.new(new_instance_call)
end

#equivalent?(to) ⇒ Boolean

Returns true if the supplied argument is equivant to this instance call container. They are equivalent if the their classes are the same, their subjects are equivalent, their method_calls are the same and use equivalent arguments.

Returns:



240
241
242
243
244
245
246
247
248
249
250
# File 'lib/core/InstanceCallContainer.rb', line 240

def equivalent?(to)
  return false unless self.class == to.class
  return false unless self.length == to.length
  return false unless @subject.equivalent?(to.subject)
  return false unless @method_call.class == to.method_call.class
  return false if @parameters.length != to.parameters.length
  @parameters.each_with_index do |x,i|
    return false unless x.equivalent?(to.parameters[i])
  end
  return true
end

#find_actual_variable(id) ⇒ Object

Returns the actual variable with the specified id.

Parameters:

  • id


129
130
131
132
133
134
135
# File 'lib/core/InstanceCallContainer.rb', line 129

def find_actual_variable(id)
  return @subject if @subject.uniq_id == id
  @parameters.each do |x|
    return x if x.uniq_id == id
  end
  throw :variable_not_found
end

#find_variable(id) ⇒ Object

Raises:

  • (StandardError)


193
194
195
196
# File 'lib/core/InstanceCallContainer.rb', line 193

def find_variable(id)
  raise StandardError.new('This instance call does not contain the variable with the id '+id.to_s) unless variable_id == id
  return contextual_subject
end

#literalisable?Boolean

Returns:



107
108
109
# File 'lib/core/InstanceCallContainer.rb', line 107

def literalisable?
  return @subject.literalisable?
end

#literalise(context = self) ⇒ Object

Attempts to return a literal value for the result of calling this instance call.



114
115
116
117
118
119
# File 'lib/core/InstanceCallContainer.rb', line 114

def literalise(context=self)
  unless(@subject.literalisable?)
    raise FailedToLiteraliseError.new('Failed to literalise '+self.write+' couln\'t literalise '+@subject.write)
  end 
  return eval(@subject.literalise.write(context)+@method_call.write)
end

#replace_theory_variables!(mapping) ⇒ Object

TODO There are many similar version of this method used



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/core/InstanceCallContainer.rb', line 35

def replace_theory_variables!(mapping)
  if @subject.kind_of?(TheoryVariable) && mapping.has_key?(@subject.theory_variable_id)
    @subject = mapping[@subject.theory_variable_id].copy
  elsif @subject.respond_to?(:replace_theory_variables!)
    @subject.replace_theory_variables!(mapping)
  end
  @parameters.each_with_index do |x,i|
    if x.kind_of?(TheoryVariable) && mapping.has_key?(x.theory_variable_id)
      @parameters[i] = mapping[x.theory_variable_id].copy
      next
    end
    @parameters[i].replace_theory_variables!(mapping) if @parameters[i].respond_to?(:replace_theory_variables!)
  end

end

#replace_variable_if(var, &block) ⇒ Object

Returns a duplicate instance call container with any matching variables replaced with the variable supplied.

Parameters:

  • var

    The variable that will replace any matching variables



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/core/InstanceCallContainer.rb', line 73

def replace_variable_if(var,&block)
  container = self.copy.clear   
  if block.call(@subject)    
    container.subject = var if block.call(@subject)
  end
  @parameters.each do |x|
    if x.kind_of?(Variable)
      container.push(var) if block.call(x)  
    end
    container.push(x.copy)           
  end
  return container
end

#replace_variables!(mapping) ⇒ Object

TODO This can probably replace replace_theory_variables!



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/core/InstanceCallContainer.rb', line 52

def replace_variables!(mapping)
  if @subject.kind_of?(Variable) && mapping.has_key?(@subject.variable_id)
    @subject = mapping[@subject.variable_id].copy
  elsif @subject.respond_to?(:replace_variables!)
    @subject.replace_variables!(mapping)
  end
  @parameters.each_with_index do |x,i|
    if x.kind_of?(Variable) && mapping.has_key?(x.variable_id)
      @parameters[i] = mapping[x.variable_id].copy
      next
    end
    @parameters[i].replace_variables!(mapping) if @parameters[i].respond_to?(:replace_variables!)
  end

end

#replace_variables_alt!(map) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/core/InstanceCallContainer.rb', line 252

def replace_variables_alt!(map)
  if @subject.kind_of?(TheoryVariable)
    map.each do |key,value|
      if @subject.theory_variable_id == key.theory_variable_id
        @subject = value
        break
      end
    end
  elsif(@subject.kind_of?(StringLengthClass))
  else
    @subject.replace_variables_alt!(map)
  end
  
  @parameters.each_with_index do |x,i|
    if x.kind_of?(TheoryVariable)
      map.each do |key,value|
        if x == key
          @parameters[i] = value
        end
      end
    else
      @parameters.replace_variables_alt!(map)
    end
  end
end

#responseObject



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/core/InstanceCallContainer.rb', line 137

def response
  case value.class.to_s
    when 'string'.class.to_s
      return StringVariable.new(value)
      break
    when 8.class.to_s
      return FixnumVariable.new(value)
  else
    raise StandardError.new('Unknown class '+value.class.to_s)
  end
end

#subst!(var, &block) ⇒ Object

Substitutes one variable for another one providing the the block returns true.



166
167
168
169
170
171
172
173
174
# File 'lib/core/InstanceCallContainer.rb', line 166

def subst!(var,&block)
  @subject = var.copy if block.call(@subject)
  @parameters.each_with_index do |x,i|
    if block.call(x)
      @parameters[i] = var.copy
    end
  end
  return self
end

#subst_self_for(literal) ⇒ Object

Substitutes the ‘self’ in the instance call for a literal value and evaluates it.



179
180
181
182
183
# File 'lib/core/InstanceCallContainer.rb', line 179

def subst_self_for literal
  if subject.kind_of? This
    @subject = literal
  end
end

#to_declarationObject

Returns a declaration instance that will create a InstanceCallContainer with same properies as this InstanceCallContainer.



218
219
220
# File 'lib/core/InstanceCallContainer.rb', line 218

def to_declaration
  return VariableDeclaration.new('InstanceCallContainer',*[@subject.to_declaration,@method_call.to_declaration,*@parameters.collect {|x| x.to_declaration}])  
end

#to_literal_stringObject

This method is used during tracking to print a general description of what the instance call is doing. e.g. ‘test’.chop.



211
212
213
# File 'lib/core/InstanceCallContainer.rb', line 211

def to_literal_string
  return @subject.to_literal_string+'.'+@method_call.to_literal_string
end

#variableObject



103
104
105
# File 'lib/core/InstanceCallContainer.rb', line 103

def variable
  return @subject
end

#variable_idObject



189
190
191
# File 'lib/core/InstanceCallContainer.rb', line 189

def variable_id
  return @subject.variable_id
end

#variablesObject

Returns an array of all the variables used in this instance call.



224
225
226
227
228
229
230
231
232
233
# File 'lib/core/InstanceCallContainer.rb', line 224

def variables
  if @subject.kind_of?(Variable) 
    results = [@subject.copy]
  end
  results ||= []
  @parameters.each do |x|
    results.push(x.copy)
  end
  return results
end

#write_in_context(context = self) ⇒ Object



94
95
96
# File 'lib/core/InstanceCallContainer.rb', line 94

def write_in_context(context=self)  
  return @subject.write_in_context(context)+'.'+method_call.write
end

#write_with_uniq_idObject



27
28
29
30
31
32
# File 'lib/core/InstanceCallContainer.rb', line 27

def write_with_uniq_id
  if @subject.respond_to?('write_with_uniq_id')
    return @subject.write_with_uniq_id+'.'+method_call.write+write_params(@parameters)
  end
  return @subject.write+'.'+method_call.write+write_params(@parameters)
end