Class: RVM::Interpreter::CoreCall

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

Overview

A core call is a call to the direct core functions and libraries RVM proides. this is used to write core libraries for different languages.

Instance Attribute Summary collapse

Attributes inherited from Element

#pos

Instance Method Summary collapse

Constructor Details

#initialize(function, arguments, pos = nil) ⇒ CoreCall

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

Arguments is a list of the arguments to the function.



956
957
958
959
960
# File 'lib/rvm/interpreter.rb', line 956

def initialize function,  arguments, pos = nil
  super(pos)
  @function = function
  @arguments = arguments
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



950
951
952
# File 'lib/rvm/interpreter.rb', line 950

def arguments
  @arguments
end

#functionObject (readonly)

Returns the value of attribute function.



951
952
953
# File 'lib/rvm/interpreter.rb', line 951

def function
  @function
end

Instance Method Details

#data_typeObject



998
999
1000
# File 'lib/rvm/interpreter.rb', line 998

def data_type
  RVM::Functions[@function] ? RVM::Functions[@function].data_type : :any
end

#execute(env) ⇒ Object

When executed the CoreCall it will call one of the RVM’s library functions



1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
# File 'lib/rvm/interpreter.rb', line 1003

def execute env
  RVM::debug "Executing FunctionCall..." if $DEBUG
  args = @arguments.dup
  # The function is a anonymous function
 
  # Get the function from he globals
  
  if not fun = RVM::Functions[@function]
    raise RuntimeError.new("Function Not found!", @pos[0], @pos[1], @pos[2])
  end
  # Test if the arguments should be executed
  if fun.execargs
    # The arges get executed
    args.map! do |arg|
      arg.execute env
    end
  end
  # Call the function
  begin
    fun.call(args, env,  @pos)
  rescue Exception => e
    raise e
    raise RuntimeError.new("Function failed to execute: #{e}", @pos[0], @pos[1], @pos[2])
  end
end

#optimizeObject



993
994
995
996
# File 'lib/rvm/interpreter.rb', line 993

def optimize
  @arguments.map!{ |a| a.optimize}
  super
end

#pretty_print(q) ⇒ Object



962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
# File 'lib/rvm/interpreter.rb', line 962

def pretty_print(q)
  binary = {:sub => '-', :add => '+', :mul => '*', :div => '/', :mod => '%', :shl => '<<', :shr => '>>',
            :cmp => '<=>', :eq => '==', :gt => '>', :gte => '>=', :lt => '<', :lte => '<=',
            :bitwise_and => '&', :bitwise_or => '|', :bitwise_xor => '^',
            :and => '&&', :or => '||', }
  if binary.keys.include?(@function)
    first = true
    @arguments.each do |a|
      if first
        first = false
      else
        q.text " #{binary[@function]} "
      end
      q.pp a
    end
  else
    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
end