Live data from Hacker News

Implementing a Forth

ratfactor.com

61–70 of 70 posts

Re: Implementing a Forth

#61

Earlier quoted context omitted.

Did you actually test that code? Never mind the tiny buffer size or possible hallucinations, but just from looking at the "parser", it does seem to do nothing more than copy the expected tokens in the same order it reads them on input, ignoring any parentheses. This CAN'T possibly work, not even for the example input. Maybe if it reversed the string, but unless I've forgotten completely how to read Forth it doesn't d…

This was kinda joke. I have done it in real life, but that was in 1976. And the mechanism was totally different, more Lisp-like, but could not make Grok to do it, so I suggested simple pre-parser.

What I really wanted was a "pascal-type" tokenizer and second stack for commands. "(" means push next and ")" means pop and execute. In this kind of system (if (
  And yes I know it is not Lisp, perse.

Re: Implementing a Forth

#62
It's a bit of a shame that Forth has become almost synonymous with indirect threading implemented in assembly. This is true and somewhat interesting of most implementations, but obfuscates the deeper philosophy of Forth. For me, this is about self-reliance and building your own tools that are suited for the specific task at hand.

I don't care for a lot of the standard forth-isms, such as the way branching and recursion are handled. But I make extensive use of the threaded execution model. One quickly notices that anything that can be pipelined naturally fits into this model. Programming in forth style is often similar to programming with unix pipes except you aren't restricted to string input and output and you do not need to fork subprocesses (though you certainly can!).

Learning how to program in forth, which as so many have pointed out is best done by implementing your own, will fundamentally change the way you program in all languages. Chaining method calls in data pipelining is somewhat forth-like.

As far as implementing a forth, the main interpreter can be written in 14 lines of lua (using lua varargs as the parameter stack):

  local thread thread = function(first, next, ...)
    if first then
      if next then
        local cont = thread(next, ...)
        return function(...)
          return cont(first(...))
        end
      else
        return first
      end
    else
      error "require at least one task"
    end
  end
Here is how to use it:

  local push = function(value) 
    return function(...)
      return value, ...
    end
  end
  local print = function(msg, ...)
    print(msg)
    return ...
  end
  local hello_world = thread(push "Hello, world!", print)
  hello_world()
And for fun, here is dup:

  local dup = function(top, ...) return top, top, ... end
The value of these constructs may not be immediately obvious, but I have built a nice compiler in lua on top of it. (I copied the thread definition above directly from my compiler source code).

Re: Implementing a Forth

#63
post #47

Earlier quoted context omitted.

Why is it not useful for large programs (if you keep your words small)? What about Factor?

There's no compile-time error checking and words can push/pop arbitrary amounts of data to/from the stack. This means that if a program grows beyond the point where you can keep the whole thing in your head, it's a nightmare to maintain since bugs like giving a word the wrong number of parameters can cause a cascading series of errors that's challenging to unravel. I don't think it's a coincidence that Forth advocate…

There is in Factor, though!

https://github.com/factor/factor

https://factorcode.org/

Re: Implementing a Forth

#64
I went a completely different route when implementing zeptoforth (https://github.com/tabemann/zeptoforth) -- I went right for implementing a fully-featured system rather than focusing on minimalism, and came out with something that I am extremely comfortable writing non-trivial code with rather than a toy that shows how small of an implementation I can make without regard to being a practical tool. And yes, zeptoforth is very big as Forths (especially microcontroller Forths) go -- because the goal is to make something that can be used to program real systems out of the box with minimal effort on the user's part.

Re: Implementing a Forth

#65

Is writing a Forth in a high-level language feasible/useful as a learning exercise? Almost all the Forth discussion I’ve seen has been about implementing in assembly.

Not just a learning exercise, it's also very useful, it allows you to expose application internals in a controlled way so that they can be scripted without exposing your entire codebase to untrusted code.

Re: Implementing a Forth

#66

I went a completely different route when implementing zeptoforth ( https://github.com/tabemann/zeptoforth ) -- I went right for implementing a fully-featured system rather than focusing on minimalism, and came out with something that I am extremely comfortable writing non-trivial code with rather than a toy that shows how small of an implementation I can make without regard to being a practical tool. And yes, zeptofo…

Have you had any feedback from The Chuck?

How about a ZeptoFORTH-MCP bridge (to implement services in ZeptoFORTH that can be called by local or remote agents) and an official way of programmatically configuring and interacting with an LLM, like on a Jetson, from ZeptoFORTH?

Re: Implementing a Forth

#67
post #54

I have made a living with Forth since 1981. I have written very few new Forth kernels, but have ported them to many CPU architectures. Forth is no longer fashionable, but it works. Writing a new Forth may be an interesting project, but you will not write a good one until you have a few applications under your belt. Forth is a very subtle language. The internet is full of abandoned Forth kernel projects. Many of these…

The guy who invented NodeJS (a "fashionable" giant hack) got up and said he regretted it and now uses Go. Did that stop anyone from running up the tech debt with NodeJS? After all, "everyone is doing it!" So easy to start banging out JavaScript...

At the other extreme you have Swift which is not Good (as opposed to no good) until V5 which is now obsoleted by V6. Do we all know how screwed Apple is when it comes to LLM training coverage of Swift 5 or 6? Short of their own codebase (and even then), where are they going to get enough training data to help all the Vibe Coders and novices? Could take years to resolve this.

Weird future, eh? So FORTH may not be "fashionable" but it is hella fun and very good in a number of dimensions. With the right LLM interfaces (which love large libraries of concise word-like primitives), world domination is assured.

Re: Implementing a Forth

#68
post #26
post #5

Earlier quoted context omitted.

What kind of programs would one naturally reach for Forth as the optimal solution? It has always struck me as a very low level language but I rarely hear this caveat from its advocates.

I have yet to use Forth for anything serious, but it's worth pointing at an example for how expressive you can be dealing with hardware. From https://www.forth.com/embedded/#Embedded_Programming_Example the top level control for a washing machine could be: : WASHER ( -- ) WASH SPIN RINSE SPIN ; I won't repeat all the rest, even though it's short, but let's look at a few other definitions that build up to that: : RINS…

A lot of the power of Forth comes from its metaprogramming capabilities and having the compiler available at runtime, all wrapped in a tiny footprint. Similarly to Lisp, it empowers one to explore a problem domain without getting in the way. These concepts are alien to C which is downright hostile to exploratory programming.

If you really want to understand the genius of Forth and its creator, I suggest reading everything that Chuck Moore put down in writing starting with "Programming a problem-oriented language".

A lot of us today, being bogged down in the sort of tedium-inducing programming that pays the bills, tend to forget that programming languages are (or should be!) primarily about expressing ideas. Forth is still one of the best languages to do that in.

Re: Implementing a Forth

#69
post #26

Earlier quoted context omitted.

I have yet to use Forth for anything serious, but it's worth pointing at an example for how expressive you can be dealing with hardware. From https://www.forth.com/embedded/#Embedded_Programming_Example the top level control for a washing machine could be: : WASHER ( -- ) WASH SPIN RINSE SPIN ; I won't repeat all the rest, even though it's short, but let's look at a few other definitions that build up to that: : RINS…

A lot of the power of Forth comes from its metaprogramming capabilities and having the compiler available at runtime, all wrapped in a tiny footprint. Similarly to Lisp, it empowers one to explore a problem domain without getting in the way. These concepts are alien to C which is downright hostile to exploratory programming. If you really want to understand the genius of Forth and its creator, I suggest reading every…

Yeah. Pretty much agree with everything here. I'd rather encourage starting with the book Thinking Forth, though. It's not written by Moore but has some commentary from him included in it.

I'd even suggest learning a little Forth to people even if it didn't have its interactive nature (which again I don't actually find useful in my own limited embedded work, there's really no "explore" phase as the problems are all straightforward -- contrary to software I write in CL or even Java). I had a friend in college who for a project made his own language and got it working on an embedded system (I think via compiling to C, but I don't recall exactly), but it was just a boring Algol-like somewhat inspired by Ruby. That pattern has shown up again and again around the world though. Forth is one of the handful of languages that shows what expressive options there are that aren't just transparently Algol-like.

Re: Implementing a Forth

#70
post #66

I went a completely different route when implementing zeptoforth ( https://github.com/tabemann/zeptoforth ) -- I went right for implementing a fully-featured system rather than focusing on minimalism, and came out with something that I am extremely comfortable writing non-trivial code with rather than a toy that shows how small of an implementation I can make without regard to being a practical tool. And yes, zeptofo…

Have you had any feedback from The Chuck? How about a ZeptoFORTH-MCP bridge (to implement services in ZeptoFORTH that can be called by local or remote agents) and an official way of programmatically configuring and interacting with an LLM, like on a Jetson, from ZeptoFORTH?

I have not had any feedback from Chuck, but from looking at Chuck's work I think he wouldn't consider zeptoforth to be all that Forthy.

As for a zeptoforth-MCP bridge, well, I am not really a fan of LLM's. At work we have GitHub Copilot, and I find its autocomplete to be more of a nuisance than anything, as I never like its suggestions and find the fact that it suggests anything, which I then have to reject, as largely something that interferes with my flow.

Post reply on HN