exp-rs
A tiny, no_std Pratt expression parser and evaluator for embedded systems.
Key Features
- Pratt parser for minimal stack depth—handles deep nesting on embedded stacks
- Arena allocation for bounded memory and zero-allocation evaluation after setup
- no_std compatible with configurable f32/f64 precision
- Variables, constants, arrays, attributes, and custom functions
- C FFI with auto-generated headers via cbindgen
Installation
[]
= "0.2"
Floating-Point Precision
By default, exp-rs uses 64-bit floating point (double precision) for calculations. You can configure the precision using feature flags:
# Use default 64-bit precision (double)
= "0.2"
# Use 32-bit precision (float)
= { = "0.2", = ["f32"] }
The f64 mode is the default when f32 is not specified.
Custom Math Implementations
For embedded systems, you can disable the libm dependency to reduce binary size and provide your own math function implementations:
# Disable libm dependency
= { = "0.2", = false }
Quick Example
use Expression;
use Bump;
let arena = new;
let result = eval_simple.unwrap;
assert_eq!;
For the full API including parameters, batch evaluation, custom functions, and more, see the documentation.
C FFI
A C header is automatically generated during build via cbindgen:
int
The header is generated at include/exp_rs.h after running cargo build.
Build Instructions
Meson Build
QEMU Tests (ARM Cortex-M)
# Run QEMU embedded tests
# Run with allocation tracking
# Run native C tests
# See all options
Code Coverage
Project History
exp-rs began as a fork of tinyexpr-rs by Krzysztof Kondrak, which was a port of TinyExpr by Lewis Van Winkle. The grammar is based on tinyexpr-plusplus by Blake Madden.
Key differences from tinyexpr:
- Pratt parser (vs recursive descent) for shallower call stacks
- Arena allocation for predictable memory usage
- Extended operator set and short-circuit logical operators
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)