Class: RVM::Interpreter::Variable

Inherits:
Element
  • Object
show all
Defined in:
lib/rvm/interpreter.rb

Overview

Reads the value of a variable in the environment .

The name is evaluated before the variable is retrieved.

Direct Known Subclasses

SimpleVariable

Instance Attribute Summary

Attributes inherited from Element

#pos

Instance Method Summary collapse

Constructor Details

#initialize(name, pos = nil) ⇒ Variable

A Variable is initialized wiht 1 to 2 parameters.

name

The name of the variable to get, it will be executed as long as it is no Sybol in which case it is treated as a special variable.

pos

The position within the source code of the definition - for deugging purpose.



598
599
600
601
602
# File 'lib/rvm/interpreter.rb', line 598

def initialize name, pos = nil
  super(pos)
  @name = name
  @type = :any
end

Instance Method Details

#data_typeObject

The type can only be tretrieved when the name is aconstant as it can be evaluated without sideffect.



614
615
616
# File 'lib/rvm/interpreter.rb', line 614

def data_type
  @type
end

#execute(env) ⇒ Object

When the name is a symbol, the name isn’t executed and treated as a special variable. Otherwise the name is executed and converted into a string to be passed to the environment so it can go and collect the value.



622
623
624
625
626
627
628
629
630
631
632
633
634
635
# File 'lib/rvm/interpreter.rb', line 622

def execute env
  RVM::debug "Executing Variable at #{@pos}..." if $DEBUG
  begin
    n = @name
    if not @name.is_a?(Symbol)
      n = n.execute(env).to_s
    end
    r = env.read_var_val(n)
    @type = r.data_type if r.respond_to?(:data_type)
    r
  rescue Exception => e
    raise RuntimeError.new("Failed to get Varialbe #{e}", @pos[0], @pos[1], @pos[2])
  end
end

#optimizeObject



637
638
639
640
641
642
643
644
645
646
647
648
# File 'lib/rvm/interpreter.rb', line 637

def optimize
  @name = @name.optimize if not @name.is_a?(Symbol)
  if @name.is_a?(Symbol)
    RVM::debug "Optimizing #{self}, by making it simple" if $DEBUG
    RVM::Interpreter::SimpleVariable.new(@name, @pos)
  elsif @name.is_a?(RVM::Interpreter::Constant)
    RVM::debug "Optimizing #{self}, by making it simple" if $DEBUG
    RVM::Interpreter::SimpleVariable.new(@name.value, @pos)
  else
    super
  end
end

#pretty_print(q) ⇒ Object



604
605
606
607
608
609
610
# File 'lib/rvm/interpreter.rb', line 604

def pretty_print(q)
  if @name.is_a? Constant
    q.text @name.value
  else
    q.pp @name
  end
end