Class: Code::Node::LeftOperation
- Inherits:
-
Code::Node
- Object
- Code::Node
- Code::Node::LeftOperation
- Defined in:
- lib/code/node/left_operation.rb
Defined Under Namespace
Classes: Operator
Instance Method Summary collapse
- #evaluate(**args) ⇒ Object
-
#initialize(parsed) ⇒ LeftOperation
constructor
A new instance of LeftOperation.
- #resolve(**args) ⇒ Object
Constructor Details
#initialize(parsed) ⇒ LeftOperation
Returns a new instance of LeftOperation.
41 42 43 44 45 46 47 |
# File 'lib/code/node/left_operation.rb', line 41 def initialize(parsed) return if parsed.blank? @first = Statement.new(parsed.delete(:first).presence) @others = parsed.delete(:others).presence || [] @others.map! { |operator| Operator.new(operator) } end |
Instance Method Details
#evaluate(**args) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/code/node/left_operation.rb', line 49 def evaluate(**args) first = @first&.evaluate(**args) || Object::Nothing.new @others.reduce(first) do |left, right| if right.call? right.statement.evaluate(**args, object: left) elsif right.safe_call? if left.is_an?(Object::Nothing) Object::Nothing.new else right.statement.evaluate(**args, object: left) end elsif (right.or? && left.truthy?) || (right.and? && left.falsy?) left else left.call( **args, operator: right.operator, arguments: Object::List.new([right.statement.evaluate(**args)]) ) end end end |
#resolve(**args) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/code/node/left_operation.rb', line 73 def resolve(**args) first = @first&.resolve(**args) || Object::Nothing.new list = Object::IdentifierList.new([first]) (@others || []).each do |other| list.code_append( other.statement.resolve(**args, object: list.code_last) ) end list end |