rust_multistack/stdlib/
workbench.rs

1use crate::ts::{TS};
2use rust_dynamic::value::Value;
3use easy_error::{Error, bail};
4
5pub fn stdlib_return_from_current(ts: &mut TS, _value1: Option<Value>, _value2: Option<Value>) -> Result<&mut TS, Error> {
6    ts.return_from_current_to_workbench()
7}
8
9pub fn stdlib_return_from_current_inline(ts: &mut TS) -> Result<&mut TS, Error> {
10    ts.return_from_current_to_workbench()
11}
12
13pub fn stdlib_return_to_current(ts: &mut TS, _value1: Option<Value>, _value2: Option<Value>) -> Result<&mut TS, Error> {
14    ts.return_from_workbench_to_current()
15}
16
17pub fn stdlib_return_from_stack(ts: &mut TS, value1: Option<Value>, _value2: Option<Value>) -> Result<&mut TS, Error> {
18    match value1 {
19        Some(name_val) => {
20                match name_val.cast_string() {
21                    Ok(name) => {
22                        return ts.return_from_stack_to_workbench(name);
23                    }
24                    Err(err) => {
25                        bail!("Operation return_from() returned error: {}", err);
26                    }
27                }
28        }
29        None => {
30            bail!("Name of stack is missed for a return_from() operation");
31        }
32    }
33}
34
35pub fn stdlib_return_from_stack_inline(ts: &mut TS) -> Result<&mut TS, Error> {
36    if ts.current_stack_len() < 1 {
37        bail!("Stack is too shallow for inline return_from()");
38    }
39    let name = ts.pull();
40    stdlib_return_from_stack(ts, name, None)
41}
42
43pub fn stdlib_return_to_stack(ts: &mut TS, value1: Option<Value>, _value2: Option<Value>) -> Result<&mut TS, Error> {
44    match value1 {
45        Some(name_val) => {
46                match name_val.cast_string() {
47                    Ok(name) => {
48                        return ts.return_from_workbench_to_stack(name);
49                    }
50                    Err(err) => {
51                        bail!("Operation return_to() returned error: {}", err);
52                    }
53                }
54        }
55        None => {
56            bail!("Name of stack is missed for a return_to() operation");
57        }
58    }
59}
60
61pub fn stdlib_return_to_stack_inline(ts: &mut TS) -> Result<&mut TS, Error> {
62    if ts.current_stack_len() < 1 {
63        bail!("Stack is too shallow for inline return_to()");
64    }
65    let name = ts.pull();
66    stdlib_return_to_stack(ts, name, None)
67}
68
69pub fn stdlib_take_to_stack_inline(ts: &mut TS) -> Result<&mut TS, Error> {
70    stdlib_return_to_current(ts, None, None)
71}
72
73pub fn init_stdlib(ts: &mut TS) {
74    let _ = ts.register_function("return".to_string(), stdlib_return_from_current);
75    let _ = ts.register_inline("return".to_string(), stdlib_return_from_current_inline);
76    let _ = ts.register_function("return_from".to_string(), stdlib_return_from_stack);
77    let _ = ts.register_inline("return_from".to_string(), stdlib_return_from_stack_inline);
78    let _ = ts.register_function("from_workbench".to_string(), stdlib_return_to_current);
79    let _ = ts.register_function("return_to".to_string(), stdlib_return_to_stack);
80    let _ = ts.register_inline("return_to".to_string(), stdlib_return_to_stack_inline);
81    let _ = ts.register_inline("take".to_string(), stdlib_take_to_stack_inline);
82}