79802961

Date: 2025-10-28 15:02:51
Score: 1
Natty:
Report link

Here is an alternative allowing for any size stack. A further advantage is that it counts up, rather than down, allowing for sp? to indicate stack depth.


\ A 3rd stack as in JForth

32 CONSTANT us_max
VARIABLE us_ptr 0 us_ptr !
CREATE us us_max 1+ CELLS ALLOT
us us_max CELLS ERASE
: us? ( -- u ) us_ptr @ ; \ Circular: 0, 1, 2 ... us_max ... 2, 1, 0
: >us ( n -- ) us? DUP us_max = IF DROP 0 ELSE 1+ THEN DUP us_ptr ! CELLS us + ! ;
: us@ ( -- n ) us us? CELLS + @ ;
: us> ( -- n ) us@ us? DUP 0= IF DROP us_max ELSE 1- THEN us_ptr ! ;

: test.3rd.stack
  CR CR ." Testing user stack."

  CR ." Will now fill stack in a loop."
  us_max 1+ 0 DO I >us LOOP
  CR ." Success at filling stack in a loop!"

  CR CR ." Will next empty the stack in a loop."
  CR ." Press any key to continue." KEY DROP
  0 us_max DO
    CR I . ." = " us> . 
  -1 +LOOP
  CR ." Success if all above are equal."

  CR ." Done."
;

test.3rd.stack
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Gan Uesli Starling