Show HN: I built a minimal Forth-like stack interpreter library in C

3 Forgret 1 9/11/2025, 12:15:22 PM
This weekend I created stacklib.h - a single-header library that brings Forth-style stack operations to C. It implements a basic interpreter with:

- Stack operations (push/pop/dup/swap/over/drop) - Arithmetic (+, -, *, /) - Output (., emit, cr) - Stack inspection (.s, depth)

Example usage: Stack s; stack_init(&s); dict_init(); exec(&s, "10 20 + ."); // Prints "30" exec(&s, "1 2 3 4 .s"); // Shows stack contents

The library is self-contained, requires no dependencies, and handles basic error checking. It was inspired by wanting to understand how Forth works at a fundamental level while keeping the simplicity of C.

I'm curious what other stack-based or concatenative programming enthusiasts think about this approach. Has anyone else built something similar? What features would you add to make it more useful?

GitHub: https://github.com/Ferki-git-creator/stacklib

Comments (1)

fjfaase · 13m ago
Interesting. I am currently in the process of writing an interpreter for a similar stack based language. I already wrote a compiler for this language to x86 assembly that can be compiled to an ELF. The language is used as an intermediate language for a C compiler that I am writing. It is maybe less Forth-like than your language. For more information see: https://github.com/FransFaase/MES-replacement For the interpreter have a look at: stack_c_interpreter.c

At WHY2025, I gave a talk about the reasons why am working on this. See: https://www.youtube.com/watch?v=akzyyO5wvm0