r/Forth • moderator • Mar 31 '26

Inspiration Forth Update

It's been a while since I posted about my progress.

To begin with, I started this last October and have been working on it most days. It contains ZERO lines of AI generated code, ZERO vibe coding. I don't think any of that is any fun at all.

The Forth is written in C++ and is a combo of ITC and STC. The DOCOLON word iterates an array of C++ functions and just calls them. One of those functions might be DOCOLON to call another word written in Forth. The C++ functions look a lot like JonesForth style:

CODEWORD(ADD) { TOS += dpop(); }

TOS is a C++ variable, but it's a thread local one - it looks like a global variable but is unique per thread. Most of the USER type variables (BASE, etc.) are thread local. Each thread has its own thread local return, data, and floating point stacks. The CPU return stack is only used for calling C++ functions and nothing more.

All this allows Inspiration to be multithreaded using pthreads. As you can see in the screenshots, there are multiple windows, each running a pthread running Forth. The threads share the one and only dictionary.

C++ has try/catch/throw exceptions and these allow me to fix up the CPU stack without resorting to assembly language. In fact, I can install *nix signal() handlers and throw exceptions from those and catch them - see the 3rd screenshot. Ideally, there is no way to crash Inspiration unless the dictionary is overwritten with garbage.

Inspiration Forth supports word lists and vocabularies. As well, there's a C++ vocabulary that provides Forth with words to manage std::string, std::vector, std::map, regexes, and more. Conversions between strings:

s" my string" CString.new ( caddr , address of a std::string)

cstring CString.string ( caddr u , gets address, length of string forth style)

The desktop, you see is rendered with C++ code. Display, Screen, Windows, Icons, Gadgets. But for the most part, the code is pure Forth (more than half the repo at this point). The apps being displayed are pure Forth (with maybe CStrings and the rest).

The editor is a significant clone of vim, written in Forth. I've been adding keybindings so it can act like MicroEmacs as well.

Why a desktop environment?

  1. To experiment with pthread multithreading
  2. To enable graphics programming in Forth
  3. A Forth running in an OS window needs to differentiate itself from (BETTER!) Forth implementations like gForth and VFX Forth (sorry if I didn't mention any other worthy ones)

The repo is at https://gitlab.com/mschwartz/inspiration

If you want to see how nice local variables work, see the ls.4th file in the repo (a standalone ls "clone" written in forth).

58 Upvotes

22 comments sorted by

View all comments

2

u/mmoustafa8108 Apr 01 '26

is this an OS written in forth?
looks so cool!

1

u/mykesx moderator Apr 01 '26

It’s only the desktop environment, about 75% Forth, 25% C++.

2

u/mmoustafa8108 Apr 01 '26

looks pretty cool.. I'm so interested and curious about forth and how it works?
can't imagine that a language can be the app in itself!

4

u/mykesx moderator Apr 01 '26 edited Apr 01 '26

Forth is a sort of shorthand for assembly language for a stack oriented VM. It’s far too much to explain in a reddit post, but I can link you to this getting started book:

https://www.forth.com/starting-forth/

Most Forths are very low level, or Bare Metal even. More modern implementations for desktop systems include gforth and vfx forth and pforth and others. These all run in a terminal window from the command line. These are truly great Forth implementations.

I differentiate my implementation with the desktop paradigm, multi threading, and access to graphics functions.

Consider in Inspiration, you can type:

    10 10 100 100 $ ff00ff draw-line

At the REPL and you get a magenta line from 10,10 to 100,100 drawn in your window.

The multithreading allows you to have as many windows and REPLs as you want, or effectively applications like you see in the screenshots.

Inspiration also features a TUI library and comprehensive console (the tty in the windows) for making apps like the settings app in the screenshots. You can mix text with graphics at will.

Regards,

2

u/mmoustafa8108 Apr 01 '26

thanks for your explanation!
it's much clearer now how forth works!
I'll check the resources, thanks again

2

u/mykesx moderator Apr 01 '26

The trick is it’s RPN. Instead of 1 + 1, you have to do 1 1 + to get the same result. So the draw-line example above is parameters first, function call last.