Class: Code::Object::Function

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

Constant Summary

Constants inherited from Code::Object

NUMBER_CLASSES

Instance Attribute Summary collapse

Attributes included from Concerns::Shared

#methods, #raw

Instance Method Summary collapse

Methods inherited from Code::Object

code_new, #code_new, maybe, #name, repeat, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #code_and, #code_as_json, #code_compare, #code_deep_duplicate, #code_different, #code_duplicate, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_falsy?, code_fetch, #code_fetch, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_inclusive_range, #code_inspect, #code_less, #code_less_or_equal, #code_methods, #code_name, #code_nothing?, #code_or, #code_self, #code_set, code_set, #code_something?, #code_strict_different, #code_strict_equal, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #to_s, #truthy?

Constructor Details

#initialize(*args, **_kargs, &_block) ⇒ Function

Returns a new instance of Function.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/code/object/function.rb', line 8

def initialize(*args, **_kargs, &_block)
  @code_parameters =
    List
      .new(args.first)
      .raw
      .map { |parameter| Parameter.new(parameter) }
      .to_code

  @code_body = Code.new(args.second.presence)

  self.raw = List.new([code_parameters, code_body])
end

Instance Attribute Details

#code_bodyObject (readonly)

Returns the value of attribute code_body.



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

def code_body
  @code_body
end

#code_parametersObject (readonly)

Returns the value of attribute code_parameters.



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

def code_parameters
  @code_parameters
end

Instance Method Details

#call(**args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/code/object/function.rb', line 21

def call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, List.new).to_code
  globals = multi_fetch(args, *GLOBALS)

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

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



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
66
67
68
69
70
71
72
73
74
75
# File 'lib/code/object/function.rb', line 35

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

  code_parameters.raw.each.with_index do |code_parameter, index|
    code_argument =
      if code_parameter.spread?
        code_arguments
      elsif code_parameter.regular_splat?
        code_arguments
      elsif code_parameter.keyword_splat?
        code_arguments.raw.detect do |code_argument|
          code_argument.is_a?(Dictionary)
        end || Dictionary.new
      elsif code_parameter.block?
        code_arguments
          .raw
          .detect { |code_argument| code_argument.is_a?(Function) }
          .to_code
      elsif code_parameter.keyword?
        code_arguments
          .raw
          .select { |code_argument| code_argument.is_a?(Dictionary) }
          .detect do |code_dictionary|
            code_dictionary.code_has_key?(
              code_parameter.code_name
            ).truthy?
          end
          &.code_get(code_parameter.code_name)
          .to_code
      else
        code_arguments.raw[index].to_code
      end

    code_argument = code_parameter.code_default if code_argument.nothing?

    code_context.code_set(code_parameter.code_name, code_argument)
  end

  code_body.code_evaluate(**globals, context: code_context)
end

#signature_for_callObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/code/object/function.rb', line 77

def signature_for_call
  code_parameters
    .raw
    .inject([]) do |signature, code_parameter|
      if code_parameter.spread?
        signature + [Object.repeat]
      elsif code_parameter.block?
        signature + [Function]
      elsif code_parameter.keyword_splat?
        signature + [Dictionary.maybe]
      elsif code_parameter.regular_splat?
        signature + [Object.repeat]
      elsif code_parameter.keyword? && code_parameter.required?
        if signature.last.is_a?(::Hash)
          signature.last[code_parameter.code_name] = Object
          signature
        else
          signature + [{ code_parameter.code_name => Object }]
        end
      elsif code_parameter.keyword?
        if signature.last.is_a?(::Hash)
          signature.last[code_parameter.code_name] = Object.maybe
          signature
        else
          signature + [{ code_parameter.code_name => Object.maybe }]
        end
      elsif code_parameter.required?
        signature + [Object]
      else
        signature + [Object.maybe]
      end
    end + [Object.repeat]
end