Class: RVM::Interpreter::FunctionCall

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

Overview

A function call or a function or a block. The initialization of a new environment is done by the function Class as it also sorts the arguments into it.

Arguments are only executed when the function does return true for execargs.

Instance Attribute Summary collapse

Attributes inherited from Element

#pos

Instance Method Summary collapse

Constructor Details

#initialize(function, arguments, pos = [0,0,0]) ⇒ FunctionCall

The constructor. function can either be a block object or a function class.

Arguments is a list of the arguments to the function.



862
863
864
865
866
# File 'lib/rvm/interpreter.rb', line 862

def initialize function,  arguments, pos = [0,0,0]
  super(pos)
  @function = function
  @arguments = arguments
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



856
857
858
# File 'lib/rvm/interpreter.rb', line 856

def arguments
  @arguments
end

#functionObject (readonly)

Returns the value of attribute function.



857
858
859
# File 'lib/rvm/interpreter.rb', line 857

def function
  @function
end

Instance Method Details

#==(v) ⇒ Object

Comparing two function calls will result in a match when the passed arguments are the same and the function to call the same



891
892
893
894
895
896
897
# File 'lib/rvm/interpreter.rb', line 891

def == v
  if v.is_a? FunctionCall
   (@arguments == v.arguments) && (@function == v.function)
  else
    false
  end
end

#data_typeObject

The data type of the FunctionCall is the return value of the function that it calls.



885
886
887
# File 'lib/rvm/interpreter.rb', line 885

def data_type
  @function.data_type
end

#execute(env) ⇒ Object

When executed the FunctionCall has four possible behaviours.

1) If the function is a block, so an anonymous function, the arguments

will be executed and then the block is called.

2) The function is a locally defined function and then the arguments are

executed and passed to the locally defined function.

3) The function is a RVM::Functions::Function and execargs returns true,

the arguments are executed and the function called.

4) The function is a RVM::Functions::Function and execargs returns

false, the arguments are passed along unexecuted and the function has
to take care of that itself. This is important for logical functions
as and and or which execute only some of the arguments


920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
# File 'lib/rvm/interpreter.rb', line 920

def execute env
  RVM::debug "Executing FunctionCall..." if $DEBUG
  args = @arguments.dup
  # The function is a anonymous function
  if @function.is_a? RVM::Classes[:block]
    # The arguments are executed.
    args.map! do |arg|
      arg.execute env
    end
    # Call the function
    @function.call(args, env,  @pos)
    # The function is a selfdefined function (or object function)
  elsif fun = env.function(@function)
    # The arguments are executed.
    args.map! do |arg|
      arg.execute env
    end
    # Call the function
    fun.call(args, env, @pos)
    # If nothing of the above the function must be a global function.
  else
    raise RuntimeError.new("Function '#{@function}' Not found!", @pos[0], @pos[1], @pos[2])
  end
end

#optimizeObject



899
900
901
902
903
# File 'lib/rvm/interpreter.rb', line 899

def optimize
  @arguments.map!{ |a| a.optimize}
  @function.optimize if @function.respond_to?(:optimize)
  super
end

#pretty_print(q) ⇒ Object



868
869
870
871
872
873
874
875
876
877
878
879
880
881
# File 'lib/rvm/interpreter.rb', line 868

def pretty_print(q)
  first = true
  q.pp @function
  q.text "("
  @arguments.each do |a|
    if first
      first = false
    else
      q.text ', '
    end
    q.pp a
  end
  q.text ")"
end