Sha256: 8414f58a7ad858ff29282b7ff7b9b352c4a7ca3e84bc62a736015e7fa6535100

Contents?: true

Size: 1.98 KB

Versions: 10

Compression:

Stored size: 1.98 KB

Contents

module Steep
  module AST
    module Types
      class Intersection
        attr_reader :types

        def initialize(types:)
          @types = types
        end

        def self.build(types:)
          types.flat_map do |type|
            if type.is_a?(Intersection)
              type.types
            else
              [type]
            end
          end.map do |type|
            case type
            when AST::Types::Any
              return AST::Types::Any.instance()
            when AST::Types::Bot
              return AST::Types::Bot.instance
            when AST::Types::Top
              nil
            else
              type
            end
          end.compact.yield_self do |tys|
            dups = Set.new(tys)

            case dups.size
            when 0
              AST::Types::Top.instance
            when 1
              tys.first || raise
            else
              new(types: dups.to_a)
            end
          end
        end

        def ==(other)
          other.is_a?(Intersection) && other.types == types
        end

        def hash
          @hash ||= self.class.hash ^ types.hash
        end

        alias eql? ==

        def subst(s)
          self.class.build(types: types.map {|ty| ty.subst(s) })
        end

        def to_s
          "(#{types.map(&:to_s).join(" & ")})"
        end

        def free_variables()
          @fvs ||= begin
                     set = Set.new
                     types.each do |type|
                       set.merge(type.free_variables)
                     end
                     set
                   end
        end

        include Helper::ChildrenLevel

        def each_child(&block)
          if block
            types.each(&block)
          else
            types.each
          end
        end

        def map_type(&block)
          self.class.build(
            types: each_child.map(&block)
          )
        end

        def level
          [0] + level_of_children(types)
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
steep-1.9.1 lib/steep/ast/types/intersection.rb
steep-1.9.0 lib/steep/ast/types/intersection.rb
steep-1.9.0.dev.2 lib/steep/ast/types/intersection.rb
steep-1.9.0.dev.1 lib/steep/ast/types/intersection.rb
steep-1.8.3 lib/steep/ast/types/intersection.rb
steep-1.8.2 lib/steep/ast/types/intersection.rb
steep-1.8.1 lib/steep/ast/types/intersection.rb
steep-1.8.0 lib/steep/ast/types/intersection.rb
steep-1.8.0.pre.2 lib/steep/ast/types/intersection.rb
steep-1.8.0.pre.1 lib/steep/ast/types/intersection.rb