Crate cbqn

source ·
Expand description

cbqn

A crate for running BQN code within a Rust program using CBQN interpreter compiled as a shared object or a WASI reactor.

Usage

Simple expressions can be run with the BQN! convenience macro. For more advanced use, the methods of BQNValue provide the necessary functionality.

Examples using the BQN! macro

let sum = BQN!("1+1")?;
assert_eq!(sum.to_f64()?, 2.0);
assert_eq!(BQN!("⌽≡⊢", "BQN")?.to_f64()?, 0.0);
let strs = BQN!(' ', "(⊢-˜+`׬)∘=⊔⊢", "Rust ❤️ BQN")?
    .to_bqnvalue_vec()?
    .iter()
    .map(|v| v.to_string())
    .collect::<Result<Vec<String>, _>>()?;
assert_eq!(strs, ["Rust", "❤️", "BQN"]);
let strings = ["join", "these", "please"];
assert_eq!(BQN!("∾", strings)?.to_string()?, "jointheseplease");

Examples using BQNValue

let sum = eval("1+1")?;
assert_eq!(sum.to_f64()?, 2.0);
let is_anagram = eval("⌽≡⊢")?;
assert_eq!(is_anagram.call1(&"BQN".into())?.to_f64()?, 0.0);
let split = eval("(⊢-˜+`׬)∘=⊔⊢")?;
let strs = split.call2(&' '.into(), &"Rust ❤️ BQN".into())?
    .to_bqnvalue_vec()?
    .iter()
    .map(BQNValue::to_string)
    .collect::<Result<Vec<String>, _>>()?;
assert_eq!(strs, ["Rust", "❤️", "BQN"]);
let counter = BQN!("{v←0 ⋄ Inc⇐{v+↩𝕩}}")?;
let increment = counter.get_field("inc")?.unwrap();
increment.call1(&1.into())?;
increment.call1(&2.into())?;
let result = increment.call1(&3.into())?;
assert_eq!(result.to_f64()?, 6.0);

Macros

  • Convenience macro for running BQN expressions

Structs

Enums

Functions

  • Evaluates BQN code
  • Initializes the CBQN interpreter