functions

Macro functions 

Source
macro_rules! functions {
    ($(
        $(#[doc = $doc: literal])* $name: literal ($argc: tt) => $callback: expr)
    *) => { ... };
}
Expand description

Declares a group of builtin functions and exports them.

ยงExample

use regulus::prelude::*;
functions! {
    /// Length of a string.
    "strlen"(1) => |state, args| {
        let len = args[0].eval(state)?.string()?.len();
        Ok(Atom::Int(len as i64))
    }
    /// Concatenate strings.
    "strconcat"(_) => |state, args| {
        let mut string = String::new();
        for arg in args {
            string.push_str(&arg.eval(state)?.string()?);
        }
        Ok(Atom::String(string))
    }
    /// Logical AND.
    "&&"(2) => |state, args| Ok(Atom::Bool(
        args[0].eval(state)?.bool()? &&
        args[1].eval(state)?.bool()?
    ))
 }

Here, the name before the parens is the function ident, the parens contain the argc (_ if any number of args is allowed) and the right side is the closure body of the builtin function.

The macro invocation generates a pub function called functions that returns Vec<(&'static str, Function)>.