|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Lexical scope stack for Key to Value associations.
More...
#include <tpl_scope.H>
Classes | |
| struct | Binding |
| Individual entry in a lexical frame. More... | |
| struct | Frame |
Public Member Functions | |
| Scope ()=default | |
| Default constructor. | |
| size_t | depth () const noexcept |
| Queries the current depth of the scope stack. | |
| bool | empty () const noexcept |
| Indicates if the structure is empty of frames. | |
| void | clear () noexcept |
| Resets the structure by removing all frames and bindings. | |
| size_t | enter_scope () |
| Creates a new visibility level. | |
| void | leave_scope () |
| Destroys the current visibility level and all its associations. | |
| bool | insert (const Key &key, const Value &value) |
| Registers a new association in the current scope. | |
| bool | insert (Key &&key, Value &&value) |
Move version of insert(). | |
| Value * | lookup_local (const Key &key) noexcept |
| Searches for a key only in the innermost (current) scope. | |
| const Value * | lookup_local (const Key &key) const noexcept |
Constant version of lookup_local(). | |
| Value * | lookup (const Key &key) noexcept |
| Searches for a key traversing all active scopes (from inside out). | |
| const Value * | lookup (const Key &key) const noexcept |
Constant version of lookup(). | |
Private Member Functions | |
| Frame & | current_frame () |
| const Frame & | current_frame () const |
Static Private Member Functions | |
| static Value * | find_value (DynArray< Binding > &bindings, const Key &key) noexcept |
| static const Value * | find_value (const DynArray< Binding > &bindings, const Key &key) noexcept |
Private Attributes | |
| DynArray< Frame > | frames |
Lexical scope stack for Key to Value associations.
The Scope class implements a hierarchical dictionary. It is ideal for symbol tables where declarations in inner blocks hide those from the outside.
| Key | Search key type. Must be equality comparable. |
| Value | Payload associated with the key. |
Definition at line 87 of file tpl_scope.H.
Default constructor.
Creates an instance without active frames.
enter_scope() must be called before performing the first insertion.
Resets the structure by removing all frames and bindings.
Useful for reusing the Scope object in a new translation unit.
Definition at line 167 of file tpl_scope.H.
References Aleph::Scope< Key, Value >::frames.
Referenced by Aleph::Compiler_Symbol_Bindings::clear().
|
inlineprivate |
Definition at line 124 of file tpl_scope.H.
References ah_runtime_error_unless, and Aleph::Scope< Key, Value >::frames.
Referenced by Aleph::Scope< Key, Value >::insert(), and Aleph::Scope< Key, Value >::insert().
|
inlineprivate |
Definition at line 130 of file tpl_scope.H.
References ah_runtime_error_unless, and Aleph::Scope< Key, Value >::frames.
|
inlinenoexcept |
Queries the current depth of the scope stack.
enter_scope() is called. Definition at line 149 of file tpl_scope.H.
References Aleph::Scope< Key, Value >::frames.
Referenced by Aleph::Compiler_Symbol_Bindings::append_symbol(), Aleph::Compiler_Symbol_Bindings::declare(), and Aleph::Compiler_Symbol_Bindings::depth().
|
inlinenoexcept |
Indicates if the structure is empty of frames.
true if there is no active scope. Definition at line 158 of file tpl_scope.H.
References Aleph::Scope< Key, Value >::frames.
|
inline |
Creates a new visibility level.
Pushes an empty frame. All subsequent insert() calls will affect this frame until leave_scope() is called.
Definition at line 179 of file tpl_scope.H.
References Aleph::Scope< Key, Value >::frames.
Referenced by Aleph::Compiler_Symbol_Bindings::enter_scope(), TEST(), and TEST().
|
inlinestaticprivatenoexcept |
Definition at line 116 of file tpl_scope.H.
|
inlinestaticprivatenoexcept |
Definition at line 108 of file tpl_scope.H.
Referenced by Aleph::Scope< Key, Value >::insert(), Aleph::Scope< Key, Value >::insert(), Aleph::Scope< Key, Value >::lookup(), Aleph::Scope< Key, Value >::lookup(), Aleph::Scope< Key, Value >::lookup_local(), and Aleph::Scope< Key, Value >::lookup_local().
Registers a new association in the current scope.
The name is added to the innermost frame. If the name already exists in an outer frame, this method will "shadow" it without error. If it already exists in the current frame, the insertion will fail to prevent collisions.
| key | Key to register (e.g., variable name). |
| value | Value to associate (e.g., symbol metadata). |
true if the insertion was successful, false if the key already exists in the current visibility level (redefinition forbidden). | std::runtime_error | If there is no active scope. |
Definition at line 215 of file tpl_scope.H.
References Aleph::Scope< Key, Value >::current_frame(), Aleph::Scope< Key, Value >::find_value(), and value.
Referenced by Aleph::Compiler_Symbol_Bindings::declare().
|
inline |
Move version of insert().
Avoids expensive copies of keys and values.
Definition at line 229 of file tpl_scope.H.
References Aleph::Scope< Key, Value >::current_frame(), Aleph::Scope< Key, Value >::find_value(), and value.
Destroys the current visibility level and all its associations.
Restores the visibility of the previous (outer) frame.
| std::runtime_error | If called without active frames. |
Definition at line 195 of file tpl_scope.H.
References ah_runtime_error_unless, Aleph::blossom_maximum_cardinality_matching(), and Aleph::Scope< Key, Value >::frames.
Referenced by Aleph::Compiler_Symbol_Bindings::leave_scope().
|
inlinenoexcept |
Constant version of lookup().
Definition at line 280 of file tpl_scope.H.
References Aleph::Scope< Key, Value >::find_value(), Aleph::Scope< Key, Value >::frames, and value.
|
inlinenoexcept |
Searches for a key traversing all active scopes (from inside out).
Implements standard lexical lookup: searches first in the current frame, then in the immediately superior one, and so on until the global one. This allows name shadowing.
| key | Key to search. |
nullptr if the key is not defined in any accessible level. Definition at line 271 of file tpl_scope.H.
References Aleph::Scope< Key, Value >::find_value(), Aleph::Scope< Key, Value >::frames, and value.
Referenced by Aleph::Compiler_Symbol_Bindings::declare(), and Aleph::Compiler_Symbol_Bindings::lookup().
|
inlinenoexcept |
Constant version of lookup_local().
Definition at line 254 of file tpl_scope.H.
References Aleph::Scope< Key, Value >::find_value(), and Aleph::Scope< Key, Value >::frames.
|
inlinenoexcept |
Searches for a key only in the innermost (current) scope.
Useful for detecting redefinitions in the same code block without being affected by what happens in outer scopes.
| key | Key to search. |
nullptr otherwise. Definition at line 246 of file tpl_scope.H.
References Aleph::Scope< Key, Value >::find_value(), and Aleph::Scope< Key, Value >::frames.
Referenced by Aleph::Compiler_Symbol_Bindings::declare(), and Aleph::Compiler_Symbol_Bindings::lookup_local().
Definition at line 106 of file tpl_scope.H.
Referenced by Aleph::Scope< Key, Value >::clear(), Aleph::Scope< Key, Value >::current_frame(), Aleph::Scope< Key, Value >::current_frame(), Aleph::Scope< Key, Value >::depth(), Aleph::Scope< Key, Value >::empty(), Aleph::Scope< Key, Value >::enter_scope(), Aleph::Scope< Key, Value >::leave_scope(), Aleph::Scope< Key, Value >::lookup(), Aleph::Scope< Key, Value >::lookup(), Aleph::Scope< Key, Value >::lookup_local(), and Aleph::Scope< Key, Value >::lookup_local().