Class: RLSL::Prism::ScopeStack

Inherits:
Object
  • Object
show all
Defined in:
lib/rlsl/prism/type_inference/scope_stack.rb

Instance Method Summary collapse

Constructor Details

#initializeScopeStack

Returns a new instance of ScopeStack.



6
7
8
# File 'lib/rlsl/prism/type_inference/scope_stack.rb', line 6

def initialize
  @scopes = [{}]
end

Instance Method Details

#lookup(name) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/rlsl/prism/type_inference/scope_stack.rb', line 24

def lookup(name)
  @scopes.reverse_each do |scope|
    return scope[name.to_sym] if scope.key?(name.to_sym)
  end

  nil
end

#popObject



14
15
16
17
18
# File 'lib/rlsl/prism/type_inference/scope_stack.rb', line 14

def pop
  raise "Cannot pop the global scope" if @scopes.length == 1

  @scopes.pop
end

#push(initial_scope = {}) ⇒ Object



10
11
12
# File 'lib/rlsl/prism/type_inference/scope_stack.rb', line 10

def push(initial_scope = {})
  @scopes << normalize(initial_scope)
end

#register(name, type) ⇒ Object



20
21
22
# File 'lib/rlsl/prism/type_inference/scope_stack.rb', line 20

def register(name, type)
  @scopes.last[name.to_sym] = type
end

#to_hObject



32
33
34
35
36
# File 'lib/rlsl/prism/type_inference/scope_stack.rb', line 32

def to_h
  @scopes.each_with_object({}) do |scope, merged|
    merged.merge!(scope)
  end
end