Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #9864 > unrolled thread

In Forth, is it valid or safe to use the value of 'I' after LOOP?

Started by"Rod Pemberton" <do_not_have@noavailemail.cmm>
First post2012-03-05 16:23 -0500
Last post2012-03-14 20:13 -0400
Articles 20 on this page of 102 — 20 participants

Back to article view | Back to comp.lang.forth


Contents

  In Forth, is it valid or safe to use the value of 'I' after LOOP? "Rod Pemberton" <do_not_have@noavailemail.cmm> - 2012-03-05 16:23 -0500
    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Elizabeth D. Rather" <erather@forth.com> - 2012-03-05 12:11 -1000
      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Elizabeth D. Rather" <erather@forth.com> - 2012-03-05 13:44 -1000
        Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-03-06 11:26 +0000
          Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? rickman <gnuarm@gmail.com> - 2012-03-06 05:42 -0800
            Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-03-06 14:51 +0000
              Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-03-06 10:17 -0600
                Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-03-06 16:49 +0000
                  Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-03-06 11:56 -0600
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-03-06 18:07 +0000
                      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-03-07 04:14 -0600
                        Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-03-12 15:56 +0000
                          Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-03-12 11:23 -0500
                            Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-03-12 16:26 +0000
                              Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-03-12 12:28 -0500
                  Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Rod Pemberton" <do_not_have@noavailemail.cmm> - 2012-03-06 13:36 -0500
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Elizabeth D. Rather" <erather@forth.com> - 2012-03-06 09:18 -1000
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? hwfwguy@gmail.com - 2012-03-06 20:14 -0800
                      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? BruceMcF <agila61@netscape.net> - 2012-03-07 08:22 -0800
                        Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? rickman <gnuarm@gmail.com> - 2012-03-07 10:20 -0800
                          Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? BruceMcF <agila61@netscape.net> - 2012-03-07 11:11 -0800
                        Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? hwfwguy@gmail.com - 2012-03-07 19:07 -0800
                          Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? BruceMcF <agila61@netscape.net> - 2012-03-08 10:11 -0800
                          Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-03-12 09:36 +0000
                      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-03-12 16:01 +0000
                  Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Alex McDonald <blog@rivadpm.com> - 2012-03-06 13:13 -0800
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-03-12 16:04 +0000
                  Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? BruceMcF <agila61@netscape.net> - 2012-03-12 13:40 -0700
              Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? rickman <gnuarm@gmail.com> - 2012-03-07 10:15 -0800
                Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? BruceMcF <agila61@netscape.net> - 2012-03-07 10:33 -0800
                Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Elizabeth D. Rather" <erather@forth.com> - 2012-03-07 17:16 -1000
                Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-03-12 11:18 +0000
                  Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Elizabeth D. Rather" <erather@forth.com> - 2012-03-12 08:01 -1000
            Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? jacko <jackokring@gmail.com> - 2012-03-15 18:59 -0700
              Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Coos Haak <chforth@hccnet.nl> - 2012-03-16 20:36 +0100
      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Rod Pemberton" <do_not_have@noavailemail.cmm> - 2012-03-06 13:33 -0500
        Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Elizabeth D. Rather" <erather@forth.com> - 2012-03-06 09:07 -1000
          Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Rod Pemberton" <do_not_have@noavailemail.cmm> - 2012-03-06 18:20 -0500
            Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Elizabeth D. Rather" <erather@forth.com> - 2012-03-06 14:07 -1000
              Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? John Passaniti <john.passaniti@gmail.com> - 2012-03-06 16:53 -0800
                Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Elizabeth D. Rather" <erather@forth.com> - 2012-03-06 19:26 -1000
              Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Rod Pemberton" <do_not_have@noavailemail.cmm> - 2012-03-10 19:04 -0500
                Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? BruceMcF <agila61@netscape.net> - 2012-03-10 18:44 -0800
                  Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Rod Pemberton" <do_not_have@noavailemail.cmm> - 2012-03-11 17:06 -0400
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? BruceMcF <agila61@netscape.net> - 2012-03-11 17:01 -0700
                Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Paul E. Bennett" <Paul_E.Bennett@topmail.co.uk> - 2012-03-11 12:51 +0000
                  Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? BruceMcF <agila61@netscape.net> - 2012-03-11 14:04 -0700
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Paul Rubin <no.email@nospam.invalid> - 2012-03-11 14:41 -0700
                      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-03-12 05:16 -0500
                      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? BruceMcF <agila61@netscape.net> - 2012-03-12 08:06 -0700
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Paul E. Bennett" <Paul_E.Bennett@topmail.co.uk> - 2012-03-11 21:55 +0000
                      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? BruceMcF <agila61@netscape.net> - 2012-03-11 17:17 -0700
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-03-12 11:00 +0000
                Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Josh Grams <josh@qualdan.com> - 2012-03-11 17:27 +0000
                  Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Paul Rubin <no.email@nospam.invalid> - 2012-03-11 14:12 -0700
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Josh Grams <josh@qualdan.com> - 2012-03-11 21:48 +0000
                      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Elizabeth D. Rather" <erather@forth.com> - 2012-03-11 14:24 -1000
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? BruceMcF <agila61@netscape.net> - 2012-03-11 17:09 -0700
                  Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Rod Pemberton" <do_not_have@noavailemail.cmm> - 2012-03-11 17:34 -0400
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Elizabeth D. Rather" <erather@forth.com> - 2012-03-11 13:18 -1000
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? BruceMcF <agila61@netscape.net> - 2012-03-11 17:32 -0700
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Josh Grams <josh@qualdan.com> - 2012-03-13 13:18 +0000
                      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Rod Pemberton" <do_not_have@noavailemail.cmm> - 2012-03-19 03:36 -0400
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? jacko <jackokring@gmail.com> - 2012-03-15 19:37 -0700
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? BruceMcF <agila61@netscape.net> - 2012-03-16 05:31 -0700
                      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Rod Pemberton" <do_not_have@noavailemail.cmm> - 2012-03-19 03:38 -0400
                        Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? BruceMcF <agila61@netscape.net> - 2012-03-19 11:35 -0700
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Coos Haak <chforth@hccnet.nl> - 2012-03-16 20:39 +0100
                Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Mark Wills <markrobertwills@yahoo.co.uk> - 2012-03-12 12:50 -0700
                Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? jacko <jackokring@gmail.com> - 2012-03-15 19:27 -0700
            Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? BruceMcF <agila61@netscape.net> - 2012-03-07 08:16 -0800
            Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? jacko <jackokring@gmail.com> - 2012-03-15 19:09 -0700
    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? rickman <gnuarm@gmail.com> - 2012-03-05 14:23 -0800
    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? BruceMcF <agila61@netscape.net> - 2012-03-05 17:42 -0800
    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? hwfwguy@gmail.com - 2012-03-06 09:36 -0800
    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-03-08 10:41 +0000
    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Hugh Aguilar <hughaguilar96@yahoo.com> - 2012-03-08 05:38 -0800
      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? hwfwguy@gmail.com - 2012-03-09 10:32 -0800
        Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Hugh Aguilar <hughaguilar96@yahoo.com> - 2012-03-12 19:26 -0700
        Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Rod Pemberton" <do_not_have@noavailemail.cmm> - 2012-03-13 04:15 -0400
    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Coos Haak <chforth@hccnet.nl> - 2012-03-12 01:11 +0100
      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Rod Pemberton" <do_not_have@noavailemail.cmm> - 2012-03-12 19:41 -0400
        Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Elizabeth D. Rather" <erather@forth.com> - 2012-03-12 16:46 -1000
          Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Hugh Aguilar <hughaguilar96@yahoo.com> - 2012-03-12 21:17 -0700
            Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Rod Pemberton" <do_not_have@noavailemail.cmm> - 2012-03-13 05:01 -0400
              Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Mark Wills <markrobertwills@yahoo.co.uk> - 2012-03-13 03:06 -0700
                Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Paul Rubin <no.email@nospam.invalid> - 2012-03-13 12:32 -0700
                  Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Mark Wills <markrobertwills@yahoo.co.uk> - 2012-03-13 14:01 -0700
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Paul Rubin <no.email@nospam.invalid> - 2012-03-14 11:47 -0700
                      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "A. K." <akk@nospam.org> - 2012-03-14 22:13 +0100
                  Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-03-14 03:39 -0500
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Elizabeth D. Rather" <erather@forth.com> - 2012-03-14 06:54 -1000
                      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Hugh Aguilar <hughaguilar96@yahoo.com> - 2012-03-14 16:39 -0700
                      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-03-15 10:49 +0000
                  Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Alex McDonald <blog@rivadpm.com> - 2012-03-14 08:53 -0700
                    Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "A. K." <akk@nospam.org> - 2012-03-14 17:01 +0100
                      Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? George Hubert <georgeahubert@yahoo.co.uk> - 2012-03-14 11:21 -0700
                        Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "A. K." <akk@nospam.org> - 2012-03-14 19:25 +0100
                Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? Hugh Aguilar <hughaguilar96@yahoo.com> - 2012-03-14 16:37 -0700
                  Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Rod Pemberton" <do_not_have@noavailemail.cmm> - 2012-03-15 04:28 -0400
              Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "David N. Williams" <williams@umich.edu> - 2012-03-13 16:19 -0400
                Re: In Forth, is it valid or safe to use the value of 'I' after LOOP? "Rod Pemberton" <do_not_have@noavailemail.cmm> - 2012-03-14 20:13 -0400

