Skip to main content

encode_algol

Function encode_algol 

Source
pub fn encode_algol(
    expr: &Expr,
    table: &PrattTable,
    parent_bp: u16,
    cx: &mut WriteCx<'_>,
) -> Result<String>
Expand description

Renders an Expr back to Algol infix text using the operator table.

parent_bp is the binding power of the enclosing context; when an infix operator binds more loosely than its parent the rendered subexpression is wrapped in parentheses so the text reparses to the same tree. Forms outside the infix grammar (collections, quotes, and similar) are emitted through a expr.lisp(...) escape so the general-purpose codec still round-trips every expression the kernel can hold.

ยงExamples

use sim_codec_algol::{default_pratt_table, encode_algol};
use sim_kernel::{EncodeOptions, Expr, Symbol, WriteCx};
use sim_test_support::core_cx;

let mut cx = core_cx();
let table = default_pratt_table();
let expr = Expr::Infix {
    operator: Symbol::new("+"),
    left: Box::new(Expr::Symbol(Symbol::new("a"))),
    right: Box::new(Expr::Symbol(Symbol::new("b"))),
};
let mut write = WriteCx {
    cx: &mut cx,
    codec: sim_kernel::CodecId(0),
    options: EncodeOptions::default(),
};
assert_eq!(encode_algol(&expr, &table, 0, &mut write).unwrap(), "a + b");