Class: SyntaxTree::Environment
- Inherits:
-
Object
- Object
- SyntaxTree::Environment
- Defined in:
- lib/syntax_tree/visitor/environment.rb
Overview
The environment class is used to keep track of local variables and arguments inside a particular scope
Defined Under Namespace
Classes: Local
Instance Attribute Summary collapse
-
#locals ⇒ Object
readonly
- Array
-
The local variables and arguments defined in this environment.
Instance Method Summary collapse
-
#add_local_definition(identifier, type) ⇒ Object
Adding a local definition will either insert a new entry in the locals hash or append a new definition location to an existing local.
-
#add_local_usage(identifier, type) ⇒ Object
Adding a local usage will either insert a new entry in the locals hash or append a new usage location to an existing local.
-
#find_local(name) ⇒ Object
Try to find the local given its name in this environment or any of its parents find_local: (String name) -> Local | nil.
-
#initialize(parent = nil) ⇒ Environment
constructor
initialize: (Environment | nil parent) -> void.
Constructor Details
#initialize(parent = nil) ⇒ Environment
initialize: (Environment | nil parent) -> void
42 43 44 45 |
# File 'lib/syntax_tree/visitor/environment.rb', line 42 def initialize(parent = nil) @locals = {} @parent = parent end |
Instance Attribute Details
#locals ⇒ Object (readonly)
- Array
-
The local variables and arguments defined in this
environment
9 10 11 |
# File 'lib/syntax_tree/visitor/environment.rb', line 9 def locals @locals end |
Instance Method Details
#add_local_definition(identifier, type) ⇒ Object
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
add_local_definition: (Ident | Label identifier, Symbol type) -> void
52 53 54 55 56 57 |
# File 'lib/syntax_tree/visitor/environment.rb', line 52 def add_local_definition(identifier, type) name = identifier.value.delete_suffix(":") @locals[name] ||= Local.new(type) @locals[name].add_definition(identifier.location) end |
#add_local_usage(identifier, type) ⇒ Object
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
add_local_usage: (Ident | Label identifier, Symbol type) -> void
64 65 66 67 68 69 |
# File 'lib/syntax_tree/visitor/environment.rb', line 64 def add_local_usage(identifier, type) name = identifier.value.delete_suffix(":") @locals[name] ||= Local.new(type) @locals[name].add_usage(identifier.location) end |
#find_local(name) ⇒ Object
Try to find the local given its name in this environment or any of its parents
find_local: (String name) -> Local | nil
74 75 76 77 78 79 |
# File 'lib/syntax_tree/visitor/environment.rb', line 74 def find_local(name) local = @locals[name] return local unless local.nil? @parent&.find_local(name) end |