More fun with Miser's CASE
  Home FAQ Contact Sign in
comp.lang.forth only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.forth Profile…
 Up
More fun with Miser's CASE         


Author: Ed
Date: Jun 9, 2008 00:47

Earlier this year I presented "Miser's CASE". One thing it lacked
was a "flow through" mode such as found in C's switch statement.

This feature (which some consider a "mis-feature") can easily
be emulated with an addition of a function I call CONTINUE.

But first some background...

When a match occurs in a CASE statement, statements within
the corresponding OF..ENDOF clause are executed, after which
program flow passes straight to ENDCASE .

( 2 -- )
CASE
1 OF ." One " ENDOF
2 OF ." Two " ENDOF
3 OF ." Three " ENDOF
4 OF ." Four " ENDOF
....
ENDCASE

In the above example "Two" is printed. Program flow then passes
to ENDCASE.

Let's now insert a CONTINUE :
Show full article (6.70Kb)
101 Comments
Re: More fun with Miser's CASE         


Author: mark4
Date: Jun 9, 2008 15:46

On Jun 9, 12:47 am, "Ed" invalid.com> wrote:
> Earlier this year I presented "Miser's CASE". One thing it lacked
> was a "flow through" mode such as found in C's switch statement.
>
> This feature (which some consider a "mis-feature") can easily
> be emulated with an addition of a function I call CONTINUE.
>
> But first some background...
>
> When a match occurs in a CASE statement, statements within
> the corresponding OF..ENDOF clause are executed, after which
> program flow passes straight to ENDCASE .
>
> ( 2 -- )
> CASE
> 1 OF ." One " ENDOF
> 2 OF ." Two " ENDOF
> 3 OF ." Three " ENDOF
> 4 OF ." Four " ENDOF
> .... ...
Show full article (7.41Kb)
no comments
Re: More fun with Miser's CASE         


Author: Ed
Date: Jun 9, 2008 20:10

"mark4" mailcity.com> wrote in message news:3abd53f7-a53c-491a-924a-d79c3613c05d@v1g2000pra.googlegroups.com...
> ...
> not a big fan of switch statements in C, not a big fan of case
> statements in forth. not sure i like the way things keep getting more
> complificated just to make forth look more like c :/

I'm the last person who wants to make forth look like C :)

Where is the complication? When one looks at the compile
and run-time code, it couldn't be simpler.

Admittedly TEST2 was a poor (and inefficient) example.
A C programmer could probably come up with something
more convincing!

CONTINUE isn't meant to turn CASE into a C switch.
It is a feature that's been available to the classical Forth
CASE but apparently so far overlooked.

Consider it an extra tool in the toolbox - one that costs
nothing to implement.
no comments
Re: More fun with Miser's CASE         


Author: Ian Osgood
Date: Jun 10, 2008 10:53

On Jun 9, 12:47 am, "Ed" invalid.com> wrote:
> Earlier this year I presented "Miser's CASE".  One thing it lacked
> was a "flow through" mode such as found in C's switch statement.
>
> This feature (which some consider a "mis-feature") can easily
> be emulated with an addition of a function I call CONTINUE.

The real test of this construct is whether it can be used to write
Duff's device, an interleaving of C's while and switch constructs to
implement an efficient partially unrolled I/O port bitbang loop on an
arbitrary length of data.

Here is Tom Duff's own K&R C code for reference:
Show full article (1.26Kb)
no comments
Re: More fun with Miser's CASE         


Author: Andrew Haley
Date: Jun 10, 2008 12:18

Ian Osgood quirkster.com> wrote:
> On Jun 9, 12:47?am, "Ed" invalid.com> wrote:
>> Earlier this year I presented "Miser's CASE". ?One thing it lacked
>> was a "flow through" mode such as found in C's switch statement.
>>
>> This feature (which some consider a "mis-feature") can easily
>> be emulated with an addition of a function I call CONTINUE.
> The real test of this construct is whether it can be used to write
> Duff's device, an interleaving of C's while and switch constructs to
> implement an efficient partially unrolled I/O port bitbang loop on an
> arbitrary length of data.

Yes, but if it can be used to write Duff's device, would that be a
pass of the test, or a failure?

Andrew.
no comments
Re: More fun with Miser's CASE         


Author: m_l_g3
Date: Jun 10, 2008 16:21

> cr .( Are you using gForth Y/N )
> key dup emit cr dup char Y = swap char y = or
> [if]
"theme 2" of http://www.forth.org.ru/~mlg/lections/choose.htm

