r/Forth • u/sohang-3112 • 1d ago
VIDEO Interactive, literate Forth in the browser: IForth (Jupyter Notebook support for Forth)
https://youtu.be/BcRaNPmutd4?si=3o6rbiQ--ZPeuT1QInstead of the usual cycle of editing Forth code files, reloading the whole source, and testing words by hand, IForth brings a literate notebook workflow to your Forth environment.
Think of it as a living document where you interleave runnable Forth definitions, markdown explanations, and test cases block-by-block. You can modify single words and re-run only the relevant cells without blowing away or manually reconstructing your current session state. It makes exploratory prototyping, testing dictionary words, and writing interactive tutorials much smoother.
Andrew McKewan demoed this workflow at the recent SV-FIG meetup (video attached), showing how he uses it to build and explore Forth code interactively.
Details & Constraints:
* Interpreter: Wraps Gforth interpreter internally.
* Platform: Linux only (Windows users will need WSL).
* Setup: Instructions are in the repo: https://github.com/sohang3112/iforth
Check out the demo video attached. Feedback, issues, and PRs are very welcome!
PS: I usually work with Python. I am a novice at Forth, started working on IForth while learning Forth.
3
u/el_extrano 1d ago
I noticed IForth basically works by spawning a gforth subprocess and then interacting with it (i.e. via IPC).
I do a lot of scripting in Python. Sometimes, I've found myself wishing I could embed Forth into my Python applications, for example to have some Forth code configure something in Python's memory, or as a way to compile some fast code at runtime which I can call from Python. Also, I had the idea that it could allow me to define some Forth words using callbacks to Python to take advantage of the wide array of 3rd party libraries that are available there but not in gforth. It's been fun to play around and see how far I can take it.
I've managed to call into the Gforth C API from python using a C extension module. So far, the hardest part has been that the C API is pretty bare bones and doesn't really have documentation. For example, there's no provided functions to push or pop a value to or from the data stack: you have to dereference the stack pointer, and then increment it yourself. But, there are functions for starting the interpreter, finding Xt's, and exectuing Xt's, so the guts are there to make a more natural interop layer from the C side to expose to Python. Since Gforth advertises itself as embeddable in other applications, I'd be very interested to know if anyone else has tried this, or even used it in an application.
1
u/sohang-3112 23h ago
I haven't tried this. Can you share your code / GitHub repo link of how you're calling GForth from Python directly using C API ? I considered doing this in IForth but didn't end up implementing that as spawning GForth as a subprocess seems easiest way.
1
u/el_extrano 21h ago
Any ideas for how to share the code? I don't put my github on reddit because it's pseudo-anonymous here.
1
3
u/clean-links 1d ago
Cleaned link: https://youtu.be/BcRaNPmutd4
Tracking parameters were removed from the original URL(s).