use crate::stack::Stack;
use crate::stdlib::init_stdlib;
use std::collections;
use rust_dynamic::value::Value;
use easy_error::{Error};
use nanoid::nanoid;
pub type AppFn = fn(&mut TS, Option<Value>, Option<Value>) -> Result<&mut TS, Error>;
pub type InlineFn = fn(&mut TS) -> Result<&mut TS, Error>;
#[derive(Clone)]
pub struct TS {
pub id: String,
pub stack: collections::HashMap<String, Stack<Value>>,
pub stacks: collections::VecDeque<String>,
pub stack_cap: collections::HashMap<String, usize>,
pub workbench: Stack<Value>,
pub functions: collections::HashMap<String, AppFn>,
pub inline_fun: collections::HashMap<String, InlineFn>,
}
impl TS {
fn init() -> Self {
Self {
id: nanoid!(),
stack: collections::HashMap::new(),
stacks: collections::VecDeque::new(),
stack_cap: collections::HashMap::new(),
workbench: Stack::new(),
functions: collections::HashMap::new(),
inline_fun: collections::HashMap::new(),
}
}
pub fn new() -> Self {
let mut res = TS::init();
init_stdlib(&mut res);
res.ensure();
res
}
pub fn new_with_named(name: String) -> Self {
let mut res = TS::init();
init_stdlib(&mut res);
res.ensure_stack(name);
res
}
}