Class: RVM::Interpreter::Assignment

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

Overview

A variable assignment that sets a local variable. A declaration is not required before the assignment can be done, yet it can be used to force a laready declaed variale into the local scope.

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

Direct Known Subclasses

SimpleAssignment

Instance Attribute Summary

Attributes inherited from Element

#pos

Instance Method Summary collapse

Constructor Details

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

A Assignment 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.



475
476
477
478
479
# File 'lib/rvm/interpreter.rb', line 475

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.



492
493
494
# File 'lib/rvm/interpreter.rb', line 492

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.

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



502
503
504
505
# File 'lib/rvm/interpreter.rb', line 502

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

#optimizeObject



506
507
508
509
510
511
512
513
514
515
# File 'lib/rvm/interpreter.rb', line 506

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::SimpleAssignment.new(@name.value, @value, @pos)
  else
    super
  end
end

#pretty_print(q) ⇒ Object



481
482
483
484
485
486
487
488
489
# File 'lib/rvm/interpreter.rb', line 481

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