Class: BaseVariable

Inherits:
Array show all
Includes:
Token, Variable
Defined in:
lib/core/variable/BaseVariable.rb

Overview

require “set” This was introduced so I could maintain the use of the class variable @@variable_id with the ArrayVarialble without having to include requirements with the array.

Instance Attribute Summary collapse

Attributes included from Variable

#scope_id, #uniq_id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Variable

#increament_uniq_id!, #meets_requirements?, #to_var, variable_id

Methods included from VariableIncluded

#variable

Methods inherited from Array

#cauldron_method_calls, #contains?, #copy, #select_all, #to_declaration, #to_intrinsic, #to_literal, #to_var

Constructor Details

#initialize(id = nil) ⇒ BaseVariable

TODO I think I’ll get rid of the id parameter and just use blocks

NOTE:  You can set the variable ids by passing it in a block
       e.g. BaseVariable.new {[8,9]}
       where 8 is the variable_id and unique_id is 9


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/core/variable/BaseVariable.rb', line 19

def initialize(id=nil)  
  super()
  
  # TODO  I should maybe include a check on all .copy methods to ensure a block is provided
  # Set the variable id for the newly created variable    
  if block_given?
    # TEMP
    if yield.kind_of?(Fixnum)
      raise StandardError.new('Expecting array')
    end
    unless yield[:variable_id].nil?      
      @variable_id = yield[:variable_id]      
    else
      @variable_id = @@variable_id
      @@variable_id += 1
      # TODO  Should have a separate call that returns @@variable_id and increaments @@variable_id
    end
    unless yield[:uniq_id].nil? 
      raise StandardError.new('Expecting Fixnum') unless yield[:uniq_id].kind_of?(Fixnum)
      @uniq_id = yield[:uniq_id]
    else  
      @uniq_id = @@uniq_id
      @@uniq_id += 1
    end
    
    @uniq_id_history = yield[:uniq_id_history] unless yield[:uniq_id_history].nil? 
    
  else
    if id.nil?
      @variable_id = @@variable_id
      @@variable_id += 1      
    else
      @variable_id = id
    end
    
  end
  
  # Give the variable an unique id
  if @uniq_id.nil?
    @uniq_id = @@uniq_id 
    #raise StandardError.new('Uniq id change point') if (@uniq_id+1) == 86
    @@uniq_id += 1      
  end
  
  @uniq_id_history ||= []
  
  # Flag indicating if this is a scope variable or a instance variable
  @instance_variable = false
  
end

Instance Attribute Details

#instance_variableObject

Returns the value of attribute instance_variable.



7
8
9
# File 'lib/core/variable/BaseVariable.rb', line 7

def instance_variable
  @instance_variable
end

#uniq_id_historyObject

Returns the value of attribute uniq_id_history.



7
8
9
# File 'lib/core/variable/BaseVariable.rb', line 7

def uniq_id_history
  @uniq_id_history
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/core/variable/BaseVariable.rb', line 7

def value
  @value
end

#variable_idObject (readonly)

Returns the value of attribute variable_id.



7
8
9
# File 'lib/core/variable/BaseVariable.rb', line 7

def variable_id
  @variable_id
end

Class Method Details

.reset_global_idObject



70
71
72
73
74
75
76
# File 'lib/core/variable/BaseVariable.rb', line 70

def self.reset_global_id
  #http://www.zenspider.com/Languages/Ruby/QuickRef.html
  unless $".include?('test/unit.rb')
    StandardLogger.log 'WARNING: Resetting variable id, this should only be done for tests'
  end
  @@variable_id = 0
end

Instance Method Details

#destructive_instance_calls(with = []) ⇒ Object

Raises:

  • (StandardError)


124
125
126
# File 'lib/core/variable/BaseVariable.rb', line 124

def destructive_instance_calls(with=[])
  raise StandardError.new(" 'destructive_instance_calls' method should be overridden for "+self.class.to_s)
end

#find_actual_variable(uniq_id) ⇒ Object

Returns the variable if the ids matched

Parameters:

  • id

    The uniq_id of the variable that is being looked for



82
83
84
85
# File 'lib/core/variable/BaseVariable.rb', line 82

def find_actual_variable(uniq_id)
  return self if @uniq_id == uniq_id
  throw :variable_not_found
end

#instance_calls(with = []) ⇒ Object

Returns an array of instance calls that can be created from this variable.

NOTE: I checked that instance_calls wasn’t a intrinsic method.

TODO Test this (esp with ArrayVariable)



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/core/variable/BaseVariable.rb', line 94

def instance_calls(with=[])
  return [] unless self.realised? 

  # Attempt to retrieve the literal value for the variable
  #val = self.literalise.value
  val = self.value
  
  # TODO  I suspect this method is redundant since it is always overwritten
  
  # Now retrieve all the possible instance call structures for that value
  results = []
  val.instance_call_structures.each do |struct|

    if struct.instance_class != self.class then raise StandardError.new('Unexpected class type: - expecting '+val.class.to_s+' but was '+struct.instance_class.to_s) end
    
    # Now create a method call of the instance call
    inst_call = InstanceCallContainer.new(self.copy,struct.method_call.copy) 
    results.push inst_call

  end
  return results    
end

#realised?Boolean

Always returns true since realised variables should extend this class but overwrite this method.

Returns:



148
149
150
# File 'lib/core/variable/BaseVariable.rb', line 148

def realised? 
  return false  
end

#returning_instance_calls(available = []) ⇒ Object

Returns an array of instances calls that return a value and are therefore approriate for declaration statements.



120
121
122
# File 'lib/core/variable/BaseVariable.rb', line 120

def returning_instance_calls(available=[])
  return instance_calls(available)  
end

#writeObject



131
132
133
134
135
136
# File 'lib/core/variable/BaseVariable.rb', line 131

def write()
  if (@instance_variable)
    return '@var_'+variable_id.to_s    
  end
  return 'var_'+variable_id.to_s    
end

#write_with_uniq_idObject



138
139
140
141
142
143
# File 'lib/core/variable/BaseVariable.rb', line 138

def write_with_uniq_id
  if (@instance_variable)
    return '@var_'+variable_id.to_s+'_u'+uniq_id.to_s    
  end
  return 'var_'+variable_id.to_s+'_u'+uniq_id.to_s            
end