home .. forth .. colorforth mail list archive ..

Re: [colorforth] if then else - but why is there no ‘else’ in colorForth?


Quoting Jason Kemp <jason.kemp@xxxxxxxxxxxxxxxx>:

>... " Hi Nick,
>... " 
>... " [snip]  ‘then’ first copies the
>... " top of return stack to 
>... " list (don’t know why—for later optimisation?)
>...  

Nick here:  The optimization is explained in Karig's
Commentary (see
Compiler, and follow the leads to 'list' 'then' and
'semi').  Inclusion of TORS in 'list' by 'then' allows 
'semicolon ;' to make an 'if' recursive by jumping back
on its calling word.  

>... " [snip]
>... " So I can create an ‘else’ macro as:
>... " : else eb 2, here swap then ;
>... " 
>... " eb is dark green, then is cyan.  eb is the
>... " opcode for JMP rel8. This can 
>... " then be used:
>... " 
>... " : test w1 if w2 else w3 then w4 ;
>... " 
>... " w1 goes first, then either w2 is executed and
>... " then w4 or w3 and then 
>... " w4.  This seems to work, but there must be a
>... " reason why Mr Moore didn't 
>... " put an ‘else’ in.  Any idea why?  ...
>... " 
>... " Jason
>... " 
 
Nick here:  Perhaps the word 'Jump' (together with the
ability to concatenate if;then;then; makes other multi
choice constructions unnecessary?  Karig explains
'jump' also.  I have not tried it, but it would seem to
act like 'case' ie choose one out of any number of
options by setting n.

   Anyway, for what it's worth, here is my two-word
if;else;then.  Word1 returns 1 if n=1, else 2 if n=2,
else 3 for any other value of n.  Then word2 adds 10
so, whatever the value of n, the bottom line is limited
to 11, 12 or 13.  

  ( n)  : word1  dup -1 + ( n n/0) drop 0if ( n=1)
; then ( n) dup -2 + drop 0if ( n=2) ; then ( n) drop 3
; 

  ( n)  : word2  word1 10 + ;  

Caritas, 

Nick 

---------------------------------------------------------------------
To unsubscribe, e-mail: colorforth-unsubscribe@xxxxxxxxxxxxxxxxxx
For additional commands, e-mail: colorforth-help@xxxxxxxxxxxxxxxxxx
Main web page - http://www.colorforth.com