BREP_render 0.4.0

BREP Rust rendering engine: kernel-fed scene store + wgpu renderer (headless artifact, desktop window, and wasm canvas shells).
Documentation
use super::*;

// Expression edits rerun the current history prefix so parameters that reference
// variables rebuild against the updated environment.
impl EngineState {
    /// The history's `expressions` source string (the variable sheet the panel's
    /// editor binds to). Raw source text — despite the `_json` suffix it mirrors
    /// the other engine readouts' naming; the verifier reads it verbatim.
    pub fn expressions_json(&self) -> String {
        self.history.expressions()
    }

    /// Replace the `expressions` source and re-run the rolled-to prefix so every
    /// feature param referencing a variable (e.g. `sizeX = "boxW"`) rebuilds with
    /// the new value — the panel's live-update path. Returns the build-report JSON.
    pub fn set_expressions(&mut self, expressions: &str) -> String {
        self.history.set_expressions(expressions);
        self.rerun_history()
    }

    /// The history's `configurator` object (typed named inputs) as JSON — a
    /// read/display surface for the panel; deeper configurator editing is deferred.
    pub fn configurator_json(&self) -> String {
        self.history.configurator().to_string()
    }

    /// The parsed variable list for the sheet's name/value view:
    /// `[{ "name": "...", "expr": "..." }, …]` — one entry per `name = rhs;`
    /// assignment in the expressions source, in source order. The RHS is shown
    /// verbatim (its DEFINING expression); evaluating it to a live scalar needs the
    /// kernel's private `Env`, so a computed value column is deferred — the text
    /// editor + re-run is the source of truth for applied values.
    pub fn expression_variables_json(&self) -> String {
        let vars = parse_expression_variables(&self.history.expressions());
        serde_json::to_string(&vars).unwrap_or_else(|_| "[]".to_string())
    }
}

/// Parse the `name = rhs;` assignments from an expressions source into an ordered
/// `[{name, expr}]` list for the sheet's variable view. Mirrors the kernel
/// evaluator's statement grammar (`IDENT '=' expr ';'`) without reaching into its
/// private `Env`: `//` line comments are stripped, statements split on `;`, and
/// each `IDENT = rhs` yields one entry (a non-identifier LHS or empty RHS is
/// skipped, matching what the evaluator would reject).
fn parse_expression_variables(source: &str) -> Vec<serde_json::Value> {
    // Strip `//` line comments line-by-line (the DSL has no strings, so a bare
    // `//` always starts a comment), then split statements on ';'.
    let mut cleaned = String::with_capacity(source.len());
    for line in source.lines() {
        let code = match line.find("//") {
            Some(idx) => &line[..idx],
            None => line,
        };
        cleaned.push_str(code);
        cleaned.push('\n');
    }
    let mut out = Vec::new();
    for stmt in cleaned.split(';') {
        let stmt = stmt.trim();
        if stmt.is_empty() {
            continue;
        }
        let Some(eq) = stmt.find('=') else {
            continue;
        };
        let name = stmt[..eq].trim();
        let expr = stmt[eq + 1..].trim();
        if name.is_empty() || expr.is_empty() || !is_identifier(name) {
            continue;
        }
        out.push(serde_json::json!({ "name": name, "expr": expr }));
    }
    out
}

/// Whether `s` is a single expression-DSL identifier (`[A-Za-z_$][A-Za-z0-9_$]*`)
/// — the valid LHS of an assignment statement.
fn is_identifier(s: &str) -> bool {
    let mut chars = s.chars();
    match chars.next() {
        Some(c) if c.is_ascii_alphabetic() || c == '_' || c == '$' => {}
        _ => return false,
    }
    chars.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '$')
}

// BREP private tests: b1558d84d885bf5c