gforth
[Top][All Lists]
Advanced

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

Re: [gforth] CASE syntax?


From: Anton Ertl
Subject: Re: [gforth] CASE syntax?
Date: Sun, 7 Mar 2010 15:11:54 +0100
User-agent: Mutt/1.5.18 (2008-05-17)

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?).

> The best example I could find in the source tree was:
> 
> : address@hidden ( n -- ) \ gforth compile-f-fetch-local
>  case
>     0        of postpone address@hidden endof
>     1 floats of postpone address@hidden endof
>    ( otherwise ) dup postpone address@hidden ,
>  endcase ;
> 
> And clearly there is no ( ) after ENDCASE.

Even if you don't follow the code, you can derive from the stack
effect ( n -- ) that the stack state at the end of the word is "( )",
i.e, stack items below n are not used or changed in this word.

> I cant say whether there is ( n ) after default-code because I am
> completely confused by this Forth code. It is far too advanced for me
> to understand completely.

It can be analysed, though: POSTPONE address@hidden does not change the
stack, because address@hidden has default compilation semantics, so that
phrase can be ignored wrt the stack effect, leaving

DUP ,

So if we have ( n ) at the start, we get ( n n ) after the DUP, and 
( n ) again after the ",".

Similarly, the other POSTPONE ... phrases in this definition also have
no stack effect and can be ignored as far as the stack depth at this
word's run-time is concerned, making this program easy to analyse.

Anyway, I am adding an example that does not need that kind of
reasoning, but uses well-known words instead (inspired by this
thread):

: .spell ( n -- )
 case
   0 of ." zero " endof
   1 of ." one "  endof
   2 of ." two "  endof
   dup .
 endcase ;

- anton




reply via email to

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