Class: Code::Object::Function

Inherits:
Code::Object show all
Defined in:
lib/code/object/function.rb

Direct Known Subclasses

RubyFunction

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, #code_and_operator, #code_different, #code_equal_equal, #code_equal_equal_equal, #code_exclamation_point, #code_exclusive_range, #code_inclusive_range, #code_or_operator, #code_self, #code_to_string, #falsy?, #hash, maybe, #multi_fetch, repeat, #sig, #truthy?, |

Constructor Details

#initialize(parameters:, body:) ⇒ Function

Returns a new instance of Function.



8
9
10
11
# File 'lib/code/object/function.rb', line 8

def initialize(parameters:, body:)
  @parameters = parameters
  @body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/code/object/function.rb', line 6

def body
  @body
end

#parametersObject (readonly)

Returns the value of attribute parameters.



6
7
8
# File 'lib/code/object/function.rb', line 6

def parameters
  @parameters
end

Class Method Details

.nameObject



13
14
15
# File 'lib/code/object/function.rb', line 13

def self.name
  "Function"
end

Instance Method Details

#call(**args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/code/object/function.rb', line 17

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, [])
  globals = multi_fetch(args, *::Code::GLOBALS)

  case operator.to_s
  when "", "call"
    sig(args) { signature_for_call }
    code_call(*arguments, **globals)
  else
    super
  end
end

#code_call(*arguments, **globals) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/code/object/function.rb', line 31

def code_call(*arguments, **globals)
  context = Context.new({}, parent: globals[:context])

  parameters.each.with_index do |parameter, index|
    if parameter.regular?
      if parameter.regular_splat?
        context.code_set(
          parameter.name,
          List.new(
            arguments.select(&:regular?).map(&:value)
          )
        )
      elsif parameter.keyword_splat?
        context.code_set(
          parameter.name,
          Dictionary.new(
            arguments.select(&:keyword?).map(&:name_value).to_h
          )
        )
      else
        argument = arguments[index]&.value
        argument = parameter.evaluate(**globals) if argument.nil?
        context.code_set(parameter.name, argument)
      end
    elsif parameter.keyword?
      argument = arguments.detect { |argument| argument.name == parameter.name }&.value
      argument = parameter.evaluate(**globals) if argument.nil?
      context.code_set(parameter.name, argument)
    else
      raise NotImplementedError
    end
  end

  body.evaluate(**globals, context:)
end

#inspectObject



67
68
69
# File 'lib/code/object/function.rb', line 67

def inspect
  "function"
end

#signature_for_callObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/code/object/function.rb', line 71

def signature_for_call
  parameters.inject([]) do |signature, parameter|
    if parameter.keyword?
      if signature.last.is_a?(::Hash)
        signature.last[parameter.name] = Object
        signature
      else
        signature + [{ parameter.name => Object }]
      end
    else
      signature + [Object]
    end
  end + [Object.repeat]
end

#to_sObject



86
87
88
# File 'lib/code/object/function.rb', line 86

def to_s
  ""
end