another 4th

58 views
Skip to first unread message

p da

unread,
Jan 29, 2024, 4:00:36 AMJan 29
to 4tH-compiler
Hello,

Browsing the web I landed in this page what is a forth compiler named 4th, I'd like to know if it is somehow related to TheBeez's 4th  (I understand it has nothing in common)

It's interesting since it seems to share the same design, an image, a compiler to intermediate language and a transpiler to C to generate the final executable program.

regards,

The Beez

unread,
Jan 29, 2024, 4:20:58 AMJan 29
to 4tH-compiler
Hi!

I tend to have several pages around the web (although most are linked in some way) and I know of at least one repository copying a previous version of 4tH. Shame you didn't include the link, since Google came up empty. But:
  1. I know of one single other Forth compiler: https://almy.us/forthcmp.html It's got a different design, though;
  2. Win32forth sometimes uses "4th" in their name - but it's got nothing to do with 4tH (https://sourceforge.net/projects/win32forth/);
  3. A German Forth group sometimes uses "4th" in their communication or logo. Still no connection with 4tH.
AFAIK I'm the only one who ever created an architecture like this and made it into a viable project. There is some experiment to hook up Forth into the GCC (or CLang) backend, but is does not have the maturity of my project.

So without an actual link, all hypotheses remain firmly in place:
  1. It's a derivative of 4tH - or some random page pointing to 4tH;
  2. It's the same name, but has nothing to do with 4tH (less likely);
  3. It's a project based on the same philosophy I know nothing about.
Hans Bezemer

The Beez

unread,
Jan 29, 2024, 4:24:51 AMJan 29
to 4tH-compiler
Sorry, it wasn't GCC, it was LLVM: https://www.youtube.com/watch?v=a6UKYeMiFyQ

Hans Bezemer

The Beez

unread,
Jan 29, 2024, 4:40:57 AMJan 29
to 4tH-compiler
Is it this one? https://github.com/leonardoce/4th

No relation. BTW, the last update was 12 years ago - and still features v0.1. Mine was updated yesterday. So I assume it was some experiment of some Italian guy who abandoned the project 12 years ago. You will find plenty of abandoned Forth experiments on the web. But there is a difference - His 4th features the original Forth architecture. You have to bring the system in a certain stage to allow an image dump, which (I assume) is merged with the kernel. I don't know if the image is stripped before being dumped (some classical Forth do that when making turnkey apps). I also assume this dump contains a dictionary - and not bytecode.

There is NO kernel or dictionary in 4tH, so you can't do anything interactive (unless you include an interpreter in the code). It compiles straight to bytecode - and is not merged with a kernel, but a runtime.

Hans Bezemer

p da

unread,
Jan 29, 2024, 5:05:30 PMJan 29
to 4tH-compiler
El lunes, 29 de enero de 2024 a las 10:20:58 UTC+1, The Beez escribió:
Hi!

I tend to have several pages around the web (although most are linked in some way) and I know of at least one repository copying a previous version of 4tH. Shame you didn't include the link, 

p da

unread,
Jan 29, 2024, 5:05:30 PMJan 29
to 4tH-compiler
El lunes, 29 de enero de 2024 a las 10:40:57 UTC+1, The Beez escribió:
Is it this one? https://github.com/leonardoce/4th


