Class: Code::Object::Function
- Inherits:
-
Code::Object
- Object
- Code::Object
- Code::Object::Function
- Defined in:
- lib/code/object/function.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
Attributes inherited from Code::Object
Instance Method Summary collapse
- #call(**args) ⇒ Object
- #code_call(*arguments, **globals) ⇒ Object
-
#initialize(*args, **_kargs) ⇒ Function
constructor
A new instance of Function.
- #signature_for_call ⇒ Object
Methods inherited from Code::Object
#<=>, #==, as_json, #as_json, call, #code_and_operator, code_and_operator, #code_as_json, code_as_json, 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_json, code_to_json, falsy?, #falsy?, #hash, inspect, maybe, multi_fetch, #multi_fetch, #nothing?, nothing?, repeat, sig, #sig, #succ, to_json, #to_json, to_s, truthy?, #truthy?, |
Constructor Details
#initialize(*args, **_kargs) ⇒ Function
Returns a new instance of Function.
8 9 10 11 12 13 |
# File 'lib/code/object/function.rb', line 8 def initialize(*args, **_kargs, &) @parameters = List.new(args.first.presence || []) @parameters.raw.map! { |parameter| Parameter.new(parameter) } @body = Code.new(args.second.presence || Nothing.new) @raw = List.new(parameters, body) end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
6 7 8 |
# File 'lib/code/object/function.rb', line 6 def body @body end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
6 7 8 |
# File 'lib/code/object/function.rb', line 6 def parameters @parameters end |
Instance Method Details
#call(**args) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/code/object/function.rb', line 15 def call(**args) operator = args.fetch(:operator, nil) arguments = args.fetch(:arguments, List.new) globals = multi_fetch(args, *GLOBALS) case operator.to_s when "", "call" sig(args) { signature_for_call } code_call(*arguments.raw, **globals) else super end end |
#code_call(*arguments, **globals) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/code/object/function.rb', line 29 def code_call(*arguments, **globals) context = Context.new({}, globals[:context]) parameters.raw.each.with_index do |parameter, index| argument = if parameter.keyword? arguments .detect do |dictionary| dictionary.code_has_value?(parameter.code_name) end &.code_get(parameter.code_name) else arguments[index] end argument = parameter.evaluate(**globals) if argument.nil? context.code_set(parameter.code_name, argument) end body.evaluate(**globals, context:) end |
#signature_for_call ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/code/object/function.rb', line 50 def signature_for_call parameters .raw .inject([]) do |signature, parameter| if parameter.keyword? if signature.last.is_a?(::Hash) signature.last.code_set(parameter.code_name, Object) signature else signature + [{ parameter.code_name => Object }] end else signature + [Object] end end + [Object.repeat] end |