Class: Code::Object::Function
Direct Known Subclasses
Super
Constant Summary
Constants inherited
from Code::Object
NUMBER_CLASSES
Instance Attribute Summary collapse
#methods, #raw
Instance Method Summary
collapse
code_new, #code_new, maybe, #name, repeat, |
#<=>, #==, #as_json, #blank?, #code_and, #code_as_json, #code_blank?, #code_compare, #code_deep_duplicate, #code_different, #code_duplicate, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_falsy?, code_fetch, 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_presence, #code_presence_in, #code_present?, #code_self, 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_time, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #present?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #to_s, #truthy?
Constructor Details
#initialize(*args, parent: nil, methods: nil, **_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
|
# File 'lib/code/object/function.rb', line 8
def initialize(*args, parent: nil, methods: nil, **_kargs, &_block)
@code_parameters =
List
.new(args.first)
.raw
.map { |parameter| Parameter.new(parameter) }
.to_code
@code_body = Code.new(args.second.presence)
@definition_context = args.third if args.third.is_a?(Context)
@parent = parent.to_code
self.methods = methods.to_code
self.methods = Dictionary.new if self.methods.nothing?
self.raw = List.new([code_parameters, code_body])
end
|
Instance Attribute Details
#code_body ⇒ Object
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_parameters ⇒ Object
Returns the value of attribute code_parameters.
6
7
8
|
# File 'lib/code/object/function.rb', line 6
def code_parameters
@code_parameters
end
|
#definition_context ⇒ Object
Returns the value of attribute definition_context.
6
7
8
|
# File 'lib/code/object/function.rb', line 6
def definition_context
@definition_context
end
|
Returns the value of attribute parent.
6
7
8
|
# File 'lib/code/object/function.rb', line 6
def parent
@parent
end
|
Instance Method Details
#call(**args) ⇒ Object
25
26
27
28
29
30
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
|
# File 'lib/code/object/function.rb', line 25
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,
explicit_arguments: args.fetch(:explicit_arguments, true),
bound_self: args.fetch(:bound_self, nil),
**globals
)
when "extend"
sig(args) { Function }
code_extend(code_arguments.code_first)
when /=$/
sig(args) { Object }
code_set(code_operator.to_s.chop, code_value)
when ->(operator) { code_has_key?(operator).truthy? }
result = code_fetch(code_operator)
if result.is_a?(Function)
result.call(**args, operator: nil, bound_self: self)
else
sig(args)
result
end
else
super
end
end
|
#code_call(*arguments, explicit_arguments: true, bound_self: nil, **globals) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/code/object/function.rb', line 59
def code_call(
*arguments,
explicit_arguments: true,
bound_self: nil,
**globals
)
code_arguments = arguments.to_code
code_context = Context.new({}, definition_context || globals[:context])
code_self = bound_self.to_code
code_self = Dictionary.new if code_self.nil? || code_self.nothing?
code_parent = captured_self.to_code
code_context.code_set("self", code_self)
bind_parent(code_context, code_self, code_parent)
if parent.is_a?(Function)
code_context.code_set(
"super",
Super.new(
parent,
code_arguments,
code_self,
definition_context || globals[:context],
explicit_arguments: explicit_arguments
)
)
end
code_parameters.raw.each.with_index do |code_parameter, index|
code_argument =
if code_parameter.spread? || 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
.grep(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
if code_argument.nothing?
code_default = code_parameter.code_default
code_argument =
if code_default.is_a?(Code)
code_default.code_evaluate(
**globals,
context: code_context
)
else
code_default
end
end
code_context.code_set(code_parameter.code_name, code_argument)
end
code_body.code_evaluate(**globals, context: code_context)
rescue Error::Return => e
e.code_value
end
|
#code_extend(function) ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/code/object/function.rb', line 186
def code_extend(function)
code_function = function.to_code
Function.new(
code_function.code_parameters,
code_function.code_body.raw,
code_function.definition_context,
parent: self,
methods: methods.code_deep_duplicate
)
end
|
#code_fetch(key) ⇒ Object
198
199
200
|
# File 'lib/code/object/function.rb', line 198
def code_fetch(key)
methods.code_fetch(key)
end
|
#code_get(key) ⇒ Object
202
203
204
|
# File 'lib/code/object/function.rb', line 202
def code_get(key)
methods.code_get(key)
end
|
#code_has_key?(key) ⇒ Boolean
206
207
208
|
# File 'lib/code/object/function.rb', line 206
def code_has_key?(key)
methods.code_has_key?(key)
end
|
#code_set(key, value) ⇒ Object
210
211
212
|
# File 'lib/code/object/function.rb', line 210
def code_set(key, value)
methods.code_set(key, value)
end
|
#code_to_string ⇒ Object
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/code/object/function.rb', line 168
def code_to_string
String.new(
Format.format(
[
{
function: {
parameters:
code_parameters.raw.map do |parameter|
parameter_to_raw(parameter)
end,
body: code_body.raw.to_raw
}
}
]
)
)
end
|
#signature_for_call ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/code/object/function.rb', line 136
def signature_for_call
code_parameters
.raw
.inject([]) do |signature, code_parameter|
if code_parameter.spread? || code_parameter.regular_splat?
signature + [Object.repeat]
elsif code_parameter.block?
signature + [Function]
elsif code_parameter.keyword_splat?
signature + [Dictionary.maybe]
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
|