CBCL / DocumentationDocs v0.3.1

Install a dialect in Rust

Use cbcl-parser to read a dialect from text and cbcl-core to install it in a registry. This example adds a greeting verb to the core language.

On this page

Create the application

cargo new greeting
cd greeting
cargo add 'cbcl-core@=0.3.1' 'cbcl-parser@=0.3.1'

Parse and install

Replace src/main.rs with:

use cbcl_core::dialect::DialectRegistry;
use cbcl_parser::{parse, parse_dialect};

fn main() {
    let source = r#"(define greeting () @alice (extend greet (who) (tell who "hello")))"#;
    let expr = parse(source).expect("read S-expression");
    let dialect = parse_dialect(&expr).expect("read dialect definition");
    let mut registry = DialectRegistry::new();
    registry.install(dialect).expect("install greeting");
    println!("Installed greeting");
}

Run cargo run. The program prints Installed greeting. Change the verb name from greet to tell. Run the program again. The parser can read the definition. The installer rejects it because the core language defines tell.

Handle application input

The example uses expect to identify each stage at the terminal. If an error occurs, return its diagnostic to the caller. Keep the registry across installations when a definition extends an installed dialect. For incoming message text, cbcl_parser::run_pipeline parses and validates a message. A stateful host then checks the act against the thread’s history. See How a thread becomes state.

Browse the parser API and core API.

To examine a definition at the terminal, use the command-line toolkit.