rune/indexing/
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
#![allow(unused)]

use core::cell::RefCell;
use core::fmt;
use core::num::NonZeroUsize;

use crate::alloc::{self, BTreeSet, HashMap, Vec};
use crate::ast::Span;
use crate::compile::error::{MissingScope, PopError};

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub(crate) struct Scope(usize);

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub(crate) struct Variable(usize);

/// The kind of a layer.
#[derive(Default)]
enum LayerKind {
    #[default]
    Default,
    Captures,
}

#[derive(Default)]
pub(crate) struct Layer {
    scope: Scope,
    parent: Option<NonZeroUsize>,
    pub(crate) awaits: Vec<Span>,
    pub(crate) yields: Vec<Span>,
}

impl Layer {
    fn parent(&self) -> Option<usize> {
        Some(self.parent?.get().wrapping_sub(1))
    }
}

pub(crate) struct Scopes {
    scope: Scope,
    scopes: Vec<Layer>,
}

impl Scopes {
    /// Root scope.
    pub const ROOT: Scope = Scope(0);

    pub(crate) fn new() -> alloc::Result<Self> {
        let mut scopes = Vec::new();
        scopes.try_push(Layer::default())?;

        Ok(Self {
            scope: Scopes::ROOT,
            scopes,
        })
    }

    /// Push a scope.
    pub(crate) fn push(&mut self) -> alloc::Result<()> {
        let scope = Scope(self.scopes.len());

        let layer = Layer {
            scope,
            parent: Some(NonZeroUsize::new(self.scope.0.wrapping_add(1)).expect("ran out of ids")),
            awaits: Vec::new(),
            yields: Vec::new(),
        };

        self.scopes.try_push(layer)?;
        self.scope = scope;
        Ok(())
    }

    /// Pop the given scope.
    pub(crate) fn pop(&mut self) -> Result<Layer, PopError> {
        let Some(layer) = self.scopes.pop() else {
            return Err(PopError::MissingScope(self.scope.0));
        };

        if layer.scope.0 != self.scope.0 {
            return Err(PopError::MissingScope(self.scope.0));
        }

        let Some(parent) = layer.parent() else {
            return Err(PopError::MissingParentScope(self.scope.0));
        };

        self.scope = Scope(parent);
        Ok(layer)
    }

    /// Define the given variable.
    #[tracing::instrument(skip_all)]
    pub(crate) fn mark(&mut self) -> Result<&mut Layer, MissingScope> {
        tracing::trace!(?self.scope, "mark await");

        let Some(layer) = self.scopes.get_mut(self.scope.0) else {
            return Err(MissingScope(self.scope.0));
        };

        Ok(layer)
    }
}