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
use crate::data::{
    functions::Scope,
    token::Span,
};


#[derive(Clone, Debug)]
pub struct CallStack {
    pub frames: Vec<StackFrame>,
}

impl CallStack {
    pub fn new() -> Self {
        Self {
            frames: Vec::new(),
        }
    }

    pub fn operator(&self) -> &StackFrame {
        self.frames.last()
            // This shouldn't generally happen, as this is only called by
            // builtins, and when a builtin is executed, there must be a frame
            // on the call stack.
            .expect("Tried to get operator from empty call stack")
    }

    pub fn caller(&self) -> Option<&StackFrame> {
        if self.frames.len() < 2 {
            return None;
        }

        Some(&self.frames[self.frames.len() - 2])
    }
}


#[derive(Clone, Debug)]
pub struct StackFrame {
    pub scope: Scope,
    pub span:  Span,
}