Class: Code::Object::Function
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#<=>, #==, 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_or_operator, #code_or_operator, code_self, #code_self, code_to_string, #code_to_string, falsy?, #falsy?, hash, #hash, maybe, multi_fetch, #multi_fetch, repeat, sig, #sig, to_s, truthy?, #truthy?, |
Constructor Details
#initialize(parameters:, body:) ⇒ 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
Returns the value of attribute body.
6
7
8
|
# File 'lib/code/object/function.rb', line 6
def body
@body
end
|
#parameters ⇒ Object
Returns the value of attribute parameters.
6
7
8
|
# File 'lib/code/object/function.rb', line 6
def parameters
@parameters
end
|
Class Method Details
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
66
|
# 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
|
68
69
70
|
# File 'lib/code/object/function.rb', line 68
def inspect
"function"
end
|
#signature_for_call ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/code/object/function.rb', line 72
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
|
87
88
89
|
# File 'lib/code/object/function.rb', line 87
def to_s
""
end
|