Detecting Multiple Events (was: Case Statements...) 
Author Message
 Detecting Multiple Events (was: Case Statements...)

Quote:

> : ??
>    POSTPONE IF COMP' EXECUTE POSTPONE THEN ; IMMEDIATE

> Unfortunately, this is not standard (yet?).

Why do you think that

: ??
    postpone IF  postpone postpone  postpone THEN ; immediate

won't work? It should do exactly what's desired.

--
Bernd Paysan
"Late answers are wrong answers!"
http://www.*-*-*.com/ ~paysan/



Wed, 08 Mar 2000 03:00:00 GMT  
 Detecting Multiple Events (was: Case Statements...)


Quote:


>> : ??
>>    POSTPONE IF COMP' EXECUTE POSTPONE THEN ; IMMEDIATE
>> Unfortunately, this is not standard (yet?).
>Why do you think that
>: ??
>    postpone IF  postpone postpone  postpone THEN ; immediate
>won't work? It should do exactly what's desired.

I thought so too.  I sent the message, logged off, took a quick trip to
the bathroom, thought again, logged back on and canceled it.

Consider that

POSTPONE IF BL WORD COUNT EVALUATE POSTPONE THEN

is supposed to work.  How can we expect the same result from executing
EVALUATE that we get from executing POSTPONE ?  We can't.  Your (and my
canceled) ?? do something different from Wil's.



Wed, 08 Mar 2000 03:00:00 GMT  
 When to use Macros Was: Re: EVALUATE and Macros

0 [IF] COMMENT

    The major reason for macros is to avoid re-writing code.

END COMMENT [THEN]

ANEW --Pseudo-Random-- DECIMAL

