[][src]Crate rune

Read the Book 📖

Build Status Book Status Chat on Discord

An embeddable dynamic programming language for Rust.

Contributing

If you want to help out, there should be a number of optimization tasks available in Future Optimizations. Or have a look at Open Issues.

Create an issue about the optimization you want to work on and communicate that you are working on it.


Highlights of Rune


Rune scripts

You can run Rune programs with the bundled CLI:

cargo run -- scripts/hello_world.rn

If you want to see detailed diagnostics of your program while it's running, you can use:

cargo run -- scripts/hello_world.rn --dump-unit --trace --dump-vm

See --help for more information.

Running scripts from Rust

use rune::termcolor::{ColorChoice, StandardStream};
use rune::EmitDiagnostics as _;
use runestick::{FromValue as _, Item, Source};

use std::error::Error;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let source = Source::new(
        "script",
        r#"
        fn calculate(a, b) {
            println("Hello World");
            a + b
        }
        "#,
    );

    let context = Arc::new(rune::default_context()?);
    let options = rune::Options::default();
    let mut warnings = rune::Warnings::new();

    let unit = match rune::load_source(&*context, &options, &mut warnings, source) {
        Ok(unit) => unit,
        Err(error) => {
            let mut writer = StandardStream::stderr(ColorChoice::Always);
            error.emit_diagnostics(&mut writer)?;
            return Ok(());
        }
    };

    let unit = Arc::new(unit);
    let vm = runestick::Vm::new(context.clone(), unit.clone());

    if !warnings.is_empty() {
        let mut writer = StandardStream::stderr(ColorChoice::Always);
        rune::emit_warning_diagnostics(&mut writer, &warnings, &*unit)?;
    }

    let mut execution: runestick::VmExecution =
        vm.call_function(Item::of(&["calculate"]), (10i64, 20i64))?;
    let value = execution.async_complete().await?;

    let value = i64::from_value(value)?;

    println!("{}", value);
    Ok(())
}

Re-exports

pub use diagnostics::termcolor;

Modules

ast

AST for the Rune language.

Structs

Lexer

Lexer for the rune language.

LoadError

An error raised when using one of the load_* functions.

Options

Compiler options.

Parser

Parser for the rune language.

Warning

Compilation warning.

Warnings

Compilation warnings.

Enums

CompileError

Error when encoding AST.

DiagnosticsError

Errors that can be raised when formatting diagnostics.

LoadErrorKind

The kind of the load error.

ParseError

Error when parsing.

WarningKind

Compilation warning kind.

Traits

EmitDiagnostics

Helper trait for emitting diagnostics.

Functions

compile

Compile the given source with default options.

default_context

Construct a a default context runestick context.

emit_warning_diagnostics

Emit warning diagnostics.

load_path

Load the given path.

load_source

Load and compile the given source.

parse_all

Parse the given input as the given type that implements [Parse][crate::traits::Parse].