Page 3 of 6 — ← Prev page 1 2 [3] 4 5 6  Next page →


#9905

From"Elizabeth D. Rather" <erather@forth.com>
Date2012-03-06 19:26 -1000
Message-ID<-q6dnS8cN8LibMvSnZ2dnUVZ_hOdnZ2d@supernews.com>
In reply to#9901
On 3/6/12 2:53 PM, John Passaniti wrote:
> On Mar 6, 7:07 pm, "Elizabeth D. Rather"<erat...@forth.com>  wrote:
>>> You don't find this mechanism clumsy or obtuse or awkward?
>>
>> Frankly, no. It is consistent with the general Forth philosophy
>> of having many small, simple words rather than a few big,
>> complicated, "smart" ones. A different way of looking at things,
>> but one that has been very successful in the environment for
>> which it was designed.
>
> I would imagine a stronger reason would be that some Forths might
> implement the loop counter on the stack, in a register, or with
> optimization unroll the loop and replace any references to the loop
> counter with a constant.  Having a word to access and modify the loop
> counter would abstract that.

Indeed. I was speaking generally, but you're quite right about 
approaches to this specific issue. Most Forths do use the Return Stack 
for loop values, or optimization as you say. And the resulting looping 
structures are quite fast and flexible to use.

Cheers,
Elizabeth

-- 
==================================================
Elizabeth D. Rather   (US & Canada)   800-55-FORTH
FORTH Inc.                         +1 310.999.6784
5959 West Century Blvd. Suite 700
Los Angeles, CA 90045
http://www.forth.com

"Forth-based products and Services for real-time
applications since 1973."
==================================================

[toc] | [prev] | [next] | [standalone]


#9984

