gforth
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [gforth] CASE syntax?


From: Terrence Brannon
Subject: Re: [gforth] CASE syntax?
Date: Sun, 7 Mar 2010 14:30:24 -0500

On Sun, Mar 7, 2010 at 9:11 AM, Anton Ertl
<address@hidden> wrote:
> On Sat, Mar 06, 2010 at 05:54:56PM -0500, Terrence Brannon wrote:
>> The documented CASE syntax:
>> http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Selection.html#Selection
>>
>> terminates with
>>
>>  ( n ) default-code ( n )
>>      ENDCASE ( )
>>
>> but I grepped the source tree and
>> 1 - nowhere after default-code is ( n ) seen.
>> 2 - nowhere after ENDCASE is ( ) seen.
>
> This makes me wonder whether you groked that these are stack comments,
> i.e., comments that describe the stack at this point.  Looking at the
> tutorial I see that such comments are mentioned cursorily in the
> Comments Tutorial, and with an example in the Stack-Effect Comments
> Tutorial.  So I have a hard time seeing where the documentation could
> be improved to avoid that misunderstanding (or maybe it was about
> something else?).
>

no, you're right. I guess I was just taking what I saw literally
without thinking clearly.

but the documentation should emphasize the rather unusual and
inconsistent dropping of the case value a bit more. You wrote a
concise summary in one sentence of it. But let's make your example
emphasize the counter-intuitive and inconsistent auto-DROP actions of
the CASE:

: number2name ( n -- s )
 CASE
  0 OF ( auto-drop ) s" zero" ENDOF
  1 OF ( auto-drop ) s" one"  ENDOF
  2 OF ( auto-drop ) s" two"  ENDOF
  ( otherwise )  s" no name for number found"
 ROT ( rotate n to front so it can be auto-dropped ) ENDCASE ;

the auto-drop occurs before the value you wish to leave on the stack
in the OF .. ENDOF clauses
the auto-drop occurs AFTER the value you wish to leave on he stack in
the default clause

it's also useful to move the ROT down to the ENDCASE so that each case
expression is focused on relevant value generation, not stack dancing
to prepare for an auto-drop




reply via email to

[Prev in Thread] Current Thread [Next in Thread]