Class: Interpreter::NativeFunction

Inherits:
Object
  • Object
show all
Includes:
Lox::Callable
Defined in:
lib/loxby/helpers/native_functions.rb

Overview

A ‘NativeFunction` is a loxby function which references a callable Ruby object (block, proc, method, etc.).

For example: “‘ruby @environment.set(

'clock',
NativeFunction.new(0) do |_interpreter, _args|
  Time.now.to_i.to_f
end

) “‘

Instance Method Summary collapse

Constructor Details

#initialize(given_arity = 0, &block) ⇒ NativeFunction

Returns a new instance of NativeFunction.



23
24
25
26
# File 'lib/loxby/helpers/native_functions.rb', line 23

def initialize(given_arity = 0, &block)
  @block = block
  @arity = given_arity
end

Instance Method Details

#arityObject



30
31
32
33
34
35
36
# File 'lib/loxby/helpers/native_functions.rb', line 30

def arity
  if @arity.respond_to? :call
    @arity.call
  else
    @arity
  end
end

#call(interpreter, args) ⇒ Object



28
# File 'lib/loxby/helpers/native_functions.rb', line 28

def call(interpreter, args) = @block.call(interpreter, args)

#to_sObject



38
39
40
# File 'lib/loxby/helpers/native_functions.rb', line 38

def to_s
  '<native fn>'
end