Class: Steep::Typing

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/typing.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTyping

Returns a new instance of Typing.



8
9
10
11
12
13
# File 'lib/steep/typing.rb', line 8

def initialize
  @errors = []
  @nodes = {}
  @var_typing = {}
  @typing = {}
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'lib/steep/typing.rb', line 3

def errors
  @errors
end

#nodesObject (readonly)

Returns the value of attribute nodes.



5
6
7
# File 'lib/steep/typing.rb', line 5

def nodes
  @nodes
end

#typingObject (readonly)

Returns the value of attribute typing.



4
5
6
# File 'lib/steep/typing.rb', line 4

def typing
  @typing
end

#var_typingObject (readonly)

Returns the value of attribute var_typing.



6
7
8
# File 'lib/steep/typing.rb', line 6

def var_typing
  @var_typing
end

Class Method Details

.summary(node) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/steep/typing.rb', line 66

def self.summary(node)
  src = node.loc.expression.source.split(/\n/).first
  line = node.loc.first_line
  col = node.loc.column

  "#{line}:#{col}:#{src}"
end

Instance Method Details

#add_error(error) ⇒ Object



15
16
17
# File 'lib/steep/typing.rb', line 15

def add_error(error)
  errors << error
end

#add_typing(node, type) ⇒ Object



19
20
21
22
23
24
# File 'lib/steep/typing.rb', line 19

def add_typing(node, type)
  typing[node.__id__] = type
  nodes[node.__id__] = node

  type
end

#add_var_type(variable, type) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/steep/typing.rb', line 30

def add_var_type(variable, type)
  if var_typing.key?(variable)
    unless var_typing[variable] == type
      raise "Unexpected variable typing: existing=#{var_typing[variable]}, new=#{type}"
    end
  end

  var_typing[variable] = type
end

#dump(io) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/steep/typing.rb', line 54

def dump(io)
  io.puts "Typing: "
  nodes.each_value do |node|
    io.puts "  #{Typing.summary(node)} => #{type_of(node: node).inspect}"
  end

  io.puts "Errors: "
  errors.each do |error|
    io.puts "  #{Typing.summary(error.node)} => #{error.inspect}"
  end
end

#has_type?(node) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/steep/typing.rb', line 26

def has_type?(node)
  typing.key?(node.__id__)
end

#type_of(node:) ⇒ Object



40
41
42
# File 'lib/steep/typing.rb', line 40

def type_of(node:)
  typing[node.__id__] or raise "Unknown node for typing: #{node.inspect}"
end

#type_of_variable(name: nil, label: nil) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/steep/typing.rb', line 44

def type_of_variable(name: nil, label: nil)
  var_typing.each do |var, type|
    if (!name || var.name == name) && (!label || var.label == label)
      return type
    end
  end

  raise "Unknown variable for typing: #{name}@#{label}"
end