stackr_rs/
lib.rs

1//! A stack-based interpreter written in Rust. Meant to be embedded in your application.
2//! [Visit the repository for more information](https://github.com/ericrobolson/stackr-rs).
3//!
4//!
5//! Basic usage:
6//! ```rust
7//! use stackr_rs::*;
8//!
9//! let code = "1 1 +";
10//! let mut interpreter = Interpreter::new(());
11//! interpreter.evaluate(code, None).unwrap();
12//! println!("1 1 + ={}", interpreter.pop_number().unwrap());
13//! ```
14//!
15//! [See more built-in words](https://github.com/ericrobolson/stackr-rs?tab=readme-ov-file#useful-words).
16//!
17//! Example with custom built-in words:
18//! ```rust
19//! use stackr_rs::*;
20//!
21//! let state: u32 = 0;
22//! let mut interpreter = Interpreter::new(state);
23//!
24//! interpreter.register_builtin(
25//!     "increment-state",
26//!     "--",
27//!     "Increments the state.",
28//!     "increment-state",
29//!     |interpreter| {
30//!         interpreter.state += 1;
31//!         Ok(())
32//!     },
33//! );
34//!
35//! interpreter.register_builtin(
36//!     "get-state",
37//!     "-- n",
38//!     "Gets the state.",
39//!     "get-state",
40//!     |interpreter| {
41//!         interpreter.push_number(interpreter.state as f32);
42//!         Ok(())
43//!     },
44//! );
45//!
46//! let code = r#"
47//! print-stack
48//! increment-state
49//! get-state
50//! "The state has been modified!"
51//! print-stack
52//! "#;
53//!
54//! println!("State before execution: {:?}", interpreter.state);
55//! interpreter.evaluate(code, None).unwrap();
56//! println!("State after execution: {:?}", interpreter.state);
57//! ```
58//!
59//! Example of a custom word:
60//! ```stackr
61//! : squared
62//! "n -- n^2"
63//! "Squares the top of the stack"
64//! "2 squared"
65//! dup *
66//! ;
67//!
68//! 2 squared
69//! print-stack
70//! ```
71//!
72//! See the examples directory for more examples or the [README](https://github.com/ericrobolson/stackr-rs/blob/main/README.md) for more information.
73
74mod interpreter;
75
76pub use interpreter::*;