Skip to content
S

SafeExpr

Runtime Math, Zero Dependencies

Created on 3rd August 2026

•

S

SafeExpr

Runtime Math, Zero Dependencies

The problem SafeExpr solves

Many applications need to evaluate mathematical expressions that are only known at runtime rather than compile time — for example, a formula entered by a user, a rule stored in a configuration file, or a calculation defined in a spreadsheet-like interface. Embedding a full scripting language (Python, Lua, JavaScript) to solve this is often excessive: it adds significant binary size, dependencies, and security surface area for what is fundamentally a simple need — parsing and computing an arithmetic expression.

TinyExpr addresses this by providing a lightweight, dependency-free expression parser and evaluation engine that can be embedded directly into an application. It supports standard operator precedence, a set of built-in mathematical functions, and runtime variable binding — allowing an expression to be compiled once and then re-evaluated efficiently as bound variables change, without re-parsing the string each time.

Our contribution was to port this library from C to Rust, preserving its exact parsing logic, operator behavior, and error-reporting semantics, while eliminating the memory-safety risks inherent to the original's manual memory management. This makes the same lightweight runtime-expression-evaluation capability available natively to Rust projects, without requiring unsafe FFI bindings to a C library.

If you'd like, I can also draft a short "impact" or "use case" line to go alongside this (e.g. game scripting, financial/config-driven calculations, embedded systems), which submission forms often ask for separately.

Challenges we ran into

One of the primary challenges was that the original C implementation relied heavily on bit-level encoding to represent expression metadata. Macros such as "TYPE_MASK", "ARITY(TYPE)", and "IS_CLOSURE" encoded multiple pieces of information within a single integer using bitmasks. Additionally, the union "{ double value; const double *bound; const void *function; }" reused the same memory region to represent entirely different data depending on the expression type. Accurately porting this design required carefully reverse-engineering the semantics of each macro before modeling the representation as a type-safe Rust enum.

Error position tracking presented another significant challenge. In the C implementation, syntax errors are reported using a byte offset ("s->next - s->start"), calculated only once after parsing terminates. This behavior depends on a subtle invariant: the lexer immediately stops advancing when an error is encountered. During our initial port, expressions such as "1**1" incorrectly reported the error position as 4 instead of 3. Further analysis revealed that the original implementation explicitly assigns the token type "TOK_ERROR" at every failure point, preventing any subsequent lexer iterations and effectively "freezing" the error position. Replicating this behavior in Rust resolved the discrepancy and produced identical results.

While analyzing the lexer, we also identified a latent infinite-loop bug in the original C implementation. A malformed input consisting of a standalone "." with no surrounding digits causes "strtod" to report no conversion progress, while the lexer itself also fails to advance the input pointer, resulting in an infinite loop. We evaluated whether to preserve this behavior for complete fidelity or address the defect. We chose to fix the issue while ensuring identical behavior for all valid inputs, and documented the change explicitly as an intentional improvement.

Finally, the original implementation relied extensively on function-pointer polymorphism. Function pointers were stored as "void*" and cast to different signatures at runtime depending on the function's arity (0–7 arguments) and whether it represented a closure with bound context. Because Rust does not permit this form of unchecked reinterpretation in safe code, we modeled callable objects as an enum containing one strongly typed variant for each supported function signature. This approach preserves the original functionality while leveraging Rust's type system to eliminate unsafe function-pointer casts.

Technologies used

Discussion

Builders also viewed

See more projects on Devfolio