@raviqqe/stak 0.3.9 → 0.3.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +99 -16
- package/package.json +1 -1
- package/stak_wasm_bg.wasm +0 -0
package/README.md
CHANGED
|
@@ -7,37 +7,38 @@
|
|
|
7
7
|
|
|
8
8
|
The miniature, embeddable R7RS Scheme implementation in Rust
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
Stak Scheme aims to be:
|
|
11
|
+
|
|
12
|
+
- An embeddable Scheme interpreter for Rust with very small memory footprint and reasonable performance
|
|
13
|
+
- The minimal implementation of [the R7RS-small standard][r7rs-small]
|
|
14
|
+
- A subset of [Chibi Scheme](https://github.com/ashinn/chibi-scheme), [Gauche](https://github.com/shirok/Gauche), and [Guile](https://www.gnu.org/software/guile/)
|
|
15
|
+
|
|
16
|
+
For more information and usage, visit [the full documentation](https://raviqqe.github.io/stak).
|
|
11
17
|
|
|
12
18
|
## Install
|
|
13
19
|
|
|
14
|
-
###
|
|
20
|
+
### Libraries
|
|
15
21
|
|
|
16
22
|
To install Stak Scheme as a library in your Rust project, run:
|
|
17
23
|
|
|
18
24
|
```sh
|
|
19
25
|
cargo add stak
|
|
26
|
+
cargo add --build stak-build
|
|
20
27
|
```
|
|
21
28
|
|
|
29
|
+
For full examples, see [the `examples` directory](./examples).
|
|
30
|
+
|
|
22
31
|
### Command line tools
|
|
23
32
|
|
|
24
|
-
To install the Scheme interpreter
|
|
33
|
+
To install the Scheme interpreter as a command, run:
|
|
25
34
|
|
|
26
35
|
```sh
|
|
27
|
-
# Install the Scheme interpreter.
|
|
28
36
|
cargo install stak
|
|
29
|
-
|
|
30
|
-
# Install the minimal Scheme interpreter (6 times smaller!)
|
|
31
|
-
cargo install mstak
|
|
32
|
-
|
|
33
|
-
# Install the Scheme-to-bytecode compiler and bytecode interpreter.
|
|
34
|
-
cargo install stak-compile
|
|
35
|
-
cargo install stak-interpret
|
|
36
37
|
```
|
|
37
38
|
|
|
38
39
|
## Examples
|
|
39
40
|
|
|
40
|
-
###
|
|
41
|
+
### Embedding Scheme scripts in Rust
|
|
41
42
|
|
|
42
43
|
First, prepare a Scheme script at `src/hello.scm`.
|
|
43
44
|
|
|
@@ -57,35 +58,107 @@ fn main() -> Result<(), BuildError> {
|
|
|
57
58
|
}
|
|
58
59
|
```
|
|
59
60
|
|
|
60
|
-
Now, you can include the Scheme script into a program in Rust using [the `stak::
|
|
61
|
+
Now, you can include the Scheme script into a program in Rust using [the `stak::include_module` macro](https://docs.rs/stak/latest/stak/macro.include_module.html).
|
|
61
62
|
|
|
62
63
|
```rust
|
|
63
64
|
use core::error::Error;
|
|
64
65
|
use stak::{
|
|
65
66
|
device::StdioDevice,
|
|
66
67
|
file::VoidFileSystem,
|
|
67
|
-
|
|
68
|
+
include_module,
|
|
68
69
|
process_context::VoidProcessContext,
|
|
70
|
+
module::{Module, UniversalModule},
|
|
69
71
|
r7rs::{SmallError, SmallPrimitiveSet},
|
|
70
72
|
time::VoidClock,
|
|
71
73
|
vm::Vm,
|
|
72
74
|
};
|
|
73
75
|
|
|
74
76
|
const HEAP_SIZE: usize = 1 << 16;
|
|
75
|
-
|
|
77
|
+
|
|
78
|
+
// Include a Scheme script in the bytecode format built by the build script above.
|
|
79
|
+
static MODULE: UniversalModule = include_module!("hello.scm");
|
|
76
80
|
|
|
77
81
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
78
|
-
run(
|
|
82
|
+
run(&MODULE.bytecode())?;
|
|
79
83
|
|
|
80
84
|
Ok(())
|
|
81
85
|
}
|
|
82
86
|
|
|
83
87
|
fn run(bytecodes: &[u8]) -> Result<(), SmallError> {
|
|
88
|
+
// Prepare a heap memory of a virtual machine.
|
|
84
89
|
let mut heap = [Default::default(); HEAP_SIZE];
|
|
90
|
+
// Create a virtual machine with its heap memory primitive procedures.
|
|
85
91
|
let mut vm = Vm::new(
|
|
86
92
|
&mut heap,
|
|
87
93
|
SmallPrimitiveSet::new(
|
|
94
|
+
// Attach standard input, output, and error of this process to a virtual machine.
|
|
88
95
|
StdioDevice::new(),
|
|
96
|
+
// Use void system interfaces for security because we don't need them for this example.
|
|
97
|
+
VoidFileSystem::new(),
|
|
98
|
+
VoidProcessContext::new(),
|
|
99
|
+
VoidClock::new(),
|
|
100
|
+
),
|
|
101
|
+
)?;
|
|
102
|
+
|
|
103
|
+
// Initialize a virtual machine with bytecodes.
|
|
104
|
+
vm.initialize(bytecodes.iter().copied())?;
|
|
105
|
+
// Run bytecodes on a virtual machine.
|
|
106
|
+
vm.run()
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Communication between Scheme and Rust
|
|
111
|
+
|
|
112
|
+
Currently, in-memory standard input (`stdin`) and output (`stdout`) to Scheme scripts are the only way to communicate information between Rust programs and Scheme scripts.
|
|
113
|
+
|
|
114
|
+
```rust
|
|
115
|
+
use core::{error::Error, str::{self, FromStr}};
|
|
116
|
+
use stak::{
|
|
117
|
+
device::ReadWriteDevice,
|
|
118
|
+
file::VoidFileSystem,
|
|
119
|
+
include_module,
|
|
120
|
+
process_context::VoidProcessContext,
|
|
121
|
+
module::{Module, UniversalModule},
|
|
122
|
+
r7rs::{SmallError, SmallPrimitiveSet},
|
|
123
|
+
time::VoidClock,
|
|
124
|
+
vm::Vm,
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const BUFFER_SIZE: usize = 1 << 8;
|
|
128
|
+
const HEAP_SIZE: usize = 1 << 16;
|
|
129
|
+
|
|
130
|
+
static MODULE: UniversalModule = include_module!("fibonacci.scm");
|
|
131
|
+
|
|
132
|
+
fn main() -> Result<(), Box<dyn Error>> {
|
|
133
|
+
let input = 24;
|
|
134
|
+
let mut output = vec![];
|
|
135
|
+
let mut error = vec![];
|
|
136
|
+
|
|
137
|
+
run(&MODULE.bytecode(), input.to_string().as_bytes(), &mut output, &mut error)?;
|
|
138
|
+
|
|
139
|
+
// If stderr is not empty, we assume that some error has occurred.
|
|
140
|
+
if !error.is_empty() {
|
|
141
|
+
return Err(str::from_utf8(&error)?.into());
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Decode and print the output.
|
|
145
|
+
println!("Answer: {}", isize::from_str(&str::from_utf8(&output)?)?);
|
|
146
|
+
|
|
147
|
+
Ok(())
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
fn run(
|
|
151
|
+
bytecodes: &[u8],
|
|
152
|
+
input: &[u8],
|
|
153
|
+
output: &mut Vec<u8>,
|
|
154
|
+
error: &mut Vec<u8>,
|
|
155
|
+
) -> Result<(), SmallError> {
|
|
156
|
+
let mut heap = [Default::default(); HEAP_SIZE];
|
|
157
|
+
let mut vm = Vm::new(
|
|
158
|
+
&mut heap,
|
|
159
|
+
SmallPrimitiveSet::new(
|
|
160
|
+
// Create and attach an in-memory I/O device.
|
|
161
|
+
ReadWriteDevice::new(input, output, error),
|
|
89
162
|
VoidFileSystem::new(),
|
|
90
163
|
VoidProcessContext::new(),
|
|
91
164
|
VoidClock::new(),
|
|
@@ -97,6 +170,16 @@ fn run(bytecodes: &[u8]) -> Result<(), SmallError> {
|
|
|
97
170
|
}
|
|
98
171
|
```
|
|
99
172
|
|
|
173
|
+
## References
|
|
174
|
+
|
|
175
|
+
- This project is based on [Ribbit Scheme][ribbit], the small and portable R4RS implementation.
|
|
176
|
+
- [Scheme][scheme]
|
|
177
|
+
- [The R7RS-small standard][r7rs-small]
|
|
178
|
+
|
|
100
179
|
## License
|
|
101
180
|
|
|
102
181
|
[MIT](https://github.com/raviqqe/stak/blob/main/LICENSE)
|
|
182
|
+
|
|
183
|
+
[scheme]: https://www.scheme.org/
|
|
184
|
+
[r7rs-small]: https://small.r7rs.org/
|
|
185
|
+
[ribbit]: https://github.com/udem-dlteam/ribbit
|
package/package.json
CHANGED
package/stak_wasm_bg.wasm
CHANGED
|
Binary file
|