Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #45360

Re: The extended Eaker-Pelc CASE as general control structure

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject Re: The extended Eaker-Pelc CASE as general control structure
Date 2016-02-08 12:59 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2016Feb8.135932@mips.complang.tuwien.ac.at> (permalink)
References <2016Feb6.191548@mips.complang.tuwien.ac.at> <3c828c96-5a9a-4e36-affe-d948d2c1a5d7@googlegroups.com>

Show all headers | View raw


JennyB <jennybrien@googlemail.com> writes:
>THe obvious way to do this (and the way you have in fact done it) means that CONTOF and ENDOF do not need to be enclosed in a CASE statement (they are simply Continue and Break respectively in any uncounted loop), and OF can be used instead of DUP IF DROP

OF could be used instead of OVER = IF DROP.

And yes, you can use these implementations of ENDOF and CONTOF without
CASE.  I am not sure it is a good idea, though.  We can do everything
with BEGIN AGAIN UNTIL IF AHEAD THEN CS-ROLL, but we usually choose a
more restricted approach (some more restricted, some less restricted);
I guess most programmers are not that keen on thinking about the
control-flow stack and in terms of conditional and unconditional
branches.  They rather prefer to think that, e.g., ENDOF jumps behind
ENDCASE, never mind the control-flow stack.  And I think that using
ENDOF, CONTOF, and NEXT-CASE as intended gives one this simplified,
more abstract world-view.

>
>To completely integrate Eaker's Case into the control structure wordset we should expose the factor UNCASE, such that:
>
>       : ENDCASE POSTPONE DROP POSTPONE UNCASE ; IMMEDIATE 

Your UNCASE is VFX's END-CASE.  My thinking is that I write 0 ENDCASE
when I need that.

>That gives us simple ANDifs
>
>   CASE 
>     cond1 IF
>      ...
>     cond n IF
>
>     action
>   UNCASE

In my implementation, the ENDOFs are needed to deal with the dest that
allows CONTOF and NEXT-CASE to work.

> And Wil Baden's short-circuiting ORIF and AND IF
>
>  If cond1 or (cond2 and cond3)
>
>   CASE
>    cond1 FALSE OF
>    cond2 TRUE OF
>    cond3
>   UNCASE
>    IF

Same here.  Also, if we are going for representing conditions by using
control-flow, I think we should also eliminate the IF at the end and
the flag that controls it.  Maybe something along the lines of

MULTI-COND
  cond1 -COND
  cond2 +COND
  cond3 +COND
  code-when-all-satisfied
OTHERWISE
  code-when-one-cond-failed
THEN

A disadvantage of control-flow-based approaches for combining
conditions is that we get more control flow paths to test.  For
multi-exit loops with different exit actions that's unavoidable, but
for this kind of code it is not; however, this disadvantage may be
balanced by simpler data stack handling (only very short-lived flags
on the data stack).

How would one implement this in the context of CASE?

CASE
  cond1 ?OF ENDOF
  cond2 0= ?OF ENDOF
  cond3 0= ?OF ENDOF
  action-when-all-satisfied
AHEAD [ SAVE-ORIG ] ENDCASE
  actions-when-one-cond-failed
[ RESTORE-ORIG ] THEN

We are not quite there.  More generally, if we want to use the
control-flow method of dealing with multiple conditions, we would need
to be able to express that one (or both?) of the two resulting actions
are again conditions on whose result we want to jump again to one of
two actions, which may again be conditions.  I don't have a good idea
how to express this nicely.

>It works here, but more generally:
>
>     begin
>       cond1 if action contof
>       ...
>       condn while action repeat
>     final_action
>
>Maybe Continue is a better name for Contof?

CONTINUE does not make it clear that it concludes the OF/IF part; it
certainly does not in C.

>The options I see (provided OF is available outside CASE)
>
>CASE ... UNCASE is used only to resolve forward branches. The basic Eaker Case is unchanged. ENDOF is a synonym for ELSE

In my implementation it is not.

