frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

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

19•Forgret•4h ago
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

fjfaase•3h 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

bertili•2h ago
Another thread on small forth interpreters from just 15 days ago:

https://news.ycombinator.com/item?id=45039301

Forth can be beautifully and efficiently implemented in portable c++ using the using continuation passing style via the clang musttail attribute.

Have a look at Tails (not my project):

[1] https://github.com/snej/tails

acidx•41m ago
I recently wrote one, in C, using tail calls to implement dispatch with CPS: https://tia.mat.br/posts/2025/08/30/forth-haiku.html

It's already pretty efficient but I'm working on it to make it even more efficient so I can use it as some sort of primitive fragment shader for an art project. This Forth variant is intended to execute Forth Haikus, as defined by the Forth Salon website.

tdeck•2h ago
If you're interested in learning more about how FORTH works I, I can recommend two very old books.

Starting FORTH https://archive.org/details/LeoBrodieStartingFORTHIntroducti...

Threaded Interpretive Languages https://archive.org/details/R.G.LoeligerThreadedInterpretive...

The latter doesn't even mention FORTH, and describes some very archaic CPU architectures, but I found it fascinating because it builds things from the ground up.

elcritch•48m ago
In particular learning about threaded interpreters, sub-routine interpreters, etc is very eye opening. That and really internalizing that everything, even code, is really just numbers.
gabrielsroka•1h ago
1. You should add a URL when you you create a post on HN. You can indent code two spaces on HN, eg:

  Stack s;
  stack_init(&s);
  dict_init();
  exec(&s, "10 20 + ."); // Prints "30"
  exec(&s, "1 2 3 4 .s"); // Shows stack contents
2. Your readme mentions a repl but I don't see it in the source code.

3. I'm not an expert in C but I thought header files shouldn't have code in them. The code should be in a .c file

4. Maybe move the code from USAGE into its own .c file.

  #include "stacklib.h"
  
  int main() {
    Stack s;
    stack_init(&s);
    dict_init();
    exec(&s, "10 20 + .");
    printf("\n");
    return 0;
  }
zoezoezoezoe•1h ago
technically, "header only libraries" can be exceptions to C code not being in header files. See STB as an example https://github.com/nothings/stb. The advantage theoretically is that you can #include this library and use its code and types from just one file, its a decent model IMHO, but it can be jarring to someone unfamiliar with header only libraries.
vdupras•23m ago
Your experiment is certainly a good tool to grok stack mechanics, but that is only one element of what makes a Forth what it is. You're missing out on other crucial ingredients: colon definition and immediateness.

I wrote a series of articles that can help in that kind of discovery: http://tumbleforth.hardcoded.net/

GrapheneOS and Forensic Extraction of Data (2024)

https://discuss.grapheneos.org/d/13107-grapheneos-and-forensic-extraction-of-data
198•SoKamil•3h ago•64 comments

Gregg Kellogg has passed away

https://lists.w3.org/Archives/Public/public-json-ld-wg/2025Sep/0012.html
200•daenney•4h ago•29 comments

Spiral

https://spiraldb.com/post/announcing-spiral
15•jorangreef•35m ago•1 comments

Behind the Scenes of Bun Install

https://bun.com/blog/behind-the-scenes-of-bun-install
147•Bogdanp•3h ago•51 comments

Conway's Game of Life, but Musical

https://www.hudsong.dev/digital-darwin
61•hudsongr•2h ago•14 comments

Reshaped is now open source

https://reshaped.so/blog/reshaped-oss
180•michaelmior•6h ago•40 comments

An Engineering History of the Manhattan Project

https://www.construction-physics.com/p/an-engineering-history-of-the-manhattan
49•rbanffy•3h ago•18 comments

The US is now the largest investor in commercial spyware

https://arstechnica.com/security/2025/09/the-us-is-now-the-largest-investor-in-commercial-spyware/
68•furcyd•1h ago•19 comments

The Rise of Async Programming

https://www.braintrust.dev/blog/async-programming
39•mooreds•4h ago•24 comments

Strong Eventual Consistency – The Big Idea Behind CRDTs

https://lewiscampbell.tech/blog/250908.html
10•todsacerdoti•3d ago•1 comments

I Solved PyTorch's Cross-Platform Nightmare

https://svana.name/2025/09/how-i-solved-pytorchs-cross-platform-nightmare/
40•msvana•3d ago•9 comments

Mapping to the PICO-8 palette, perceptually

https://30fps.net/pages/perceptual-pico8-pixel-mapping/
48•ibobev•3d ago•13 comments

DeepCodeBench: Real-World Codebase Understanding by Q&A Benchmarking

https://www.qodo.ai/blog/deepcodebench-real-world-codebase-understanding-by-qa-benchmarking/
63•blazercohen•6h ago•4 comments

GrapheneOS accessed Android security patches but not allowed to publish sources

https://grapheneos.social/@GrapheneOS/115164133992525834
118•uneven9434•8h ago•21 comments

Piramidal (YC W24) Is Hiring Back End Engineer

https://www.ycombinator.com/companies/piramidal/jobs/1HvdaXs-full-stack-engineer-platform
1•dsacellarius•4h ago

KDE launches its own distribution

https://lwn.net/SubscriberLink/1037166/caa6979c16a99c9e/
625•Bogdanp•18h ago•427 comments

PgEdge Goes Open Source

https://www.pgedge.com/blog/pgedge-goes-open-source
69•Bogdanp•8h ago•13 comments

Show HN: Term.everything – Run any GUI app in the terminal

https://github.com/mmulet/term.everything
1004•mmulet•2d ago•135 comments

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

19•Forgret•4h ago•8 comments

DOOMscrolling: The Game

https://ironicsans.ghost.io/doomscrolling-the-game/
378•jfil•17h ago•88 comments

Hashed sorting is typically faster than hash tables

https://reiner.org/hashed-sorting
153•Bogdanp•3d ago•32 comments

ChatGPT Developer Mode: Full MCP client access

https://platform.openai.com/docs/guides/developer-mode
487•meetpateltech•1d ago•268 comments

How the tz database works (2020)

https://yatsushi.com/blog/tz-database/
57•jumbosushi•3d ago•10 comments

CRISPR Offers New Hope for Treating Diabetes

https://www.wired.com/story/no-more-injections-crispr-offers-new-hope-for-treating-diabetes/
35•manveerc•2h ago•10 comments

Germany is not supporting ChatControl – blocking minority secured

https://digitalcourage.social/@echo_pbreyer/115184350819592476
920•xyzal•7h ago•290 comments

Where did the Smurfs get their hats (2018)

https://www.pipelinecomics.com/beginning-bd-smurfs-hats-origin/
121•andsoitis•15h ago•51 comments

C++20 Modules: Practical Insights, Status and TODOs

https://chuanqixu9.github.io/c++/2025/08/14/C++20-Modules.en.html
52•ashvardanian•3d ago•47 comments

Court rejects Verizon claim that selling location data without consent is legal

https://arstechnica.com/tech-policy/2025/09/court-rejects-verizon-claim-that-selling-location-dat...
583•nobody9999•14h ago•69 comments

Brussels faces privacy crossroads over encryption backdoors

https://www.theregister.com/2025/09/11/eu_chat_control/
60•jjgreen•4h ago•18 comments

A desktop environment without graphics (tmux-like)

https://github.com/Julien-cpsn/desktop-tui
138•mustaphah•3d ago•44 comments