15.6.2.1908 N>R n-to-r TOOLS EXT
Interpretation:
Execution:
Remove n+1 items from the data stack and store them for later retrieval by NR>. The return stack may be used to store the data. Until this data has been retrieved by NR>:
- this data will not be overwritten by a subsequent invocation of N>R and
- a program may not access data placed on the return stack before the invocation of N>R.
See:
Rationale:
Implementation:
Testing:
T{ 1 2 10 20 30 3 TNR1 -> 2 1 10 20 30 3 }T
: TNR2 N>R N>R SWAP NR> NR> ;
T{ 1 2 10 20 30 3 40 50 2 TNR2 -> 2 1 10 20 30 3 40 50 2 }T
ContributeContributions
JimPeterson
[281] Bad Stack Notation?Comment2023-02-13 15:22:04
Given that "A stack-constrained system may prefer to use a buffer to store the items and place a reference to the buffer on the return stack.", I think a better stack notation for execution semantics would be "( i * x +n -- ) ( R: -- strg-sys )" (or some other "*-sys"), to indicate pretty clearly that: 1) the user can not rely on what data format gets pushed onto the return stack, and 2) something gets pushed on to the return stack and should not be meddled with.
EricBlake
[394] Enhance n>r test to validate that n>r r@ r>n sees nSuggested Testcase2025-08-04 14:23:53
The standard is clear that although the implementation can store j*x words for the rest of the saved portion of the data stack with no 1:1 correspondence to the original i*x words, the return stack MUST have n as its top entry.
There is debate on whether this should be relaxed; ruv mentioned at https://forth-standard.org/standard/tools/NtoR#reply-964 changing things in a backward-incompatible way as:
N>R ( u*x u -- ) ( R: -- nr-sys )
NR> ( -- u*x u ) ( R: nr-sys -- )
But unless that change is made, we should probably test that an implementation complies with the current wording, which is observable to a standard program via R@. I (temporarily) made the mistake of not complying in one of my attempted reference implementations at https://forth-standard.org/standard/tools/NRfrom#contribution-393, when I had swapped the order of n and a buffer address on the return stack. Thus, I suggest adding:
: TNR3 N>R SWAP R@ NR> ;
T{ 1 2 10 20 30 3 TNR3 -> 2 1 3 10 20 30 3 }T