Class: AmberVM::Interpreter::Parameter

Inherits:
Element show all
Defined in:
lib/amber/interpreter.rb

Overview

This evauates to the parameter passed to the function call. The number of it is evaluated and typecasted to an interger.

Instance Attribute Summary

Attributes inherited from Element

#pos

Instance Method Summary collapse

Constructor Details

#initialize(num, pos = nil) ⇒ Parameter

A Parameter is initialized wiht 1 to 2 parameters.

num

The number if the parameter to get, 0 is the first parameter passed, 1 the second and so on.

pos

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



911
912
913
914
915
# File 'lib/amber/interpreter.rb', line 911

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

Instance Method Details

#data_typeObject

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



925
926
927
# File 'lib/amber/interpreter.rb', line 925

def data_type
  @type
end

#execute(env) ⇒ Object

When executed the Parameter evaluates the number, of the parameter and then queries the environment to get the function parameter requested.

After the first execution the parameter remembers the type of the value it returns.



934
935
936
937
938
939
# File 'lib/amber/interpreter.rb', line 934

def execute env
  AmberVM::debug "Executing Parameter at #{@pos}..." if $DEBUG
  r = env.param(@num.execute(env).to_i)
  @type = r.data_type if r && r.respond_to?(:data_type)
  r
end

#optimize(variables = {}) ⇒ Object



941
942
943
944
# File 'lib/amber/interpreter.rb', line 941

def optimize variables = {}
  num = @num.optimize variables
  Parameter.new(num, @pos)
end

#pretty_print(q) ⇒ Object



917
918
919
920
921
# File 'lib/amber/interpreter.rb', line 917

def pretty_print(q)
  q.text "!!PARAMS["
  q.pp @num
  q.text "]"
end