=?windows-1255?Q?Re=3A_=5Bcolorforth=5D_if_then_else_-?= =?windows-1255?Q?_but_why_is_there_no_=91else=92_in_colo?= =?windows-1255?Q?rForth=3F?=

Jason Kemp <[email protected]> Tue, 28 Oct 2008 18:21:13 +0000
Newsgroups gmane.comp.lang.forth.colorforth
Message-ID <[email protected]>
Hi Nick
> Nick here:  Sorry for the typo, of course it can't
> work!
>  Here (I hope) is a correct version: this one should
> act
> to yield 1 if
> n is 1 or else yield 2 if n is 2, then for any other
> value of n it should yield n+3.
>
>     ( n)  : word1 dup ( nn) -1 + drop ( n) 0if ( n,f)
> drop -2 ( no ";" ) then ( n/1) dup ( nn/11) -2 + drop
> 0if -1 (still no ";") then  3 + ; 
>
>     But is this what is meant by "if-else-then" ?
Not quite what I mean.  This is so hard to explain and I have spent 
literally hours over this.  I have never given ג€˜ifג€™s any thought in 
other languages.  Who says colorForth is simple?

in this example
   : w1 if w2 then w3 ;
the if the ZF is not set then w2 will execute and then w3 will too.  If 
the ZF is set then only w3 goes.

in this one:
   : w1 if w2 ; then w3 ;
the if the ZF is not set then only w2 will execute.  If the ZF is set 
then only w3.  This is what I think of as an if-then-else (or 
if-else-then in Forths) but you cannot include any more after this as 
anything you put between the ג€˜w3ג€™ and the ג€˜;ג€™ will only execute if the 
ZF is set.

However, if you want to execute w4 directly after as part of w1 then you 
have to do this:
   : w1 if w2 w4 ; then w3 w4 ;
which duplicates some code.

or this:
   : w5 if w2 ; then w3 ;
   : w1 w5 w4 ;
Which takes two words.

So I thought of this:
   : w1 if w2 else w3 then w4 ;
the if the ZF is not set then only w2 will execute.  If the ZF is set 
then only w3.  w4 will execute afterwards regardless. Pygmy has an 
ג€˜elseג€™ that, I think, works like this.

Now I am beginning to see that colorForth encourages all sorts of 
ג€˜factoringג€™ and also that you are not restricted by rules.  Yours is an 
example of this in that what happens if the first ג€˜ifג€™ executes (ie. if 
ZF=1) changes what the second ג€˜ifג€™ does to achieve the effect you want.  
I'm looking for something that makes the two parts (what goes when ZF 
set and what when it isnג€™t) independent and then allows more to follow 
within the same word definition.  My colorForth understanding is so 
limited that I have no examples and I am unable to contrive something 
satisfactory either.  I'm ignoring recursion at the moment too.

What I wonder now though is if I will find, with more experience, that 
there is no need for an if-else-then construct;  I suspect there might 
be no ג€˜elseג€™ because Mr Moore has no need for it.  Also you have not 
needed it yet.
>     Thank you for the explanation of how "if" works.  I
> shall try to digest it then post an example with 2
> words as you suggest - this time with a " ; " betwee. 
> "if" and "then" (if;else;then ?).
>   
Ah, but what I mean is an if-else-then that doesnג€™t require ג€˜;ג€™ to 
prevent execution of the following items.
   : w1 if w2 else w3 then w4 ;
reduces the pair of definitions required if there is no ג€˜elseג€™
   : w5 if w2 ; then w3 ;
   : w1 w5 w4 ;
>      Also, re 'then' has to match an 'if', there are 2
> 'then' matched with only 1 'if' in the definition of '
> +! ' (on block 26 of CF05).  I have noticed such multi
> concatenations elsewhere in CF.  Perhaps the structure
> of 'if' in CF is different from previous Forth,
> allowing one to build multi choice equivalents  of
> 'if;else;then' or 'case' ? 
>   
That example actually has two ג€˜ifג€™s which are nested.  It is easy to 
overlook the first one.  I think that the way ג€˜ifג€™ works precludes 
having multiple ג€˜thenג€™s: the action is to jump to the ג€˜thenג€™ if the ZF 
is set, so having two ג€˜thenג€™s means you can only jump to the first, the 
second being ignored.  I haven't looked at how the compiler works yet, 
but I would expect that this would cause a stack underflow during 
compilation, yet a simple example I tried compiled fine.


Still perplexed,
Jason