Live data from Hacker News

Implementing Forth in Go and C

eli.thegreenplace.net

11–20 of 29 posts

Re: Implementing Forth in Go and C

#11
post #10

I feel like as if the article itself is not finished at the end: > How can you know the arity of the functions without adding explicit comments? Sure, if you have a handful of words like bar and foo you know like the back of your hand, this is easy. But imagine reading a large, unfamiliar code base full of code like this and trying to comprehend it. Cool project by the way! Well done.

Thanks for the feedback! What do you feel is not finished?

I think (unless I missed it) it could be mentioned that typically you are supposed to have as small words as possible, with least amount of stack shuffling, and comment code as much as possible (stack effects). That said, I have not had to maintain large Forth projects so I have no clue how well I would fare ^^.

BTW, have you heard of https://factorcode.org? You might like it!

Re: Implementing Forth in Go and C

#12
post #2

Nice to see another developer who tried to implement Forth in Go (here’s mine: [0]). It’s not easy and usually takes several iterations. His implementation, while working for his use cases, is actually quite far from the originals. He uses separate Go structures for words, memory, for-loops, etc. Most of the default Forth implementations of words will fail, since they expect the heap and stack to behave in a certain…

One of your "Click to run" examples is called "faculty calculation". I'm not a forth expert but it looks like a factorial function. Is "faculty" a replacement for "factorial" in some contexts?

Re: Implementing Forth in Go and C

#13
post #12
post #2

Nice to see another developer who tried to implement Forth in Go (here’s mine: [0]). It’s not easy and usually takes several iterations. His implementation, while working for his use cases, is actually quite far from the originals. He uses separate Go structures for words, memory, for-loops, etc. Most of the default Forth implementations of words will fail, since they expect the heap and stack to behave in a certain…

One of your "Click to run" examples is called "faculty calculation". I'm not a forth expert but it looks like a factorial function. Is "faculty" a replacement for "factorial" in some contexts?

German, Dutch, Danish, Norwegian, and Swedish use the same word for faculty (university) and factorial (mathematics), so I'm guessing it's a mistranslation from Fakultät/faculteit/fakultet.

Re: Implementing Forth in Go and C

#15
I must say, implementing Forth in almost any high-level language, even C, can be awkward and almost antithetical. A "high-level" Forth implementation often merely presents a Forth-compatible runtime, which runs the risk of missing what makes Forth so unique and useful. (The author actually seems to understand this: "The first implementation I tried is stubbornly different. Can we just make a pure interpreter?")

That's because of two main reasons. First, Forth relies heavily on passing control to the next word, instead of calling word implementations as subroutines. This is difficult to do correctly, and dynamically, in C. I suppose you could get by with computed gotos, but...

Secondly, Forth and assembly (or more generally any lower-level instruction/register view of what it's running on) go hand in hand. Forth words may carry their own assembly implementation within them, or they may be partially assembly.

Without those two things, you don't really have a Forth system, just a Forth interpreter. And you cannot define new primitive words, or words with very custom compilation semantics, from within Forth itself.

And therein also lies Forth's strength. It is very easily on-the-fly expandable, and it can even bootstrap itself. It is a great tool for machine-level exploration: Do complex low-level things with a lot less boilerplate.

The author mentions that this second attempt is a "hacker level" implementation, but short of reading the source code does not seem to go into any detail what that means exactly, and how it's implemented?

Re: Implementing Forth in Go and C

#16
Speaking of Forth... Here's a fun bit of trivia.

PowerPC and Intel Macs used to use Open Firmware[0] to define their firmware (BIOS). Macs with M1[1] architectures and subsequent generations may also, but I cannot attest to this.

For those Macs which do use Open Firmware[0], a key implementation component of it is FCode[2]:

  FCode is a Forth dialect compliant to ANS Forth, that is 
  available in two different forms: source and bytecode. 
  FCode bytecode is the compiled form of FCode source.
So if anyone asks, "who uses Forth anymore?", a correct answer is - a large percentage of everyday people.

0 - https://www.openbios.org/Open_Firmware.html

1 - https://en.wikipedia.org/wiki/Apple_M1

2 - https://www.openbios.org/Forth_FCode.html

Re: Implementing Forth in Go and C

#17
post #10

Earlier quoted context omitted.

Thanks for the feedback! What do you feel is not finished?

I think (unless I missed it) it could be mentioned that typically you are supposed to have as small words as possible, with least amount of stack shuffling, and comment code as much as possible (stack effects). That said, I have not had to maintain large Forth projects so I have no clue how well I would fare ^^. BTW, have you heard of https://factorcode.org ? You might like it!

Yes, Factor is mentioned in the first paragraph of the post!

Re: Implementing Forth in Go and C

#18

Speaking of Forth... Here's a fun bit of trivia. PowerPC and Intel Macs used to use Open Firmware[0] to define their firmware (BIOS). Macs with M1[1] architectures and subsequent generations may also, but I cannot attest to this. For those Macs which do use Open Firmware[0], a key implementation component of it is FCode[2]: FCode is a Forth dialect compliant to ANS Forth, that is available in two different forms: sou…

More trivia: OpenFirmware is descended from Sun Microsystems' firmware, OpenBoot. Add-on cards could contain Forth code that would initialize their own hardware and provide a device driver that could be used at boot time (e.g., a network card could initialize itself and provide commands to download and boot an OS off the network).

Re: Implementing Forth in Go and C

#19
post #15

I must say, implementing Forth in almost any high-level language, even C, can be awkward and almost antithetical. A "high-level" Forth implementation often merely presents a Forth-compatible runtime, which runs the risk of missing what makes Forth so unique and useful. (The author actually seems to understand this: "The first implementation I tried is stubbornly different. Can we just make a pure interpreter?") That'…

Ah, but which assembly? If you just roll a simple VM in the host language, your instruction set becomes the assembly Forth integrates so well with. I'm assuming that's what they did in their C implementation.

Re: Implementing Forth in Go and C

#20
post #17

Earlier quoted context omitted.

I think (unless I missed it) it could be mentioned that typically you are supposed to have as small words as possible, with least amount of stack shuffling, and comment code as much as possible (stack effects). That said, I have not had to maintain large Forth projects so I have no clue how well I would fare ^^. BTW, have you heard of https://factorcode.org ? You might like it!

Yes, Factor is mentioned in the first paragraph of the post!

Oh I completely forgot by the time we left the comments. Embarrassing. In my defense, I always mention Factor. :D
Post reply on HN