Class: Code::Object::Function

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

Instance Attribute Summary collapse

Attributes inherited from Code::Object

#raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, call, #code_and_operator, code_and_operator, #code_different, code_different, code_equal_equal, #code_equal_equal, code_equal_equal_equal, #code_equal_equal_equal, code_exclamation_point, #code_exclamation_point, code_exclusive_range, #code_exclusive_range, #code_inclusive_range, code_inclusive_range, code_new, code_or_operator, #code_or_operator, code_self, #code_self, #code_to_string, code_to_string, falsy?, #falsy?, #hash, inspect, maybe, multi_fetch, #multi_fetch, repeat, #sig, sig, #to_json, to_s, truthy?, #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
20
21
22
23
24
25
26
27
28
29
# File 'lib/code/object/function.rb', line 8

def initialize(*args, **_kargs, &_block)
  parameters = args.first.presence || List.new
  parameters = parameters.raw if parameters.is_an?(Object)
  @parameters = List.new(parameters)
  @parameters.raw.map! do |parameter|
    if parameter.is_a?(Node::FunctionParameter)
      Parameter.new(
        Dictionary.new({
          String.new(:name) => String.new(parameter.name),
          String.new(:keyword) => Boolean.new(parameter.keyword?),
          String.new(:regular_splat) => Boolean.new(parameter.regular_splat?),
          String.new(:keyword_splat) => Boolean.new(parameter.keyword_splat?),
          String.new(:default) => Code.new(parameter.default),
        })
      )
    else
      Parameter.new(parameter)
    end
  end
  @body = Code.new(args.second.presence || Nothing.new)
  super
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



31
32
33
# File 'lib/code/object/function.rb', line 31

def self.name
  "Function"
end

Instance Method Details

#as_jsonObject



105
106
107
# File 'lib/code/object/function.rb', line 105

def as_json(...)
  raw.as_json(...)
end

#call(**args) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/code/object/function.rb', line 35

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, [])
  globals = multi_fetch(args, *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



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
76
77
78
79
80
# File 'lib/code/object/function.rb', line 49

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

  parameters.raw.each.with_index do |parameter, index|
    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
        )
      )
    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)
    elsif parameter.regular?
      argument = arguments[index]&.value
      argument = parameter.evaluate(**globals) if argument.nil?
      context.code_set(parameter.name, argument)
    end
  end

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

#inspectObject



82
83
84
# File 'lib/code/object/function.rb', line 82

def inspect
  "function"
end

#signature_for_callObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/code/object/function.rb', line 86

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

#to_sObject



101
102
103
# File 'lib/code/object/function.rb', line 101

def to_s
  ""
end