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

#initialize(parent: nil, parent_last_update: parent&.last_update) ⇒ Typing

Returns a new instance of Typing.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/steep/typing.rb', line 12

def initialize(parent: nil, parent_last_update: parent&.last_update)
  @parent = parent
  @parent_last_update = parent_last_update
  @last_update = parent&.last_update || 0
  @should_update = false

  @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

#last_updateObject (readonly)

Returns the value of attribute last_update.



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

def last_update
  @last_update
end

#nodesObject (readonly)

Returns the value of attribute nodes.



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

def nodes
  @nodes
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#parent_last_updateObject (readonly)

Returns the value of attribute parent_last_update.



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

def parent_last_update
  @parent_last_update
end

#should_updateObject (readonly)

Returns the value of attribute should_update.



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

def should_update
  @should_update
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



70
71
72
73
74
75
76
# File 'lib/steep/typing.rb', line 70

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



24
25
26
# File 'lib/steep/typing.rb', line 24

def add_error(error)
  errors << error
end

#add_typing(node, type) ⇒ Object



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

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

  if should_update
    @last_update += 1
    @should_update = false
  end

  type
end

#dump(io) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/steep/typing.rb', line 58

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

#each_typingObject



89
90
91
92
93
# File 'lib/steep/typing.rb', line 89

def each_typing
  nodes.each do |id, node|
    yield node, typing[id]
  end
end

#has_type?(node) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#new_childObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/steep/typing.rb', line 78

def new_child
  child = self.class.new(parent: self)
  @should_update = true

  if block_given?
    yield child
  else
    child
  end
end

#save!Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/steep/typing.rb', line 95

def save!
  raise "Unexpected save!" unless parent
  raise "Parent modified since new_child" unless parent.last_update == parent_last_update

  each_typing do |node, type|
    parent.add_typing(node, type)
  end

  errors.each do |error|
    parent.add_error error
  end
end

#type_of(node:) ⇒ Object



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

def type_of(node:)
  type = typing[node.__id__]

  if type
    type
  else
    if parent
      parent.type_of(node: node)
    else
      raise "Unknown node for typing: #{node.inspect}"
    end
  end
end