?EXIT added

32 views
Skip to first unread message

The Beez

unread,
Aug 19, 2017, 9:01:36 AM8/19/17
to 4tH-compiler
Hi 4tH-ers!

One of the fine words we've added lately is ;THEN. It is equivalent to "EXIT THEN" and created to seduce you to create code which is nice to the tail call optimizer, because - admitted - this is quite ugly:

  : hello ." Hello" ; : goodbye ." Goodbye" ;
  : hello? if hello exit then goodbye ;


Why not write?

  : hello ." Hello" ; : goodbye ." Goodbye" ;
  : hello? if hello else goodbye then ;

Well, because of this:

[   0] branch      (2)
[   1] ."          (0)  Hello
[   2] exit        (0)
[   3] branch      (5)
[   4] ."          (6)  Goodbye
[   5] exit        (0)
[   6] branch      (9)
[   7] 0branch     (8)
[   8] branch      (0)
[   9] branch      (3)


This version - functionally completely equivalent - uses two instructions more + the added function call overhead - TWICE!

[   0] branch      (2)
[   1] ."          (0)  Hello
[   2] exit        (0)
[   3] branch      (5)
[   4] ."          (6)  Goodbye
[   5] exit        (0)
[   6] branch      (11)
[   7] 0branch     (9)
[   8] call        (0)
[   9] branch      (10)
[  10] branch      (3)
[  11] exit        (0)

But again, the first version feels somehow wrong. This syntactic sugar makes it a bit more palatable, because it resembles ColorForth:

  : hello ." Hello" ; : goodbye ." Goodbye" ;
  : hello? if hello ;then goodbye ;

But that on its turn made me realize you could turn a frequent jargon like this:


  if exit then

Into something as ugly as this:


  if ;then

Ok, so you got it: ?EXIT. Be happy now. ;-)

Hans Bezemer



Reply all
Reply to author
Forward
0 new messages