Krishna Myneni <kris…@ccreweb.org> writes:
>On 10/17/26 12:27 AM, Anton Ertl wrote:
>> Krishna Myneni <kris…@ccreweb.org> writes:
>>> IF ... IF ... ELSE ... ELSE ... THEN THEN
>>>
>>> Is this consistent with the standard?
>>
>> Yes, but
>>
>> if A if B else C else D THEN THEN
>>
>> is equivalent to
>>
>> if A if B D else C then then
>> ...
>This does not seem to be what it does in either Gforth or kForth with
>the following example, considering all possible boolean inputs.
>
>: test ( n1 n2 -- )
> IF
> ." n2 is true" cr
> IF
> ." n1 is true" cr
> ELSE
> ." n1 is false" cr
> ELSE
> ." ??" cr
> THEN
> THEN
>;
>
>
>=== Gforth output ===
>\ 1 false false ok
>false false test ok 1
>.s <1> 0 ok 1
>drop
>
>\ 2 false true ok
>
>\ 3 true false ok
>
>\ 4 true true ok
>.s <0> ok
>=== end output ===
>
>With Gforth, nothing is ever printed.
If n2 is false nothing should be printed. And that's the only test
you did. The outer IF...THEN is just a distraction and is not
involved in the IF ... ELSE ... ELSE ... THEN. Here are more
interesting cases:
input:
cr true true test
output:
n2 is true
n1 is true
??
input:
cr false true test
output:
n2 is true
n1 is false
That's exactly what the standard specifies: ELSE ( orig1 -- orig2 )
continues the current execution at the place where orig2 is resolved,
and then resolves orig1, continuing the execution from there. In a
way it swaps between the current execution and the one in the orig.
>=== kForth output ===
>\ 1 false false
> ok
>false false test .s
>
> 0
> ok
>drop
> ok
>
>\ 2 false true
> ok
>false true test .s
>n2 is true
>n1 is false
><empty>
> ok
>
>\ 3 true false
> ok
>true false test .s
>
> -1
> ok
>drop
> ok
>
>\ 4 true true
> ok
>true true test .s
>n2 is true
>n1 is true
>??
><empty>
> ok
>=== end output ===
Judging by these tests, kforth also implements ELSE correctly.
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2026 CFP: http://www.euroforth.org/ef26/cfp.html