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
//! Utilities to build executable binaries from bytecode files.

#[doc(hidden)]
pub mod __private {
    pub use clap;
    pub use main_error;
    pub use stak_configuration;
    pub use stak_device;
    pub use stak_macro;
    pub use stak_primitive;
    pub use stak_vm;
    pub use std;
}

/// Defines a `main` function that executes a bytecode file at a given path.
///
/// The given bytecode file is bundled into a resulting binary.
#[macro_export]
macro_rules! main {
    ($path:expr) => {
        $crate::main!(
            $path,
            $crate::__private::stak_configuration::DEFAULT_HEAP_SIZE
        );
    };
    ($path:expr, $heap_size:expr) => {
        use $crate::__private::{
            clap::{self, Parser},
            main_error::MainError,
            stak_device::StdioDevice,
            stak_macro::include_r7rs,
            stak_primitive::SmallPrimitiveSet,
            stak_vm::Vm,
            std::{env, error::Error},
        };

        #[derive(clap::Parser)]
        #[command(about, version)]
        struct Arguments {
            #[arg(short = 's', long, default_value_t = $heap_size)]
            heap_size: usize,
        }

        fn main() -> Result<(), MainError> {
            let arguments = Arguments::parse();

            let mut heap = vec![Default::default(); arguments.heap_size];
            let mut vm = Vm::new(&mut heap, SmallPrimitiveSet::new(StdioDevice::new()))?;

            vm.initialize(include_r7rs!($path).iter().copied())?;

            Ok(vm.run()?)
        }
    };
}