hypervox_expr 0.2.1

High-performance mathematical expression parser, AST optimizer (constant folding, CSE), and closure compiler with multi-dimensional invariant hoisting
Documentation
use std::{fs::File, io::Write};

fn main() {
    println!("cargo:rerun-if-changed=build.rs");

    let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR must be set by Cargo");
    let out_path = std::path::Path::new(&out_dir);

    gen_unop(&mut File::create(out_path.join("unop.rs")).expect("failed to create unop.rs"));
    gen_binop(&mut File::create(out_path.join("binop.rs")).expect("failed to create binop.rs"));
    gen_terop(&mut File::create(out_path.join("terop.rs")).expect("failed to create terop.rs"));
}

const VARIANTS: &[&str] = &["Num", "Var", "Cse", "Compound"];

fn variant_read(name: &str, slot: &str) -> String {
    match name {
        "Num" => slot.to_owned(),
        "Var" => format!("*_vars.get({slot}).unwrap_or(&0.0_f64)"),
        "Cse" => format!("*_cse.get({slot}).unwrap_or(&0.0_f64)"),
        "Compound" => format!("{slot}(_vars, _cse)"),
        _ => unreachable!("Unknown InlinedChild variant: {}", name),
    }
}

fn writeln_out(out: &mut impl Write, s: &str) {
    writeln!(out, "{s}").expect("failed to write generated code");
}

fn gen_unop(out: &mut impl Write) {
    writeln_out(out, "// @generated by build.rs. Do not edit manually.");
    writeln_out(out, "#[expect(clippy::used_underscore_binding)]");
    writeln_out(out, "match self.inlined() {");
    for name in VARIANTS {
        let read = variant_read(name, "v0");
        writeln_out(
            out,
            &format!("    InlinedChild::{name}(v0) => Box::new(move |_vars, _cse| f({read})),"),
        );
    }
    writeln_out(out, "}");
}

fn gen_binop(out: &mut impl Write) {
    writeln_out(out, "// @generated by build.rs. Do not edit manually.");
    writeln_out(out, "#[expect(clippy::used_underscore_binding)]");
    writeln_out(out, "match (self.inlined(), b.inlined()) {");
    for a_name in VARIANTS {
        for b_name in VARIANTS {
            let a_read = variant_read(a_name, "v0");
            let b_read = variant_read(b_name, "v1");
            writeln_out(
                out,
                &format!(
                    "    (InlinedChild::{a_name}(v0), InlinedChild::{b_name}(v1)) => Box::new(move |_vars, _cse| op({a_read}, {b_read})),"
                ),
            );
        }
    }
    writeln_out(out, "}");
}

fn gen_terop(out: &mut impl Write) {
    writeln_out(out, "// @generated by build.rs. Do not edit manually.");
    writeln_out(out, "#[expect(clippy::used_underscore_binding)]");
    writeln_out(out, "match (self.inlined(), b.inlined(), c.inlined()) {");
    for a_name in VARIANTS {
        for b_name in VARIANTS {
            for c_name in VARIANTS {
                let a_read = variant_read(a_name, "v0");
                let b_read = variant_read(b_name, "v1");
                let c_read = variant_read(c_name, "v2");
                writeln_out(
                    out,
                    &format!(
                        "    (InlinedChild::{a_name}(v0), InlinedChild::{b_name}(v1), InlinedChild::{c_name}(v2)) => Box::new(move |_vars, _cse| op({a_read}, {b_read}, {c_read})),"
                    ),
                );
            }
        }
    }
    writeln_out(out, "}");
}