Page 2 of 2

Re: Extra stacks

Posted: Tue Oct 06, 2020 9:13 pm
by JimBoyd

My UNLOOP is a primitive. It's only nine bytes.

Code: Select all

CODE UNLOOP  ( S: -- )
             ( R: ADR N1 N2 -- )
   PLA,  PLA,
   PLA,  PLA,
   PLA,  PLA,
   NEXT JMP,
END-CODE

Following a suggestion from Garth Wilson, my do loops have three loop parameters. ADR is the address that LEAVE or ?LEAVE ( if present) and LOOP or +LOOP branch to.
UNLOOP is from the Ansi Forth standard. Note that it doesn't actually exit the word the loop is in nor does it leave the loop. It just discards the loop parameters. This is so a word can be exited from within a do loop or within a nested do loop. There needs to be an UNLOOP for each level of loop nesting.
Here are two hypothetical examples:

Code: Select all

: SOMEWORD  ( -- N1 )
   10 0 DO
      I DO_SOMETHING_WITH_I
      I SOMETEST
      IF  I UNLOOP EXIT  THEN
   LOOP
   TRUE ;

: ANOTHERWORD  ( -- N1 N2 )
   10 0 DO
      25 0 DO
         I J DO_SOMETHING
         I J SOME_OTHER_TEST
         IF  I UNLOOP I UNLOOP EXIT  THEN
      LOOP
   LOOP
   TRUE TRUE ;


Re: Extra stacks

Posted: Thu Oct 08, 2020 11:59 pm
by SamCoVT
JimBoyd wrote:
Has anyone else implemented an extra stack ( or more) in Forth? How did it affect your programming?
Well... now I have. I ended up with:

Code: Select all

( Auxiliary Stack - 2020-10-05        SamCoVT )
( Based on 6502.org discussion.  License: CC0 )

( Create the stack.  AUXTOS points to just *after* TOS )
CREATE AUXSTACK 16 CELLS ALLOT
VARIABLE AUXTOS   AUXSTACK AUXTOS ! ( Start with empty stack. )

: .AS   ( Print the aux stack in Tali2 format )
   ( Print <#items>  at the beginning. )
   ." <"   AUXTOS @ AUXSTACK -   1 CELLS /
   0 <# #S #> TYPE ( Number with no trailing space )
   ." > "
   ( Print the stack values with TOS on the right. )
   AUXSTACK               ( Start at the bottom of the stack. )
   BEGIN DUP AUXTOS @ < WHILE
      DUP @ .   1 CELLS + ( Print and move up the stack. )
   REPEAT  DROP ;

: >A   ( S: n  A: -- S:  A: n )
   AUXTOS @ !  1 CELLS AUXTOS +! ;

: A>   ( S:  A: n -- S: n  A: )
   AUXTOS @ AUXSTACK = IF
      ." AUXSTACK EMPTY"
   ELSE
      -1 CELLS AUXTOS +! AUXTOS @ @
   THEN ;

: A@   ( S:  A: n -- S: n  A: n )
   AUXTOS @ AUXSTACK = IF
      ." AUXSTACK EMPTY"
   ELSE
      AUXTOS @   1 CELLS -   @
   THEN ;

: DUP>A   ( S: n  A: -- S: n  A: n )
   DUP >A ;

: 2>A   ( S: d  A: -- S:  A: d )
   SWAP >A >A ;

: 2A>   ( S:  A: d -- S: d  A: )
  A> A> SWAP ;
I've tested this on Tali2 and Gforth and it looks like it works. I learned the following unrelated things about forth in the process:

>= isn't a standard word?!?! Once I realized I was printing my stack backards, I also discovered that I didn't need this word and < would do just fine. I'm not sure how I didn't run into this before.

. prints a space after the number. If you don't want that, you get to learn about <# and #S and #>

I think this will be handy for when I'm working on a new word that uses >R and R> and R@, like in JimBoyd's example, and I want to try it out a line at a time. I especially like how .AS lets you see what's going on in the pretend return stack. I don't know as I'll use it outside of this use case, but it certainly looks handy for debugging words that use the return stack.

Edit - uppercased all of the forth words in the code for readability

Re: Extra stacks

Posted: Fri Oct 09, 2020 8:34 pm
by JimBoyd

How about replacing

Code: Select all

0 <# #S #> TYPE

with

Code: Select all

1 .R


Re: Extra stacks

Posted: Sat Oct 10, 2020 2:50 pm
by SamCoVT
JimBoyd wrote:

Code: Select all

1 .R
That's much better. I didn't realize that .R doesn't put a space after the number - that's useful to know and I think it makes the code more readable. I'm not too sad that I delved into #S and family - I understand them a little better each time I look them up and play with them.

Re: Extra stacks

Posted: Sat Oct 31, 2020 1:16 am
by JimBoyd

The return stack normally holds return addresses and DO LOOP parameters. Some Forth implementations may place the loop parameters elsewhere ( which in itself would be another stack, assuming the loops are nestable) but the latest standard indicates that the loop parameters will be on the return stack. The latest Forth standard states that a program may use the return stack for temporary storage subject to certain restrictions. The Forth-83 standard merely states that the return stack may cautiously be used for other values. An auxiliary stack may be used as freely as the data stack, without any of these restrictions. Nothing surprising there. I wonder, are we are so used to the restrictions with the return stack that we apply the same restrictions to an auxiliary stack?

Re: Extra stacks

Posted: Sun Aug 06, 2023 9:55 pm
by JimBoyd
GARTHWILSON wrote:
Jim, you have quite a few words above that are neither part of any standard I know of, nor defined above. One I'll ask about however is REDEFINE:.
I already mentioned the string "REDEFINE:" is a message from CREATE to let me know I redefined a word. I've changed that. CREATE will now print the name of the word followed by the string "EXISTS" when there is a redefinition.

Code: Select all

: TEST ;  OK
: TEST ; 
TEST EXISTS
 OK

One point to keep in mind with these session logs is that the printout does not show a distinction between what the programmer types and the computer's response. It is a faithful printout of the text sent to the computer screen.
Fleet Forth word redefinition.png