tinyexpr.zig

tinyexpr Zig port that keeps C smoke green

Repo
tinyexpr.zig banner

About

## The problem it 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](https://assets.devfolio.co/content/9e6fe0dc215d46a7a536f76322dafaa8/f54824c9-46f1-44a4-84bd-1bc721b34e21.png)

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](https://assets.devfolio.co/content/9e6fe0dc215d46a7a536f76322dafaa8/3f56c8bc-55bc-407a-bec2-52dd815eabb8.png)

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](https://assets.devfolio.co/content/9e6fe0dc215d46a7a536f76322dafaa8/4b0ae478-d184-4f7d-bb98-85016bc5e4e6.png)

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](https://assets.devfolio.co/content/9e6fe0dc215d46a7a536f76322dafaa8/ad6c6a12-174a-4265-b142-7a5facb61ef7.png)

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](https://assets.devfolio.co/content/9e6fe0dc215d46a7a536f76322dafaa8/89faa1a4-5cd3-4547-ba7a-8cef54457185.png)

Screenshots

tinyexpr.zig screenshot 1tinyexpr.zig screenshot 2tinyexpr.zig screenshot 3tinyexpr.zig screenshot 4tinyexpr.zig screenshot 5

Team

From the public Devfolio project page. Votes, swipes and the judge score are hackfeed's own.

AI judge

Not judged yet. A free model reads this page, compares it with similar projects and this event's winners, then scores it and says what would make it win.

Still being built?

Not checked yet.

Swipe stats

0upvotes
0skips
0saves

Discussion 0

No comments yet. Ask the team something, or say what you'd use it for.

Posting as a guest. Plain text, 1,000 characters max.

Already built nearby

Closest projects by meaning and shared words. Similar isn't the same as copied: read them.