Crate tulisp

source ·
Expand description

Tulisp is a Lisp interpreter that can be embedded into Rust programs. The syntax tries to closely match that of Emacs Lisp. It was primarily designed to be a configuration language, but it also works well as a general purpose embedded scripting language.

One of the many benefits of using the Emacs Lisp syntax is that we can reuse its documentation for the builtin functions and macros. And for people who are already familiar with Emacs Lisp, there’s no need to learn an extra language.

Getting started

Tulisp requires rustc version 1.58 or higher.

It is very easy to get started. Here’s an example:

use tulisp::{TulispContext, tulisp_fn, Error};

fn main() -> Result<(), Error> {
    // Create a new Tulisp execution context.
    let mut ctx = TulispContext::new();

    // Add a function called `add_nums` to `ctx`.
    #[tulisp_fn(add_func = "ctx")]
    fn add_nums(num1: i64, num2: i64) -> i64 {
        num1 + num2
    }

    // Write a lisp program that calls `add_nums`
    let program = "(add_nums 10 20)";

    // Evaluate the program, and save the result.
    let sum: i64 = ctx.eval_string(program)?.try_into()?;

    assert_eq!(sum, 30);
    Ok(())
}

Next steps

  1. Values in Tulisp are represented in rust as TulispObjects. That struct implements methods for performing operations on Tulisp values.

  2. TulispContext tracks the state of the interpreter and provides methods for executing Tulisp programs.

  3. #[tulisp_fn] and #[tulisp_fn_no_eval] are flexible attribute macros for adding many different kinds of functions to a TulispContext object, so that they can be called from lisp code.

Macros

Destructures lists and binds the components to separate symbols.
Provides a lisp-like syntax for constructing lists.

Structs

Represents an instance of the Tulisp interpreter.
A type for representing tulisp objects.

Enums

Functions

Finds the first association (key . value) by comparing key with alist elements, and, if found, returns the value of that association.
Returns the first association for key in alist, comparing key against the alist elements using testfn if it is a function, and equal otherwise.
Returns the value of the property property stored in the property list plist.

Attribute Macros