6 releases

0.4.5 Jun 26, 2024
0.4.4 Jun 25, 2024

#145 in Programming languages

Download history 14/week @ 2024-09-16 21/week @ 2024-09-23 4/week @ 2024-09-30

222 downloads per month
Used in molt-wasm

MIT license

430KB
7.5K SLoC

molt -- An Embeddable Tcl Interpreter for Rust

New in Molt 0.3.1

See the Annotated Change Log in the Molt Book for the complete list of new features by version.

Description

Molt is an embeddable TCL interpreter for Rust. Applications can define new TCL commands in Rust and execute TCL scripts and strings. For example,

use molt::Interp;
let mut interp = Interp::new();

let four = interp.eval("expr {2 + 2}")?;
assert_eq!(four.as_int(), 4);

A new command is defined like so:

use molt::check_args;
use molt::MoltResult;
use molt::Value;
use molt::Interp;
use molt::molt_ok;

/// # square *x*
///
/// Computes the square of a value
pub fn cmd_square(_interp: &mut Interp, _: &[ContextID], argv: &[Value]) -> MoltResult {
    // Correct number of arguments?
    check_args(1, argv, 2, 2, "x")?;

    // Get x, if it's an integer
    let x = argv[1].as_int()?;

    // Return the result.
    molt_ok!(x * x)
}

and installed like so:

use molt::Interp;
let mut interp = Interp::new();
interp.add_command("square", cmd_square);

let num = interp.eval("square 5")?;
assert_eq!(num.as_int(), 25);

Values are represented by the Value type, which can be converted to and from any type consistent with the value's string representation: integers, floats, lists, and any type that defines the MoltAny trait.

Molt is still a work in progress. The basic TCL language is in place, but many TCL commands remain to be implemented. See the Molt Book for details.

The molt-sample repo contains a sample Molt extension, including a shell application and a library create, both of which define new Molt commands.

See my blog for news, The Molt Book for details, and the GitHub Repo for issue tracking, etc.

Dependencies

~0.7–2.8MB
~47K SLoC