Skip to main content

nomiscript/runtime/symbol/
carry.rs

1//! What a host makes of a global once its unit has run (plan 0014 PERSIST):
2//! the value the next units see.
3
4use crate::ast::Expr;
5
6use super::definitions::{Slot, Written};
7use super::table::SymbolTable;
8
9/// A unit global read back by the host.
10#[derive(Clone, Debug, PartialEq)]
11pub enum Carried {
12    /// The global's value as the constant a DEFVAR of it would have bound.
13    Constant(Expr),
14    /// A value of the named type that no constant can hold: a read of it in
15    /// a later unit is an error naming the type.
16    Opaque(String),
17}
18
19impl Carried {
20    /// The constant of a carried value; none for an opaque one.
21    #[must_use]
22    pub fn into_constant(self) -> Option<Expr> {
23        match self {
24            Self::Constant(value) => Some(value),
25            Self::Opaque(_) => None,
26        }
27    }
28}
29
30/// Whether `value` is the source a host carries a list of closures as.
31#[must_use]
32pub(crate) fn is_list_source(value: &Expr) -> bool {
33    matches!(value, Expr::List(items) if matches!(items.first(), Some(Expr::Symbol(head)) if head == "LET"))
34}
35
36impl SymbolTable {
37    /// The type of the value an earlier unit left in the global `name` that no
38    /// constant holds.
39    #[must_use]
40    pub(crate) fn lost_type(&self, name: &str) -> Option<&str> {
41        self.lost_homes.get(name).map(String::as_str)
42    }
43
44    /// Keeps the special declaration of `name` with no value: a DEFVAR of it
45    /// ran in the unit but nothing bound it. A name the unit started with a
46    /// value no store replaced keeps that value.
47    pub fn declare_unbound(&mut self, name: &str) {
48        let held = self.unit_pre.get(name).is_some_and(Written::holds_value);
49        match held {
50            true => {
51                self.settle(name, Slot::Value, None);
52                self.mark_special(name);
53            }
54            false => {
55                if let Some(symbol) = self.lookup_mut(name) {
56                    symbol.clear_value();
57                }
58            }
59        }
60    }
61
62    /// Puts the declaration of the global `name` back as it stood before the
63    /// unit, when no DEFVAR of it ran: a global a store gave a value is then
64    /// one a `SETF` created.
65    pub fn restore_declaration(&mut self, name: &str, stored: bool) {
66        let pre = self.unit_pre.get(name);
67        let created = pre.map_or(stored, |pre| pre.created || (stored && !pre.special));
68        let special = created || pre.is_some_and(|pre| pre.special);
69        match special {
70            true => self.special_names.insert(name.to_string()),
71            false => self.special_names.remove(name),
72        };
73        match created {
74            true => self.created_globals.insert(name.to_string()),
75            false => self.created_globals.remove(name),
76        };
77    }
78
79    /// Makes `carried` the session value of the global `name`.
80    pub fn carry(&mut self, name: &str, carried: Carried) {
81        match carried {
82            Carried::Constant(value) => {
83                self.lost_homes.remove(name);
84                if let Some(symbol) = self.lookup_mut(name) {
85                    symbol.set_value(value.clone());
86                    symbol.set_expansion_value(value);
87                }
88            }
89            Carried::Opaque(type_name) => {
90                if let Some(symbol) = self.lookup_mut(name) {
91                    symbol.clear_value();
92                }
93                self.lost_homes.insert(name.to_string(), type_name);
94            }
95        }
96    }
97}
98
99#[cfg(test)]
100#[path = "carry_tests.rs"]
101mod tests;