retroforth/example/save-and-restore-stack.retro
crc 9e03717deb normalize names for examples (with a couple of exceptions), closes #38
FossilOrigin-Name: 088675e452ed86a712563c8b2597fe4d47da59bdea0e40becdd1e028a84c47b0
2021-01-24 01:13:04 +00:00

22 lines
383 B
Forth

# Save and Restore Stack
It's sometimes useful to temporarily save and restore the entire
stack. These two words allow for this.
## The Code
~~~
:stack:save (-a)
here [ depth dup , &, times ] dip ;
:stack:restore (a-)
&reset dip
dup fetch over + swap fetch [ dup fetch swap n:dec ] times drop ;
~~~
## Test Case
```
#1 #2 #3 #4 #5
stack:save #3 swap stack:restore
```