Skip to main content

Crate cel_rs

Crate cel_rs 

Source
Expand description

cel-rs provides a stack-based runtime for developing domain specific languages, including concatenative languages to describe concurrent processes.

This crate exposes two main components:

  • cel-runtime: The core stack-based runtime and CEL parser
  • cel-rs-macros: Procedural macros for CEL expressions

For Adam — the property-model DSL built on top of this runtime (adam-lang, adam-rs) — see The Adam Programming Language, a tutorial and reference manual for that language specifically.

§Examples

§Using the Runtime

use cel_rs::cel_runtime::*;

// Create a segment that takes a u32 and &str as arguments
let segment = Segment::<(u32, &str)>::new()
    .op1r(|s| {
        let r = s.parse::<u32>()?;
        Ok(r)
    })
    .op2(|a, b| a + b)
    .op1(|r| r.to_string());
assert_eq!(segment.call((1u32, "2")).unwrap(), "3");

§Using the Parser

use cel_rs::cel_parser::{CELParser, OpLookup};
use proc_macro2::TokenStream;
use std::str::FromStr;

let input = TokenStream::from_str("10").unwrap();
let mut parser = CELParser::new(OpLookup::new());
parser.set_tokens(input.into_iter());
let result = parser.is_expression();
assert!(result.is_ok());

§Parsing an Array Literal

CEL array literals use canonical Array storage. A checked host call returns OwnedValue. Bind its complete shape to recover typed storage without moving or reallocating array elements. Prepared calls use DynSegment::prepare directly:

use cel_rs::parser::{CELParser, OpLookup};
use cel_rs::runtime::{ArrayOf, Leaf, Nil, IntoCStackList};

let segment = CELParser::new(OpLookup::new()).parse_str("[0, 1, 2]").unwrap();
let mut prepared = segment.prepare::<Nil, ArrayOf<Leaf<i32>>>().unwrap();
let array = prepared.call(&().into_c_stack_list()).unwrap();
assert_eq!(array.into_vec(), vec![0, 1, 2]);

§Using the Macros

use cel_rs::cel_rs_macros::expression;

expression! {
    54 + 25 * (11 + 6 * 6)
};

Re-exports§

pub use cel_parser;
pub use cel_rs_macros;
pub use cel_runtime;

Modules§

macros
Re-exports from the cel-rs-macros crate for convenient access.
parser
Re-exports for the CEL parser.
runtime
Re-exports from the cel-runtime crate for convenient access.