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
use r_derive::builtin;

use crate::callable::core::*;
use crate::lang::{CallStack, EvalResult};
use crate::{formals, object::*};

/// Get the Current Call Stack
///
/// Returns a list of frames in the call stack.
///
/// # In-Language
///
/// ## Usage
///
/// ```custom,{class=r}
/// callstack()
/// ```
///
/// ## Arguments
///
/// _none_
///
/// ## Examples
///
/// ```custom,{class=r-repl}
/// h <- fn() callstack()
/// g <- fn() h()
/// f <- fn() g()
/// f()
/// ```
///
#[doc(alias = "callstack")]
#[builtin(sym = "callstack")]
#[derive(Debug, Clone, PartialEq)]
pub struct PrimitiveCallstack;

formals!(PrimitiveCallstack, "()");

impl Callable for PrimitiveCallstack {
    fn call(&self, _args: ExprList, stack: &mut CallStack) -> EvalResult {
        Ok(Obj::List(List::from(
            stack
                .frames
                .iter()
                .skip(1) // skip global frame
                .map(|f| (OptionNA::NA, Obj::Expr(f.call.clone())))
                .collect::<Vec<_>>(),
        )))
    }
}