logo

Function itsy::build_str

source · []
pub fn build_str<F>(source: &str) -> Result<Program<F>, Error> where
    F: VMFunc<F>, 
Expand description

Parses, resolves and compiles given Itsy source code.

The name of the entry function must be main. This utility-function does not support external Itsy modules. For more control, see either build or parser::parse, resolver::resolve and compiler::compile. Use run or VM::run to execute the given program.

Examples

The following example builds and runs a program:

use itsy::{itsy_api, build_str, run};

// Define an API of Rust functions that are callable from the Itsy script.
itsy_api!(MyAPI, (), {
    fn print(&mut context, value: &str) {
        println!("print: {}", value);
    }
    // ... more functions ...
});

fn main() {
    // Build the itsy program and link it to the MyAPI API we created above.
    let program = build_str::<MyAPI>("
        // An Itsy program that calls the Rust 'print' function.
        fn main() {
            print(\"Hello from Itsy!\");
        }
    ").unwrap();

    run(&program, &mut ()).unwrap();
}