>New words:  UNCASE CONTINUE BREAK (for the new use of Endof)  
>
>New structure is (CASE) BEGIN (IF/OF...CONTINUE/BREAK) AGAIN/REPEAT/UNTIL (THEN/UNCASE)

That's also an interesting variant.  So we would either write things as

CASE ... OF/IF ... ENDOF ... OF/IF ... ENDOF ... ENDCASE

or 

CASE BEGIN ... OF/IF ... BREAK/CONTINUE ... OF/IF ... BREAK/CONTINUE ... AGAIN ENDCASE

NEXT-CASE would become unnecessary, but you introduce the need for
BREAK.  CONTINUE is just the same as CONTOF, and IF is the same as
?OF, and UNCASE is 0 ENDCASE.  So, in word-counting mode, we have not
reduced the number of words necessary.  Is it clearer than the
extended CASE I suggested?

>Alternatively:
>
>CASE always includes an implicit BEGIN, which UNCASE removes.  ENDOF is a synonym of BREAK. This is compatible with the above, but also allows NEXT-CASE/UNCASE/ENDCASE without an explicit BEGIN.

Yes, that's what I proposed and implemented, modulo name variations.

- 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: http://www.forth200x.org/forth200x.html
   EuroForth 2015: http://www.rigwit.co.uk/EuroForth2015/

Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-06 18:15 +0000
  Re: The extended Eaker-Pelc CASE as general control structure JennyB <jennybrien@googlemail.com> - 2016-02-07 03:57 -0800
    Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 12:59 +0000
      Re: The extended Eaker-Pelc CASE as general control structure JennyB <jennybrien@googlemail.com> - 2016-02-08 09:48 -0800
        Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 18:10 +0000
          Re: The extended Eaker-Pelc CASE as general control structure JennyB <jennybrien@googlemail.com> - 2016-02-09 14:02 -0800
            Short-circuit evaluation (was: The extended Eaker-Pelc CASE ...) anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-10 16:10 +0000
  Re: The extended Eaker-Pelc CASE as general control structure "WJ" <w_a_x_man@yahoo.com> - 2016-02-07 14:32 +0000
  Re: The extended Eaker-Pelc CASE as general control structure "WJ" <w_a_x_man@yahoo.com> - 2016-02-07 14:53 +0000
    Re: The extended Eaker-Pelc CASE as general control structure Albert van der Horst <albert@spenarnc.xs4all.nl> - 2016-02-07 15:56 +0000
      Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-07 18:11 +0000
        Re: The extended Eaker-Pelc CASE as general control structure "HAA" <someone@microsoft.com> - 2016-02-08 12:04 +1100
          Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-08 10:29 +0000
            Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 11:00 +0000
              Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-08 12:28 +0000
                Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 15:26 +0000
                Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-08 16:16 +0000
                Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 16:37 +0000
                Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 16:21 +0000
            Re: The extended Eaker-Pelc CASE as general control structure "HAA" <someone@microsoft.com> - 2016-02-11 21:12 +1100
              Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-11 10:46 +0000
      Re: The extended Eaker-Pelc CASE as general control structure JennyB <jennybrien@googlemail.com> - 2016-02-07 12:33 -0800
        Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-08 10:38 +0000
  Re: The extended Eaker-Pelc CASE as general control structure "HAA" <someone@microsoft.com> - 2016-02-08 12:07 +1100
    Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 11:11 +0000
      Re: The extended Eaker-Pelc CASE as general control structure Albert van der Horst <albert@spenarnc.xs4all.nl> - 2016-02-08 14:49 +0000
      Re: The extended Eaker-Pelc CASE as general control structure "HAA" <someone@microsoft.com> - 2016-02-09 20:31 +1100
        Re: The extended Eaker-Pelc CASE as general control structure hughaguilar96@gmail.com - 2016-02-09 12:38 -0800
      Re: The extended Eaker-Pelc CASE as general control structure "WJ" <w_a_x_man@yahoo.com> - 2016-02-11 14:23 +0000
        Re: The extended Eaker-Pelc CASE as general control structure "WJ" <w_a_x_man@yahoo.com> - 2016-02-11 19:45 +0000

csiph-web