From"Rod Pemberton" <do_not_have@noavailemail.cmm>
Date2012-03-10 19:04 -0500
Message-ID<jjgq7n$4j0$1@speranza.aioe.org>
In reply to#9897
"Elizabeth D. Rather" <erather@forth.com> wrote in message
news:AO6dnfQQq64jO8vSnZ2dnUVZ_hSdnZ2d@supernews.com...
> On 3/6/12 1:20 PM, Rod Pemberton wrote:
> >
> > The Forth looping mechanism *almost* seems perverse to me.
> [...]

Actually, it seems that I wasn't just being grumpy.

I J K in Forth are even more complicated than first appearance.  The naming
I J K is *relative* to the loop.  The naming is not fixed!  In other
languages, the naming is fixed.  I.e., if K is used for the outer most of
three loops, it's referenced as K in the middle and inner most loops also.
They are variables.  For three nested loops in Forth, K in the inner most
loop is J in the middle loop and is I in the outer most loop ... (sigh).

E.g., apparently, this is correct in Forth:

: T  5 0 DO I . 5 0 DO I . J . LOOP LOOP ;

J in the inner loop is I in the outer loop.

E.g., with fixed naming, or variables, or non-relative naming:

: T  5 0 DO J . 5 0 DO I . J . LOOP LOOP ;

J is J everywhere.

fig-Forth only has I not J and K.  fig-Forth uses primitive (low-level)
words, i.e., (DO) and (LOOP) to implement runtime routines for DO and LOOP.
These manipulate items stacked on the return stack, not a variable.  As
primitives, these are not coded in Forth and so they don't push additional
items onto the return stack.  My (DO) and (LOOP) are coded in Forth, and the
interpreter pushes additional items onto the return stack.  Implementing
I J K using the return stack will require working around those other stacked
items.  So far, even with a stack-optimizer, their definitions are becoming
long, complicated, and is still not working ...

However, not using variables for I J K and using the return stack means they
are nameless, and relative too.  In which case, even though I J K are only
specified after the DO, I shouldn't need to know which one is going to be
used when DO occurs.  I think I'll have to come back to this issue at a
later time.  For now, I'm going back to just having 'I' using a variable ...



Rod Pemberton




[toc] | [prev] | [next] | [standalone]


#9992

FromBruceMcF <agila61@netscape.net>
Date2012-03-10 18:44 -0800
Message-ID<da558dfe-b169-42da-ab93-9b4fc8410a23@t15g2000yqi.googlegroups.com>
In reply to#9984
On Mar 10, 7:04 pm, "Rod Pemberton" <do_not_h...@noavailemail.cmm>
wrote:
> I J K in Forth are even more complicated than first appearance.
> The naming I J K is *relative* to the loop.

That's not a complication, its just a difference to what you are used
to. If you want an index-variable loop, then the existing variables,
memory access, BEGIN-UNTIL and BEGIN-WHILE-UNTIL give you all the raw
material you need to define an index-variable loop. It can all be done
in Forth, there are no additional primitives required to support it.

If you want a bounded iterator, DO-LOOP gives you that, and I
generates the current iterator index for you. Its an action, not a
variable, so expecting it to act like a variable does indeed
complicate things ~ but there's nothing *intrinsically* complicated
about it.

[toc] | [prev] | [next] | [standalone]


#10014

From"Rod Pemberton" <do_not_have@noavailemail.cmm>
Date2012-03-11 17:06 -0400
Message-ID<jjj45i$k6m$1@speranza.aioe.org>
In reply to#9992
"BruceMcF" <agila61@netscape.net> wrote in message
news:da558dfe-b169-42da-ab93-9b4fc8410a23@t15g2000yqi.googlegroups.com...
> On Mar 10, 7:04 pm, "Rod Pemberton" <do_not_h...@noavailemail.cmm>
> wrote:
> > I J K in Forth are even more complicated than first appearance.
> > The naming I J K is *relative* to the loop.
>
> That's not a complication, its just a difference to what you are used
> to.
>
> [unrelated issue]
> If you want an index-variable loop, then the existing variables,
> memory access, BEGIN-UNTIL and BEGIN-WHILE-UNTIL give
> you all the raw material you need to define an index-variable loop.
> It can all be done in Forth, there are no additional primitives required
> to support it.
>

That's originally how I implemented 'I' for DO and LOOP.

>
> If you want a bounded iterator, DO-LOOP gives you that, and I
> generates the current iterator index for you. Its an action, not a
> variable, so expecting it to act like a variable does indeed
> complicate things ~ but there's nothing *intrinsically*
> complicated about it.

There is nothing intrinsically complicated about using them.  But, I'm not
using them.  I'm implementing them.  There are other items being stored on
the return stack between storing of I J K loop control parameters.  So, I've
got to be able to access them to implement I J K, while preserving the other
return stack items.  That appears to be a total mess, so far ...  There is
no ROT or ROLL or PICK etc for the return stack.


Rod Pemberton


[toc] | [prev] | [next] | [standalone]


#10026

FromBruceMcF <agila61@netscape.net>
Date2012-03-11 17:01 -0700
Message-ID<c79a03d0-9033-4e6f-84cb-a1666fcfc83b@2g2000yqk.googlegroups.com>
In reply to#10014
On Mar 11, 5:06 pm, "Rod Pemberton" <do_not_h...@noavailemail.cmm>
wrote:

> There is nothing intrinsically complicated about using them.  But,
> I'm not using them.  I'm implementing them.  There are other items
> being stored on the return stack between storing of I J K loop control
> parameters.

But only things you've decided to put there as part of the do-loop,
since the user can't put something on the return stack before calling
the inner loop and expect J and K to work.

