class SyntaxTree::WithScope::Scope
The scope class is used to keep track of local variables and arguments inside a particular scope.
Attributes
id[R]
- Integer
-
a unique identifier for this scope
locals[R]
- Hash[String, Local]
-
The local variables and arguments defined in this
scope
parent[R]
- scope | nil
-
The parent scope
Public Class Methods
new(id, parent = nil)
click to toggle source
# File lib/syntax_tree/with_scope.rb, line 68 def initialize(id, parent = nil) @id = id @parent = parent @locals = {} end
Public Instance Methods
add_local_definition(identifier, type)
click to toggle source
Adding a local definition will either insert a new entry in the locals hash or append a new definition location to an existing local. Notice that it’s not possible to change the type of a local after it has been registered.
# File lib/syntax_tree/with_scope.rb, line 78 def add_local_definition(identifier, type) name = identifier.value.delete_suffix(":") local = if type == :argument locals[name] ||= Local.new(type) else resolve_local(name, type) end local.add_definition(identifier.location) end
add_local_usage(identifier, type)
click to toggle source
Adding a local usage will either insert a new entry in the locals hash or append a new usage location to an existing local. Notice that it’s not possible to change the type of a local after it has been registered.
# File lib/syntax_tree/with_scope.rb, line 95 def add_local_usage(identifier, type) name = identifier.value.delete_suffix(":") resolve_local(name, type).add_usage(identifier.location) end
find_local(name)
click to toggle source
Try to find the local given its name in this scope or any of its parents.
# File lib/syntax_tree/with_scope.rb, line 102 def find_local(name) locals[name] || parent&.find_local(name) end