rune/compile/ir/
scopes.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use crate::alloc::prelude::*;
use crate::alloc::{self, HashMap};
use crate::ast::Spanned;
use crate::compile::{self, ErrorKind};
use crate::hir;
use crate::runtime::Value;

/// Error indicating that a local variable is missing.
pub(crate) struct MissingLocal(pub(crate) Box<str>);

/// A hierarchy of constant scopes.
pub(crate) struct Scopes {
    scopes: Vec<Scope>,
}

impl Scopes {
    /// Construct a new empty scope.
    pub(crate) fn new() -> alloc::Result<Self> {
        Ok(Self {
            scopes: try_vec![Scope::default()],
        })
    }

    /// Clear the current scope.
    pub(crate) fn clear_current(&mut self) -> Result<(), &'static str> {
        let last = self
            .scopes
            .last_mut()
            .ok_or("expected at least one scope")?;

        last.locals.clear();
        Ok(())
    }

    /// Declare a value in the scope.
    pub(crate) fn decl(&mut self, name: hir::Variable, value: Value) -> Result<(), ErrorKind> {
        let last = self
            .last_mut()
            .ok_or_else(|| ErrorKind::msg("Expected at least one scope"))?;
        last.locals.try_insert(name, value)?;
        Ok(())
    }

    /// Try to get the value out from the scopes.
    pub(crate) fn try_get(&self, name: &hir::Variable) -> Option<&Value> {
        for scope in self.scopes.iter().rev() {
            if let Some(current) = scope.locals.get(name) {
                return Some(current);
            }

            // don't look past isolate scopes.
            if let ScopeKind::Isolate = scope.kind {
                break;
            }
        }

        None
    }

    /// Get the given variable.
    pub(crate) fn get_name(
        &self,
        name: &hir::Variable,
        span: &dyn Spanned,
    ) -> compile::Result<&Value> {
        for scope in self.scopes.iter().rev() {
            if let Some(current) = scope.locals.get(name) {
                return Ok(current);
            }

            // don't look past isolate scopes.
            if let ScopeKind::Isolate = scope.kind {
                break;
            }
        }

        Err(compile::Error::new(
            span,
            MissingLocal(name.try_to_string()?.try_into_boxed_str()?),
        ))
    }

    /// Get the given variable as mutable.
    pub(crate) fn get_name_mut(
        &mut self,
        name: &hir::Variable,
        span: &dyn Spanned,
    ) -> compile::Result<&mut Value> {
        for scope in self.scopes.iter_mut().rev() {
            if let Some(current) = scope.locals.get_mut(name) {
                return Ok(current);
            }

            // don't look past isolate scopes.
            if let ScopeKind::Isolate = scope.kind {
                break;
            }
        }

        Err(compile::Error::new(
            span,
            MissingLocal(name.try_to_string()?.try_into_boxed_str()?),
        ))
    }

    /// Push a scope and return the guard associated with the scope.
    pub(crate) fn push(&mut self) -> alloc::Result<ScopeGuard> {
        let length = self.scopes.len();
        self.scopes.try_push(Scope::default())?;
        Ok(ScopeGuard { length })
    }

    /// Push an isolate scope and return the guard associated with the scope.
    pub(crate) fn isolate(&mut self) -> alloc::Result<ScopeGuard> {
        let length = self.scopes.len();
        let scope = Scope {
            kind: ScopeKind::Isolate,
            ..Default::default()
        };
        self.scopes.try_push(scope)?;
        Ok(ScopeGuard { length })
    }

    pub(crate) fn pop(&mut self, guard: ScopeGuard) -> Result<(), &'static str> {
        if self.scopes.pop().is_none() {
            return Err("expected at least one scope to pop");
        }

        if self.scopes.len() != guard.length {
            return Err("scope length mismatch");
        }

        Ok(())
    }

    /// Get the last scope mutably.
    pub(crate) fn last_mut(&mut self) -> Option<&mut Scope> {
        self.scopes.last_mut()
    }
}

#[repr(transparent)]
pub(crate) struct ScopeGuard {
    length: usize,
}

#[derive(Debug, Clone, Copy)]
enum ScopeKind {
    None,
    Isolate,
}

pub(crate) struct Scope {
    kind: ScopeKind,
    /// Locals in the current scope.
    locals: HashMap<hir::Variable, Value>,
}

impl Default for Scope {
    fn default() -> Self {
        Self {
            kind: ScopeKind::None,
            locals: HashMap::new(),
        }
    }
}