1
//! What a host makes of a global once its unit has run (plan 0014 PERSIST):
2
//! the value the next units see.
3

            
4
use crate::ast::Expr;
5

            
6
use super::definitions::{Slot, Written};
7
use super::table::SymbolTable;
8

            
9
/// A unit global read back by the host.
10
#[derive(Clone, Debug, PartialEq)]
11
pub 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

            
19
impl Carried {
20
    /// The constant of a carried value; none for an opaque one.
21
    #[must_use]
22
34114
    pub fn into_constant(self) -> Option<Expr> {
23
34114
        match self {
24
33281
            Self::Constant(value) => Some(value),
25
833
            Self::Opaque(_) => None,
26
        }
27
34114
    }
28
}
29

            
30
/// Whether `value` is the source a host carries a list of closures as.
31
#[must_use]
32
1213783
pub(crate) fn is_list_source(value: &Expr) -> bool {
33
1213783
    matches!(value, Expr::List(items) if matches!(items.first(), Some(Expr::Symbol(head)) if head == "LET"))
34
1213783
}
35

            
36
impl SymbolTable {
37
    /// The type of the value an earlier unit left in the global `name` that no
38
    /// constant holds.
39
    #[must_use]
40
298648
    pub(crate) fn lost_type(&self, name: &str) -> Option<&str> {
41
298648
        self.lost_homes.get(name).map(String::as_str)
42
298648
    }
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
4579
    pub fn declare_unbound(&mut self, name: &str) {
48
4579
        let held = self.unit_pre.get(name).is_some_and(Written::holds_value);
49
4579
        match held {
50
            true => {
51
834
                self.settle(name, Slot::Value, None);
52
834
                self.mark_special(name);
53
            }
54
            false => {
55
3745
                if let Some(symbol) = self.lookup_mut(name) {
56
3745
                    symbol.clear_value();
57
                }
58
            }
59
        }
60
4579
    }
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
12072
    pub fn restore_declaration(&mut self, name: &str, stored: bool) {
66
12072
        let pre = self.unit_pre.get(name);
67
12072
        let created = pre.map_or(stored, |pre| pre.created || (stored && !pre.special));
68
12072
        let special = created || pre.is_some_and(|pre| pre.special);
69
12072
        match special {
70
6245
            true => self.special_names.insert(name.to_string()),
71
5827
            false => self.special_names.remove(name),
72
        };
73
12072
        match created {
74
2707
            true => self.created_globals.insert(name.to_string()),
75
9365
            false => self.created_globals.remove(name),
76
        };
77
12072
    }
78

            
79
    /// Makes `carried` the session value of the global `name`.
80
478019
    pub fn carry(&mut self, name: &str, carried: Carried) {
81
478019
        match carried {
82
            Carried::Constant(value) => {
83
473436
                self.lost_homes.remove(name);
84
473436
                if let Some(symbol) = self.lookup_mut(name) {
85
473435
                    symbol.set_value(value.clone());
86
473435
                    symbol.set_expansion_value(value);
87
1
                }
88
            }
89
            Carried::Opaque(type_name) => {
90
4583
                if let Some(symbol) = self.lookup_mut(name) {
91
4583
                    symbol.clear_value();
92
                }
93
4583
                self.lost_homes.insert(name.to_string(), type_name);
94
            }
95
        }
96
478019
    }
97
}
98

            
99
#[cfg(test)]
100
#[path = "carry_tests.rs"]
101
mod tests;