0 value cs-size
:noname [ depth to cs-size ] ahead [ depth cs-size - to cs-size ]
then ; drop

( n "text}" -- )
: times{ [char] } parse rot 0 ?do 2dup 2>r evaluate 2r> loop 2drop ;
immediate

: cs->r [ cs-size ] times{ postpone >r } ; immediate
: cs-r> [ cs-size ] times{ postpone r> } ; immediate
>
> : CONTINUE
>>r >r >r thens 0 r> r> r> ; immediate
>

This definition of CONTINUE is not what I would expect from its
description.
Show full article (1.00Kb)
no comments
Re: More fun with Miser's CASE         


Author: Ed
Date: Jun 11, 2008 02:35

"m_l_g3" yahoo.com> wrote in message news:a7244667-8b19-4037-b760-950e235838ef@56g2000hsm.googlegroups.com...
>
>> cr .( Are you using gForth Y/N )
>> key dup emit cr dup char Y = swap char y = or
>> [if]
> "theme 2" of http://www.forth.org.ru/~mlg/lections/choose.htm
>
> 0 value cs-size
> :noname [ depth to cs-size ] ahead [ depth cs-size - to cs-size ]
> then ; drop
>
> ( n "text}" -- )
> : times{ [char] } parse rot 0 ?do 2dup 2>r evaluate 2r> loop 2drop ;
> immediate
>
> : cs->r [ cs-size ] times{ postpone >r } ; immediate
> : cs-r> [ cs-size ] times{ postpone r> } ; immediate

Yes. Perhaps the CS wordset needs expanding.
Show full article (1.32Kb)
no comments
Re: More fun with Miser's CASE         


Author: Ed
Date: Jun 11, 2008 21:20

"Ian Osgood" quirkster.com> wrote in message
news:94e34c91-c1dd-423b-9a00-7c6032196cfb@y22g2000prd.googlegroups.com...
On Jun 9, 12:47 am, "Ed" invalid.com> wrote:
> Earlier this year I presented "Miser's CASE". One thing it lacked
> was a "flow through" mode such as found in C's switch statement.
>
> This feature (which some consider a "mis-feature") can easily
> be emulated with an addition of a function I call CONTINUE.

The real test of this construct is whether it can be used to write
Duff's device, an interleaving of C's while and switch constructs to
implement an efficient partially unrolled I/O port bitbang loop on an
arbitrary length of data.

Here is Tom Duff's own K&R C code for reference:
Show full article (2.82Kb)
no comments
Duff's device [Re: More fun with Miser's CASE]         


Author: Ian Osgood
Date: Jun 12, 2008 10:54

On Jun 11, 9:20 pm, "Ed" invalid.com> wrote:
> "Ian Osgood" quirkster.com> wrote in message
>
> news:94e34c91-c1dd-423b-9a00-7c6032196cfb@y22g2000prd.googlegroups.com...
> On Jun 9, 12:47 am, "Ed" invalid.com> wrote:
>
>> Earlier this year I presented "Miser's CASE". One thing it lacked
>> was a "flow through" mode such as found in C's switch statement.
>
>> This feature (which some consider a "mis-feature") can easily
>> be emulated with an addition of a function I call CONTINUE.
>
>     The real test of this construct is whether it can be used to write
>     Duff's device, an interleaving of C's while and switch constructs to
>     implement an efficient partially unrolled I/O port bitbang loop on an
>     arbitrary length of data.
>
>     Here is Tom Duff's own K&R C code for reference:
>
>              send(to, from, count) ...
Show full article (2.19Kb)
no comments
Re: Duff's device [Re: More fun with Miser's CASE]         


Author: Gerry
Date: Jun 13, 2008 04:50

On 12 Jun, 18:54, Ian Osgood quirkster.com> wrote:

[...]
>
> Actually, I've never mastered meta-programming that requires carnal
> knowledge of the flow control internals (besides some of the tricks
> you can play with WHILE-ELSE-THEN). I honestly would like to know how
> one could portably implement Duff's device efficiently in Forth (i.e.
> multiple entry points into the code of a while loop).
>

An interesting challenge, I think This code does what you want
and I think it is in ANS Forth and therefore portable.

(I'm still using Google Groups as I haven't got round
to changing yet so I'm sorry if the "quoted printables"
upsets anyone's news reader)

It must be the most horrible code I have written in Forth but
it should be quite efficient as all the CS-ROLLing takes place
at compile time.

I've used string evaluation to get the send code in-line,
POSTPONE could be used but a lot of them would be needed.
Show full article (3.89Kb)
1 Comment
1 2 3 4 5 6 7 8 9