So you know how deep the value representing J or K or the pair of
values from which J or K can be computed can be found. Go get them.

> So, I've got to be able to access them to implement I J K, while
> preserving the other return stack items.

Why would a read a certain depth down the return stack upset the other
return stack items?

> That appears to be a total mess, so far ...  There is
> no ROT or ROLL or PICK etc for the return stack.

Nor where there ROT or ROLL or PICK until you implemented them, right?
RROT or RROLL for the return stack would be of no use, since its only
a read you want to do. RPICK or 2RPICK would do the trick, though.

[toc] | [prev] | [next] | [standalone]


#10002

From"Paul E. Bennett" <Paul_E.Bennett@topmail.co.uk>
Date2012-03-11 12:51 +0000
Message-ID<9s3lhiFmbrU1@mid.individual.net>
In reply to#9984
Rod Pemberton wrote:

> "Elizabeth D. Rather" <erather@forth.com> wrote in message
> news:AO6dnfQQq64jO8vSnZ2dnUVZ_hSdnZ2d@supernews.com...
>> On 3/6/12 1:20 PM, Rod Pemberton wrote:
>> >
>> > The Forth looping mechanism *almost* seems perverse to me.
>> [...]
> 
> Actually, it seems that I wasn't just being grumpy.
> 
> I J K in Forth are even more complicated than first appearance.  The
> naming
> I J K is *relative* to the loop.  The naming is not fixed!  In other
> languages, the naming is fixed.  I.e., if K is used for the outer most of
> three loops, it's referenced as K in the middle and inner most loops also.
> They are variables.  For three nested loops in Forth, K in the inner most
> loop is J in the middle loop and is I in the outer most loop ... (sigh).

[%X]

I would tend to suggest that considering any word with three nested 
DO...LOOP's hints at lack of proper factoring. It would certainly cause me 
to ask if it were really necessary or could it be simplified further. Often 
the answer is in the affirmative.

-- 
********************************************************************
Paul E. Bennett...............<email://Paul_E.Bennett@topmail.co.uk>
Forth based HIDECS Consultancy
Mob: +44 (0)7811-639972
Tel: +44 (0)1235-510979
Going Forth Safely ..... EBA. www.electric-boat-association.org.uk..
********************************************************************

[toc] | [prev] | [next] | [standalone]


#10015

FromBruceMcF <agila61@netscape.net>
Date2012-03-11 14:04 -0700
Message-ID<db9d125e-38dd-4d63-a225-022435ac49ca@v7g2000yqb.googlegroups.com>
In reply to#10002
On Mar 11, 8:51 am, "Paul E. Bennett" <Paul_E.Benn...@topmail.co.uk>
wrote:
> I would tend to suggest that considering any word with three nested
> DO...LOOP's hints at lack of proper factoring.

If you are iterating over a three dimensional subspace of a three
dimensional array, it could seem to be appropriate ~ but single
dimensional vectors and two dimensional arrays are far more common.

With a time dimension, I normally prefer a time vector of pointers to
the data at each time increment, so even with data in a three
dimensional space, I can't see why one would need more than three
dimensions.

[toc] | [prev] | [next] | [standalone]


#10020

FromPaul Rubin <no.email@nospam.invalid>
Date2012-03-11 14:41 -0700
Message-ID<7xmx7molwj.fsf@ruckus.brouhaha.com>
In reply to#10015
BruceMcF <agila61@netscape.net> writes:
> If you are iterating over a three dimensional subspace of a three
> dimensional array, it could seem to be appropriate ~ but single
> dimensional vectors and two dimensional arrays are far more common.

The usual description of matrix multiplication involves three
subscripts.  I guess you could factor it out, but is it such a good
thing if you have to?

[toc] | [prev] | [next] | [standalone]


#10038

FromAndrew Haley <andrew29@littlepinkcloud.invalid>
Date2012-03-12 05:16 -0500
Message-ID<k5adnemY6JCSUMDSnZ2dnUVZ_q2dnZ2d@supernews.com>
In reply to#10020
Paul Rubin <no.email@nospam.invalid> wrote:
> BruceMcF <agila61@netscape.net> writes:
>> If you are iterating over a three dimensional subspace of a three
>> dimensional array, it could seem to be appropriate ~ but single
>> dimensional vectors and two dimensional arrays are far more common.
> 
> The usual description of matrix multiplication involves three
> subscripts.  I guess you could factor it out, but is it such a good
> thing if you have to?

Hmm, it sounds reasonable enough to me.  Each element of the result is
the dot product of a row and a column, so you can factor out dot
product as a word.  How would an unfactored version of this help?  I
suppose I tend to think of the factored version of an algorithm as
the norm, and the unfactored version as a special case.

Andrew.

[toc] | [prev] | [next] | [standalone]


#10046

FromBruceMcF <agila61@netscape.net>
Date2012-03-12 08:06 -0700
Message-ID<978dcada-cfbc-417f-ad60-2d851be6aa97@k29g2000yqc.googlegroups.com>
In reply to#10020
On Mar 11, 5:41 pm, Paul Rubin <no.em...@nospam.invalid> wrote:
> BruceMcF <agil...@netscape.net> writes:
> > If you are iterating over a three dimensional subspace of a three
> > dimensional array, it could seem to be appropriate ~ but single
> > dimensional vectors and two dimensional arrays are far more common.

> The usual description of matrix multiplication involves three
> subscripts.  I guess you could factor it out, but is it such a good
> thing if you have to?