Yes, it is that one  (I've already replied you previous email asking for the link before seeing this email)

No relation. BTW, the last update was 12 years ago - and still features v0.1. Mine was updated yesterday. So I assume it was some experiment of some Italian guy who abandoned the project 12 years ago. You will find plenty of abandoned Forth experiments on the web. But there is a difference - His 4th features the original Forth architecture. You have to bring the system in a certain stage to allow an image dump, which (I assume) is merged with the kernel. I don't know if the image is stripped before being dumped (some classical Forth do that when making turnkey apps). I also assume this dump contains a dictionary - and not bytecode.

It's true his project seems to be abandoned but I think it's interesting for learning purposes, it is small and includes a transpiler to C, I think you can learn something from his ideas.

Obviously it doesn't compare with you project not only because yours is an active one but also because it's multi platform and has a long collection of libraries, also I find you project very interesting, I just a newbie coming to your project to learn because I was interesting in your proposals and solution, quite different to "classic" forth.  I'm just studying your sources (slowly) and I find them pretty interesting, also I've seen your videos about 4th internals.

I'd like to know the exact meaning of the term "turnkey" applied to forth parlance,  what I understand for turnkey app is a finished software application for a specific domain you sell to someone to solve his specific needs. What this means in forth world?
 

There is NO kernel or dictionary in 4tH, so you can't do anything interactive (unless you include an interpreter in the code). It compiles straight to bytecode - and is not merged with a kernel, but a runtime.


As far as I know (but I'm just in the very begining!) 4th compilation to C seems to be merely a generation of intermediate code (the hx vm) into a plain C array  (an array of word structs, consinting in vm token and value), so the C source code is a hx "asm" code that is a direct translation of 4th words,  the important C function is "exec_4th" that does all the hard work

So, since a direct translation of forth words to hx tokens exists,  I wonder if it would be possible to make a direct translation of forth words to real machine code or at least a translation from hx to machine code, I suppose it should be pretty straigth forward provided you can init an environment to recreate the vm

regards

p da

unread,
Jan 29, 2024, 7:54:35 PMJan 29
to 4tH-compiler
El lunes, 29 de enero de 2024 a las 23:05:30 UTC+1, p da escribió:

It's true his project seems to be abandoned but I think it's interesting for learning purposes, it is small and includes a transpiler to C, I think you can learn something from his ideas.

I'm not suggesting that  *you* can learn from his ideas, but that  *I* (and anyone) can learn from his ideas (and yours). I'm not pretending to tell you what you should do or if your code and project is worse than others, in fact I intended to say the opposite in any case

Sorry, but I don't want to be misunderstood 

regards
 

David Meyer

unread,
Jan 29, 2024, 8:57:40 PMJan 29
to 4th-co...@googlegroups.com
4tH is a great language and tool, and The Beez is a great programmer ;), but I would advise p da or anyone else who wants to get into the Forth programming language more generally to start with Leo Brodie's book Starting Forth (an easy read and available free on-line https://www.forth.com/starting-forth/) and working through the examples with a Forth implementation that comes with an interactive environment (REPL) like Gforth and many others. 4tH is unusual in the Forth world by not having a repl.

--
David Meyer
Takarazuka, Japan
pa...@sdf.org

The Beez

unread,
Jan 30, 2024, 4:46:00 AMJan 30
to 4tH-compiler
Don't worry, I'm Dutch and we can take a lot of heat before we're offended. Nobody is too old to learn! ;-)

I dived somewhat deeper into his project last night (yeah, that's how programmers are) and found out it IS a classic system with a dictionary. And yes, it dumps the entire dictionary if you want to make a turnkey program. AFAIK it works with function pointers. You may be interested to know that I made a function pointer based exec_4th() in the old days. But it was MUCH slower. FICL came to the same conclusion and switched to a switch() based system in later incarnations. A gcc "goto" version can still be built, but where that originally gave me a 25% performance enhancement, lately I found out the switch() based version does better on newer versions of gcc.

Yes, you could easily dump assembly for the words in 4tH. The only thing is that:
(a) It works on a single platform;
(b) Apart from Z80 (yes, I'm that old) I don't know any assembly;
(c) Certain execution tricks may pose a headache.

I think Leonardo's lib could be embedded in other applications (like 4tH or FICL), but it doesn't seem to be especially designed for it. For one, it lacks documentation in that regard.

4tH is written with certain design objectives, and those are listed in the manual. First of all, it should be rock solid (no SEGV's), embeddable in C, and no REPL. Personally, I use a REPL only when either playing around or using it as a calculator. A split window editor with a CLI is fine for me. Edit, save, arrow up, enter. Having a REPL and no way to just compile the thing from the CLI - and finally make an executable was my main beef with Forth. And the screen going black on the ZX Spectrum when you made a mistake, so you had to reload the whole thing was a thing too. Yes, modern Forths are much better in that regard, but 4tH was designed back in 1994 - when the world was quite different.

Frankly, I miss it when design objectives are not make explicit - like Leonardo's 4th. I mean, there are so many Forths, why is yours on earth. ATLAST, FICL, gForth, even JonesForth do much better in that regard, just to name a few.

Hans Bezemer

p da

unread,
Jan 30, 2024, 7:00:54 AMJan 30
to 4tH-compiler
El martes, 30 de enero de 2024 a las 2:57:40 UTC+1, pa...@freeshell.org escribió:
4tH is a great language and tool, and The Beez is a great programmer ;), but I would advise p da or anyone else who wants to get into the Forth programming language more generally to start with Leo Brodie's book Starting Forth (an easy read and available free on-line https://www.forth.com/starting-forth/) and working through the examples with a Forth implementation that comes with an interactive environment (REPL) like Gforth and many others. 4tH is unusual in the Forth world by not having a repl.


yes I've already read a bunch of books about forth during my whole life, and of course Starting Forth is one of them, also thinking forth and others including one for I have special consideration... also I use Forth sometimes and so I'm used to it --that's the reason I wrongly assumed 4tH works like Forth for same words, which is not the case-- but I use forth mainly for playing and hobby ,  now I'm getting interest in learning forth much deeper so I've started learning gforth and other forth variants I find interesting like 4tH and Factor.

 

p da

unread,
Jan 30, 2024, 7:16:30 AMJan 30
to 4tH-compiler
El martes, 30 de enero de 2024 a las 10:46:00 UTC+1, The Beez escribió:
I dived somewhat deeper into his project last night (yeah, that's how programmers are) and found out it IS a classic system with a dictionary. And yes, it dumps the entire dictionary if you want to make a turnkey program. AFAIK it works with function pointers. You may be interested to know that I made a function pointer based exec_4th() in the old days. But it was MUCH slower. FICL came to the same conclusion and switched to a switch() based system in later incarnations. A gcc "goto" version can still be built, but where that originally gave me a 25% performance enhancement, lately I found out the switch() based version does better on newer versions of gcc.

interesting, reading gforth manual I see there's a lot of problems with image files when you pretend to make a compiled portable forth mainly because of the need for relocatable code, in fact it seems gforth make several images with different degree of "relocatability", there's one which is not relocatable at all and the manual states than it's not guaranted to work in different invocatons of gforth... I suppose Leonardo's may have a similar problem 

 
Yes, you could easily dump assembly for the words in 4tH. The only thing is that:
(a) It works on a single platform;
(b) Apart from Z80 (yes, I'm that old) I don't know any assembly;
(c) Certain execution tricks may pose a headache.

I also have a Z80 background strongly focused on ZX Spectrum ;-)   in fact I use abersoft forth and also I'm playing with jupiter ace forth

I see the intended way to get an executable file from a 4tH source is to generate the C source and then compile it,  so there's no need to a compiler to native code for 4tH, but maybe could be interesting as a learning exercise to program a compiler of HX code which may be simple enough for an exercise or even from 4tH source genarating assembly rather than HX, I think I would try just to learn a bit more about 4tH ;-) 


The Beez

unread,
Jan 30, 2024, 12:08:49 PMJan 30
to 4tH-compiler
4tH has got several Factor-like libs:

And I even dabbled a bit with if (for a single program):

include lib/factor.4th                 \ Factor-like support

: bottle                               ( -- xt )
  [:
     [:
        [: . ." bottles of beer on the wall, " ;]
        [: . ." bottles of beer." ;] bi cr
     ;] keep

     s" Take one down, pass it around, " rot
     1- [: <# #s #> ;] [: s" no more" ;] if*
     s"  bottles of beer on the wall!" [: print ;] 2tri@ cr
  ;] ;

: last-verse                           ( -- )
  s" Go to the store and buy some more, "
  s" no more bottles of beer on the wall!" [: print ;] 2bi@ cr ;

: bottles                              ( n -- )
  dup [: bottle keep 1- ;] times drop last-verse ;

99 bottles


4tH has no "relocation" problems since it uses its very own address space - of course - all addresses are virtual. gForth is an entirely different beast though, since it is much closer to the bare metal. An HX file just communicates with the virtual processor. It is not aware of the world outside that sandbox - and that's the fun of it.

4tH's world presents the program just enough resources to let it run comfortably. It's really like Douglas Adams puddle. Unless you size e.g. arrays excessively , nothing is unused - like ordinary Forths which have a set size for the dictionary and doesn't shrink. Note that 4tH was originally designed to be an embeddable language - so in order not to get in the way unnecessarily, as little memory is used as possible, both when compiling or interpreting.

Lots of people have forgotten about that, but I kept all those facilities in place. What most people consider to be 4tH nowadays was actually just a example program. The PP4tH executable was born, because people wanted an integrated experience (like e.g. gcc). Looking back it's fun to see how much the audience decided the course 4tH eventually took.

Hans Bezemer

The Beez

unread,
Apr 7, 2024, 10:04:03 AMApr 7
to 4tH-compiler
Hi!

I added some preprocessor stuff, so you're able to write almost pure Factor - for those features that are supported, of course. It overrides things like [, ], if and unless.

Hans Bezemer
Reply all
Reply to author
Forward
0 new messages