Class: RVM::Interpreter::Declaration

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

Overview

A variable declarion that sets a local variable, it will redelcare the variable if declared in a privouse scope.

It is very closely related to the Assignment as it acts exactly alike if the variable is not yet existing in the Environment .

Both the name and the #value# are evaluated before the assignment is done.

Direct Known Subclasses

SimpleDeclaration

Instance Attribute Summary

Attributes inherited from Element

#pos

Instance Method Summary collapse

Constructor Details

#initialize(name, value, pos = nil) ⇒ Declaration

A Declaration is initialized wiht 2 to 3 parameters.

name

The name of the variable to store, it will be executed, usually this will be a Constant, unless dynamic naming is needed by the implemented language.

value

The value that will be assigned to the variable, for a = 1 + 1 ‘1+1’ would be the value to assign, so as this already suggests the value will be executed.

pos

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



539
540
541
542
543
# File 'lib/rvm/interpreter.rb', line 539

def initialize name, value, pos = nil
  super(pos)
  @name = name
  @value = value
end

Instance Method Details

#data_typeObject

The data type of a Assignment is the data type of it’s value.



556
557
558
# File 'lib/rvm/interpreter.rb', line 556

def data_type
  @value.data_type
end

#execute(env) ⇒ Object

When executed the assignment first evaluates the name of the assignment then the value and stores the result of the executed value in the environment under the name of the executed name.

If the variable was priviosely in a environment that lays above the current one in the hearachy the old value will not be altered in any way but a new variable declared.

The return value of the execution is the value that is assigned to the variable.



570
571
572
573
# File 'lib/rvm/interpreter.rb', line 570

def execute env
  RVM::debug "Executing Assignment at #{@pos}..." if $DEBUG
  env.declare(@name.execute(env).to_s,@value.execute(env)).val
end

#optimizeObject



575
576
577
578
579
580
581
582
583
584
# File 'lib/rvm/interpreter.rb', line 575

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

#pretty_print(q) ⇒ Object



545
546
547
548
549
550
551
552
553
# File 'lib/rvm/interpreter.rb', line 545

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