Factor

17 views
Skip to first unread message

The Beez

unread,
Jun 28, 2021, 1:14:50 PM6/28/21
to 4tH-compiler
Hi 4tH-ers!

After all the FP stuff I've done lately, I felt like picking up an old, abandoned project of fine, which was a library allowing to use some Factor constructs. Before I tried to recode them in plain 4tH, but the deeper in the stack I got, the harder it got. So I let it go.

After all, I found out I was doing it wrong altogether. And I found I could recode Factor in Factor like code - which was close to ripping it from the manual. Then it went pretty quickly.

Note you got to remember 2OVER, PICK and 2NIP are not what they seem to be. PICK is THIRD, 2OVER is about THIRD THIRD and 2NIP is NIP NIP. EXECUTE is called CALL. But once you got that, it's easy sailing.

You know NOTHING gets it without testing, so that's what I'm doing now - making a formal test program. However, in the meanwhile I tried to do some programming. It's quite hard to wrap your head around, because you got quotations within quotations - which requires quite another set of thought.

Otherwise, there are things in Factor, which you cannot emulate in 4tH - like lists, ranges or quotation manipulation (remembers me of self modifying code). But with a little trouble I got this going:

include lib/factor.4th

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

     1- [: <# #s #> ;] [: s" no more" ;] if*
     s" Take one down, pass it around, " 2swap
     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 -- )
  0 swap ?do i bottle call -1 +loop last-verse ;

99 bottles

Note: THIS IS ONLY 99 BOTTLES OF BEER. You can compare the actual version at Rosetta code.

So yes, you got absolutely less stack acrobatics, but I don't find it a whole lot easier. Especially debugging is a horror. I also find the documentation lacking, since there is no tutorial to speak of. Only some kind of "tour". Stack comments are complicated. 

And finally it's not "stack friendly". E.g. EACH-INTEGER expands its range COMPLETELY in one go - so if you do 512 EACH-INTEGER you'll crash the stack.

But it's nice to play around with since it is a cool concept. And I can imagine me writing a subroutine or two in this dialect.

Lemme know what you think.

Hans Bezemer

Ron K. Jeffries

unread,
Jun 28, 2021, 1:56:26 PM6/28/21
to 4th-co...@googlegroups.com
It would be useful to know why Factor is of interest to you.

--
You received this message because you are subscribed to the Google Groups "4tH-compiler" group.
To unsubscribe from this group and stop receiving emails from it, send an email to 4th-compiler...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/4th-compiler/d3ebe9fa-f0f1-4a54-bbbc-78554dd9088dn%40googlegroups.com.

The Beez

unread,
Jun 28, 2021, 4:59:54 PM6/28/21
to 4tH-compiler
Hi Ron!

Nothing but curiosity.  AFAIK no other Forth has ever ventured into this area. 

First, how far can I take this? As I've proven to myself - quite far. Most of the library code is vanilla Factor (with some syntax changes, of course). Second, Factor is often presented as "the future of Forth". But is it really that much simpler? My first impression is: it just moves the complexion elsewhere - from stack acrobatics to nested quotations. Or is it completely clear to you what this code does?

[: [: 2dip ;] dip dip ;] dip call

I have a hard time wrapping my head around this. DIP moves one item out of the way, executes the quotation and then puts the item back on the stack. 2DIP does it for TWO items. CALL = EXECUTE. Need I say more. The complexity reminds me of using the preprocessor where you have to keep several layers of abstraction in check at the same time.

But I have no plans to "Factorize" 4tH - not even remotely, if that concerns you.

Hans Bezemer

The Beez

unread,
Jun 29, 2021, 1:14:08 PM6/29/21
to 4tH-compiler
Anyways, the code has been properly tested and is now in SVN. https://sourceforge.net/p/forth-4th/code/1333/

Hans Bezemer

Reply all
Reply to author
Forward
0 new messages