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
use std::sync::Mutex;

use crate::call_stack::{CalcitStack, CallStackList, StackKind};
use crate::primes::{Calcit, CalcitItems};

lazy_static! {
  static ref CALL_STACK: Mutex<rpds::ListSync<CalcitStack>> = Mutex::new(rpds::List::new_sync());
}

pub fn push_call_stack(ns: &str, def: &str, kind: StackKind, code: Calcit, args: &CalcitItems) {
  let mut stack = CALL_STACK.lock().expect("open call stack");
  stack.push_front_mut(CalcitStack {
    ns: ns.into(),
    def: def.into(),
    code,
    args: Box::new(args.to_owned()),
    kind,
  })
}

pub fn pop_call_stack() {
  let mut stack = CALL_STACK.lock().expect("open call stack");
  if !stack.is_empty() {
    let xs = stack.drop_first();
    match xs {
      Some(v) => *stack = v,
      None => {
        println!("empty stack")
      }
    }
  }
}

pub fn clear_stack() {
  let mut stack = CALL_STACK.lock().expect("open call stack");
  *stack = rpds::List::new_sync();
}

pub fn get_gen_stack() -> CallStackList {
  let stack = CALL_STACK.lock().expect("read call stack");
  stack.to_owned()
}