stak_engine/
lib.rs

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! Stak Scheme scripting engine for Rust.
//!
//! # Examples
//!
//! ```rust
//! use any_fn::{r#fn, Ref};
//! use core::error::Error;
//! use rand::random;
//! use stak::{
//!     engine::{Engine, EngineError},
//!     include_module,
//!     module::UniversalModule,
//! };
//!
//! const HEAP_SIZE: usize = 1 << 16;
//! const FOREIGN_VALUE_CAPACITY: usize = 1 << 10;
//!
//! struct Person {
//!     pies: usize,
//!     dodge: f64,
//!     wasted: bool,
//! }
//!
//! impl Person {
//!     pub fn new(pies: usize, dodge: f64) -> Self {
//!         Self {
//!             pies,
//!             dodge,
//!             wasted: false,
//!         }
//!     }
//!
//!     pub fn wasted(&self) -> bool {
//!         self.wasted
//!     }
//!
//!     pub fn throw_pie(&mut self, other: &mut Person) {
//!         if self.wasted {
//!             return;
//!         }
//!
//!         self.pies -= 1;
//!
//!         if random::<f64>() > other.dodge {
//!             other.wasted = true;
//!         }
//!     }
//! }
//!
//! fn main() -> Result<(), Box<dyn Error>> {
//!     static MODULE: UniversalModule = include_module!("fight.scm");
//!
//!     run(&MODULE)?;
//!
//!     Ok(())
//! }
//!
//! fn run(module: &'static UniversalModule) -> Result<(), EngineError> {
//!     let mut heap = [Default::default(); HEAP_SIZE];
//!     let mut functions = [
//!         r#fn(Person::new),
//!         r#fn(Person::throw_pie),
//!         r#fn::<(Ref<_>,), _>(Person::wasted),
//!     ];
//!     let mut engine = Engine::<FOREIGN_VALUE_CAPACITY>::new(&mut heap, &mut functions)?;
//!
//!     engine.run(module)
//! }
//! ```

#![no_std]

mod engine;
mod error;
mod primitive_set;

pub use engine::*;
pub use error::*;