Lines
95.35 %
Functions
100 %
Branches
//! What a host makes of a global once its unit has run (plan 0014 PERSIST):
//! the value the next units see.
use crate::ast::Expr;
use super::definitions::{Slot, Written};
use super::table::SymbolTable;
/// A unit global read back by the host.
#[derive(Clone, Debug, PartialEq)]
pub enum Carried {
/// The global's value as the constant a DEFVAR of it would have bound.
Constant(Expr),
/// A value of the named type that no constant can hold: a read of it in
/// a later unit is an error naming the type.
Opaque(String),
}
impl Carried {
/// The constant of a carried value; none for an opaque one.
#[must_use]
pub fn into_constant(self) -> Option<Expr> {
match self {
Self::Constant(value) => Some(value),
Self::Opaque(_) => None,
/// Whether `value` is the source a host carries a list of closures as.
pub(crate) fn is_list_source(value: &Expr) -> bool {
matches!(value, Expr::List(items) if matches!(items.first(), Some(Expr::Symbol(head)) if head == "LET"))
impl SymbolTable {
/// The type of the value an earlier unit left in the global `name` that no
/// constant holds.
pub(crate) fn lost_type(&self, name: &str) -> Option<&str> {
self.lost_homes.get(name).map(String::as_str)
/// Keeps the special declaration of `name` with no value: a DEFVAR of it
/// ran in the unit but nothing bound it. A name the unit started with a
/// value no store replaced keeps that value.
pub fn declare_unbound(&mut self, name: &str) {
let held = self.unit_pre.get(name).is_some_and(Written::holds_value);
match held {
true => {
self.settle(name, Slot::Value, None);
self.mark_special(name);
false => {
if let Some(symbol) = self.lookup_mut(name) {
symbol.clear_value();
/// Puts the declaration of the global `name` back as it stood before the
/// unit, when no DEFVAR of it ran: a global a store gave a value is then
/// one a `SETF` created.
pub fn restore_declaration(&mut self, name: &str, stored: bool) {
let pre = self.unit_pre.get(name);
let created = pre.map_or(stored, |pre| pre.created || (stored && !pre.special));
let special = created || pre.is_some_and(|pre| pre.special);
match special {
true => self.special_names.insert(name.to_string()),
false => self.special_names.remove(name),
};
match created {
true => self.created_globals.insert(name.to_string()),
false => self.created_globals.remove(name),
/// Makes `carried` the session value of the global `name`.
pub fn carry(&mut self, name: &str, carried: Carried) {
match carried {
Carried::Constant(value) => {
self.lost_homes.remove(name);
symbol.set_value(value.clone());
symbol.set_expansion_value(value);
Carried::Opaque(type_name) => {
self.lost_homes.insert(name.to_string(), type_name);
#[cfg(test)]
#[path = "carry_tests.rs"]
mod tests;