I'd rather factor that into the loop selecting the i,j and the loop
producing res_ixj. Factoring out the selection of i & k seems silly,
but factoring out the dot product to produce res_ixj seems reasonable
to me.

Of course, only I and J are CORE ~ in the subspace example, if you
know that you are going to have rectangular cross-sections, factoring
it into an outer word that walks along the z-dimension and then an
inner factor that handles the cross section seems reasonable to me, as
its easier to test ~ setting up a cross section and making sure that
works correctly and only when that tests out setting up a test space
and making sure that works correctly ~ and is more portable besides.

[toc] | [prev] | [next] | [standalone]


#10022

From"Paul E. Bennett" <Paul_E.Bennett@topmail.co.uk>
Date2012-03-11 21:55 +0000
Message-ID<9s4ld4Fjn7U1@mid.individual.net>
In reply to#10015
BruceMcF wrote:

> On Mar 11, 8:51 am, "Paul E. Bennett" <Paul_E.Benn...@topmail.co.uk>
> wrote:
>> I would tend to suggest that considering any word with three nested
>> DO...LOOP's hints at lack of proper factoring.
> 
> If you are iterating over a three dimensional subspace of a three
> dimensional array, it could seem to be appropriate ~ but single
> dimensional vectors and two dimensional arrays are far more common.
> 
> With a time dimension, I normally prefer a time vector of pointers to
> the data at each time increment, so even with data in a three
> dimensional space, I can't see why one would need more than three
> dimensions.

Even with multi-dimensional arrays you are only parsing one dimension at a 
time in the inner loop. Hence, with appropriate factoring why do you need 
more than one DO...LOOP in a words definition? It often makes it simpler to 
factor the loops (inner first, then second, then third) as separate words 
and then there is little need to consider J and K if the more outward index 
were passed on the parameter stack.

-- 
********************************************************************
Paul E. Bennett...............<email://Paul_E.Bennett@topmail.co.uk>
Forth based HIDECS Consultancy
Mob: +44 (0)7811-639972
Tel: +44 (0)1235-510979
Going Forth Safely ..... EBA. www.electric-boat-association.org.uk..
********************************************************************

[toc] | [prev] | [next] | [standalone]


#10029

FromBruceMcF <agila61@netscape.net>
Date2012-03-11 17:17 -0700
Message-ID<628e3758-ff0b-4159-af87-963e8de093c3@p12g2000yqe.googlegroups.com>
In reply to#10022
On Mar 11, 5:55 pm, "Paul E. Bennett" <Paul_E.Benn...@topmail.co.uk>
wrote:
> BruceMcF wrote:
> > On Mar 11, 8:51 am, "Paul E. Bennett" <Paul_E.Benn...@topmail.co.uk>
> > wrote:
> >> I would tend to suggest that considering any word with three nested
> >> DO...LOOP's hints at lack of proper factoring.
>
> > If you are iterating over a three dimensional subspace of a three
> > dimensional array, it could seem to be appropriate ~ but single
> > dimensional vectors and two dimensional arrays are far more common.
>
> > With a time dimension, I normally prefer a time vector of pointers to
> > the data at each time increment, so even with data in a three
> > dimensional space, I can't see why one would need more than three
> > dimensions.
>
> Even with multi-dimensional arrays you are only parsing one dimension
> at a time in the inner loop. Hence, with appropriate factoring why do
> you need more than one DO...LOOP in a words definition?

I don't believe anyone said that you *need* more than one DO...LOOP in
a words definition. However, in a number of cases two are handy, and
in some three may be.

> It often makes it simpler to factor the loops (inner first, then
> second, then third) as separate words and then there is little need
> to consider J and K if the more outward index were passed on the
> parameter stack.

Yes, it often does. If it always did, there'd be no benefit in being
able to nest two DO-LOOPs together.

[toc] | [prev] | [next] | [standalone]


#10039

FromAlbert van der Horst <albert@spenarnc.xs4all.nl>
Date2012-03-12 11:00 +0000
Message-ID<m0rql8.2hr@spenarnc.xs4all.nl>
In reply to#10015
In article <db9d125e-38dd-4d63-a225-022435ac49ca@v7g2000yqb.googlegroups.com>,
BruceMcF  <agila61@netscape.net> wrote:
>On Mar 11, 8:51=A0am, "Paul E. Bennett" <Paul_E.Benn...@topmail.co.uk>
>wrote:
>> I would tend to suggest that considering any word with three nested
>> DO...LOOP's hints at lack of proper factoring.
>
>If you are iterating over a three dimensional subspace of a three
>dimensional array, it could seem to be appropriate ~ but single
>dimensional vectors and two dimensional arrays are far more common.
>
>With a time dimension, I normally prefer a time vector of pointers to
>the data at each time increment, so even with data in a three
>dimensional space, I can't see why one would need more than three
>dimensions.

A few times I had a three-dimensional search space in an Euler problem.
That is hard to handle with separate words.

K can be defined, if you are willing to look inside your Forth.

Hack it out like this:
: test 10 0 DO 1 0 DO 1 0 DO RSP@ 100 DUMP LOOP LOOP LOOP ;

My result:

: K   RSP@ 7 CELLS + @ ;   \ ciforth specific.

or
: K FORTH "RSP@ 6 CELLS + @" EVALUATE PREVIOUS ; IMMEDIATE

Groetjes Albert

--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

[toc] | [prev] | [next] | [standalone]


#10006

