Class: Code::Object::Function
Instance Attribute Summary collapse
Attributes inherited from Code::Object
#raw
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_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.
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
31
32
33
|
# File 'lib/code/object/function.rb', line 31
def self.name
"Function"
end
|
Instance Method Details
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
|
82
83
84
|
# File 'lib/code/object/function.rb', line 82
def inspect
"function"
end
|
#signature_for_call ⇒ Object
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
|
101
102
103
|
# File 'lib/code/object/function.rb', line 101
def to_s
""
end
|