Struct endbasic_core::syms::Symbols
source · pub struct Symbols { /* private fields */ }
Expand description
Storage for all symbols that exist at runtime.
Symbols are represented as a two-layer map: the globals map contains all symbols that are visible by all scopes, and the scope contains all symbols that are only visible within a given scope.
The collection of symbols that is visible at any given point in time is thus the union of the global symbols and the symbols in the last scope.
Scopes are represented as a stack in order to support nested function calls.
Implementations§
source§impl Symbols
impl Symbols
sourcepub fn add_callable(&mut self, callable: Rc<dyn Callable>)
pub fn add_callable(&mut self, callable: Rc<dyn Callable>)
Registers the given builtin callable as a global symbol.
Given that callables cannot be defined at runtime, specifying a non-unique name results in a panic.
sourcepub fn callables(&self) -> HashMap<&SymbolKey, Rc<dyn Callable>>
pub fn callables(&self) -> HashMap<&SymbolKey, Rc<dyn Callable>>
Returns the mapping of all callables.
sourcepub fn locals(&self) -> &HashMap<SymbolKey, Symbol>
pub fn locals(&self) -> &HashMap<SymbolKey, Symbol>
Returns the mapping of all symbols in the current scope that are not globals.
sourcepub fn dim(&mut self, key: SymbolKey, etype: ExprType)
pub fn dim(&mut self, key: SymbolKey, etype: ExprType)
Defines a new local variable key
of type etype
. The variable must not yet exist.
Defines a new global variable key
of type etype
. The variable must not yet exist.
sourcepub fn dim_array(
&mut self,
key: SymbolKey,
subtype: ExprType,
dimensions: Vec<usize>,
)
pub fn dim_array( &mut self, key: SymbolKey, subtype: ExprType, dimensions: Vec<usize>, )
Defines a new array key
of type subtype
with dimensions
. The array must not yet
exist, and the name may not overlap function or variable names.
Defines a new global array key
of type subtype
with dimensions
. The array must not yet
exist, and the name may not overlap function or variable names.
sourcepub fn get(&self, vref: &VarRef) -> Result<Option<&Symbol>>
pub fn get(&self, vref: &VarRef) -> Result<Option<&Symbol>>
Obtains the value of a symbol or None
if it is not defined.
Returns an error if the type annotation in the symbol reference does not match its type.
sourcepub fn get_auto(&self, var: &str) -> Option<&Symbol>
pub fn get_auto(&self, var: &str) -> Option<&Symbol>
Obtains the value of a symbol or None
if it is not defined.
sourcepub fn get_mut(&mut self, vref: &VarRef) -> Result<Option<&mut Symbol>>
pub fn get_mut(&mut self, vref: &VarRef) -> Result<Option<&mut Symbol>>
Obtains the value of a symbol or None
if it is not defined.
Returns an error if the type annotation in the symbol reference does not match its type.
sourcepub fn set_var(&mut self, vref: &VarRef, value: Value) -> Result<()>
pub fn set_var(&mut self, vref: &VarRef, value: Value) -> Result<()>
Sets the value of a variable.
If vref
contains a type annotation, the type of the value must be compatible with that
type annotation.
If the variable is already defined, then the type of the new value must be compatible with the existing variable. In other words: a variable cannot change types while it’s alive.