FromJosh Grams <josh@qualdan.com>
Date2012-03-11 17:27 +0000
Message-ID<4f5ce06b$0$6709$882e7ee2@usenet-news.net>
In reply to#9984
Rod Pemberton wrote: <jjgq7n$4j0$1@speranza.aioe.org>
> fig-Forth only has I not J and K.  fig-Forth uses primitive (low-level)
> words, i.e., (DO) and (LOOP) to implement runtime routines for DO and LOOP.
> These manipulate items stacked on the return stack, not a variable.  As
> primitives, these are not coded in Forth and so they don't push additional
> items onto the return stack.  My (DO) and (LOOP) are coded in Forth, and the
> interpreter pushes additional items onto the return stack.  Implementing
> I J K using the return stack will require working around those other stacked
> items.  So far, even with a stack-optimizer, their definitions are becoming
> long, complicated, and is still not working ...

Can we see what you're doing?  It shouldn't be complicated.

(DO) pushes the loop parameters (and whatever else) onto the rstack.
This `loop-sys` should be a fixed size.

(LOOP) and (+LOOP) adjust the loop index on the rstack and either branch
back to the beginning of the loop or drop the parameters from the
rstack.

Then I simply extracts the loop index from the parameters on the rstack,
and J and K do the same but from a fixed offset deeper in the rstack.

--Josh

[toc] | [prev] | [next] | [standalone]


#10016

FromPaul Rubin <no.email@nospam.invalid>
Date2012-03-11 14:12 -0700
Message-ID<7xpqciq1tr.fsf@ruckus.brouhaha.com>
In reply to#10006
Josh Grams <josh@qualdan.com> writes:
> Then I simply extracts the loop index from the parameters on the rstack,
> and J and K do the same but from a fixed offset deeper in the rstack.

Wait, does that mean J and K don't work if you're storing temporary
values on the rstack, as is common in Forth?  Yucch, that sort of
defeats their purpose, I would have thought.

I find myself wishing Forth had a bunch of extra stacks for various
purposes.  With enough of them, they could be just one level deep, and
then they could be called "registers".

[toc] | [prev] | [next] | [standalone]


#10021

FromJosh Grams <josh@qualdan.com>
Date2012-03-11 21:48 +0000
Message-ID<4f5d1d9a$0$6355$882e7ee2@usenet-news.net>
In reply to#10016
Paul Rubin wrote: <7xpqciq1tr.fsf@ruckus.brouhaha.com>
> Josh Grams <josh@qualdan.com> writes:
>> Then I simply extracts the loop index from the parameters on the rstack,
>> and J and K do the same but from a fixed offset deeper in the rstack.
>
> Wait, does that mean J and K don't work if you're storing temporary
> values on the rstack, as is common in Forth?

Yeah, DO loops don't play nicely with other rstack usage.  You can put
stuff on the rstack, but you can't use I, J, K, LOOP, +LOOP, LEAVE, or
UNLOOP until you take them off, and so on.

> Yucch, that sort of defeats their purpose, I would have thought.

I suppose it depends how you use them.  I usually find I use a DO loop
*instead* of keeping stuff on the rstack.  But then I don't use nested
loops in the same word much -- I can't remember the last time I used J
at all.

> I find myself wishing Forth had a bunch of extra stacks for various
> purposes.  With enough of them, they could be just one level deep, and
> then they could be called "registers".

Heh.  I believe there are a few systems which put some of this stuff on
other stacks.  It's allowed by the standard, but not too common in
practice.

--Josh

[toc] | [prev] | [next] | [standalone]


#10031

From"Elizabeth D. Rather" <erather@forth.com>
Date2012-03-11 14:24 -1000
Message-ID<XIidnSt4qeHR38DSnZ2dnUVZ_s6dnZ2d@supernews.com>
In reply to#10021
On 3/11/12 11:48 AM, Josh Grams wrote:
> Paul Rubin wrote:<7xpqciq1tr.fsf@ruckus.brouhaha.com>
>> Josh Grams<josh@qualdan.com>  writes:
>>> Then I simply extracts the loop index from the parameters on the rstack,
>>> and J and K do the same but from a fixed offset deeper in the rstack.
>>
>> Wait, does that mean J and K don't work if you're storing temporary
>> values on the rstack, as is common in Forth?
>
> Yeah, DO loops don't play nicely with other rstack usage.  You can put
> stuff on the rstack, but you can't use I, J, K, LOOP, +LOOP, LEAVE, or
> UNLOOP until you take them off, and so on.

The rules are fairly clearly stated in Forth94 3.2.3.3 Return stack:

A program [note, "program" here refers to a theoretically portable 
application] may use the return stack for temporary storage during the 
execution of a definition subject to the following restrictions:

– A program shall not access values on the return stack (using R@, R>, 
2R@ or 2R>) that it did not place there using >R or 2>R;

– A program shall not access from within a do-loop values placed on the 
return stack before the loop was entered;

– All values placed on the return stack within a do-loop shall be 
removed before I, J, LOOP, +LOOP, UNLOOP, or LEAVE is executed;

– All values placed on the return stack within a definition shall be 
removed before the definition is terminated or before EXIT is executed.


>> Yucch, that sort of defeats their purpose, I would have thought.
>
> I suppose it depends how you use them.  I usually find I use a DO loop
> *instead* of keeping stuff on the rstack.  But then I don't use nested
> loops in the same word much -- I can't remember the last time I used J
> at all.

Yes, use of the Return Stack for temporary storage is limited, usually 
only for very short code sequences. It's not considered a real user 
facility; it's a system facility that users can get at on a limited basis.

>> I find myself wishing Forth had a bunch of extra stacks for various
>> purposes.  With enough of them, they could be just one level deep, and
>> then they could be called "registers".
>
> Heh.  I believe there are a few systems which put some of this stuff on
> other stacks.  It's allowed by the standard, but not too common in
> practice.

