Class: AmberVM::Interpreter::SimpleDeclaration

Inherits:
Declaration show all
Defined in:
lib/amber/optimisation.rb

Overview

For details see AmberVM::Interpreter::Declration, this is a optimized version vor simple cases.

Only #value# is evaluated before the assignment is done.

Instance Attribute Summary

Attributes inherited from Element

#pos

Instance Method Summary collapse

Methods inherited from Declaration

#data_type, #pretty_print

Constructor Details

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

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.



242
243
244
245
246
# File 'lib/amber/optimisation.rb', line 242

def initialize name, value, pos = nil
  @pos = pos
  @name = name.to_s
  @value = value
end

Instance Method Details

#execute(env) ⇒ Object

Only the value is evaluated before assigning it, the name is taken to be a string.

For details see AmberVM::Interpreter::Declration



252
253
254
255
# File 'lib/amber/optimisation.rb', line 252

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

#optimize(variables = {}) ⇒ Object



257
258
259
260
261
# File 'lib/amber/optimisation.rb', line 257

def optimize variables = {}
  variables[@name] = VariableStorage.new(AmberVM::Classes[:null].new(nil))
  value = @value.optimize variables
  Assignment.new(StaticVariable.new(variables[@name]), value)
end