pub struct CodeGen { /* private fields */ }Expand description
Builder for generating Rust source code from symbolic expressions.
Collects multiple functions (scalar and matrix) and writes them all to a single output file with shared preamble (cfg-gated math module, etc.).
Implementations§
Source§impl CodeGen
impl CodeGen
Sourcepub fn options(self, options: CodegenOptions) -> Self
pub fn options(self, options: CodegenOptions) -> Self
Set custom code generation options.
Every registered function is emitted with these options. The
special-function runtime (mod symplex_rt, needed by gamma,
lambertw, Bessel functions, …) is emitted once at the top of
the file when CodegenOptions::emit_runtime is true (the
default) and at least one function needs it. Set emit_runtime: false to leave it out entirely — e.g. when several generated files
share one copy of CodegenOptions::runtime_module:
use symplex::matrix::{CodegenOptions, MathBackend};
use symplex::prelude::*;
use symplex_build::CodeGen;
let ctx = Context::new();
let x = ctx.symbol("x");
let opts = CodegenOptions {
math_backend: MathBackend::CfgGated,
emit_runtime: false,
..Default::default()
};
let body = CodeGen::new()
.options(opts.clone())
.add_scalar_fn("g", &x.gamma(), &["x"])
.add_scalar_fn("w", &x.lambertw(), &["x"])
.generate()
.unwrap();
std::fs::write("robot_math.rs", body).unwrap();
std::fs::write("symplex_rt.rs", opts.runtime_module()).unwrap();Sourcepub fn no_std(self, enabled: bool) -> Self
pub fn no_std(self, enabled: bool) -> Self
Enable or disable no_std-compatible output (uses the CfgGated math backend).
Sourcepub fn precision_f32(self) -> Self
pub fn precision_f32(self) -> Self
Use f32 precision for generated code.
Sourcepub fn inline(self, enabled: bool) -> Self
pub fn inline(self, enabled: bool) -> Self
Set whether to emit #[inline] annotations on generated functions.
Sourcepub fn add_scalar_fn(self, name: &str, expr: &Ex, params: &[&str]) -> Self
pub fn add_scalar_fn(self, name: &str, expr: &Ex, params: &[&str]) -> Self
Add a scalar function to the output.
The generated function will take the named parameters as f64 (or f32)
arguments and return the scalar result.
Sourcepub fn add_matrix_fn(self, name: &str, matrix: &Matrix, params: &[&str]) -> Self
pub fn add_matrix_fn(self, name: &str, matrix: &Matrix, params: &[&str]) -> Self
Add a matrix function to the output.
The generated function will take the named parameters and return
a flat array [f64; rows*cols] in row-major order.
Sourcepub fn with_tests(self, enabled: bool) -> Self
pub fn with_tests(self, enabled: bool) -> Self
Enable or disable companion test generation.
When enabled, a #[cfg(test)] mod generated_tests { ... } block is
appended with test functions that evaluate at any configured test points.
Sourcepub fn add_test_point(self, point: &[f64]) -> Self
pub fn add_test_point(self, point: &[f64]) -> Self
Add a test evaluation point.
Each test point is a slice of f64 values corresponding to the
function parameters in order. During test generation, each function
is called with each test point to verify it produces a finite result.
Sourcepub fn generate(&self) -> Result<String, Box<dyn Error>>
pub fn generate(&self) -> Result<String, Box<dyn Error>>
Generate the full source file as a String.
- If the math backend is
CfgGated, emits the cfg-gated math module (once; the per-function copies are stripped). - If
CodegenOptions::emit_runtimeis set and any registered function uses a special function, emits themod symplex_rtruntime once, containing exactly the helpers the file needs. - For each registered function, calls the appropriate symplex codegen method.
- If test generation is enabled, emits a
#[cfg(test)]module.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for CodeGen
impl !UnwindSafe for CodeGen
impl Freeze for CodeGen
impl Send for CodeGen
impl Sync for CodeGen
impl Unpin for CodeGen
impl UnsafeUnpin for CodeGen
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more