As I mentioned in another post, some Forths have implemented separate 
loop stacks. The folks I know who have looked at this don't feel the 
extra complexity is justified.

Cheers,
Elizabeth

-- 
==================================================
Elizabeth D. Rather   (US & Canada)   800-55-FORTH
FORTH Inc.                         +1 310.999.6784
5959 West Century Blvd. Suite 700
Los Angeles, CA 90045
http://www.forth.com

"Forth-based products and Services for real-time
applications since 1973."
==================================================

[toc] | [prev] | [next] | [standalone]


#10030

FromBruceMcF <agila61@netscape.net>
Date2012-03-11 17:09 -0700
Message-ID<960c1669-0fb7-4257-be3a-602ff01f5d30@x17g2000yqj.googlegroups.com>
In reply to#10016
On Mar 11, 5:12 pm, Paul Rubin <no.em...@nospam.invalid> wrote:
> Josh Grams <j...@qualdan.com> writes:
>> Then I simply extracts the loop index from the parameters on the
>> rstack, and J and K do the same but from a fixed offset deeper in
>> the rstack.

> Wait, does that mean J and K don't work if you're storing temporary
> values on the rstack, as is common in Forth?

Depends on how you are storing them ~ if they are locals, there's no
problem, and if you are preserving a variable value at the beginning
so it can be restored at the end, it'll still be there, safe and
sound, after the DO-LOOPS are finished.

And of course, if you want to get them with R@ and 2R@ in the middle
of the loop, you don't use a DO-LOOP.

Their still there, safe and sound, after the DO-LOOP is finished.

[toc] | [prev] | [next] | [standalone]


#10017

From"Rod Pemberton" <do_not_have@noavailemail.cmm>
Date2012-03-11 17:34 -0400
Message-ID<jjj5pr$o84$1@speranza.aioe.org>
In reply to#10006
"Josh Grams" <josh@qualdan.com> wrote in message
news:4f5ce06b$0$6709$882e7ee2@usenet-news.net...
> Rod Pemberton wrote: <jjgq7n$4j0$1@speranza.aioe.org>
> > fig-Forth only has I not J and K.  fig-Forth uses primitive (low-level)
> > words, i.e., (DO) and (LOOP) to implement runtime routines for DO and
> > LOOP.  These manipulate items stacked on the return stack, not a
> > variable. As primitives, these are not coded in Forth and so they don't
> > push additional items onto the return stack.  My (DO) and (LOOP) are
> > coded in Forth, and the interpreter pushes additional items onto the
> > return stack.  Implementing I J K using the return stack will require
> > working around those other stacked items.  So far, even with a
> > stack-optimizer, their definitions are becoming long, complicated, and
> > is still not working ...
>
> Can we see what you're doing?  It shouldn't be complicated.
>
> (DO) pushes the loop parameters (and whatever else) onto the rstack.
> This `loop-sys` should be a fixed size.
>
> (LOOP) and (+LOOP) adjust the loop index on the rstack and either branch
> back to the beginning of the loop or drop the parameters from the
> rstack.
>
> Then I simply extracts the loop index from the parameters on the rstack,
> and J and K do the same but from a fixed offset deeper in the rstack.
>

Ok, let's start with what I have working and am currently using.  I have a
single 'I' based DO-LOOP using variables.  DO and LOOP are setup similarly
to fig-Forth (in Forth):

  : DO COMPILE (DO) HERE ; IMMEDIATE
  : LOOP COMPILE (LOOP) COMPILE 0BRANCH , ; IMMEDIATE

Obviously, those compile (DO) and (LOOP) into the definitions.  Those may or
may not need later modification either for I J K or for LEAVE or UNLOOP.
I'm leaving those issues for later on.

Currently, I have IX as the variable for 'I' and LX as the variable for the
loop limit.  'I' and (DO) and (LOOP) are:

  : I IX @ ;
  : (DO) IX ! LX ! ;
  : (LOOP) 1 IX +! LX @ IX @ - 0= ;

That works.  I think I did good in capturing the (undocumented) minus
zero-equal termination condition of Forth loops.  Usually, the manuals
say the termination is greater-than or equal ... (not).

Of course, I first need to convert 'I' to a return stack method prior to
implementing J K .  My first attempt at return stack based (DO) was:

  : (DO) >R >R ;

However, the parameters weren't where I expect them to be.  E.g., supposedly
R@ can be used to get 'I' which indicates it is on the top (or bottom) of
the stack.  In the definition above, it'd be 2nd instead of 1st.  So, next I
added SWAP to (DO), and tried the following:

  : I R@ ;
  : (DO) SWAP >R >R ;
  : (LOOP) >R 1+ >R R@ R> R@ -ROT >R - 0= ;

I believed that should've worked.  However, my interpreter places another
item on the return stack, i.e., when the interpreter enters (DO).  So,
instead of being able to fetch 'I' using R@, I had to do this to I (DO) and
(LOOP) to preserve another stack item:

  : I R> R@ SWAP >R;
  : (DO) R> SWAP ROT >R >R >R ;
  : (LOOP) R> R> 1+ >R R@ R> R@ -ROT >R - 0= SWAP >R ;

I believe (LOOP) to be wrong.  It may just have a trivial error.  However, I
got frustrated at about that point ...  So, I'll revisit it later.

FYI, these are all in Forth as ASCII text as above and being parsed and
defined by my Forth interpreter (which is pre-compiled Forth in C, like
Forth in assembly).  The branches and stack operators are primitive
(low-level) words and map to C routines.

