← Retour au blog
tech 30 July 2026

Overengineered Calculator: Zig + QBE

Discover how Zig and QBE turn a simple calculator project into a demonstration of native code power, illustrating the steps of code generation.

Article inspired by the original source
Overengineered calculator: Zig + QBE ↗ tomekw.com

Introduction

In the programming world, simplicity can often mask underlying complexities. Take the example of a calculator, a tool we use daily without much thought. But what if we decided to overengineer a calculator using cutting-edge technologies like Zig and QBE? This is exactly what Tomek Wałkuski set out to do, and in this article, we will explore how it works.

Why Zig and QBE?

Zig is a modern programming language designed to be robust, simple, and performant. It offers features that make memory management and code safety easy, making it an excellent choice for low-level applications.

On the other hand, QBE is a compiler backend that promises 70% of the performance of industrial compilers with only 10% of their complexity. This efficiency makes QBE a perfect tool for those who want to experiment with language innovations without getting lost in optimization details.

Defining a Grammar

To transform a classic arithmetic expression like "2 + 2 * 2" into a correct result, it's essential to define a clear grammar. This grammar will help manage operation priorities: multiplication and division should be performed before addition and subtraction.

Here is a simplified grammar used by Tomek:

`` Expression = Sum; Sum = Product , { ( "+" | "-" ) , Product }; Product = Unary , { ( "*" | "/" ) , Unary }; Unary = ( ( "-" | "+" ) , Unary ) | Primary; Primary = Number | "(" , Expression , ")"; ``

Tokenizer in Zig

The tokenizer is a crucial component that breaks the source code into a series of interpretable tokens. In Zig, the tokenizer can be implemented as a simple state machine, recognizing tokens such as numbers, operators, and parentheses.

Example of Implementation:

Using an enum for token types:

``zig pub const Tag = enum { invalid, eof, left_paren, right_paren, plus, minus, star, slash, number, }; ``

Code Generation with QBE

Once the arithmetic expressions are properly tokenized and parsed, the next step is to generate native machine code. This is where QBE comes in, allowing these instructions to be efficiently compiled into executable code.

QBE stands out for its ability to provide a compact and performant backend, making the process of generating native code more accessible and less daunting for developers.

Conclusion

Using Zig and QBE to build an overengineered calculator is more than just an intellectual exercise. It's a demonstration of how modern tools can transform a basic project into an in-depth exploration of native code generation. If you're ready to explore how these technologies can boost your project, let's discuss it in 15 minutes.

Zig QBE calculatrice génération de code compilation
Deepthix newsletter · 100% AI · every Monday 8am

An AI agent reads tech for you.

Our AI agent scans ~200 sources per week and ships the best articles to your inbox Monday 8am. Free. One click to unsubscribe.

Visit the newsletter page →

Want to automate your operations?

Let's talk about your project in 15 minutes.

Book a call