Skip to content
tinyexpr.zig

tinyexpr.zig

tinyexpr Zig port that keeps C smoke green

Created on 3rd August 2026

•

tinyexpr.zig

tinyexpr.zig

tinyexpr Zig port that keeps C smoke green

The problem tinyexpr.zig solves

tinyexpr is a small C library for compiling and evaluating math expressions. People embed it for config formulas, tools, and game logic when they want a tiny expression language without a full scripting runtime.

We ported codeplea/tinyexpr to Zig 0.16.0 for Port Mortem Track G (C to Zig). Downstream C code can keep calling the same te_interp / te_compile / te_eval / te_free surface: the Zig build ships that C ABI.

The original smoke suite is untouched (byte-identical smoke.c and minctest.h) and links against the Zig static library. All four flag configs pass on both Zig-linked and C-linked smoke:

  • default: 4930/4930
  • pow: 4924/4924
  • natlog: 4934/4934
  • pow_natlog: 4928/4928

image

Escape hatches live only in src/c_abi.zig: 16 total, CI threshold 20, zero outside that file. A certified 60s differential fuzz ran 48,512,558 executions with 0 divergences. An enumerator over 11,505 short inputs (string length at most 3, AST depth at most 1) also reported 0 divergences on compile nullness, error position, f64 bits, and te_print bytes.

image

Where the C original stack-overflows on deep nests (upstream issue 136), the port fails closed with a bounded error instead of SIGSEGV. Fair interp bench (C -O2 vs Zig ReleaseSafe, Linux Docker aarch64): Safe arithmetic p99 about 1.03% slower (8209 vs 8125 ns).

One command for judges: docker compose up --build verify
Repo: https://github.com/aadi-joshi/tinyexpr.zig

Challenges we ran into

Parity without editing tests. The original smoke suite had to stay byte-identical. Loose float tolerances would have made Zig look green for the wrong reason. We kept bit-exact compares and fixed the port until all four compile-flag matrices passed on both Zig-linked and C-linked smoke. The scorecard CI fails if README numbers drift from measured evidence.

C ABI versus escape-hatch budget. Matching te_expr layout and allocators on Zig 0.16 needs pointer casts and allocator bridging. Every hatch is confined to src/c_abi.zig, counted in CI (threshold 20), and the parser core stays hatch-free. That is our Track G answer to shipping a port that compiles but is full of unchecked escapes.

image

Differential comparison is finicky. In-process C versus Zig needs symbol renaming, shared libm strtod for locale parity, and a written equivalence scope: C locale, below nest bound, no C undefined behaviour. Deep nests that crash C are documented divergences, not silent wins. Host-dependent edges like fac(0/0) stay in probe data and are scoped in the write-up.

Bugs showed up while matching C. Differential work led to confirming nest overflow (issue 136) and signed-char ctype UB (issue 105), filing next_token linkage (issue 141) and locale/strtod (issue 142), and opening fix PRs 143 and 144. Novelty is stated honestly in docs/findings.md: 136 and 105 were not claimed as first discoveries.

image

Allocator-misuse Track G criterion: honest miss. Fault injection over 271 ordinals produced 0 crashes on either side. We recorded the negative result instead of forcing a story.

Two machines, one invariant. WSL and Darwin in parallel meant scrubbing absolute host paths from evidence, keeping the four suite totals identical, and freezing on tag submission-v1 with those counts in the tag message.

image

Discussion

Builders also viewed

See more projects on Devfolio