Three Weeks and a Button-Sized Chip: The Birth of a C Language Interpreter (Open Source)

Inspired by the legendary creation of UNIX, the miracle of a C language interpreter was recreated on a button-sized chip over three special weeks.During the three special weeks from late 2024 to early 2025, drawing inspiration from the legendary creation of UNIX as an ordinary programming enthusiast, I completed a challenging task on an ESP32 chip that is hundreds of times more powerful than the PDP-7 computer from the 1970s—building and optimizing a complete C language interpreter.Three Weeks and a Button-Sized Chip: The Birth of a C Language Interpreter (Open Source)Three Weeks and a Button-Sized Chip: The Birth of a C Language Interpreter (Open Source)Three Weeks and a Button-Sized Chip: The Birth of a C Language Interpreter (Open Source)

Source of Inspiration: Insights from UNIX and Personal Opportunities

In 1969, ten years before the author’s birth, Ken Thompson created the prototype of the UNIX operating system on the PDP-7 computer at Bell Labs. Faced with limited hardware resources and a desire for new computing methods, they wrote the first UNIX system in assembly language.

Three Weeks and a Button-Sized Chip: The Birth of a C Language Interpreter (Open Source)

Half a century later, gazing at the ESP32 chip in my hand, which is hundreds of times more powerful than the PDP-7 yet only the size of a coin, I posed a question: “If Thompson could build UNIX with limited resources, why can’t I implement a C language interpreter on this chip?”

This idea found its opportunity during the National Day week of 2024.With my family away, I had a whole week of solitude, and I happened to have an ESP32 development board. On my workbench, a computer, an ESP32 board, and a few connecting wires formed the starting point of my dream.

Three Weeks and a Button-Sized Chip: The Birth of a C Language Interpreter (Open Source)

Week One: Core Porting and Platform Adaptation (A Week of Solitude During National Day)

The first week of the project coincided with National Day, and with my family traveling, I gained a rare full week of solitude.

The porting process first faced the challenge of memory management. Although the ESP32 has relatively ample memory (typically 512KB, with Wi-Fi and Bluetooth consuming about half), a precise memory management strategy is still required. I implemented a clever memory allocation mechanism to ensure that the interpreter itself would not exhaust device resources. Nevertheless, due to my programming skills, there were occasional crashes, with a crash rate of about 0.9%.

Next came the adaptation of the lexer and parser. Drawing from the design philosophy of the PicoC interpreter by rhelmus from seven years ago, I restructured the code base to ensure stable operation in the constrained environment of the ESP32.

Three Weeks and a Button-Sized Chip: The Birth of a C Language Interpreter (Open Source)

By the weekend, the initial success was evident—the interpreter could run simple C code on the ESP32, such as variable declarations, arithmetic operations, and function calls. This milestone achievement proved the feasibility of the concept.

Three Weeks and a Button-Sized Chip: The Birth of a C Language Interpreter (Open Source)

At the end of the first week, I was able to implement the first character printing program in the ESP’s C language interpreter, similar to the “hello world” in C programming.

Week Two: Hardware Interaction and Function Expansion (Focused Work During Annual Leave)

The second week of work coincided with my annual leave, providing another full week of independent work to focus on project development. This phase focused on expanding the hardware interaction capabilities. The real value lies in enabling C code to control the hardware peripherals of the ESP32, which was a key feature lacking in the original project.

Any development that does not solve real problems is merely a game; it is not as simple as outputting a “hello world”.

GPIO control was the first breakthrough. I added built-in functions that allow C code to directly read and write digital pins:

Three Weeks and a Button-Sized Chip: The Birth of a C Language Interpreter (Open Source)Three Weeks and a Button-Sized Chip: The Birth of a C Language Interpreter (Open Source)

Next, I implemented support for I2C and SPI interfaces, allowing the connection of various sensors and peripheral devices. This opened up the possibility of IoT applications for the interpreter, utilizing I2C for temperature and humidity sensors, and combined with I2C functionality to read environmental data such as temperature, humidity, and light. Using the SPI interface, I achieved high-speed data transmission, and I am currently working on a feature to drive a TFT LCD interface, aiming to replace the serial LCD screen.

Memory monitoring functionality was also added, allowing real-time monitoring of memory usage to prevent code from exhausting system resources. This feature is crucial for debugging and ensuring system stability.

By the weekend, I even attempted a simple game—a simplified version of Space Invaders, displayed on a dot matrix screen, proving that the system could not only handle hardware interaction but also support a certain level of graphics processing capability.

Week Three: Web Integration and Optimization Debugging (Perfect Wrap-Up During Spring Festival)

By the third week, as the year came to a close, I continued to refine the project while spending time with my parents and family. The final phase of work was to integrate the interpreter with the ESP32’s built-in web server, creating a user interface.

This step fundamentally changed the practical value of the project, allowing users to write, upload, and run C code directly through a browser without the need for complex development environments or firmware flashing.Three Weeks and a Button-Sized Chip: The Birth of a C Language Interpreter (Open Source)The web interface provides:

  • • A code editor with basic syntax highlighting
  • • Code upload functionality: sending programs to the ESP32
  • • Real-time output: displaying program execution results
  • • Hardware status monitoring: showing memory usage and system status

The interface design follows the principle of simplicity from UNIX philosophy, providing sufficient functionality without excessive complexity. Ultimately, at the end of this project spanning three special weeks, the interpreter was able to:

  • • Parse and execute most C language constructs
  • • Directly control the hardware pins of the ESP32
  • • Communicate with external devices via I2C and SPI
  • • Engage in interactive programming through the web interface
  • • Monitor system resource usage

Addressing Pain Points and Project Value

This C language interpreter addresses a core pain point in ESP32 development: in traditional development processes, every code modification requires going through the complete cycle of compile-upload-flash, which is particularly cumbersome during debugging and iteration phases. For already deployed devices, even the smallest logical changes often require a complete OTA update or physical access to the device for flashing.

With this C language interpreter, it is possible to:

  • • Instantly test code snippets without the complete compile-upload cycle
  • • Remotely update device logic without a full OTA update
  • • Adjust device behavior on-site to meet new requirements or fix issues

This is especially valuable for the management and maintenance of IoT devices, significantly reducing iteration costs and maintenance difficulties.

As is customary, the project is open source:https://github.com/lyxer123/ESP32cLan.

Leave a Comment