Anyway, that's what I've done or attempted so far.  In a worst case
scenario, I could implement (DO) (LOOP) and 'I' as primitives.


Rod Pemberton



[toc] | [prev] | [next] | [standalone]


#10025

From"Elizabeth D. Rather" <erather@forth.com>
Date2012-03-11 13:18 -1000
Message-ID<-oOdnc1pVftXr8DSnZ2dnUVZ_hydnZ2d@supernews.com>
In reply to#10017
On 3/11/12 11:34 AM, Rod Pemberton wrote:
> "Josh Grams"<josh@qualdan.com>  wrote in message
> news:4f5ce06b$0$6709$882e7ee2@usenet-news.net...
>> Rod Pemberton wrote:<jjgq7n$4j0$1@speranza.aioe.org>
>>> fig-Forth only has I not J and K.  fig-Forth uses primitive (low-level)
>>> words, i.e., (DO) and (LOOP) to implement runtime routines for DO and
>>> LOOP.  These manipulate items stacked on the return stack, not a
>>> variable. As primitives, these are not coded in Forth and so they don't
>>> push additional items onto the return stack.  My (DO) and (LOOP) are
>>> coded in Forth, and the interpreter pushes additional items onto the
>>> return stack.  Implementing I J K using the return stack will require
>>> working around those other stacked items.  So far, even with a
>>> stack-optimizer, their definitions are becoming long, complicated, and
>>> is still not working ...

This is a good example of why it's helpful to learn to program in Forth 
before trying to implement one. Also why it is so often misleading to 
transfer expectations from other languages to Forth.

>> Can we see what you're doing?  It shouldn't be complicated.
...
>
> Ok, let's start with what I have working and am currently using.  I have a
> single 'I' based DO-LOOP using variables.  DO and LOOP are setup similarly
> to fig-Forth (in Forth):
>
>    : DO COMPILE (DO) HERE ; IMMEDIATE
>    : LOOP COMPILE (LOOP) COMPILE 0BRANCH , ; IMMEDIATE
>
> Obviously, those compile (DO) and (LOOP) into the definitions.  Those may or
> may not need later modification either for I J K or for LEAVE or UNLOOP.
> I'm leaving those issues for later on.

So far, so good.

> Currently, I have IX as the variable for 'I' and LX as the variable for the
> loop limit.  'I' and (DO) and (LOOP) are:
>
>    : I IX @ ;
>    : (DO) IX ! LX ! ;
>    : (LOOP) 1 IX +! LX @ IX @ - 0= ;
>
> That works.  I think I did good in capturing the (undocumented) minus
> zero-equal termination condition of Forth loops.  Usually, the manuals
> say the termination is greater-than or equal ... (not).

Well, LOOP terminates on 'equal'. The rule for +LOOP was exhaustively 
discussed just recently. I don't think any of this is "undocumented".

> Of course, I first need to convert 'I' to a return stack method prior to
> implementing J K .  My first attempt at return stack based (DO) was:
>
>    : (DO)>R>R ;

You really, really should put stack comments in your definitions. If you 
did, it would make it much easier to see why this is a problem.

> However, the parameters weren't where I expect them to be.  E.g., supposedly
> R@ can be used to get 'I' which indicates it is on the top (or bottom) of
> the stack.  In the definition above, it'd be 2nd instead of 1st.  So, next I
> added SWAP to (DO), and tried the following:
>
>    : I R@ ;
>    : (DO) SWAP>R>R ;
>    : (LOOP)>R 1+>R R@ R>  R@ -ROT>R - 0= ;
>
> I believed that should've worked.  However, my interpreter places another
> item on the return stack, i.e., when the interpreter enters (DO).

Yes, that's what the Return Stack is for, after all. You need to follow 
and understand the rules in ANS Forth's Section 3.2.3.3 Return stack in 
order to avoid trouble here.

> So,
> instead of being able to fetch 'I' using R@, I had to do this to I (DO) and
> (LOOP) to preserve another stack item:
>
>    : I R>  R@ SWAP>R;
>    : (DO) R>  SWAP ROT>R>R>R ;
>    : (LOOP) R>  R>  1+>R R@ R>  R@ -ROT>R - 0= SWAP>R ;
>
> I believe (LOOP) to be wrong.  It may just have a trivial error.  However, I
> got frustrated at about that point ...  So, I'll revisit it later.
>
> FYI, these are all in Forth as ASCII text as above and being parsed and
> defined by my Forth interpreter (which is pre-compiled Forth in C, like
> Forth in assembly).  The branches and stack operators are primitive
> (low-level) words and map to C routines.
>
> Anyway, that's what I've done or attempted so far.  In a worst case
> scenario, I could implement (DO) (LOOP) and 'I' as primitives.

That is the recommended practice. Trying to manipulate the return stack 
in high-level Forth is very difficult, and will lead to tortured 
definitions and very slow execution.  And one wants loops to be fast!

An alternative would be to add a separate loop stack, which some people 
have done. It adds a whole bunch of extra machinery, though, and will 
still be much slower than primitive loop words.

Cheers,
Elizabeth

-- 
==================================================
Elizabeth D. Rather   (US & Canada)   800-55-FORTH
FORTH Inc.                         +1 310.999.6784
5959 West Century Blvd. Suite 700
Los Angeles, CA 90045
http://www.forth.com

"Forth-based products and Services for real-time
applications since 1973."
==================================================

[toc] | [prev] | [next] | [standalone]


Page 3 of 6 — ← Prev page 1 2 [3] 4 5 6  Next page →

Back to top | Article view | comp.lang.forth


csiph-web