Class: TypeProf::Core::BasicVertex

Inherits:
Object
  • Object
show all
Defined in:
lib/typeprof/core/graph/vertex.rb

Direct Known Subclasses

Source, Vertex

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(types) ⇒ BasicVertex

Returns a new instance of BasicVertex.



3
4
5
# File 'lib/typeprof/core/graph/vertex.rb', line 3

def initialize(types)
  @types = types
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



7
8
9
# File 'lib/typeprof/core/graph/vertex.rb', line 7

def types
  @types
end

Instance Method Details

#each_type(&blk) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/typeprof/core/graph/vertex.rb', line 9

def each_type(&blk)
  @types.each_key(&blk)

  if @types_to_be_added
    until @types_to_be_added.empty?
      h = @types_to_be_added.dup
      h.each do |ty, source|
        @types[ty] = source
      end
      @types_to_be_added.clear
      h.each_key(&blk)
    end
  end
end

#new_vertex(genv, origin) ⇒ Object

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/typeprof/core/graph/vertex.rb', line 68

def new_vertex(genv, origin)
  raise NotImplementedError
end

#showObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/typeprof/core/graph/vertex.rb', line 24

def show
  Fiber[:show_rec] ||= Set.empty
  if Fiber[:show_rec].include?(self)
    "untyped"
  else
    begin
      Fiber[:show_rec] << self
      types = []
      bot = @types.keys.any? {|ty| ty.is_a?(Type::Bot) }
      optional = true_exist = false_exist = false
      each_type do |ty|
        if ty.is_a?(Type::Instance)
          case ty.mod.cpath
          when [:NilClass] then optional = true
          when [:TrueClass] then true_exist = true
          when [:FalseClass] then false_exist = true
          end
        end
      end
      bool = true_exist && false_exist
      types << "bool" if bool
      each_type do |ty, _source|
        if ty.is_a?(Type::Instance)
          next if ty.mod.cpath == [:NilClass]
          next if bool && (ty.mod.cpath == [:TrueClass] || ty.mod.cpath == [:FalseClass])
        end
        next if ty.is_a?(Type::Bot)
        types << ty.show
      end
      types = types.uniq.sort
      case types.size
      when 0
        optional ? "nil" : bot ? "bot" : "untyped"
      when 1
        types.first + (optional ? "?" : "")
      else
        "(#{ types.join(" | ") })" + (optional ? "?" : "")
      end
    ensure
      Fiber[:show_rec].delete(self) || raise
    end
  end
end