( Why you shouldn't use `MOD` for large ranges of random values. )

1000000 VALUE Samples         ( The number of samples to take. )

2000000000 VALUE Range-Limit  ( The range limit of each sample. )

CREATE Bins  10 CELLS ALLOT   ( 10 is a nice number for bins. )

: Clear-Bins ( -- ) Bins 10 CELLS ERASE ;

( Defined in Tool Belt #2. )
MACRO #DO  " 0 ?DO "
MACRO 'th  " CELLS \ + "
MACRO ::   " ANEW NONCE  : NONCE-DEF "
MACRO ;;   " ;  NONCE-DEF  NONCE "

( The boiler-plate for the tests. )
MACRO CHECK " ::  Clear-Bins  Samples #DO  \ classify  LOOP  Bar-Graph  ;; "

    : classify ( n -- ) Range-Limit 10 /  / 'th Bins  1 SWAP +! ;

    ( Graph has average of 40 characters per line. )
    : Bar-Graph                            ( -- )
        10 #DO  CR
                Range-Limit 10 /  I *  10 U.R  SPACE

                      #DO  ." *"  LOOP
           LOOP CR
    ;

    VARIABLE Randseed
: srand ( u -- ) Randseed ! ;   1 srand

: Method-A ( -- n ) Rand 0 Range-Limit UM/MOD DROP ;
: Method-B ( -- n ) Range-Limit Rand UM* NIP ;

CHECK Method-A
(
         0 *******************************************************
 200000000 *********************************************
 400000000 *************************************
 600000000 *************************************
 800000000 *************************************
1000000000 *************************************
1200000000 *************************************
1400000000 *************************************
1600000000 *************************************
1800000000 *************************************
)
CHECK Method-B
(
         0 ****************************************
 200000000 ***************************************
 400000000 ***************************************
 600000000 ***************************************
 800000000 ****************************************
1000000000 ****************************************
1200000000 ***************************************
1400000000 ****************************************
1600000000 ***************************************
1800000000 ****************************************
)



Wed, 08 Mar 2000 03:00:00 GMT  
 When to use Macros Was: Re: EVALUATE and Macros


Quote:

> > : ??
> >    POSTPONE IF COMP' EXECUTE POSTPONE THEN ; IMMEDIATE

> > Unfortunately, this is not standard (yet?).

> Why do you think that

> : ??
>     postpone IF  postpone postpone  postpone THEN ; immediate

> won't work? It should do exactly what's desired.

No, if you look at the definition of POSTPONE, you'll see it does
something different:

: POSTPONE ( "name" -- ) \ core

    COMP' postpone, ; immediate restrict

(That's the source of POSTPONE in GForth)

It parses and binds at the right time, but compiles instead of
executing the word.

- anton
--
M. Anton Ertl                    Some things have to be seen to be believed

http://www.complang.tuwien.ac.at/anton/home.html



Fri, 10 Mar 2000 03:00:00 GMT  
 When to use Macros Was: Re: EVALUATE and Macros

Quote:

> As you've indicated EVALUATE with ?? is not a problem.

To be more precise: it is not a problem for the paramter; it is a
problem for the rest.

Quote:
> With a text orientation (80 and 1 are not constants here) the following are
> available -- not with the above definition.

>     ?DUP 0= ?? 80

>     DEPTH 0= ?? 1

Yes, so

: ?? POSTPONE IF  BL WORD COUNT EVALAUTE  POSTPONE THEN ; IMMEDIATE

is the best version yet IMO. We can make it even more flexible:

: ?? POSTPONE IF EVALAUTE  POSTPONE THEN ; IMMEDIATE

S" drop 1" ??

You all know that I don't like parsing words.

Quote:
> The simple macros I've defined have severe limitations on arguments, but
> I still find them useful for what I do.  A major virtue to me is the
> simplicity of use.

> Another place where I want text.

> MACRO :: " ANEW NONCE  : NONCE-DEF "  

> MACRO ;; " ;  NONCE-DEF  NONCE "

> MACRO H: " <hex> \ </hex> "

> How would you define those?

In ANS Forth I guess I'll have to use EVALUATE (that's a shortcoming
of ANS Forth). With Gforth extensions I'd write it like this:

: ::
  noname anew lastxt :noname ;

: ;;
  POSTPONE ; execute execute ; immediate

Well, almost ANS, except for NONAME, which makes the next defining
word create an anonymous word, and LASTXT, which produces the XT of
the last word.

Quote:

> > You mean that your

> > MACRO ?? " IF \ THEN "

> > still means the same even if the search order does not contain IF at
> > all? Or if the IF found first in the search order has a completely
> > different meaning (e.g., it could be from a keyword table (represented
> > as wordlist) of a compiler)? I don't think many would agree.
> ***

> If this ?? is in the search-order I expect `?? a-word` to become
> `IF a-word THEN`.  Simple macros expand text into text.

So, with

5 constant IF
6 constant THEN

you expect

?? a-word

to be equivalent to

5 a-word 6

I, OTOH, expect that the IF and THEN in a macro have a meaning, and
not be just a piece of text that could be bound to anything. In
particular, I expect them to mean what they meant at the place of the
macro definition.

- anton
--
M. Anton Ertl                    Some things have to be seen to be believed

http://www.complang.tuwien.ac.at/anton/home.html



Sun, 12 Mar 2000 03:00:00 GMT  
 When to use Macros Was: Re: EVALUATE and Macros


Quote:
(Anton Ertl) writes:

Baden) writes:
>> As you've indicated EVALUATE with ?? is not a problem.
>To be more precise: it is not a problem for the paramter; it is a
>problem for the rest.

...

Quote:
>You all know that I don't like parsing words.

Yes, you and Wil have made your preferences clear, and you both have
tools that work.  It looks to me like a difference in emphasis.  

There's no reason that an end-user shouldn't have text macros that let
him cut down his typing.  His macros should have the same result as
typing in the whole string.  If the meaning of the text changes due to
his actions, that's his lookout.

If you embed text macros deep in the heart of a complex Forth system,
you're opening it up to possible mysterious bugs.  That's an
inappropriate use, unless you carefully think out why it's right this
one time.

So, horses for courses.  When you're solving a problem using a Forth
application as a flexible toolset, text macros can help.  When you're
building a complex compiler to be used by other users, compiler macros
are generally safer.  

I think if you can use a colon definition instead, that's probably
better usually than a compiler macro; compiler macros are mostly for
times where colon definitions don't work.

But an end user might be better off with text macros than colon
definitions, particularly when his job involves parsing text as an end
result.  It's tricky to keep track of compilation state etc with
compiler macros, as we've demonstrated.  When it's all text strings
that get interpreted and that direct further interpretation, it can
still be confusing but it's a more fractal system, the different levels
are the same kind of thing instead of different kinds of things.

Horses for courses.



Sun, 12 Mar 2000 03:00:00 GMT  
 
 [ 37 post ]  Go to page: [1] [2] [3]

 Relevant Pages 
 

 
Powered by phpBB® Forum Software