Sha256: 8526f65dfcb4d70c23e3a7b83b88ca00c8ce7ed446f370e4df77baef438a07e7

Contents?: true

Size: 1.92 KB

Versions: 16

Compression:

Stored size: 1.92 KB

Contents

module Steep
  module Interface
    class Substitution
      attr_reader :dictionary
      attr_reader :instance_type
      attr_reader :module_type
      attr_reader :self_type

      def initialize(dictionary:, instance_type:, module_type:, self_type:)
        @dictionary = dictionary
        @instance_type = instance_type
        @module_type = module_type
        @self_type = self_type
      end

      def self.empty
        new(dictionary: {}, instance_type: AST::Types::Instance.new, module_type: AST::Types::Class.new, self_type: AST::Types::Self.new)
      end

      def [](key)
        dictionary[key] or raise "Unknown variable: #{key}"
      end

      def key?(var)
        dictionary.key?(var)
      end

      def self.build(vars, types = nil, instance_type: AST::Types::Instance.new, module_type: AST::Types::Class.new, self_type: AST::Types::Self.new)
        types ||= vars.map {|var| AST::Types::Var.fresh(var) }

        raise "Invalid substitution: vars.size=#{vars.size}, types.size=#{types.size}" unless vars.size == types.size

        dic = vars.zip(types).each.with_object({}) do |(var, type), d|
          d[var] = type
        end

        new(dictionary: dic, instance_type: instance_type, module_type: module_type, self_type: self_type)
      end

      def except(vars)
        self.class.new(
          dictionary: dictionary.reject {|k, _| vars.include?(k) },
          instance_type: instance_type,
          module_type: module_type,
          self_type: self_type
        )
      end

      def merge!(s)
        dictionary.transform_values! {|ty| ty.subst(s) }
        dictionary.merge!(s.dictionary) do |key, a, b|
          if a == b
            a
          else
            raise "Duplicated key on merge!: #{key}, #{a}, #{b}"
          end
        end
      end

      def add!(v, ty)
        merge!(Substitution.new(dictionary: { v => ty }, instance_type: nil, module_type: nil, self_type: nil))
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
steep-0.11.1 lib/steep/interface/substitution.rb
steep-0.11.0 lib/steep/interface/substitution.rb
steep-0.10.0 lib/steep/interface/substitution.rb
steep-0.9.0 lib/steep/interface/substitution.rb
steep-0.8.2 lib/steep/interface/substitution.rb
steep-0.8.1 lib/steep/interface/substitution.rb
steep-0.8.0 lib/steep/interface/substitution.rb
steep-0.7.1 lib/steep/interface/substitution.rb
steep-0.7.0 lib/steep/interface/substitution.rb
steep-0.6.0 lib/steep/interface/substitution.rb
steep-0.5.1 lib/steep/interface/substitution.rb
steep-0.5.0 lib/steep/interface/substitution.rb
steep-0.4.0 lib/steep/interface/substitution.rb
steep-0.3.0 lib/steep/interface/substitution.rb
steep-0.2.0 lib/steep/interface/substitution.rb
steep-0.1.0 lib/steep/interface/substitution.rb