Lines
97.56 %
Functions
100 %
Branches
//! Writing a name's cells: `define`.
use super::entry::Symbol;
use super::table::SymbolTable;
impl SymbolTable {
/// Defines a symbol: it writes only the cells it carries, so a variable
/// leaves the function of its name alone and the other way round.
pub fn define(&mut self, mut symbol: Symbol) {
if self.in_macro_expansion {
self.macro_locals.insert(symbol.name.clone());
}
let fresh_global = symbol
.function
.as_ref()
.is_some_and(|function| function.definition == 0 && !function.local);
self.stamp_definitions(&mut symbol);
let under_local = self
.symbols
.get(&symbol.name)
.is_some_and(Symbol::is_local_function);
if let Some(cell) = symbol.function.take() {
symbol.function = match fresh_global && under_local {
true => self.defer_function(&symbol.name, cell),
false => Some(cell),
};
match self.symbols.get_mut(&symbol.name) {
Some(entry) => entry.absorb(symbol),
None => {
self.symbols.insert(symbol.name.clone(), symbol);
/// Gives a fresh cell its binding id; a cell a closing scope puts back
/// keeps its own.
fn stamp_definitions(&mut self, symbol: &mut Symbol) {
if let Some(variable) = symbol.value.as_mut() {
if variable.definition == 0 {
self.shadow_special(&symbol.name);
variable.definition = self.next_definition_id();
} else if self
.shadowed_globals
.is_some_and(|global| global.definition == variable.definition)
{
self.shadowed_globals.remove(&symbol.name);
if symbol.function.as_ref().is_some_and(|f| f.definition == 0) {
self.shadow_function(symbol);
if let Some(function) = symbol.function.as_mut() {
function.definition = self.next_definition_id();