Sha256: aa58f52a1f27340c8af2bf9ab3a4dce193ffd4a3ebf838a9c62a1f3e6c4cfb21

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

class Code
  class Node
    class LeftOperation < Node
      class Operator < Node
        DOT = "."
        COLON_COLON = "::"

        attr_reader :operator, :statement

        def initialize(parsed)
          return if parsed.blank?

          @operator = parsed.delete(:operator).presence
          @statement = Statement.new(parsed.delete(:statement).presence)
        end

        def call?
          operator == DOT || operator == COLON_COLON
        end
      end

      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

      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)
          else
            left.call(
              **args,
              operator: right.operator,
              arguments: Object::List.new([right.statement.evaluate(**args)])
            )
          end
        end
      end

      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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
code-ruby-0.14.2 lib/code/node/left_operation.rb
code-ruby-0.14.1 lib/code/node/left_operation.rb
code-ruby-0.14.0 lib/code/node/left_operation.rb