gforth
[Top][All Lists]
Advanced

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

Re: [gforth] If then else in interpreted mode


From: John Allsup
Subject: Re: [gforth] If then else in interpreted mode
Date: Thu, 02 May 2013 17:12:04 +0100
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:17.0) Gecko/20130328 Thunderbird/17.0.5

Thanks,

I'm quite new to Forth itself, so haven't learned the various simplifying shortcut words. Also, I may tend not to use them when illustrating an idea to ask others about it. This was motivated by my reading the gforth manual 3.16 'In Forth you can use control structures only inside colon definitions.' and thinking about ways round this.

Of the two simplifying suggestions, I note that abs would at some point
in its implementation need to do a branch since it does not know that the result of 0<> is either -1 0 or 1.

The thought was that where you have a construct such as:
BEGIN
...a
<cond>
WHILE
...b
REPEAT
you can factor the ...a, ...b and <cond> bits into words and then
define a control structure word that you feed execution tokens.

This is much like what happens in C where you have either
        if( a == b ) statement;
or
        if( a == b ) { block; of; statements; }
and here you achieve this by giving a block a name.  Thus
        if( a == b ) WORD;
and factoring as WORD := { block; of; statements; }.

The jif example and the : fy pick execute ; seemed to give an easy example, so I thought I'd throw it onto the list as a way of saying hi and getting feedback. ( The nice thing about fy defined this way is that you can define various conditional execution words and name them after the roots of your favourite adjectives, for example fluf puf buf duf -- it make seem and in fact be unprofessional, but cute and cuddly names are far easier to remember intuitively if you have a mind like mine, for the reason that concepts learned as a young child are more efficiently represented in my mind than abstract stuff learned at uni. )

As an aside, if we have already defined
: a 1 1 + ;
and
: b 2 a * 3 a * + ;
is there a way to get at the definition of b (like with see) so that
one could search for a's in the definition, expand some or all of them
with their current definition and turn the result into a new word, say c. In this example, a function that, given "a" and b produces the effect of
: c 2 1 1 + * 3 1 1 + * + ;
and indeed also if we had defined
: d 4 3 + ;
being able to produce (i.e. search and replace)
: e 2 d * 3 d * + ;
and
: e 2 4 3 + * 3 4 3 + * + ;

(I'm attempting to present trivial examples first so as not to have real world examples's real world purposes distracting from what is essentially a pure theoretical question.)

John


On 01/05/2013 23:57, Marcos Cruz wrote:
En/Je/On 2013-05-01 23:15, John Allsup escribió / skribis / wrote :

Hi John, welcome!

As you show, execution tokens (on the stack, in lists or any other data
structure), can be used to make decisions based on calculations. That's
a powerful feature of Forth.

     : jif 0 = if 0 else 1 then ;

A simpler alternative:

        : jyf 0<> abs ;

Usually in Forth a calculation can be used instead of a condition.

so as to have two nicely named words: jiffy and jemmy that do the
work of interpret time conditional execution.

You can use also '[if]', '[else]' and '[then]'. They work quite
differently than 'if', 'else' and 'then', but they provide the same
syntax.

As to taking this much further, we hit a place where it is really
nice to have more stacks (since you ideally want an operator stack
to do the picking from rather than the data stack

There are several Forth extra stack tools you can use. I think they are
not hard to find on the web. Anyway the simpler implementations need
only a few lines of code.

Cheers,

Marcos



--
: jda ." John D. Allsup" ." woz 'ere" ." yeah!" ; immediate jda
: website ." allsup.co" and ." chalisque.com" ; 1 1 website if
: blog ." deardiary.chalisque.org" ; immediate then
( no, I haven't checked whether this actually runs... )



reply via email to

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